'TypeError:undefined不是使用Protractor的函数'

时间:2015-02-23 01:50:20

标签: javascript angularjs node.js protractor karma-jasmine

我知道这个错误代码有很多主题但是我很难找到任何有用的解决方案(或者我可能只是愚蠢)。

我正在为网页构建一个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 c
reate 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();

任何帮助都会很精彩,如果我忘了给你任何细节,请告诉我:)。

2 个答案:

答案 0 :(得分:1)

至少有一个问题:addUserButton不是一个功能。替换:

usersPage.addUserButton().click();

使用:

usersPage.addUserButton.click();

confirmback页面对象字段也一样。


此外,而不是:

usersPage.userRole[0];

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

usersPage.userRole(0);

除此之外,您可能想要实例化一个新的&#34;用户页面&#34;宾语。替换:

var usersPage = new login_page;

使用:

var usersPage = new users_page;

答案 1 :(得分:0)

&#13;
&#13;
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;
&#13;
&#13;
&#13;

&#13;
&#13;
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();
&#13;
&#13;
&#13;

请注意每页的最后一行:

工作文件中没有新的&#39; &quot; module.exports&#39;

之后的声明或括号

注意到新的&#39;部分由于Phil的评论,但直到现在才意识到括号,所以归功于Phil!