简体   繁体   中英

How to use loop in nightwatch.js

There is a function in pageObject

this.openFilmPage = function() {
    browser.waitForElementVisible("div[class='pg'", 1000);
    for (var i = 0; i < 4; i += 1) {
        console.log('Итерация № ' + i);

        browser.waitForElementVisible("div[class='app'] > div[class='pg'] > div[class='home carousel'] > div[class='home-lst carousel-lst'] > div[class*='home-lst-itm nav-itm']:nth-of-type(" + i + ") > div[class='badge badge-currency']", 100, function(result) {
            if (result.value) {
                return browser;
                console.log('Платный фильм обнаружен');
            } else {
                return browser;
                console.log('Платный фильм отсутствует');
            }
        });

    }
    return browser;
}

There is a challenge in the test

'TC67 Переход на страницу платного фильма': function(browser) {
    browser
        .page.App().open()
        .page.App().openFilmPage()
        .end();
}

After starting i get next result in console

Running: TC67 Переход на страницу платного фильма Итерация № 0 Итерация № 1 Итерация № 2 Итерация № 3

Then there is a check, once a zero position, the test fails because in this position is not expected item.

I expect that all tests will be carried out one by one and as a result I get an element that matches the specified css path.

But as experience has shown this is not so, the cycles appear to run in asynchronous mode, as it can be overcome?

PS. Sorry for my english.

It looks like you're attempting to create a custom command. Check out this section:

http://nightwatchjs.org/guide#writing-custom-commands

You'll notice that the name of the file you need to create should be the actual method name (so in your case: openFilmPage.js)

Lastly; you'll need to add the path in nightwatch.json to what folder your custom-commands are in ( http://nightwatchjs.org/guide#basic-settings )

Hope that helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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