Mihail Kuznetsov
Mihail Kuznetsov

Reputation: 331

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.

Upvotes: 2

Views: 4555

Answers (1)

Petrogad
Petrogad

Reputation: 4423

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!

Upvotes: 1

Related Questions