Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to wait for an element to disappear in 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?

like image 236
Adam Genshaft Avatar asked Mar 03 '19 15:03

Adam Genshaft


People also ask

How do you wait for an element to be visible in TestCafe?

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` .

How do you assert in TestCafe?

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.

What is fixture in TestCafe?

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.

What is selector in TestCafe?

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.


1 Answers

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
});
like image 176
Vladimir A. Avatar answered Sep 26 '22 04:09

Vladimir A.