Nightwatch 0.9.19
Node: 9.3.0
OS: darwin x64
exports.command = function (username, password) {
const TIMEOUT = 6000;
const USER_NAME = '[name="username"]';
const PASSWORD = '[name="password"]';
const BODY = 'body';
const FORM = 'form';
if (!username) {
username = this.globals.username;
}
if (!password) {
password = this.globals.password;
}
console.log('login', this.globals.baseUrl, username, password);
this.url(this.globals.baseUrl)
.waitForElementVisible(BODY, TIMEOUT)
.setValue(USER_NAME, username)
.getValue(USER_NAME, (result) => {
console.log(`=====> Username is: ${result.value} <=====`);
})
.waitForElementVisible(USER_NAME, TIMEOUT)
.waitForElementVisible(PASSWORD, TIMEOUT);
this
.execute(function (data) {
document.querySelector(PASSWORD).value = password;
return true;
}, [], null)
.pause(TIMEOUT)
.getValue(PASSWORD, (result) => {
console.log(`=====> Password is: ${result.value} <=====`);
})
.submitForm(FORM)
};发布于 2018-02-01 14:38:23
我看到您试图使用下面的代码在password字段上设置一个值:
this
.execute(function (data) {
document.querySelector(PASSWORD).value = password;
return true;
}, [], null)函数中的PASSWORD和password变量在执行时都是undefined。
这里的问题是,传递给execute的函数基本上将在测试的网页上下文(浏览器JavaScript引擎)中运行,而不是在当前脚本(node.js)的上下文中运行。
但是,对于需要向此类函数传递一些参数的情况,可以使用execute()的第二个参数,它基本上是传递给函数的参数数组。
在修复了上面的代码片段之后,您应该得到以下内容:
this
.execute(function (PASSWORD, password) {
document.querySelector(PASSWORD).value = password;
return true;
}, [PASSWORD, password], null)关于execute()的文档:http://nightwatchjs.org/api/execute.html
https://stackoverflow.com/questions/48547542
复制相似问题