How to wait for an element to disappear in TestCafe?

我的未来我决定 提交于 2019-12-10 17:25:42

问题


When I need to wait for an element to become visible I can simple call the selector as a function like this:

await element.with({ visibilityCheck: true })();

But how can I wait for an element to disappear?


回答1:


To wait for an element to disappear you can use our built-in waiting mechanism for assertions. Please see the documentation for more information on how it works.

import { Selector } from 'testcafe';

fixture `fixture`
    .page `http://localhost/testcafe/`;

test('test 2', async t => {
    //step 1

    //wait for the element to disappear (assertion with timeout)
    await t.expect(Selector('element').exists).notOk({ timeout: 5000 });

    //next steps
});

Or you can use the ClientFunction:

import { ClientFunction } from 'testcafe';

fixture `fixture`
    .page `http://localhost/testcafe/`;

const elementVisibilityWatcher = ClientFunction(() => {
    return new Promise(resolve => {
        var interval = setInterval(() => {
            if (document.querySelector('element'))
                return;

            clearInterval(interval);
            resolve();
        }, 100);
    });
});

test('test 1', async t => {
    //step 1

    //wait for the element to disappear
    await elementVisibilityWatcher();

    //next steps
});


来源:https://stackoverflow.com/questions/54970442/how-to-wait-for-an-element-to-disappear-in-testcafe

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!