cypress: Simple tests pass in opened UI but fail on command line (application error)
Current behavior:
Simple tests against web-app work, but when I run them in the headless mode, they fail and the error given is an application error that seems unrelated (or an indication that the web-page didn’t finish loading.
Uncaught SyntaxError: Unexpected token (

If I replace the ‘uncaught:exception’ event in cypress like the documentation suggests,
Cypress.on('uncaught:exception', (err, runnable) => {
return false
})
I get a totally different error complaining about my usage of get
CypressError: Timed out retrying: Expected to find element: 'input[name=email]', but never found it.
I suspect this is because the page never finished loading, so it’s probably not important.

Desired behavior:
My tests should run in both interactive mode and headless mode.
How to reproduce:
I’m not sure how to reproduce except with my app (React/Redux with Apollo). I have confirmed that I can run the example cypress tests fine (in a clean state) both from UI and from command-line.
Test code:
const DEFAULT_URL = 'http://localhost:3000'
describe('Basic e2e tests', function() {
it('.should() - assert that <title> is correct', function() {
cy.visit(`${DEFAULT_URL}/login`)
cy.title().should('include', 'React App') // <----- this works
})
context('Logging in', function() {
beforeEach(function() {
cy.visit(`${DEFAULT_URL}/login`)
})
it('Should be able to login', function() {
cy.get('input[name=email]') // <------ failure here
I tried adding a wait, but it seems like it never loads. The screenshot definitely makes it look like it never loads.
Additional Info (images, stack traces, etc)
Full stack trace of the code where I let the error propagate:
CypressError: Timed out retrying: Expected to find element: 'input[name=email]', but never found it.
at Object.cypressErr (http://localhost:3000/__cypress/runner/cypress_runner.js:65978:11)
at Object.throwErr (http://localhost:3000/__cypress/runner/cypress_runner.js:65943:18)
at Object.throwErrByPath (http://localhost:3000/__cypress/runner/cypress_runner.js:65970:17)
at retry (http://localhost:3000/__cypress/runner/cypress_runner.js:60079:16)
at http://localhost:3000/__cypress/runner/cypress_runner.js:52597:18
at tryCatcher (http://localhost:3000/__cypress/runner/cypress_runner.js:6268:23)
at Promise._settlePromiseFromHandler (http://localhost:3000/__cypress/runner/cypress_runner.js:4290:31)
at Promise._settlePromise (http://localhost:3000/__cypress/runner/cypress_runner.js:4347:18)
at Promise._settlePromise0 (http://localhost:3000/__cypress/runner/cypress_runner.js:4392:10)
at Promise._settlePromises (http://localhost:3000/__cypress/runner/cypress_runner.js:4467:18)
at Async._drainQueue (http://localhost:3000/__cypress/runner/cypress_runner.js:1200:16)
at Async._drainQueues (http://localhost:3000/__cypress/runner/cypress_runner.js:1210:10)
at Async.drainQueues (http://localhost:3000/__cypress/runner/cypress_runner.js:1084:14)
- Operating System: macOS Sierra 10.12.6 (16G1212)
- Cypress Version:1.4.2
- Browser Version:Chrome?
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Reactions: 6
- Comments: 25 (8 by maintainers)
I have the same issue with latest Cypress, is there an official workaround for this issue?
In my experience, most of these cases (when tests fails only in headless mode) it’s due the fast speed.
Example: in my app, after the login, we have a request to get more info about the user, and only then store some data in local storage and cookies.
While testing in Chrome, or even with Electron, it works. Headless, it was randomly failing (because the request was not completed, so user info was not saved, redirecting to login again).
To fix, I just include a check for some element that should be displayed only after the user request is finished, like one of item of the logged user menu, like
cy.contains('Home').I just found that running headless never waited when the page was loading. The issue for me is that running tests in chrome or headed only works for the first 5 tests. After that, the chrome or electron became very slow, and sometimes chrome would be broken and show the error message ‘Aw, Snap’. RIP
Hi, I am having same issue where my test works on cy.open but fails on cy.run , I am using cy.request to POST a call to my api, giving timeout when using cy.run, why could it be ?
The API call is POST to login.microsoftonline.com/mycompany.onmicrosoft.com
Thanks.
Or you could also just run in Chrome from the command line with
--browser chromeargument.https://docs.cypress.io/guides/guides/command-line.html#
@mkatrenik there’s no way based on what you’ve provided to be of any assistance. In headless you get a full recorded video + screenshots. You’d need to post all of those, plus your test code.
If I were to guess it’s likely a problem of elements being re-rendered while Cypress code is running. It’s possible that you can lead Cypress down a path of elements, deeper and deeper, and then a parent element is rerendered which detaches all of the children. In those cases it’s impossible to proceed and it will fail.
Best practice is to always perform actions as high up in the chain as possible. In your case you have a
getand thenfindand thenclick. It’s best to dogetand thenclickto avoid this as much as possible.Another option is to add more assertions and “guards” around Cypress so it doesn’t proceed until the state of your application has settled first.
@brian-mann I have same problem - piece if code in headed electron it works, in headless electron it fails. But there is 1 strange thing - it fails on selector which returns 1 match, this is visible in inspector, but in UI it says NO matches.
I am almost 100% positive this is because when running headlessly we use Electron (Chromium 53) - and when you launch the browser it is a much newer version of Chrome.
The difference is that the older browser likely does not have newer JS features which is why it’s failing. Turning off uncaught exceptions won’t actually make the page render - it just hides catching that error. If you were to open your dev tools when running from Electron you would see the error.
This document explains all of this: https://docs.cypress.io/guides/guides/launching-browsers.html
However we are about to release
2.0.0very soon - like today or tomorrow which bumps Chromium 53 to 59 - which is much newer and these types of differences will be covered up.I’m closing the issue assuming this is the problem.