在JS上使用require语句加载多个文件

时间:2018-07-20 14:22:37

标签: javascript protractor requirejs cucumber pageobjects

当尝试加载多个js时,执行将返回以下错误:

TypeError: Cannot read property 'get' of undefined

我正在实施的解决方案具有 login_steps.js

var LoginSteps = function() {

var LoginSteps = require("../pages/pages.js");
browser.ignoreSynchronization = true;

this.World = function MyWorld() {
    this.page = new LoginSteps();
};

this.Given(/^the page is open$/, function (callback) {
    this.page.login_page.get();
    callback();
});
};

module.exports = LoginSteps;

一个 page.js ,我要在其中包含所需的所有模块。

var Pages = function() {
module.exports = {
    shipments_page: require('./shipments_page.js'),
    login_page: require('./login_page.js'),
};
};

module.exports = Pages;

以及模块 login_page.js:

var chai = require('chai').use(require('chai-as-promised'));
var expect = chai.expect;

this.get = function() {
    browser.get('https://aaa/login');
};

this.setEmail = function(value) {
    element(by.id('login-email')).sendKeys(value);
};

this.setPassword = function(value) {
    element(by.id('login-password')).sendKeys(value);
};

this.clickAccede = function() {
    element(by.id('login-submit')).click()
};

shipment_page.js:

var chai = require('chai').use(require('chai-as-promised'));
var expect = chai.expect;

this.pageIsLoaded = function() {
    browser.waitForAngular();
    expect(browser.getTitle()).to.be.eventually.equals('title');
};

然后当我执行测试时,日志显示

Failures:

1) Scenario: User login - features/login.feature:3
Step: Given the page is open - features/login.feature:4
Step Definition: features/steps/login_steps.js:16
Message:
 TypeError: Cannot read property 'get' of undefined
     at MyWorld.<anonymous> (/Users/mj/IdeaProjects/atpro/features/steps/login_steps.js:17:30)
     at process._tickCallback (internal/process/next_tick.js:61:11)

1 scenario (1 failed)
5 steps (1 failed, 4 skipped)

2 个答案:

答案 0 :(得分:1)

这是您可以尝试的代码。它对我有用。我根据您的代码段进行了修改。我不会在测试中使用此模式。您可能不想使用所遵循的模式编写量角器-黄瓜测试。一个人应该始终在步骤定义代码中使用验证/断言。如果您在页面对象中进行验证,即使验证失败,您的测试仍将显示通过。

login_steps.js

<div style="position:relative; height:100%;">
      <asp:TextBox ID="txtTons" CssClass="tonnage" runat="server"
                       OnChange="txtTons_TextChanged(this)"
                       MaxLength="6" Width="40" pattern="^\d{1,3}(?:\.\d{1,2})?$">
      </asp:TextBox>
</div>
<div style="position:relative; bottom:0; color:red">
      <asp:Literal ID="litMaxTons" runat="server"></asp:Literal>
</div>

pages.js:

var LoginSteps = function() {

var LoginSteps = require("../pages/pages.js");
browser.ignoreSynchronization = true;

this.World = function MyWorld() {
this.page = LoginSteps;
};

this.Given(/^the page is open$/, function (callback) {
this.page.login_page.get();
callback();
});
};

module.exports = LoginSteps;

答案 1 :(得分:0)

如果我正确解释了这就是您想要的。您只需稍微更改page.js。

module.exports = function() {
   this.shipments_page = require('./shipments_page.js');
   this.login_page = require('./login_page.js');
};

请尝试这个。我目前无法测试:=)