本文介绍了如何等待元素在TestCafe中消失?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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?

推荐答案

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

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

或者您可以使用 ClientFunction :

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

这篇关于如何等待元素在TestCafe中消失?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 07:36