如何断言NightwatchJS中的元素列表

时间:2016-04-07 20:43:31

标签: javascript selenium nightwatch.js e2e-testing

如果给定类名的所有元素都可见,我想用NightwatchJS断言。所以有这样的话:

module.exports = {
    //some tests

    'page changes to English': (browser) => {
        browser.click('label[for="switch-language"]').pause(1000);
        browser.elements('css selector', '.fr', results => {
            for (let i = 0; i < results; ++i) {
                browser.expect.element(results[i]).to.be.visible;
            }
        });
    }
};

但是在测试完成后,我得到了No assertions ran.

的输出

1 个答案:

答案 0 :(得分:3)

感谢Juhi Saxena对following thread的回答,我能够找到问题的答案。以下是我可能遇到同样困难的人的代码:

module.exports = {
    //some tests

    'page changes to English': browser => {
        browser.click('label[for="switch-language"]').pause(1000);
        browser.elements('css selector','.fr', areDisplayed.bind(null, browser, false));
        browser.elements('css selector','.en', areDisplayed.bind(null, browser, true));
    }
};

function areDisplayed(browser, expected, elements) {
    elements.value.forEach(element => {
        browser.elementIdDisplayed(element.ELEMENT, result => {
            browser.assert.equal(result.value, expected);
        });
    });
}