簡體   English   中英

使用 Node.js(和 WebdriverIO)的 Gherkin 數據表

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

我想在 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()
}

這是我希望功能文件的樣子。

test-desired.feature

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

我嘗試了一些方法來修改login.js以傳遞數據表和loginAsCreds.js以接受它,但我是 Node.js 和 WebdriverIO 的新手(但不是 Selenium 或 Gherkin)。 有人可以指出我正確的方向嗎? 數據表對於我計划編寫的其他步驟也非常有用。 謝謝!

我有人指出我cucumber js文檔。 我不知道要搜索,所以找到了此頁面:

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

...這向我展示了我需要在參數中添加.raw()來訪問數據(因為表是二維數組),並指向此示例:

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

這是我的代碼現在的樣子:

測試desired.feature

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

login.js

import loginAsCreds from './loginAsCreds'

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

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()
}

使用 bitloops-gherkin package,您可以從 Google 表格中提取數據,並且可以在其中添加任何類型的格式,然后將其轉換為 Gherkin 表中的緩沖區數據。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM