Element is never found when timeout options are overrided

This issue has been tracked since 2022-02-19.

Hello,

Have an example here of checking if element is visible on the page, but it can appear later so I refresh the page each time. Also, overrided default options.

cy.waitUntil(
        () => {
            cy.reload().then(
                () => Cypress.$(selector).length,
            );
        },
        {
            interval: 10000,
            timeout: 120000,
            errorMsg: 'Element not found',
        },
    );

The problem is that, even if element appeared on the page, waitUntil never stops and fails with Element not found.

And this example fails with You are mixing async and sync code when element is found.

cy.waitUntil(
        () => {
            cy.reload();
            return Cypress.$(selector).length;
        },
        {
            interval: 10000,
            timeout: 120000,
            errorMsg: 'Element not found',
        },
    );

Could you please help? What am I doing wrong?
Thank you

NoriSte wrote this answer on 2022-02-21

hi @mturcan
Could you please a minimal repo that reproduces the problem, please? You can also fork this repository, change public/index.html to recreate the same circumstances, and add a test to show the problem if you prefer.

Thanks
Stefano

mturcan wrote this answer on 2022-02-21

Hi @NoriSte

I have created the PR with an additional test that searches for an existing element with reload. So element should be found after first reload right? Instead it's reloading the page the whole timeout specified and fails with 'Element not found'.

Thank you,
Maria

NoriSte wrote this answer on 2022-02-22

Change you code this way

cy.waitUntil(
-   () => {
+   () =>
        cy.reload().then(
            () => Cypress.$(selector).length,
        );
-   },
    {
        interval: 10000,
        timeout: 120000,
        errorMsg: 'Element not found',
    },
);

The error was that checkFunction, the first parameter of cy.waitUntil, doesn't return a truthy value. The README says

checkFunction: a function that must return a truthy value when the wait is over.

while your function returns undefined ๐Ÿ˜Š

By using the code I shared, it works for me locally, please let me know it it works for you too ๐Ÿ˜Š

mturcan wrote this answer on 2022-02-22

@NoriSte it works in the forked repo, however it fails in my original project :(

thank you anyway!

NoriSte wrote this answer on 2022-02-22

@NoriSte it works in the forked repo, however it fails in my original project :(

Ok, at least it's not this plugin's fault ๐Ÿ˜ I guess the element appears later compared to the load event Cypress waits before "unlocking" the visit command?

More Details About Repo
Owner Name NoriSte
Repo Name cypress-wait-until
Full Name NoriSte/cypress-wait-until
Language JavaScript
Created Date 2019-04-27
Updated Date 2023-03-19
Star Count 635
Watcher Count 8
Fork Count 24
Issue Count 3

YOU MAY BE INTERESTED

Issue Title Created Date Updated Date