使用 table 的 Gherkin 测试失败并出现错误 "Cannot read property 'bold' of undefined"

Gherkin test using table fails with error "Cannot read property 'bold' of undefined"

使用带有 Gherkin 5.1.0 法语测试的 codeceptJs 2.1.1 我正在尝试使用数据表来检查表单中是否存在所有字段以提供字段名称。

这是 Gherkin 测试:

@tabletest
Scénario: Les champs
Alors je vois les champs :
| coteComplete   |
| typeOptionCote |

对应的步骤如下:

Then('je vois les champs( de saisie) :', (name) => {
  I.say('name', name);
  I.seeElement(`input[name=${name}], select[name=${name}], textearea[name=${name}]`);
});

这是堆栈跟踪:

$ npx codeceptjs run --grep "tabletest" --debug --verbose
CodeceptJS v2.1.1
Using test root "C:\PISTARD\diffusion\dev\pistard-diffusion"
Helpers: Protractor
Plugins: screenshotOnFail, wdio

Recherche par cote @PDIFF-56 --
    Emitted | suite.before ([object Object])
 » Started SeleniumStandaloneLauncher
 » [Session] Starting singleton browser session
  Les champs @tabletest
    Emitted | test.before ([object Object])
    Emitted | hook.start ([object Object])
    Emitted | step.before (I am on page "recherche-avancee")
    Emitted | step.after (I am on page "recherche-avancee")
    Emitted | step.start (I am on page "recherche-avancee")
    Etant donné que je suis sur la page "recherche avancee"
      I am on page "recherche-avancee"
      » Visited http://localhost:4200/recherche-avancee
    Emitted | step.passed (I am on page "recherche-avancee")
    Emitted | step.finish (I am on page "recherche-avancee")
    Emitted | hook.passed ([object Object])
    Emitted | test.start ([object Object])
    [1] Error | TypeError: Cannot read property 'bold' of undefined
    [1] Starting <teardown> session
    Emitted | test.failed ([object Object])
    Emitted | test.finish ([object Object])
    [1] <teardown> Stopping recording promises
 » <screenshotOnFail> Test failed, saving screenshot
 » Screenshot has been saved to C:\PISTARD\diffusion\dev\pistard-diffusion\codeceptjs-output\Les_champs_1557763592.failed.png
  × FAILED in 287ms

    [2] Starting recording promises
    Emitted | test.after ([object Object])
    Emitted | suite.after ([object Object])

-- FAILURES:

  1) Recherche par cote @PDIFF-56
       Les champs @tabletest:
     Cannot read property 'bold' of undefined
  ypeError: Cannot read property 'bold' of undefined
      at Object.say (node_modules\codeceptjs\lib\output.js:139:53)
      at recorder.add (node_modules\codeceptjs\lib\actor.js:40:78)
      at process.internalTickCallback (internal/process/next_tick.js:77:7)


  FAIL  | 0 passed, 1 failed   // 5s
    Emitted | global.result ([object Object])
    Emitted | global.after ([object Object])
 » Stopped SeleniumStandaloneLauncher

使用节点检查器我能够在跟踪中看到更多。 将参数 --node-arg=--inspect-brk 添加到您的命令

npx --node-arg=--inspect-brk codeceptjs run --grep "tabletest" --debug 

然后通过单击节点图标从 chrome devtools 打开 nodejsinspector:

至于"bold of undefined"错误吧

I.say("InputName:" + name);

关于接收到的变量,我可以看到测试实际上接收到一个包含值数组而不是实际值的对象。实际值可以这样访问:

let name = dataTest.rows[0].cells[0].value

所以你必须在收到的数组中手动​​循环以获取每个值并对其进行测试

Then('je vois les champs( de saisie)( :)', (dataTest) => {
  let name;
  dataTest.rows.forEach(element => {
    name = element.cells[0].value;
    I.seeElement(`input[name=${name}], select[name=${name}], textearea[name=${name}]`);
  });
  debugger;
});