【问题标题】:Error: Cannot find module ' in Protractor错误:在量角器中找不到模块'
【发布时间】:2016-10-10 20:16:42
【问题描述】:

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

以下是我的主要规范文件,名为“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)

【问题讨论】:

    标签: javascript testing requirejs protractor pageobjects


    【解决方案1】:

    不是问题的直接答案,而是在导入 Page 对象或 Helper 函数时解决 Protractor 中“需要”问题的一般方法。

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

    在您的规范文件中。

    【讨论】:

    • 优秀。它现在正在工作,花了一整天的时间。
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 2016-09-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-09
      • 1970-01-01
      • 2018-01-27
      • 2019-11-19
      • 1970-01-01
      相关资源
      最近更新 更多