top of page
Search

Catching error messages in Cypress for defect tracking

  • Writer: Kimberly Coleman
    Kimberly Coleman
  • Apr 9, 2024
  • 2 min read

Updated: Apr 11, 2024


ree

Here's a little bit of Cypress magic that I used to catch error messages and post them to other defect tracking tools like TestRail and JIRA.


let errMess = ''

Cypress.on('fail', (e) => {
	errMess = e.message
	console.log(errMess)
	throw e
})

Let's dissect it a little:


  1. let errMess = '': This line creates a variable called errMess and sets it to an empty string. This is what we will use to store error messages.

  2. Cypress.on('fail', (e) => { ... }): This line sets up an event listener. It listens for test failures and executes the callback function when a failure occurs.

  3. (e) => { ... }: This is the callback function that gets executed when a test failure event occurs. It takes one parameter, e, which represents the error object associated with the failed test.

  4. errMess = e.message: This line assigns the error message (e.message) of the failed test to the errMess variable. The error message typically describes what went wrong during the test execution.

  5. console.log(errMess): This line logs the error message to the console for viewing, it isn't strictly necessary but can help when debugging your code.

  6. throw e: This line rethrows the error (e) after logging it. Rethrowing the error allows Cypress to handle the failure as it normally would and continue on.

I used this to create TestRail test results using API calls. I would have a test suite set up ahead of time with the necessary test cases. Then I wrote an afterEach() hook that would send an API call using the add_result method. It contained the testcase status (Pass/Fail), the testcase ID, and the error message, if there was one.


This handy little bit of code allowed me to automate testing on a feature, and maintain accurate test history and tracking. All for the low, low price of 5 lines of code! 🤑

Comments


Address

Straffordville, ON

Phone

226-377-0899

Email

Connect

  • LinkedIn
bottom of page