简体   繁体   English

使用 Node.js(和 WebdriverIO)的 Gherkin 数据表

[英]Gherkin Data Table using Node.js (and WebdriverIO)

I want to implement a Gherkin step involving a data table in Node.js, but I'm not sure how the data should be passed in. Here is an example that I know works that does not utilize a data table.我想在 Node.js 中实现一个涉及数据表的 Gherkin 步骤,但我不确定应该如何传入数据。这是一个我知道的不使用数据表的示例。

test.feature

Feature: Testing login
  Scenario: Logging in with credentials
    When I login with credentials "user@example.com" and "p@ssw0rd"

login.js

import loginAsCreds from './loginAsCreds'

module.exports = function when () {
  this.When(
    /^I login as "(.+)" and "(.+)"/i,
      loginAsCreds
  )

loginAsCreds.js

module.exports = (email, password, done) => {
    browser.element('#email').keys(email)
    browser.element('#password').keys(password)
    browser.element('#signInButton').click()

    done()
}

Here is what I would like the feature file to look like.这是我希望功能文件的样子。

test-desired.feature

Feature: Testing login
  Scenario: Logging in with credentials
    When I login with credentials:
      | user@example.com | p@ssw0rd |

I've tried a few things to modify login.js to pass the data table and loginAsCreds.js to accept it, but I am new to Node.js and WebdriverIO (but not Selenium or Gherkin).我尝试了一些方法来修改login.js以传递数据表和loginAsCreds.js以接受它,但我是 Node.js 和 WebdriverIO 的新手(但不是 Selenium 或 Gherkin)。 Could someone please point me in the right direction with this?有人可以指出我正确的方向吗? Data tables would be very useful for other steps I plan to write, as well.数据表对于我计划编写的其他步骤也非常有用。 Thanks!谢谢!

I had someone point me to the cucumber js documentation. 我有人指出我cucumber js文档。 I didn't know to search for that, so I found this page: 我不知道要搜索,所以找到了此页面:

https://github.com/cucumber/cucumber-js/blob/9959ca9fd9f8c64f0b5133f9bb241da9b854e570/README.md https://github.com/cucumber/cucumber-js/blob/9959ca9fd9f8c64f0b5133f9bb241da9b854e570/README.md

...which showed me I needed to add .raw() to my argument to access the data (since the table is a 2-D array), and points to this example: ...这向我展示了我需要在参数中添加.raw()来访问数据(因为表是二维数组),并指向此示例:

https://github.com/cucumber/cucumber-js/blob/9959ca9fd9f8c64f0b5133f9bb241da9b854e570/features/data_tables.feature https://github.com/cucumber/cucumber-js/blob/9959ca9fd9f8c64f0b5133f9bb241da9b854e570/features/data_tables.feature

This is what my code looks like now: 这是我的代码现在的样子:

test-desired.feature 测试desired.feature

Feature: Testing login
  Scenario: Logging in with credentials
    When I login with credentials:
      | user@example.com | p@ssw0rd |

login.js login.js

import loginAsCreds from './loginAsCreds'

module.exports = function when () {
  this.When(
    /^I login as:/i,
      loginAsCreds
  )

loginAsCreds.js loginAsCreds.js

module.exports = (creds, done) => {
  email = creds.raw()[0][0]
  password = creds.raw()[0][1]

  browser.element('#email').keys(email)
  browser.element('#password').keys(password)
  browser.element('#signInButton').click()

  done()
}

Using the bitloops-gherkin package you are able to pull data from a Google Sheet and you can add any kind of format there which is then converted into Buffer data in the Gherkin table.使用 bitloops-gherkin package,您可以从 Google 表格中提取数据,并且可以在其中添加任何类型的格式,然后将其转换为 Gherkin 表中的缓冲区数据。

https://www.npmjs.com/package/bitloops-gherkin https://www.npmjs.com/package/bitloops-gherkin

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM