如何使用Cypress更新Zephyr Scale测试周期执行?

huangapple go评论67阅读模式
英文:

How to update Zephyr Scale Test Cycle Execution using Cypress?

问题

以下是翻译好的部分:

  • 我试图找到一种方法将我的Cypress测试执行更新到Zephyr Scale。 我不需要更新测试步骤。 我只需要为了追溯和报告的目的来更新相关的测试用例。

  • 我的解决方案如下,以及在使用它之前的一些建议:

  • 我编写的代码放在Cypress的afterEach()挂钩中。 这样,每当Cypress完成执行其中一个测试时,结果将被更新到Zephyr。

  • 在Cypress中使用的测试名称 必须 包含Zephyr测试用例ID,否则脚本将无法工作。

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"
                    }
                })
            })
    }
})

欢迎任何改进实现的建议。谢谢!

英文:

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

答案1

得分: 2

您有比您需要的更多的字符串模板,您可以通过使用查找/映射对象将值插入单个cy.request()中。

根据测试结果,不同的属性被映射。

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')

// 查找与状态相关的值,根据结果将适用于其中之一
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]  // 从查找中展开属性
}

cy.request({
  headers: {
    Authorization: `Bearer ${token}`,
  },
  url,
  method: 'POST',
  body
})
英文:

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
})

答案2

得分: 0

感谢Chinaza的建议。我已根据您的建议更新了我的代码。

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
      });
    });

})
英文:

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
      });
    });

})

huangapple
  • 本文由 发表于 2023年6月2日 08:24:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/76386479.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定