Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/398.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 验证页面对象文件中的sendKeys结果会导致未定义错误(量角器)_Javascript_Node.js_Protractor - Fatal编程技术网

Javascript 验证页面对象文件中的sendKeys结果会导致未定义错误(量角器)

Javascript 验证页面对象文件中的sendKeys结果会导致未定义错误(量角器),javascript,node.js,protractor,Javascript,Node.js,Protractor,因此,我有一个页面对象文件,它为页面上的元素提供了许多方法。该页面是一个登录页面,包含一些文本、用户名和密码输入元素以及登录按钮。我已经创建了一个名为“InputLabel.js”的通用对象,它将标签和输入元素绑定在一起进行测试 我遇到的问题是,在清除输入、发送数据并验证数据后,我得到一个失败:无法读取未定义的错误的属性“verifyValue” 以下是相关代码: // InputLabel.js function InputLabel(container) { this.Contai

因此,我有一个页面对象文件,它为页面上的元素提供了许多方法。该页面是一个登录页面,包含一些文本、用户名和密码输入元素以及登录按钮。我已经创建了一个名为“InputLabel.js”的通用对象,它将标签和输入元素绑定在一起进行测试

我遇到的问题是,在清除输入、发送数据并验证数据后,我得到一个
失败:无法读取未定义的
错误的属性“verifyValue”

以下是相关代码:

// InputLabel.js

function InputLabel(container) {
    this.Container = container;
}

InputLabel.prototype = {
    constructor: InputLabel,
    // ...
    /**
     * Return the element for the input of the input/label combination of elements.
     * 
     * @returns {ElementFinder}
     */
    getInput: function () {
        return this.Container.$('input');
    },
    /**
     * Return the text shown in the input of the input/label combination of elements.
     * 
     * @returns {Promise}
     */
    getValue: function () {
        return this.getInput().getAttribute('value');
    },
    /**
     * Verify the text shown in the input of the input/label combination of elements.
     * 
     * @param expected The expected text in the input element.
     */
    verifyValue: function (expected) {
        console.log('Asserting input value [' + expected + ']');
        expect(this.getValue()).toEqual(expected);
    },
    // ...
    /**
     * Clears the input element then puts the text from data into the input element.
     * 
     * @param data The text to be entered into the input element.
     */
    sendKeys: function (data) {
        var el = this.getInput();
        el.clear().then(function () {
            el.sendKeys(data).then(function () {
                console.log("Verifying [" + data + "] was sent to the input.")
                this.verifyValue(data);
            });
        });
    }
};
在需要该文件之后,我可以调用这些方法中的任何一个,除了sendKeys之外没有问题。如果我禁用了
this.verifyValue(数据)方法,sendKeys工作正常

// LoginPage.js

var InputLabel = require('InputLabel.js');

function LoginPage() {
}

var username = new InputLabel($('#username'));
var password = new InputLabel($('#password'));

function.prototype = {
   // ...
   username: username,
   password: password,
   loginButton: {
      get: function() { return $('#Login'); },
      click: function() { return this.get().click(); }
   },
   // ...
   login: function(user, pw) {
      this.username.sendKeys(user);
      this.password.sendKeys(pw);
      this.loginButton.click()
   }
}

我是不是失去了一些东西?同样,错误是它失败了,因为它在发送键后无法读取undefined的属性“verifyValue”。

您在包含“this.verifyValue(数据);”的行中遇到了“this”关键字的作用域问题。在本例中,“this”关键字不引用InputLabel类。另外,保持页面对象断言自由也是一种很好的做法。请参见

当我取下“this”时,我仍然会得到相同的错误<代码>失败:未定义验证值
。不过,我会看看你的链接并从中学习。谢谢。一旦你进入了第一个承诺
el.clear()
你就失去了@finspin所说的
这个
的范围。在
sendKeys
函数的基本级别,put
var self=this(这将使您的范围保持在InputLabel),然后使用
self.verifyValue(数据)谢谢。这很有帮助。因为我又碰到它了。(我还在学习javascript风格。)