gpt4 book ai didi

javascript - '类型错误 : undefined is not a function' using Protractor

转载 作者:搜寻专家 更新时间:2023-11-01 00:01:40 24 4
gpt4 key购买 nike

我知道有很多关于此错误代码的主题。但是,我正在努力寻找任何有帮助的解决方案(或者我可能只是愚蠢)。

我正在为网页构建 Protractor 测试,我的原始 JavaScript 运行良好。但我想将其全部转换为使用页面对象,以便于编辑和理解以及避免代码重复。

我用于登录的第一个页面对象工作得很好,但是我用于添加新用户的第二个页面对象却没有。

我从中链接页面对象的主测试文档分别包含有效和无效的代码实例。

  • 第一个有效的实例是第 12 行。

  • 第二个不存在的实例在第 23 行。

    如下所示:

      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();
    });
    });
    });
    });

我在 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)

第一个引用的页面是:

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();

最佳答案

至少有一个问题:addUserButton 不是函数。替换:

usersPage.addUserButton().click();

与:

usersPage.addUserButton.click();

对于 confirmback 页面对象字段也是如此。


此外,代替:

usersPage.userRole[0];

您可能打算调用底层页面对象函数:

usersPage.userRole(0);

除此之外,您可能打算实例化一个新的“用户页面”对象。替换:

var usersPage = new login_page;

与:

var usersPage = new users_page;

关于javascript - '类型错误 : undefined is not a function' using Protractor,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28665841/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com