在 JS 上使用 require 语句加载多个文件
Loading multiple files with require statement on JS
执行returns尝试加载多个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;
A 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()
};
发货_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)
如果我解释正确,这就是您想要的。你只需要稍微改变一下page.js。
module.exports = function() {
this.shipments_page = require('./shipments_page.js');
this.login_page = require('./login_page.js');
};
请试试这个。我现在无法测试 :=)
这是您可以尝试的代码。它对我有用。我根据您的代码片段进行了修改。我不会在我的测试中使用这种模式 though.You 可能不想使用您正在遵循的模式编写量角器黄瓜测试。在步骤定义代码中应该始终使用 validation/assertions。如果您在页面对象中进行验证,即使您的验证失败,您的测试仍然会显示通过。
login_steps.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;
pages.js:
module.exports = {
shipments_page: require('./shipments_page.js'),
login_page: require('./login_page.js'),
};
执行returns尝试加载多个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;
A 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()
};
发货_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)
如果我解释正确,这就是您想要的。你只需要稍微改变一下page.js。
module.exports = function() {
this.shipments_page = require('./shipments_page.js');
this.login_page = require('./login_page.js');
};
请试试这个。我现在无法测试 :=)
这是您可以尝试的代码。它对我有用。我根据您的代码片段进行了修改。我不会在我的测试中使用这种模式 though.You 可能不想使用您正在遵循的模式编写量角器黄瓜测试。在步骤定义代码中应该始终使用 validation/assertions。如果您在页面对象中进行验证,即使您的验证失败,您的测试仍然会显示通过。
login_steps.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;
pages.js:
module.exports = {
shipments_page: require('./shipments_page.js'),
login_page: require('./login_page.js'),
};