javascript - 在 Nightwatch 自定义命令中赋值后,变量返回未定义

标签 javascript nightwatch.js

写一个custom command进行夜间值类测试。

为什么我无法在以下代码片段中设置newValidFromText的值?

exports.command = function () {

var newValidFromText; //Want to set value to this variable

var browser= this;
browser
    .useCss()
    .perform(function() {

        //Setting the value to the variable 'newValidFromText'
        newValidFromText = "June 1, 2017 "

        //Testing the value set - console prints "June 1, 2017"
        console.log( "newValidFromText now is: "+ newValidFromText );
    })

    .waitForElementVisible('input[id*="SubscriptionStart"]')

    //Test for correct value - getting validFromText = undefined
    .verify.valueContains('input[id*="SubscriptionStart"]', validFromText) 
 return browser.useCss();
};

最佳答案

因为您要求变量超出可见区域

exports.command = function () {

var newValidFromText; //Want to set value to this variable

var browser= this;
 browser
.useCss()
.perform(function() {

    //Setting the value to the variable 'newValidFromText'
    newValidFromText = "June 1, 2017 "

    //Testing the value set - console prints "June 1, 2017"
    console.log( "newValidFromText now is: "+ newValidFromText );

this.waitForElementVisible('input[id*="SubscriptionStart"]') 

    .verify.valueContains('input[id*="SubscriptionStart"]', validFromText); 

})

};

关于javascript - 在 Nightwatch 自定义命令中赋值后,变量返回未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43766842/

相关文章:

Javascript Electron Menu() 不是构造函数

javascript - 在 Selenium 中获取 Javascript 代码的返回值

javascript - 如何使用不在 url 中的 nightwatchjs 将基本身份验证传递到 selenium

angularjs - 在基于 AngularJS 的 Web 应用程序中使用 Selenium

javascript - Div 与 css 和后退按钮不正确对齐

javascript - 使用多个路径渲染相同的组件 React Router Dom

javascript - jQuery:按类更新带有日期选择器输入值的 span 标签?

javascript - 使用 Nightwatch (Selenium2) 无法找到包含 "anchor"的元素

javascript - nightwatch中如何根据测试环境动态加载不同的页面对象?

javascript - nightwatch.js 在测试套件结束时暂停