为什么我无法在Nightwatch.js中为特定变量指定对象值

时间:2015-05-22 11:38:40

标签: javascript nightwatch.js

我正在Nightwatch.js中编写测试脚本,在点击此元素之前,我抓取包含文本并选择value字段并将其分配给外部分配的变量。我遇到的问题是该值未在回调函数

中分配

我的代码如下:

var meeting=""

    module.exports = {
         'step 1: open event page': function (browser) {
             browser
                .url('http://example.com')
                .waitForElementVisible('.navigation-list-item:nth-of-type(2)', 20000)
                .getText('.navigation-view > .list-container > .navigation-list-item:nth-of-type(2) > a > .list-content > .list-body', function (location) {
                       meeting = location.value;
                })
                .pause(3000)
                .click('.navigation-view > .list-container > .navigation-list-item:nth-of-type(2) > a > .list-content > .list-body')
                .pause(3000)
                .assert.containsText('.ncb__title', meeting);
         }
   }

任何帮助都会受到赞赏,干杯!

注意:出于隐私原因,省略了被测试网站的实际网址

2 个答案:

答案 0 :(得分:0)

我认为您的值不会在分配回调后首先尝试打印会议变量。试试这个

final == 6.43656365691809018159119659685529768466949462890625
final == 23.167168296791945891754949116148054599761962890625
final == 81.3421476927506574838844244368374347686767578125
final == 273.99075016572106733292457647621631622314453125
final == 891.478954615459542765165679156780242919921875
final == 2825.00155444914480540319345891475677490234375
final == 8774.06526742766800452955067157745361328125
final == 26829.62188337555198813788592815399169921875
final == 81031.839275753838592208921909332275390625
final == 242292.12374287386774085462093353271484375

如果它得到打印然后它不是问题,因为我是JS新手所以不知道原因但找到了解决方法。试试这个:

.   .getText('.navigation-view > .list-container > .navigation-list-item:nth-of-type(2) > a > .list-content > .list-body', function (location) {
                   meeting = location.value;
                  console.log(meeting);

            })

希望它会对你有所帮助。

答案 1 :(得分:0)

这里的问题与Nightwatch异步运行的事实有关。因此,当您在回调中分配getText结果的值时,Nightwatch已经进入下一步,您将使用它来检查结果。

解决方案是使用步骤明确地解决这个问题。

示例:

// Declare your variable as a global at the top of the test
var myTestVar = 'foo';

module.exports = {
    // You don't necessarily have to navigate in an isolated step, but
    // this will ensure that everything else is done after navigation.
    'Step One: Navigate to page': function (browser) {
        browser
          .url('http://example.com')
          .pause(2000);
    },

    'Step Two: Gather information': function (browser) {
        browser.getText('@element', function(result) {
            // Assign the value of the result inside your callback function
            // For this test, we're assuming the text of @element is 'bar'
            myTestVar = result.value;
        });
        // Since nightwatch runs asynchronously, this will print 'foo'
        console.log(myTestVar);
    },

    'Step Three: Print out result': function (browser) {
        // This code will only run after Step Two has completed
        // therefore, this will print 'bar'
        console.log(myTestVar);
    };