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?
When TestCafe executes a Selector query, it waits for the target element to appear in the DOM. The query fails if it cannot find the target within the Selector timeout. The target element doesn't have to be visible for the Selector to succeed. import { Selector } from 'testcafe'; fixture `My fixture` .
You can specify the assertion query timeout in test code with the options. timeout option. To set the timeout when you launch tests, pass the timeout value to the runner. run method if you use API or specify the assertion-timeout option if you run TestCafe from the command line.
fixture Function A fixture is a group of tests with the same starting URL. Every test belongs to a fixture. The Fixture object offers methods that configure the following: The test page URL. Custom fixture metadata.
Element Selectors filter the DOM and return page elements that match user-defined criteria. They are similar to CSS Selectors in both purpose and syntax. For more information, see the Element Selectors guide.
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
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With