【问题标题】:Error when importing .js file in one folder to spec.js file in another folder将一个文件夹中的 .js 文件导入另一个文件夹中的 spec.js 文件时出错
【发布时间】:2020-06-20 09:52:52
【问题描述】:

我是量角器的新手,正在尝试使用 POM 在黄瓜中创建一个项目。以下是项目的结构:

在 addCustomerPage.js 中,我提到了定位器以及执行测试的函数:

var addCustomerPage = function () {

    var BankManagerButton = element(by.buttonText('Bank Manager Login'));
    ***Other locators*****

    this.create = async function(fName,lName,pCode){
        await BankManagerButton.click();
        ****rest of the steps*****
    }
}
module.exports = new addCustomerPage();

但是在spec.js中,导入上面的类,在运行代码时,会抛出错误:

E/launcher - Error: Error: Cannot find module '../pages/addCustomerPage'

以下是 spec.js 文件的代码:

var {
    setDefaultTimeout
} = require('cucumber');
const {
    expect
} = require('chai');
setDefaultTimeout(10 * 1000);
var addCustomerPage = require('../pages/addCustomerPage');
Given('I open the application and click on create customer button', async function () {
    **code*****
});

When('I enter {string}, {string}, {string}', async function (fname, lname, pcode) {
    return await addCustomerPage.create(fname, lname, pcode);
});

但是,如果 pages 文件夹位于 features 文件夹下,则可以正常工作。谁能帮助我在这里做错了什么?

【问题讨论】:

    标签: javascript angularjs angular protractor cucumber


    【解决方案1】:

    ../

    上面的符号表示向上一个文件目录。

    当变量这样声明时...

    var addCustomerPage = require('../pages/addCustomerPage');

    ...您的计算机将从当前目录上一层文件夹并搜索 pages 文件夹但找不到它。

    当您复制 pages 文件夹并将其放在功能文件夹下时,它可以检测到它,因为它位于您正在搜索的目录下

    解决办法是:

    1. 只需将您的页面文件夹粘贴到功能下
    2. 将变量中的文件路径修改为你的pages文件夹所在的位置

    我猜你必须上一两个目录,所以使用这个命令../ 来找到你的页面文件夹所在的位置

    var addCustomerPage = require('../../pages/addCustomerPage');

    想法是将文件路径修改为页面文件夹可能在的任何位置

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-04-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-04
      • 1970-01-01
      相关资源
      最近更新 更多