[英]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.