英文:
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
});
});
})
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论