繁体   English   中英

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

[英]Error: Cannot find module ' in 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 函数时解决量角器中“需要”问题的一般方法。

我们所做的是引入了 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');
};

相应地调整路径 - __dirname是您的配​​置所在的位置。提供的函数适用于以下结构:

- e2e
  - config
     protractor.conf.js
  - po
     home_page.js
  - helpers
     helpers.js
  - specs
     example_spec.js

然后,您将能够拥有:

var home_page = requirePO('home_page');

在您的规范文件中。

为了使用热键,您需要安装该软件包。 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);

    });

})

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM