0

I was trying to find a method to update my Cypress test execution into Zephyr Scale. I do not need to update the test step. I just need to update the related test cases for traceability and reporting purposes.

So this is my solution and some points before you can use it;

  • The code I wrote is placed in a Cypress afterEach() hook. By doing so, every time Cypress finish executing one of the tests, the result will get updated into Zephyr.

  • The test name as used in Cypress MUST have the Zephyr Test Case ID else the script won't work

afterEach(function() {

    console.log(this.currentTest.state)

    const pattern = /PROJECTCODE-T\d+/;
    const match = this.currentTest.title.match(pattern)

    console.log(match[0])

    const token = Cypress.env('bearerToken')
    const url = Cypress.env('zephyrBaseURL') + '/testexecutions'
    const testCycle = Cypress.env('testCycle')

    if(this.currentTest.state === 'passed'){
        cy.then(() => {
        cy.request({
            headers: {
                    Authorization: `Bearer ${token}`
                },
            url: `${url}`,
            method: 'POST',
            body: {
                    projectKey: "PROJECTCODE",
                    testCaseKey: `${match[0]}`,
                    testCycleKey: `${testCycle}`,
                    statusName: "Pass",
                    environmentName: "QA"
                }
            })
        })
    }else if (this.currentTest.state === 'failed')
    {
        cy.then(() => {
        cy.request({
            headers: {
                    Authorization: `Bearer ${token}`
                },
            url: `${url}`,
            method: 'POST',
            body: {
                    projectKey: "ALT",
                    testCaseKey: `${match[0]}`,
                    testCycleKey: `${testCycle}`,
                    statusName: "Fail",
                    environmentName: "QA"
                }
            })
        })
    }else if (this.currentTest.state === 'skipped')
    {
        cy.then(() => {
            cy.request({
                headers: {
                        Authorization: `Bearer ${token}`
                    },
                url: `${url}`,
                method: 'POST',
                body: {
                        projectKey: "ALT",
                        testCaseKey: `${match[0]}`,
                        testCycleKey: `${testCycle}`,
                        statusName: "Not Executed",
                        environmentName: "QA"
                    }
                })
            })
    }
})

Appreciate any suggestion to improve the implementation. Thanks

TesterDick
  • 3,830
  • 5
  • 18
Azri
  • 1
  • 3

2 Answers2

2

You have more string templating than you need to have, and you can slot values into a single cy.request() by using a lookup / mapping object.

Different properties are mapped depending on the test result.

const token = Cypress.env('bearerToken')
const url = Cypress.env('zephyrBaseURL') + '/testexecutions'

const testCaseKey = this.currentTest.title.match(/PROJECTCODE-T\d+/)[0]
const testCycleKey = Cypress.env('testCycle')

// look up status related values, one of these will apply depending on result
const states = {
  passed:  { projectKey: 'PROJECTCODE', statusName: 'Pass' },
  failed:  { projectKey: 'ALT', statusName: 'Fail' },
  skipped: { projectKey: 'ALT', statusName: 'Not Executed' },
}

const body = {
  testCaseKey,
  testCycleKey,
  environmentName: 'QA',
  ...states[this.currentTest.state]  // spread properties from lookup
}

cy.request({
  headers: {
    Authorization: `Bearer ${token}`,
  },
  url,
  method: 'POST',
  body
})
Chinaza
  • 21
  • 2
0

Thanks to Chinaza for your suggestion. I have updated my code accordingly.


afterEach(function() {

    const pattern = /PROJECTCODE-T\d+/
    const match = this.currentTest.title.match(pattern)
  
    const token = Cypress.env('bearerToken')
    const url = Cypress.env('zephyrBaseURL') + '/testexecutions'
    const testCycle = Cypress.env('testCycle')
  
    const requestBody = {
      projectKey: 'PROJECTCODE',
      testCaseKey: `${match[0]}`,
      testCycleKey: `${testCycle}`,
      statusName: '',
      environmentName: 'QA'
    };
  
    switch (this.currentTest.state) {
      case 'passed':
        requestBody.statusName = 'Pass'
        break;
      case 'failed':
        requestBody.statusName = 'Fail'
        break;
      case 'skipped':
        requestBody.statusName = 'Not Executed'
        break;
    }
  
    cy.then(() => {
      cy.request({
        headers: {
          Authorization: `Bearer ${token}`
        },
        url: url,
        method: 'POST',
        body: requestBody
      });
    });

})

Azri
  • 1
  • 3