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

Ada*_*aft 4 testing automated-tests e2e-testing testcafe

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

await element.with({ visibilityCheck: true })();
Run Code Online (Sandbox Code Playgroud)

但是如何等待元素消失呢?

Vla*_* A. 7

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

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
});
Run Code Online (Sandbox Code Playgroud)

或者您可以使用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
});
Run Code Online (Sandbox Code Playgroud)

  • @TallKU,他们的文档中有一个示例:https://devexpress.github.io/testcafe/documentation/test-api/built-in-waiting-mechanisms.html (2认同)
  • timeout参数定义了等待的限制。间隔检查条件。因此,您可以将“超时”设置为50秒,但在满足条件时将执行操作。如果条件在10秒钟后执行,则将忽略“超时”。 (2认同)