如何获取cucumber-js场景中失败的步骤?

问题描述 投票:0回答:1

我正在尝试创建自定义报告,到目前为止,我已经在 failed.json 中获得了这些值

{
    "status": "FAILED",
    "feature": "tc795639 To verify the negative validation check for field \"family_name\" with incorrect data",
    "from": "features/address_post_validation.feature",
    "data": "{\"incorrect_dataValue\":{\"family_name\":\"\"}}",
    "type": "AssertionError",
    "error": "expected false to be true",
    "details": "AssertionError\n    + expected - actual\n\n    -false\n    +true\n\n    at World.<anonymous> (/home/user/Documents/features/step_definitions/stepdefs_common_func.js:27:31)"
  }

From: / 在 after hook 中安慰场景时,我得到了这些值。但我没有得到任何数据 与失败的步骤相关


{
  "gherkinDocument": {
    "feature": {
      "tags": [],
      "location": {},
      "language": "en",
      "keyword": "Feature",
      "name": "Check Validations for each fields as per the spec of post /address",
      "description": "",
      "children": []
    },
    "comments": [],
    "uri": "features/address_post_validation.feature"
  },
  "pickle": {
    "id": "32ea89c6-7701-4f97-a2da-8339b35fcee7",
    "uri": "features/address_post_validation.feature",
    "astNodeIds": [],
    "name": "tc795639 To verify the negative validation check for field \"family_name\" with incorrect data",
    "language": "en",
    "steps": [
      [Object], [Object],
      [Object], [Object],
      [Object], [Object],
      [Object], [Object],
      [Object], [Object],
      [Object]
],
    "tags": []
  },
  "testCaseStartedId": "3b2118fc-1e87-4524-b5f5-ff905e70559c",
  "result": {
    "duration": { "seconds": 0, "nanos": 2827014 },
    "status": "FAILED",
    "message": "AssertionError [ERR_ASSERTION]: Expected values to be strictly equal:\n\n401 !== 400\n\n    + expected - actual\n\n    -401\n    +400\n\n    at World.<anonymous> (/home/user/Documents/features/step_definitions/stepdefs_common_func.js:133:10)",
    "exception": {
      "type": "AssertionError",
      "message": "Expected values to be strictly equal:\n\n401 !== 400\n"
    }
  },
  "willBeRetried": false
}

这就是场景.pickle.steps

[
  {
    id: '381f9425-50d0-4249-8699-b496d77062db',
    text: 'following is body data:',
    type: 'Context',
    argument: { docString: [Object] },
    astNodeIds: [ '23628ec2-499c-45c9-bf5b-4398e609f0ff' ]
  },
  {
    id: 'ab38523e-890f-4436-9b02-dd2fa506ff7b',
    text: 'user send a "post" request to "my url",
    type: 'Action',
    argument: { docString: [Object] },
    astNodeIds: [
      '04961ebc-4e8c-4cb3-b910-8775569d165f',
      'df2aec66-8d24-4972-9497-eea8bae416cf'
    ]
  },
  {
    id: '82bf8ad4-1c29-4859-b07f-b46fc3bdcd7c',
    text: 'status should be 400',
    type: 'Outcome',
    argument: undefined,
    astNodeIds: [
      '0f5ca771-e523-48f8-b215-9b47f7e67b16',
      'df2aec66-8d24-4972-9497-eea8bae416cf'
    ]
  },
  {
    id: 'a729c9db-aee7-4e91-8683-941b59943202',
    text: 'the response should contain the following keys:',
    type: 'Unknown',
    argument: { docString: [Object] },
    astNodeIds: [
      '3645a605-aa58-40b3-8868-5a965de6b5bd',
      'df2aec66-8d24-4972-9497-eea8bae416cf'
    ]
  }
] 

请注意,在此有关失败步骤的任何地方都没有详细说明。 我们知道该步骤在黄瓜中以某种方式映射为失败。

基本上我想在 ✖ 之后包含或检索这一行,即:

“✖ 那么状态应该是 400 # features/step_definitions/stepdefs_common_func.js:131”

这样我就可以将其格式化为“那么状态应该是 400”并添加到我失败的 json 中。

✖ Then status should be 400 # features/step_definitions/stepdefs_common_func.js:131

AssertionError [ERR_ASSERTION]: Expected values to be strictly equal:
       
       401 !== 400
       
           + expected - actual
       
           -401
           +400
       
           at World.<anonymous> (/home/user/Documents/features/step_definitions/stepdefs_common_func.js:133:10)

我尝试了很多来源来检索此内容,但不幸的是,这些都不起作用。我什至尝试了 gpt ,它显示了一个片段,

After(function(scenario) {
  if (scenario.result.status === 'failed') {
    const failedStep = scenario.pickle.steps.find(step => step.result.status === 'failed');
    if (failedStep) {
      console.log('Failed Scenario:', scenario.pickle.name);
      console.log('Failed Step Text:', failedStep.text);
    }
  }
});

但是这样做的问题是 'scenario' 中的任何地方都没有名为“step”的键。 我尝试过的许多事情都是相似或相同的,其中“*.step”返回未定义。

那么,如何获得失败的场景步骤?

javascript cucumber cucumberjs
1个回答
0
投票

将场景中的 gherkin 文档存储在 Before 中,最好存储到 World 对象(其中自定义的 traceScenario 函数会将步骤附加到适合您的位置)

Before({name: "Saving Scenario For Steps", timeout: 60 * 1000}, function (scenario) {
    return this.traceScenario(scenario)
})

您还需要添加 AfterStep 挂钩,就像您已有的 After 挂钩一样

将 AfterStep 中的步骤与 Before 中的步骤进行匹配 - 它们应该共享相同的 ID,并且包含该步骤的状态。最后使用 After 挂钩添加整个场景的状态。

所需的数据位于不同的位置 - 您会知道您是否愿意将其存储为对象,或者存储在您的世界类中,或者将所有内容输出到文件夹系统,以便您可以单独构建报告

这里的代码片段是您在附加步骤中需要的内容,并且应该让事情顺利进行:

AfterStep(function (step){   
   console.log(step.pickleStep)   
   console.log(step.result) 
})

step.pickle 的输出示例:

{   
   id: 'e9610476-4995-4b26-9bd7-01a915225a13',   
   text: 'I go to the "login" page',   
   type: 'Context',   
   argument: undefined,   
   astNodeIds: [ 'ffaf837f-c9ae-4b3a-999f-3ee44ff16db0' ] 
}

step.result 的输出示例:

{ 
   duration: { 
      seconds: 3, 
      nanos: 170917790 
    }, 
    status: 'PASSED' 
}
© www.soinside.com 2019 - 2024. All rights reserved.