错误:在量角器中无法找到模块

cmp*_*ore 3 javascript testing requirejs pageobjects protractor

所以我是新的量角器并尝试使用页面对象进行测试,以使代码更易于管理.在这方面遇到一些问题.

下面是我的主要spec文件'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!');
  });
});
Run Code Online (Sandbox Code Playgroud)

下一个文件是名为"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();
Run Code Online (Sandbox Code Playgroud)

问题是在运行此测试时我得到以下错误.即使为文件尝试不同的路径,我仍然会收到错误.任何帮助,将不胜感激.

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)
Run Code Online (Sandbox Code Playgroud)

ale*_*cxe 6

这不是问题的直接答案,而是在导入Page对象或Helper函数时解决Protractor中"require"问题的一般方法.

我们所做的是引入2个全局辅助函数 - 一个用于页面对象,另一个用于帮助器.把它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');
};
Run Code Online (Sandbox Code Playgroud)

相应地调整路径 - __dirname是配置所在的位置.提供的功能适用于以下结构:

- e2e
  - config
     protractor.conf.js
  - po
     home_page.js
  - helpers
     helpers.js
  - specs
     example_spec.js
Run Code Online (Sandbox Code Playgroud)

然后,你将能够:

var home_page = requirePO('home_page');
Run Code Online (Sandbox Code Playgroud)

在您的spec文件中.