如何等待元素在 TestCafe 中消失?
How to wait for an element to disappear in TestCafe?
当我需要等待一个元素变得可见时,我可以像这样简单地调用选择器作为一个函数:
await element.with({ visibilityCheck: true })();
但是我怎么能等到一个元素消失呢?
要等待元素消失,您可以使用我们内置的断言等待机制。请参阅 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
});
当您等待元素接收时,这会完成工作 display:none; ::
await browser.expect( Selector(".m-loader").visible ).notOk({ timeout: 30000 });
当我需要等待一个元素变得可见时,我可以像这样简单地调用选择器作为一个函数:
await element.with({ visibilityCheck: true })();
但是我怎么能等到一个元素消失呢?
要等待元素消失,您可以使用我们内置的断言等待机制。请参阅 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
});
当您等待元素接收时,这会完成工作 display:none; ::
await browser.expect( Selector(".m-loader").visible ).notOk({ timeout: 30000 });