[英]'TypeError: undefined is not a function' using Protractor
I am aware there are many topics on this error code.我知道这个错误代码有很多主题。 However, I am struggling to find any solutions that help (or maybe I'm just stupid).
但是,我正在努力寻找任何有帮助的解决方案(或者我可能只是愚蠢)。
I am building a Protractor test for a web page, and my original JavaScript worked perfectly.我正在为网页构建 Protractor 测试,我的原始 JavaScript 运行良好。 But I wanted to convert it all into using page objects, to make it easier to edit and understand as well as avoid code repetition.
但我想将其全部转换为使用页面对象,以便更容易编辑和理解并避免代码重复。
The first page object I use for logging in works perfectly fine, however my second page object for adding new users does not.我用于登录的第一个页面对象工作得很好,但是我用于添加新用户的第二个页面对象却没有。
The main test document from which I am linking the page objects to and from contains both instances of the code that do and don't work, respectively.我将页面对象链接到的主要测试文档分别包含有效和无效的代码实例。
The first instance that works is line 12.第一个有效的实例是第 12 行。
The second instance that doesn't is on line 23.第二个不存在的实例在第 23 行。
This is shown below:这如下所示:
var util = require('util') describe ('This is to test the functionality of the Admin page.', function() { beforeEach(function(){ browser.ignoreSynchronization = true; browser.get('https://website'); }); it ('Should log in and move to the Users page.', function() { var login_page = require('../page/login_page.js'); var loginPage = new login_page; loginPage.login('user', 'pass'); beforeEach(function () { expect(browser.getCurrentUrl()).toEqual('https://website'); }); describe('This should hold all the tests while logged in to Admin site', function () { it('Should create first new User.', function() { var users_page = require('../page/users_page.js'); var usersPage = new login_page; usersPage.addUserButton().click(); usersPage.addUser('Test', 'Smith', 'Test100@testing.co.nz', 'Password', 'Password', '0') usersPage.userRole[0]; usersPage.confirm().click(); usersPage.back().click(); }); it('Should logout', function () { element(by.cssContainingText('span', 'Logout')).element(by.linkText('Logout')).click(); }); }); }); });
The error I get when running the Protractor test in cmd is:在 cmd 中运行 Protractor 测试时出现的错误是:
1) This should hold all the tests while logged in to Admin site Should create first new User.
Message:
TypeError: undefined is not a function
Stacktrace:
TypeError: undefined is not a function
at [object Object].<anonymous> (C:\\\\\\test
04_spec.js:25:27)
The page that the first instance references is:第一个实例引用的页面是:
require ('../page/users_page.js');
var login_page = function(){
this.username = element(by.id('username'));
this.password = element(by.id('password'));
this.get = function(urlVal){
browser.get(urlVal);
};
this.login = function(un, pass){
this.username.sendKeys(un);
this.password.sendKeys(pass);
element(by.tagName('button')).click();
};
};
module.exports = login_page;
And the page that the second instance references is:第二个实例引用的页面是:
require ('../page/users_page.js');
var users_page = function(){
this.addUserButton = element(by.css('[role="button"]'));
this.firstName = element(by.model('firstname'));
this.lastName = element(by.model('lastname'));
this.userName = element(by.model('username'));
this.password = element(by.model('password'));
this.confirmPassword = element(by.model('confirmpassword'));
this.confirm = element(by.css('[class="btn btn-success marginRight10px floatLeft"]'));
this.back = element(by.css('[class="btn floatLeft"]'));
this.addUser = function(fn, ln, un, pw, cpw){
this.firstname.sendKeys(fn);
this.lastword.sendKeys(ln);
this.username.sendKeys(un);
this.password.sendKeys(pw);
this.confirmpassword.sendKeys(cpw);
};
this.userRole = function(index){
element(by.model('tes.userRole')).$('[value="'+index+'"]').click();
};
return require('./users_page.js');
};
module.exports = new users_page();
There is at least one problem: addUserButton
is not a function.至少有一个问题:
addUserButton
不是一个函数。 Replace:代替:
usersPage.addUserButton().click();
with:和:
usersPage.addUserButton.click();
And the same for confirm
and back
page object fields.对于
confirm
和back
页面对象字段也是如此。
Also, instead of:此外,而不是:
usersPage.userRole[0];
you probably meant to call the underlying page object function:您可能打算调用底层页面对象函数:
usersPage.userRole(0);
Aside from that, you probably meant to instantiate a new "users page" object.除此之外,您可能打算实例化一个新的“用户页面”对象。 Replace:
代替:
var usersPage = new login_page;
with:和:
var usersPage = new users_page;
require ('../page/users_page.js'); var login_page = function(){ this.username = element(by.id('username')); this.password = element(by.id('password')); this.get = function(urlVal){ browser.get(urlVal); }; this.login = function(un, pass){ this.username.sendKeys(un); this.password.sendKeys(pass); element(by.tagName('button')).click(); }; }; module.exports = login_page;
require ('../page/users_page.js'); var users_page = function(){ this.addUserButton = element(by.css('[role="button"]')); this.firstName = element(by.model('firstname')); this.lastName = element(by.model('lastname')); this.userName = element(by.model('username')); this.password = element(by.model('password')); this.confirmPassword = element(by.model('confirmpassword')); this.confirm = element(by.css('[class="btn btn-success marginRight10px floatLeft"]')); this.back = element(by.css('[class="btn floatLeft"]')); this.addUser = function(fn, ln, un, pw, cpw){ this.firstname.sendKeys(fn); this.lastword.sendKeys(ln); this.username.sendKeys(un); this.password.sendKeys(pw); this.confirmpassword.sendKeys(cpw); }; this.userRole = function(index){ element(by.model('tes.userRole')).$('[value="'+index+'"]').click(); }; return require('./users_page.js'); }; module.exports = new users_page();
Note the last line of each page:注意每一页的最后一行:
The working document has no 'new' statement or brackets after the 'module.exports'工作文件在“module.exports”之后没有“new”语句或括号
Noticed the 'new' part due to Phil's comment, but didn't realise the brackets until just now, so credit to Phil!由于 Phil 的评论,注意到了“新”部分,但直到现在才意识到括号,所以感谢 Phil!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.