-
Notifications
You must be signed in to change notification settings - Fork 3.3k
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
Some tests flake only if test runner's browser loses focus (or run headlessly) #5023
Comments
This has been something @bkucera has been working on. It is not an easy issue to solve. Thanks for providing a reproducible issue. I wasn't able to reproduce it in the few short times I ran this - could you explain what 'often fails' looks like? Do we have to run the test 5 times to see the error? 100 times? Test code: it('is just an example', function () {
cy.visit('https://noredink.com/logout')
cy.contains('Log In').click()
cy.get('#Nri-Ui-TextInput-Email-or-username').type('example_user')
cy.get("#Nri-Ui-TextInput-Password").type('easypassword')
cy.contains('button', 'Log in').click()
}) |
Hmm, I'm not sure. For me it fails almost every time, but a colleague said it was working for him, so there may be something about our setup or what we're doing that isn't exactly the same. The tiny example I gave runs pretty quickly, so if you don't switch focus away from the browser right away, it will already have passed. You could have it wait for a few seconds if you want to make sure you've switched focus away before it gets to that login menu. Let me know if there's anything else about my browser, computer, or config that would be helpful to share ... can also jump on a call with someone if it would be helpful to see it live. |
not sure if related but I am also noticing that tests seem to randomly fail when running headless but run perfectly and consistently when run with the --headed flag. And it's not the same tests that randomly fail, it's quite random, but only happens when running headless. not to mention that tests complete slower (sometimes MUCH slower) when running headless |
@blunderdome I think your try using the code @jennifer-shehane wrote, and see if the issue goes away. |
I am experiencing something like this related to the Specifically, I have a popup that is hidden when it loses focus (when it receives |
@fr0 this should have been fixed in 3.4.1, could you give a reproducible? |
Sure, here you go: https://github.com/fr0/cypress-focus-test |
This is a big issue for us testing our accessibility features, particular the "Skip to Main Content" feature in which the button is only visible on focus. |
This comment has been minimized.
This comment has been minimized.
Another example, this UI also seems to have some logic listening to some sort of 'focus' event. failing spec: it("Navigates to help article with mobile header menu", () => {
cy.viewport("iphone-x");
cy.visit("https://help.doterra.com/");
cy.get('[data-aura-class="cHC_StartingCategories"]', { timeout: 25000 });
cy.get('[data-aura-class="cDT_HeaderMobileMenu"]')
.click();
cy.get('.dtds-chevron-arrow-right[data-value="section-Help Mobile"]')
.click();
// this assertion fails if the window is not focused
cy.get('.mobile-menu-carousel').should('be.visible')
}); |
Same issue here with the Behaviour
Steps to reproduceSimplified code with problem Cypress test code describe("Cypress fails when not in focus", () => {
it("Try to click on the modal text button", () => {
// deployed from codesandbox
cy.visit("https://csb-lu86t-40qtfxquh.now.sh/");
// gives us time to minimize (de-focus) the Cypress window
cy.wait(5000);
cy.get("[data-cy=show-modal]").click();
// error here!
cy.get("[data-cy=modal-text]").click();
});
}); Error message (only with Chrome 80 not in focus or CI/headless)
Versions |
@bodazhao I have one test case which often fails in CI/headless with the same setup as you do:
|
Is there any way to approach this? We are having issues with native checkboxes, if the window doesn't have focus the checkboxes aren't found by the cy.get() command. |
I've been experiencing similar "flakiness" in my tests, only when the window is in the background or minimized. The tests that fail all assert on visibility or click elements like Modals or Dropdowns, and they fail with an error stating that the element has opacity set to 0 (and thus is not visible). This seems to jive with the other examples in the thread, which all seem to be Modals/Dropdowns or similar. From Mozilla:
Note that the Cypress test runner embeds the application being tested in an iframe, so the above optimization would apply here. I suspect that Chrome is pausing/throttling calls to requestAnimationFrame() when the Cypress window is backgrounded/minimized/occluded, and thus not rendering these items. I'm sorry that I don't have a solution for this, but I'm pretty confident this is at least one of the causes. Chrome also throttles JS timers in background tabs, but there is a flag to disable this, which I tried, but didn't see any improvement. A note on reproducibility: Chromium just introduced occlusion tracking on Windows for Chrome 87, but has supported it for some time on macOS and some other operating systems. This may cause the iframe on Windows to still be considered "visible" even if it was covered by another window. This may be why @jennifer-shehane couldn't reproduce this in August 2019, assuming she was using a Windows machine. I don't have a windows machine to test, but theoretically you could reproduce by opening a new tab in the test runner to force the runner to the background. |
I have a PR open to disable backgrounding occluded windows in Chrome. This could fix some of these issues for Chrome that people are encountering: #9640
As for the comment on
|
@jennifer-shehane thanks for the prompt response. I put together a little test repo to: A) reproduce the failure reliably (on my machine at least) Instructions to reproduce and demonstrate the requestAnimationFrame() throttling are in the README. The Thanks again for all that you do, Cypress is dope 👍 |
Chiming in: 6.2.1 fixed this completely for me Thanks @jennifer-shehane and team |
I've just upgraded to 6.3.0, but unfortunately that didn't fix it for me. My use case essentially boils down to: document.addEventListener('keydown', function (event) {
if (document.querySelector('[data-some-specific-selector]:focus') && event.code === 'Space') {
doSomething();
}
}); With a Cypress test which does: cy.get('[data-some-specific-selector]').focus().type(' ');
// Some kind of assertion to check that doSomething was called And it runs fine when Cypress stays in the foreground, but if I switch to another application while the test is running then I hope that's useful. Thank you for looking at this, I appreciate the effort being put into tackling this issue. |
I'm on Windows, Cypress 7.1.0, Electron 89, I'm doing a Here is the div i'm clicking on: With |
Is there any workaround for this? I'm doing a drag & drop feature and when I run the tests in interactive mode with the focus on the tests browser windows, all pass, but if I run with the window minimized or test from console always fail the same tests when I start to draw the line. Below is the test in question:
|
I've encountered a similar problem while testing an Angular JS application:
|
Hi Team, is there any update on this. We are also running into the same issue. Thanks! |
@nyghtly-derek similar issue for but with React app. We are finding focus is lose causing the app to be in an incorrect state if focus is on another app other than the cypress browser. Running headless sometimes has the same issue for us though. cypress@8.5.0 |
Current behavior:
We have a number of tests which:
Desired behavior:
Browser does not need to stay in focus; tests also pass headlessly.
Steps to reproduce: (app code and test code)
Here is a simple case - logging into our production app. This passes if you keep the browser in focus but sometimes fails if it loses focus (or is run headlessly):
Video: in focus and passing
Video: out of focus and failing
We have other examples but they involve the admin side of our site and we probably shouldn't share them here. If you need more examples, let me know and we'll try to find something else that's appropriate to share.
Versions
Cypress: 3.4.1
OS: macOS Mojave 10.14.5
Browser(s): Chrome 76 and Electron 61
Other
#1892 may be relevant.
The text was updated successfully, but these errors were encountered: