Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Element is never found when timeout options are overrided #355

Closed
mturcan opened this issue Feb 19, 2022 · 5 comments
Closed

Element is never found when timeout options are overrided #355

mturcan opened this issue Feb 19, 2022 · 5 comments

Comments

@mturcan
Copy link

mturcan commented Feb 19, 2022

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
Copy link
Owner

NoriSte commented Feb 21, 2022

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
Copy link
Author

mturcan commented Feb 21, 2022

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
Copy link
Owner

NoriSte commented Feb 22, 2022

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
Copy link
Author

mturcan commented Feb 22, 2022

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

thank you anyway!

@NoriSte
Copy link
Owner

NoriSte commented Feb 22, 2022

@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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants