所以我是 Protractor 的新手,并尝试使用页面对象进行测试以使代码更易于管理。在这方面有一些问题。
下面是我的主要规范文件,名为“example_spec.js”
describe('angularjs homepage', function() {
var home_page = require('../home_page.js');
it('should greet the named user', function() {
home_page.enterFieldValue('Jack Sparrow');
var getHomePageText = home_page.getDyanmaicText();
expect(getHomePageText).toBe('Hello Steve!');
});
});
下一个文件是名为“home_page.js”的页面对象
var home_page = function(){
//Send in a value.
this.enterFieldValue = function(value){
element(by.model('youName')).sendKeys(value);
};
this.getDyanmaicText = function(){
return element(by.binding('yourName')).getText();
};
};
module.exports = new home_page();
问题是在运行此测试时出现以下错误。即使在为文件尝试不同的路径时,我也会不断收到错误消息。任何帮助,将不胜感激。
Failures:
1) angularjs homepage encountered a declaration exception
Message:
Error: Cannot find module '../home_page.js'
Stack:
Error: Cannot find module '../home_page.js'
at Function.Module._resolveFilename (module.js:455:15)
at Function.Module._load (module.js:403:25)
at Module.require (module.js:483:17)
at require (internal/module.js:20:19)
at Suite.<anonymous> (/Users/testuser/dev/example_spec.js:3:19)
at Object.<anonymous> (/Users/testuser/dev/example_spec.js:1:1)
at Module._compile (module.js:556:32)
最佳答案
不是问题的直接答案,而是在导入 Page 对象或 Helper 函数时解决 Protractor 中“require”问题的一般方法。
我们所做的是 introduce 2 global helper functions - 一个用于页面对象,另一个用于助手。将其放入配置中的 onPrepare()
中:
// helper require function to import page objects
global.requirePO = function (relativePath) {
return require(__dirname + '/../po/' + relativePath + '.po');
};
// helper require function to import helpers
global.requireHelper = function (relativePath) {
return require(__dirname + '/../helpers/' + relativePath + '.js');
};
相应地调整路径 - __dirname
是您的配置所在的位置。提供的函数适用于以下结构:
- e2e
- config
protractor.conf.js
- po
home_page.js
- helpers
helpers.js
- specs
example_spec.js
然后,您将能够:
var home_page = requirePO('home_page');
在你的规范文件中。
关于javascript - 错误 : Cannot find module ' in Protractor,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39966086/