当元素不存在时,量角器检查元素很慢

时间:2017-08-23 18:23:07

标签: angular protractor

我正在为一个大角度4项目编写e2e测试。我编写了一个辅助函数来关闭模态对话框(如果存在)。以前的操作可能会根据条件数据自动关闭对话框,因此我无法假设对话框已启动。

function closeModal() {
    Util.logInfo('closing');
    $('.modal-backdrop').isPresent().then(exists => {
        if (exists) {
            < HANDLE CLOSING DIALOG HERE >
        }
    });
    Util.logInfo('closed outer');
}

这段代码工作正常,但我所看到的是,当模态启动时,检查模态的存在总是需要10秒。我已使用elementelement.all.lengthcount()isPresent重写了此块,检查了对话框中的元素,可能还有一些其他我无法记住的方式。无论我尝试什么,每当'.modal-backdrop'不存在时承诺解决需要整整10秒钟。如果确实存在,则包含我的逻辑的代码在大约0.2秒内执行。

模态不起来:

13:06:47:431 -  ** closing
13:06:57:451 -  ** closed outer

模态向上:

13:06:57:563 -  ** closing
13:06:57:705 -    -> action: waiting for NOT presence of element up to 5000ms
13:06:57:718 -  ** closed outer

此块被调用很多,并且可能有20%的时间不需要关闭(但我仍然需要调用它以防万一,以防止错误的失败),并且它每次测试运行大约需要30-40秒。

无论如何都要加快速度,或至少暂时改变此块中所需的超时时间?

1 个答案:

答案 0 :(得分:1)

我认为您在代码中设置了implicitWaits。我之前回答过类似的问题,阅读它以了解implicitWaits是如何工作的 - Maxim Koretskyi

但在你的情况下,这可能会有所帮助:

function closeModal() {
    Util.logInfo('closing');
    browser.manage().timeouts().implicitlyWait(0)
    $('.modal-backdrop').isPresent().then(exists => {
        if (exists) {
            < HANDLE CLOSING DIALOG HERE >
        }
    });
    // Setting back your implicit value here, i think it is 10 seconds?
    browser.manage().timeouts().implicitlyWait(10000)
    Util.logInfo('closed outer');
}

想法是在查找可能不存在的元素之前禁用隐式等待,然后将其还原。请注意,我们并没有等待您的模态窗口的存在。它可能会有一点延迟,并且isPresent已经通过 - 所以考虑小等待作为一个选项:

function closeModal() {
    browser.manage().timeouts().implicitlyWait(0)
    let modalPresent = protractor.ExpectedConditions.presenceOf($('.modal-backdrop')) 
    browser.wait(modalPresent, 1000).then(()=> {
        <handle modal is present state here>
        }, err=> {
        // Just skipping, if modal not present. This prevents promise to be rejected
    })
    // Setting back your implicit value here, i think it is 10 seconds?
    browser.manage().timeouts().implicitlyWait(10000)
}
相关问题