当元素实际存在时,NoSuchElementError

时间:2015-04-27 16:01:28

标签: javascript angularjs testing selenium protractor

我在一个角度测试中有这条线:

element(by.id('male')).click();

测试失败时收到此消息:

NoSuchElementError: No element found using locator: By.id("male")

但是当我在测试中添加browser.sleep()时,我可以看到页面上有#male元素。

所以我尝试添加显式等待并没有帮助。可能有什么不对?

1 个答案:

答案 0 :(得分:0)

我经常使用以下辅助函数来等待元素出现或显示。也许试一试,Protractor会阻止它们返回true。也许给他们一个机会。

module.exports = {
    /**
     * Block until an element is displayed on the page.
     *
     * @param {object} elem
     * The element to watch for.
     *
     * @param {number} [customTimeout=10000]
     * How long to wait for `elem` to appear.
     */
    waitForElementToBeDisplayed: function (elem, customTimeout) {
        var timeout = customTimeout || 10000;
        browser.wait(function () {
            return elem.isDisplayed().then(
                function (visible) {
                    return visible;
                },
                function () {
                    return false;
                }
            );
        }, timeout, 'failed to find element ' + elem.locator());
    },

    /**
     * Block until an element is present on the page.
     *
     * @param {Object} elem
     * The element to watch for.
     *
     * @param {Number} [customTimeout=10000]
     * How long to wait for 'elem' to appear.
     */
    waitForElementToBePresent: function (elem, customTimeout) {
        var timeout = customTimeout || 10000;
        browser.wait(function () {
            return elem.isPresent().then(
                function (visible) {
                    return visible;
                },
                function () {
                    return false;
                }
            );
        }, timeout, 'Element not present: ' + elem.locator());
    }
}