如何在 Cucumber 的参数类型中设置布尔值?

How do I set a boolean in parameter types for Cucumber?

Cucumber 允许您将 int、string、float 作为参数类型传递,有没有办法使用布尔值来实现?

Given('the property {string} is set to {string}', function (path, value) {
    this.requestBody = this.requestBody || {};
    _.set(this.requestBody, path, value);
});

简短的回答,没有,但我相信你会找到你需要的here

有多种方法可以实现这一点,其中大多数都需要您在 Given 步骤中将值解析为布尔值。我相信你的情况是 value.

祝你好运!

您可以通过为布尔值定义一个 custom parameter type 来巧妙地做到这一点,这将允许您以与 {string}、{float} 等相同的方式在步骤表达式中使用 {boolean}。

步骤定义文件

const { defineParameterType, Given } = require("@cucumber/cucumber");

defineParameterType({
  name: "boolean",
  regexp: /true|false/,
  transformer: (s) => s === "true" ? true : false
});

Given("the property {string} is set to {boolean}", function (path, value) {
  console.log(typeof value); // "boolean"
});

特征文件

Given the property "theProp" is set to true