top of page
Search

Cypress Error Capture for Automated TestRail Results

  • Writer: Kimberly Coleman
    Kimberly Coleman
  • Apr 11, 2024
  • 1 min read

Updated: Apr 25, 2024

A Cypress error event listener to capture and store error messages and report them to TestRail results. Thus aiding defect tracking, creating test result history, and reportability.


1. Cypress Error Listener

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

Create an event listener to capture error messages.



2. Cypress Command using 'add_result' API Call

Cypress.Commands.add('testRailReport', (status, testID, errMess) => {
if (status == 'failed') {
	cy.request({
	method: 'POST',
	url: 'https://yoururl.testrail.com/index.php?/api/v2/add_result/' + testID,
	body: {
		status_id: 5,
		comment: 'This test failed for reason: ' + errMess,
		elapsed: '1s'
	}
})
}

Create a Cypress command to post an 'add_result' API call to update the relevant testcase with the error message.



3. 'afterEach' hook

afterEach('Send TestRail Report', function () {
	let status = this.currentTest.state
	cy.testRailReport(status, testID, errMess)
	})
})

Update the correct testcase after each test scenario using an 'afterEach' hook.



4. (Optional) cy.task() to save variables between tests

on('task', {
	setTestID: (val) => {
		testID = val
		return null
	},
	getTestID: () => {
		return testID
	}
})
afterEach('Send TestRail Report', function () {
	let status = this.currentTest.state
	cy.task('getTestID').then((testID) => {
		cy.testRailReport(status, testID, errMess)
	})
})

It is best practice to avoid sharing variables between tests in Cypress. The framework often requires unique thinking to avoid this happening. However, there are many ways to do so if you really need to.

A task can be set up in the Cypress config file which is active in the background behind Cypress. This way the value of 'testID' can be saved between tests.

Comments


Address

Straffordville, ON

Phone

226-377-0899

Email

Connect

  • LinkedIn
bottom of page