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

时间:2016-10-10 20:16:43

标签: javascript testing requirejs protractor pageobjects

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

以下是我的主要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!');
  });
});

下一个文件是名为“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)

2 个答案:

答案 0 :(得分:6)

不是问题的直接答案,而是在导入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');

在spec文件中。

答案 1 :(得分:0)

要使用热键,您需要安装软件包。从https://www.npmjs.com/package/protractor-hotkeys获取。请在命令

下方输入
npm install -g protractor-hotkeys

然后在您的spec.js中,您需要提及模块路径。按照下面的代码。请尝试并提供反馈。

//keyeventexample.spec.js
var hotkeys = require('C:/Users/sam/AppData/Roaming/npm/node_modules/protractor-hotkeys'); 
describe('My first test class', function() {
    it('My function', function() {
        browser.driver.get('https://material.angular.io/cdk/text-field/overview');
       //Can send the value directly 
        //hotkeys.trigger('ctrl+a', { targetElement: element(by.id('text')) });
//Or pass it via element
    var txtfield=element(by.className('mat-form-field-autofill-control mat-input-element cdk-textarea-autosize ng-tns-c98-5 cdk-text-field-autofill-monitored'));

    txtfield.sendKeys("Hello 66");
        hotkeys.trigger('ctrl+a', { targetElement: txtfield });
        hotkeys.trigger('ctrl+c', { targetElement: txtfield });

        console.log("key event is fired");
        browser.sleep(5000);
        txtfield.clear();
        hotkeys.trigger('ctrl+v', { targetElement: txtfield });
        browser.sleep(5000);

    });

})