Javascript Nightwatch.js设置值并执行 Nightwatch 0.9.19 节点:9.3.0 OS:Darwinx64 exports.command=函数(用户名、密码){ 常数超时=6000; const USER_NAME='[NAME=“username”]'; 常量密码='[name=“PASSWORD”]; const BODY=‘BODY’; const FORM='FORM'; 如果(!用户名){ 用户名=this.globals.username; } 如果(!密码){ password=this.globals.password; } log('login',this.globals.baseUrl,用户名,密码); this.url(this.globals.baseUrl) .waitForElementVisible(正文,超时) .setValue(用户名、用户名) .getValue(用户名,(结果)=>{ console.log(`===>用户名为:${result.value}{ console.log(`===>密码为:${result.value}结果:1q2we4r(缺少:3)

Javascript Nightwatch.js设置值并执行 Nightwatch 0.9.19 节点:9.3.0 OS:Darwinx64 exports.command=函数(用户名、密码){ 常数超时=6000; const USER_NAME='[NAME=“username”]'; 常量密码='[name=“PASSWORD”]; const BODY=‘BODY’; const FORM='FORM'; 如果(!用户名){ 用户名=this.globals.username; } 如果(!密码){ password=this.globals.password; } log('login',this.globals.baseUrl,用户名,密码); this.url(this.globals.baseUrl) .waitForElementVisible(正文,超时) .setValue(用户名、用户名) .getValue(用户名,(结果)=>{ console.log(`===>用户名为:${result.value}{ console.log(`===>密码为:${result.value}结果:1q2we4r(缺少:3),javascript,ecmascript-6,nightwatch.js,Javascript,Ecmascript 6,Nightwatch.js,我看到您正试图使用以下代码在密码字段上设置一个值: 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 =

我看到您正试图使用以下代码在密码字段上设置一个值:

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)
};
函数中的
PASSWORD
PASSWORD
变量在执行时都是
未定义的

这里的问题是,基本上传递给
execute
的函数将在正在测试的网页上下文(浏览器JavaScript引擎)中运行,而不是在当前脚本上下文(node.js)中运行

但是,对于需要向此类函数传递一些参数的情况,可以使用
execute()
的第二个参数,它基本上是传递给函数的参数数组

修复上面的代码段后,您应获得以下信息:

this
    .execute(function (data) {
        document.querySelector(PASSWORD).value = password;
        return true;
    }, [], null)

execute()

我看到您正试图使用下面的代码在密码字段上设置一个值:

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)
};
函数中的
PASSWORD
PASSWORD
变量在执行时都是
未定义的

这里的问题是,基本上传递给
execute
的函数将在正在测试的网页上下文(浏览器JavaScript引擎)中运行,而不是在当前脚本上下文(node.js)中运行

但是,对于需要向此类函数传递一些参数的情况,可以使用
execute()
的第二个参数,它基本上是传递给函数的参数数组

修复上面的代码段后,您应获得以下信息:

this
    .execute(function (data) {
        document.querySelector(PASSWORD).value = password;
        return true;
    }, [], null)

有关
execute()
的文档:

如果在设置console.log(密码)之前执行,您会得到什么?如果在设置console.log(密码)之前执行,您会得到什么?