validationsendKeys导致页面对象文件导致未定义的错误(量angular器)

所以,我有一个页面对象文件,为页面上的元素提供了许多方法。 该页面是一个包含文本,用户名和密码input元素以及loginbutton的login页面。 我创build了一个名为“InputLabel.js”的通用对象,它将标​​签和input元素结合在一起用于testing目的。

我遇到的问题是,我清除input后,发送数据,然后validation数据,我得到一个Failed: Cannot read property 'verifyValue' of undefined错误的Failed: Cannot read property 'verifyValue' of undefined

这是相关的代码:

 // 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(data); 方法,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() } } 

我在范围内丢失了什么? 同样,错误是因为在发送密钥后无法读取未定义的属性“verifyValue”,所以失败。

您在包含“this.verifyValue(data);”的行上有“this”关键字的范围问题。 在这种情况下,“this”关键字不会引用InputLabel类。 同时保持页面对象无断言被认为是一个好习惯。 请参阅http://martinfowler.com/bliki/PageObject.html