在Nightwatch.js中等待多个异步操作

时间:2016-10-06 19:41:28

标签: javascript asynchronous nightwatch.js

我正在尝试以正确的顺序测试多个网站的节标题。当然,Nightwatch中的所有内容都是异步的,包括从元素中获取文本。以下代码导致永远不会被调用超时。

client.url(`www.oneofmyrealestatesites.com`);
client.waitForElementPresent("body", 5000);
var _ = require("underscore");
// This is the order I expect things to be in
var expected = ["Homes For Sale", "New Homes", "Apartments & Rentals"];
client.elements("css selector", ".listings .module-title .label", function (data) {
  var listings = [];
  data.value.forEach(function (element) {
    client.elementIdText(element.ELEMENT, function (result) {
      listings.push(result.value);
    })
  })
  setTimeout(function () {
    // Some of the sites have extra sections
    var diff = _.intersection(listings, expected);
    client.assert.ok(listings == diff);
  }, 5000);
});

看来无论我给出多少延迟,列表总是空的。如果我将console.log列表推送到,那么 会被填充,所以这不是问题。 client.pause也始终被忽略。

有没有办法确保在断言差异之前填充列表?

2 个答案:

答案 0 :(得分:0)

我使用async库来处理此类案件https://github.com/caolan/async 文档:https://github.com/caolan/async/blob/v1.5.2/README.md

var async = require("async");

/*use each, eachSeries or eachLimit - check docs for differences */
async.eachSeries(data.value, function (element, cb) {
    client.elementIdText(element.ELEMENT, function (result) {
      listings.push(result.value);
      // this job is done
      cb();
    });
}, function() {
    // Some of the sites have extra sections
    var diff = _.intersection(listings, expected);
    client.assert.ok(listings == diff);
});

答案 1 :(得分:0)

setTimeout只能从.execute或.executeAsync调用,因为它是实际的JavaScript。下面的函数仅在使用.executeAsync

之前有效

希望这对您有用。

干杯,罗迪

 LoopQuestionsLogSymptom: function() {
        this.waitForElementVisible('.next-button', constants.timeout.medium, false);
        this.api.executeAsync(function() {
            let checkQuestion = function() {
                // GET THE ELEMENTS
                let nextButton = document.querySelectorAll('.next-button');
                let answers = document.getElementsByClassName('flex-row');
                let blueButton = document.querySelector('.blue-inverse-button');
                let doneButton = document.querySelector('#doneButton');
                let monitor = document.querySelector('.monitor');
                //CHECK THE TYPES OF QUESTIONS AND CLICK THE RIGHT BUTTONS
                if (!blueButton) {
                    answers[0].click();
                    nextButton[0].click()
                } else if(blueButton){
                    blueButton.click();
                }
                setTimeout(() => {
                    if(!doneButton) {
                        console.log('Answering another question!');
                        checkQuestion();
                    }
                    if(doneButton){
                        doneButton.click();
                    }
                    else if(monitor) {
                        console.log("Exiting?")
                        .assert(monitor);

                        return this;
                    }
                }, 2000);
            };

            // Initiating the check question function
            return checkQuestion();

        },[], function(){
            console.log('Done?')
        });
        this.waitForElementVisible('.monitor', constants.timeout.medium);
            this.assert.elementPresent('.monitor');
            this.assert.urlEquals('https://member-portal.env-dev4.vivantehealth.org/progress');
        return this;
    },