有没有办法在赛普拉斯中访问测试用例和测试套件的详细信息?

Is there a way to access test case and test suite details in Cypress?

我正在尝试创建一个 JSON 文件来存储 Cypress 测试的详细信息,但我不知道如何访问它们。
我需要的详细信息:
1- 测试用例(它块)标题
2- 测试套件(描述块)标题
3- 当前重试次数
在每个钩子之后
3- 测试用例状态(通过、失败、跳过)
4- 测试用例持续时间
5- 测试用例错误消息(如果测试用例失败)

有关详细信息,请查看此问题: How to find the calling test in cypress custom command

此外,您可以简单地复制并使用以下函数之一:

// To get the test case title (it block description)
function testCaseTitle(inHook){
  if(inHook) // If called inside a hook
    return Cypress.mocha.getRunner().suite.ctx.currentTest.title;
  return Cypress.mocha.getRunner().suite.ctx.test.title;
}



// To get the test suite title (describe block description)
function testSuiteTitle(inHook){
  if(inHook) // If called inside a hook
    return Cypress.mocha.getRunner().suite.ctx._runnable.parent.title;
  return Cypress.mocha.getRunner().suite.ctx.test.parent.title;
}



// To get the current test retry 
function testCaseRetry(inHook){
  if(inHook) // If called inside a hook
    return Cypress.mocha.getRunner().suite.ctx.currentTest._currentRetry;
  return Cypress.mocha.getRunner().suite.ctx.test._currentRetry;
}
// To get the total number of retries
function totalRetries(inHook){
  if(inHook) // If called inside a hook
    return Cypress.mocha.getRunner().suite.ctx.currentTest._retries;
  return Cypress.mocha.getRunner().suite.ctx.test._retries;
}



// To get the test case state in after each hook
function testCaseState(){
  return Cypress.mocha.getRunner().suite.ctx.currentTest.state;
}
// Or Alternatively, to test whether the test case has passed in after each hook
function hasPassed(){
  return Cypress.mocha.getRunner().suite.ctx.currentTest.state == 'passed';
}



// To get the test case duration in seconds in after each hook
function testCaseDuration(){
  return (Cypress.mocha.getRunner().suite.ctx.currentTest.duration/1000).toFixed(2)+' sec';
}



// To get the error message of a failing test case 
function testCaseErrorMessage(){
  return Cypress.mocha.getRunner().suite.ctx.currentTest.err.message;
}