【问题标题】:Cucumber Js, What's the best practice if I need to use the value from Given step?Cucumber Js,如果我需要使用 Given 步骤中的值,最佳实践是什么?
【发布时间】:2019-11-19 17:03:51
【问题描述】:

我想出的方法是这样的

Given(
  `Step1`,
  async function() {
    const IwantToUseThisObj = {
      A: 'a',
      B: 'b'
    }

    this.IwantToUseThisObj = IwantToUseThisObj
  }
)

Then(`Step2`, async function() {
  IwantToUseThisObj = this.IwantToUseThisObj
})

但我不确定这是否是最佳做法,如果我需要大量使用它,它看起来非常重复,

有更好的方法吗?我只想使用我在 Given 步骤中使用的值

【问题讨论】:

  • 您是否尝试将数据从一个步骤传递到另一个步骤?

标签: javascript selenium-webdriver bdd cucumberjs


【解决方案1】:

是的,将数据存储在世界级以供重复使用是黄瓜的最佳实践

【讨论】:

    【解决方案2】:

    在场景中的步骤之间传递数据的最可靠和可接受的方式是使用场景上下文或“世界”对象this。这是一个例子:

    功能文件

    Feature: Passing data between steps
    
      Scenario: Passing data
        Given I set the value to "test"
        Then the value should be "test"
    

    步骤定义

    const { Given, Then } = require('cucumber');
    const assert = require('assert');
    
    
    Given('I set the value to {string}', function (value) {
        this.value = value;
    });
    
    Then('the value should be {string}', function (value) {
        assert.ok(this.value === value);
    });
    

    在线示例:https://testjam.io/?p=cHsYgzrkRiI9dmkm1IyR

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-14
      • 1970-01-01
      • 2020-03-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多