在 Cucumber 中,我在哪里声明 defineParameterType?

In Cucumber where do I declare defineParameterType?

我正在学习在 Cucumber 中创建自定义数据类型,并按照此 post 学习了如何操作。

我遇到以下错误。

? Given we have a new ApiKey called "Red"
       Undefined. Implement with the following snippet:

         Given('we have a new ApiKey called {string}', function (string) {
           // Write code here that turns the phrase above into concrete actions
           return 'pending';
         });

特征文件代码-

Feature: Try
 @test
  Scenario: try custom parameter

    Given we have a new ApiKey called "Red"
    

步骤定义文件-

const { Given } = require('@cucumber/cucumber');

    Given('we have a new ApiKey called {name}', function () {
      console.log('----Inside function---');
      return 'pending';
    });

ParameterType.js代码-

const { defineParameterType } = require('@cucumber/cucumber');

        defineParameterType({
            regexp: /"([^"]*)"/,
            transformer: function(s) {
                return s;
            },
            name: "name",
            useForSnippets: false
        });

你应该可以在任何地方声明它,Cucumber 会把它捡起来。

The recommended location to define custom parameter types, would be in features/support/parameter_types.js. This is just a convention though; Cucumber will pick them up from any file

参考:https://cucumber.io/docs/cucumber/configuration/(在顶部选择JavaScript语言)

我做了以下事情并且我的代码有效。

  1. 我将此步骤定义更改为以下部分。我忘记将名称作为参数传递,所以添加了它。

  2. 不知道为什么,但我们在设置文件夹中写入了预设置文件,之前我们已经为我们的功能文件编写了所有文件,我不得不将我的文件 PArameterTypes.js 移动到该设置文件中。

    给定('we have a new ApiKey called {name}',函数(名称){ console.log('----内部函数---'); return 'pending'; });