使用量角器"TypeError:未定义不是函数" [英] 'TypeError: undefined is not a function' using Protractor

查看:98
本文介绍了使用量角器"TypeError:未定义不是函数"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道有关此错误代码的话题很多,但是我一直在努力寻找任何有助于解决问题的解决方案(或者也许我很愚蠢).

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).

我正在为网页构建量角器测试,并且我的原始javascript效果很好.但是我想将其全部转换为使用页面对象,以使其更易于编辑和理解以及避免代码重复.

I am building a Protractor test for a web page, and my original javascript worked perfectly. 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.

我要与之链接页面对象的主要测试文档分别包含起作用和不起作用的代码实例. -第一个有效的实例是第12行. -第二个不在第23行的实例. 如下所示:

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. - The second instance that doesn't is on line 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();
            });
        });
    });
});

在cmd中运行量角器测试时出现的错误是:

The error I get when running the Protractor test in cmd is:

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)

第一个实例引用的页面是:

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

任何帮助都会很棒,如果我忘了给您提供任何详细信息,请告诉我:)

Any help would be brilliant and if I have forgotten to give you any details please let me know :)

推荐答案

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

注意每页的最后一行:

工作文档在"module.exports"之后没有新"声明或方括号.

The working document has no 'new' statement or brackets after the 'module.exports'

由于Phil的评论,删除了新"部分,但直到现在才意识到方括号,所以请归功于Phil!

Noticed the 'new' part due to Phil's comment, but didn't realise the brackets until just now, so credit to Phil!

这篇关于使用量角器"TypeError:未定义不是函数"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆