简体   繁体   English

如何在nightwatch.js中使用循环

[英]How to use loop in nightwatch.js

There is a function in pageObject 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 跑步: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. 我希望所有测试都将一一进行,结果我得到一个与指定的CSS路径匹配的元素。

But as experience has shown this is not so, the cycles appear to run in asynchronous mode, as it can be overcome? 但是,正如经验表明的那样,周期似乎以异步模式运行,可以克服吗?

PS. 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 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) 您会注意到,您需要创建的文件的名称应为实际的方法名称(因此,在您的情况下: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 ) 您需要将nightwatch.json中的路径添加到您的自定义命令所在的文件夹中( http://nightwatchjs.org/guide#basic-settings

Hope that helps! 希望有帮助!

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

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