如何等待元素在TestCafe中消失?

问题描述 投票:4回答:1

当我需要等待元素变得可见时,我可以简单地将选择器调用为如下函数:

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

但我怎么能等待元素消失呢?

testing automated-tests e2e-testing testcafe
1个回答
7
投票

要等待元素消失,您可以使用我们内置的等待机制进行断言。有关其工作原理的更多信息,请参阅the documentation

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
});

或者你可以使用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
});
© www.soinside.com 2019 - 2024. All rights reserved.