简体   繁体   English

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

[英]Waiting for multiple async operations in Nightwatch.js

I am attempting to test multiple sites for section headers being in the correct order. 我试图测试多个网站的节标题是否正确。 Of course everything is asynchronous in Nightwatch, including getting text from an element. 当然,在Nightwatch中,一切都是异步的,包括从元素获取文本。 The following code leads to the timeout never being called. 以下代码导致超时永不被调用。

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);
});

It would appear that no matter how much delay I give, listings is ALWAYS empty. 看来,无论我给多少延迟,列表始终是空的。 If I console.log listings as it's being pushed to, it is getting populated, so that's not the issue. 如果我CONSOLE.LOG上市,因为它是被推到,它越来越稀少,所以这不是问题。 client.pause is always ignored as well. client.pause也总是被忽略。

Is there a way to make sure that listings is populated before asserting the diff? 有没有一种方法可以确保在声明差异之前填充列表?

I'm using async library for such cases https://github.com/caolan/async Docs: https://github.com/caolan/async/blob/v1.5.2/README.md 我在这种情况下使用asynchttps://github.com/caolan/async Docs: 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);
});

setTimeout can only be called from .execute or .executeAsync because its actual javascript. setTimeout只能从.execute或.executeAsync调用,因为它是实际的javascript。 The function below was only working until I used .executeAsync 以下功能仅在使用.executeAsync之前有效

Hope this works for you. 希望这对您有用。

Cheers, Rody 干杯,罗迪

 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;
    },

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM