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

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

How to update Zephyr Scale Test Cycle Execution using Cypress?

问题

以下是翻译好的部分:

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

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

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

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

  1. afterEach(function() {
  2. console.log(this.currentTest.state)
  3. const pattern = /PROJECTCODE-T\d+/;
  4. const match = this.currentTest.title.match(pattern)
  5. console.log(match[0])
  6. const token = Cypress.env('bearerToken')
  7. const url = Cypress.env('zephyrBaseURL') + '/testexecutions'
  8. const testCycle = Cypress.env('testCycle')
  9. if(this.currentTest.state === 'passed'){
  10. cy.then(() => {
  11. cy.request({
  12. headers: {
  13. Authorization: `Bearer ${token}`
  14. },
  15. url: `${url}`,
  16. method: 'POST',
  17. body: {
  18. projectKey: "PROJECTCODE",
  19. testCaseKey: `${match[0]}`,
  20. testCycleKey: `${testCycle}`,
  21. statusName: "Pass",
  22. environmentName: "QA"
  23. }
  24. })
  25. })
  26. }else if (this.currentTest.state === 'failed')
  27. {
  28. cy.then(() => {
  29. cy.request({
  30. headers: {
  31. Authorization: `Bearer ${token}`
  32. },
  33. url: `${url}`,
  34. method: 'POST',
  35. body: {
  36. projectKey: "ALT",
  37. testCaseKey: `${match[0]}`,
  38. testCycleKey: `${testCycle}`,
  39. statusName: "Fail",
  40. environmentName: "QA"
  41. }
  42. })
  43. })
  44. }else if (this.currentTest.state === 'skipped')
  45. {
  46. cy.then(() => {
  47. cy.request({
  48. headers: {
  49. Authorization: `Bearer ${token}`
  50. },
  51. url: `${url}`,
  52. method: 'POST',
  53. body: {
  54. projectKey: "ALT",
  55. testCaseKey: `${match[0]}`,
  56. testCycleKey: `${testCycle}`,
  57. statusName: "Not Executed",
  58. environmentName: "QA"
  59. }
  60. })
  61. })
  62. }
  63. })

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

英文:

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

  1. afterEach(function() {
  2. console.log(this.currentTest.state)
  3. const pattern = /PROJECTCODE-T\d+/;
  4. const match = this.currentTest.title.match(pattern)
  5. console.log(match[0])
  6. const token = Cypress.env('bearerToken')
  7. const url = Cypress.env('zephyrBaseURL') + '/testexecutions'
  8. const testCycle = Cypress.env('testCycle')
  9. if(this.currentTest.state === 'passed'){
  10. cy.then(() => {
  11. cy.request({
  12. headers: {
  13. Authorization: `Bearer ${token}`
  14. },
  15. url: `${url}`,
  16. method: 'POST',
  17. body: {
  18. projectKey: "PROJECTCODE",
  19. testCaseKey: `${match[0]}`,
  20. testCycleKey: `${testCycle}`,
  21. statusName: "Pass",
  22. environmentName: "QA"
  23. }
  24. })
  25. })
  26. }else if (this.currentTest.state === 'failed')
  27. {
  28. cy.then(() => {
  29. cy.request({
  30. headers: {
  31. Authorization: `Bearer ${token}`
  32. },
  33. url: `${url}`,
  34. method: 'POST',
  35. body: {
  36. projectKey: "ALT",
  37. testCaseKey: `${match[0]}`,
  38. testCycleKey: `${testCycle}`,
  39. statusName: "Fail",
  40. environmentName: "QA"
  41. }
  42. })
  43. })
  44. }else if (this.currentTest.state === 'skipped')
  45. {
  46. cy.then(() => {
  47. cy.request({
  48. headers: {
  49. Authorization: `Bearer ${token}`
  50. },
  51. url: `${url}`,
  52. method: 'POST',
  53. body: {
  54. projectKey: "ALT",
  55. testCaseKey: `${match[0]}`,
  56. testCycleKey: `${testCycle}`,
  57. statusName: "Not Executed",
  58. environmentName: "QA"
  59. }
  60. })
  61. })
  62. }
  63. })

Appreciate any suggestion to improve the implementation. Thanks

答案1

得分: 2

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

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

  1. const token = Cypress.env('bearerToken')
  2. const url = Cypress.env('zephyrBaseURL') + '/testexecutions'
  3. const testCaseKey = this.currentTest.title.match(/PROJECTCODE-T\d+/)[0]
  4. const testCycleKey = Cypress.env('testCycle')
  5. // 查找与状态相关的值,根据结果将适用于其中之一
  6. const states = {
  7. passed: { projectKey: 'PROJECTCODE', statusName: 'Pass' },
  8. failed: { projectKey: 'ALT', statusName: 'Fail' },
  9. skipped: { projectKey: 'ALT', statusName: 'Not Executed' },
  10. }
  11. const body = {
  12. testCaseKey,
  13. testCycleKey,
  14. environmentName: 'QA',
  15. ...states[this.currentTest.state] // 从查找中展开属性
  16. }
  17. cy.request({
  18. headers: {
  19. Authorization: `Bearer ${token}`,
  20. },
  21. url,
  22. method: 'POST',
  23. body
  24. })
英文:

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.

  1. const token = Cypress.env('bearerToken')
  2. const url = Cypress.env('zephyrBaseURL') + '/testexecutions'
  3. const testCaseKey = this.currentTest.title.match(/PROJECTCODE-T\d+/)[0]
  4. const testCycleKey = Cypress.env('testCycle')
  5. // look up status related values, one of these will apply depending on result
  6. const states = {
  7. passed: { projectKey: 'PROJECTCODE', statusName: 'Pass' },
  8. failed: { projectKey: 'ALT', statusName: 'Fail' },
  9. skipped: { projectKey: 'ALT', statusName: 'Not Executed' },
  10. }
  11. const body = {
  12. testCaseKey,
  13. testCycleKey,
  14. environmentName: 'QA',
  15. ...states[this.currentTest.state] // spread properties from lookup
  16. }
  17. cy.request({
  18. headers: {
  19. Authorization: `Bearer ${token}`,
  20. },
  21. url,
  22. method: 'POST',
  23. body
  24. })

答案2

得分: 0

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

  1. afterEach(function() {
  2. const pattern = /PROJECTCODE-T\d+/
  3. const match = this.currentTest.title.match(pattern)
  4. const token = Cypress.env('bearerToken')
  5. const url = Cypress.env('zephyrBaseURL') + '/testexecutions'
  6. const testCycle = Cypress.env('testCycle')
  7. const requestBody = {
  8. projectKey: 'PROJECTCODE',
  9. testCaseKey: `${match[0]}`,
  10. testCycleKey: `${testCycle}`,
  11. statusName: '',
  12. environmentName: 'QA'
  13. };
  14. switch (this.currentTest.state) {
  15. case 'passed':
  16. requestBody.statusName = 'Pass'
  17. break;
  18. case 'failed':
  19. requestBody.statusName = 'Fail'
  20. break;
  21. case 'skipped':
  22. requestBody.statusName = 'Not Executed'
  23. break;
  24. }
  25. cy.then(() => {
  26. cy.request({
  27. headers: {
  28. Authorization: `Bearer ${token}`
  29. },
  30. url: url,
  31. method: 'POST',
  32. body: requestBody
  33. });
  34. });
  35. })
英文:

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

  1. afterEach(function() {
  2. const pattern = /PROJECTCODE-T\d+/
  3. const match = this.currentTest.title.match(pattern)
  4. const token = Cypress.env('bearerToken')
  5. const url = Cypress.env('zephyrBaseURL') + '/testexecutions'
  6. const testCycle = Cypress.env('testCycle')
  7. const requestBody = {
  8. projectKey: 'PROJECTCODE',
  9. testCaseKey: `${match[0]}`,
  10. testCycleKey: `${testCycle}`,
  11. statusName: '',
  12. environmentName: 'QA'
  13. };
  14. switch (this.currentTest.state) {
  15. case 'passed':
  16. requestBody.statusName = 'Pass'
  17. break;
  18. case 'failed':
  19. requestBody.statusName = 'Fail'
  20. break;
  21. case 'skipped':
  22. requestBody.statusName = 'Not Executed'
  23. break;
  24. }
  25. cy.then(() => {
  26. cy.request({
  27. headers: {
  28. Authorization: `Bearer ${token}`
  29. },
  30. url: url,
  31. method: 'POST',
  32. body: requestBody
  33. });
  34. });
  35. })

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:

确定