16

i tried to test this simple code

       type Url = string
       it('loads examples', () => {
      const url: Url = 'https://www.ebay.com/'
       cy.visit(url)
       cy.get('input[type="text"]').type('book')
        cy.get('#gh-btn').click();

        })

then I faced this error

enter image description here

how can I solve it

leen M
  • 477
  • 1
  • 5
  • 14
  • at first glance looks to be a resource redirect problem. You can try to set `chromeWebSecurity: false` on your `cypress.json` file and try it again. Also, your issue could be related to this https://github.com/cypress-io/cypress/issues/4220, so you can go throw the thread and find out more details – Alex Izbas Jul 20 '20 at 07:11

4 Answers4

28

Try adding this in support/index.js:

import './commands'
Cypress.on('uncaught:exception', (err, runnable) => {
  // returning false here prevents Cypress from failing the test
  return false
})

This should avoid the uncaught:exception in the click() method.

bad_coder
  • 11,289
  • 20
  • 44
  • 72
leen M
  • 477
  • 1
  • 5
  • 14
7

The accepted answer will cause Cypress to ignore all uncaught exceptions in the application. Generally, when these come up it means you found a bug in your app and should fix it.

Binding to the global Cypress object causes the event to stay bound for your entire test run. Usually, this isn't what you want.

If you actually need to ignore the exceptions though, you should be binding the event on the cy object so it's only persisted for the single test it's used in.

it('my test', () => {
  cy.once('uncaught:exception', () => false);
  
  // action that causes exception
  cy.get('body').click();
});
DJSDev
  • 812
  • 9
  • 18
0

I got Same Issue like this Cypress Error

The following error originated from your application code, not from Cypress. > Cannot read properties of null (reading 'textContent')
When Cypress detects uncaught errors originating from your application it will automatically fail the current test.
This behavior is configurable, and you can choose to turn this off by listening to the uncaught:exception event.Learn more

No Need to worry about this.

Just paste this code to your index.js file :)

import './commands'
   Cypress.on('uncaught:exception', (err, runnable) => {
   // returning false here prevents Cypress from
 // failing the test
   return false
   })
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Sep 27 '21 at 14:04
  • 2
    won't the answer accepted ignore all errors thrown by the application? – ZombiePie Dec 07 '21 at 14:13
0

Actually, if you click in the Learn more link that comes with the error, you'll get everything you need.

https://docs.cypress.io/guides/references/error-messages#Uncaught-exceptions-from-your-application


Quoting others:

won't this answer ignore all errors thrown by the application? The accepted answer will cause Cypress to ignore all uncaught exceptions

That is true.

Also, @DJSDev did not work for me when using Cypress v10.0.3.

The aforementioned link provides a working alternative:

it('is doing something very important', (done) => {
  // this event will automatically be unbound when this
  // test ends because it's attached to 'cy'
  cy.on('uncaught:exception', (err, runnable) => {
    expect(err.message).to.include('something about the error')

    // using mocha's async done callback to finish
    // this test so we prove that an uncaught exception
    // was thrown
    done()

    // return false to prevent the error from
    // failing this test
    return false
  })

  // assume this causes an error
  cy.get('button').click()
})
410Gone
  • 1
  • 1