<p>Cucumber-js 找不到步骤定义</p>

创建于 2017-01-11  ·  48评论  ·  资料来源: cucumber/cucumber-js

嗨,我是一个新手,他花了几个小时试图让 Cucumber-js 找到我的步骤定义。 我想出了这个简单的例子。

在 features/foo.feature 中:

Feature: Foo
  Scenario: Bar
    Given FooBar

在 features/step_definitions/step_defs.js 中:

var {defineSupportCode} = require('cucumber');

defineSupportCode(function({Given}) {
    Given('FooBar', function () {
      return 'pending';
    });    
});

我得到这个结果:

Feature: Foo

  Scenario: Bar
  ? Given FooBar

Warnings:

1) Scenario: Bar - features\foo.feature:2
   Step: Given FooBar - features\foo.feature:3
   Message:
     Undefined. Implement with the following snippet:

       Given('FooBar', function (callback) {
         // Write code here that turns the phrase above into concrete actions
         callback(null, 'pending');
       });

1 scenario (1 undefined)
1 step (1 undefined)
0m00.000s
help wanted

最有用的评论

好吧。 我相信我想通了这一点。

当使用全局安装的 Cucumber 运行功能测试时,您仍然需要通过本地安装 Cucumber 来定义支持代码。 这是两个不同的实例,因此不会加载任何步骤定义。

是否有任何原因不能使用本地安装来运行命令? 我在 mac 上通过将“./node_modules/.bin”添加到我的路径来做到这一点。

我不喜欢通过让全局安装尝试要求本地安装来获得步骤定义来尝试使这项工作的想法。 我也不喜欢全局安装,因为它使您必须通过绝对路径指定诸如编译模块之类的内容。

如果这在 Windows 上是可能的,我宁愿只更新文档以提及这不能用作全局安装。

所有48条评论

我知道cucumber-js 正在查找步骤定义文件,因为当我向其中注入语法错误时,cucumber-js 会正确报告错误。

你用的是什么版本的cucumber-js?

它说 2.0.0-rc.6

也是一个新手并且在运行相同版本的cucumber.js时遇到了同样的问题
我正在学习另一个教程,但此时卡住了。

/features/add-item.feature

Feature: Shopper can add an item to their Grocery List
  As a grocery shopper
  I want to add an item to my grocery list
  So that I can remember to buy that item at the grocery store

  Scenario: Item added to grocery list
    Given I have an empty grocery list
    When I add an item to the list
    Then The grocery list contains a single item

  Scenario: Item accessible from grocery list
    Given I have an empty grocery list
    When I add an item to the list
    Then I can access that item from the grocery list`

/features/step_definitions/add-item.steps.js
'use strict';

module.exports = function() {

    Given('I have an empty grocery list', function (callback) {
         // Write code here that turns the phrase above into concrete actions
         callback(null, 'pending');
       });


    When('I add an item to the list', function (callback) {
         // Write code here that turns the phrase above into concrete actions
         callback(null, 'pending');
       });

    Then('The grocery list contains a single item', function (callback) {
         // Write code here that turns the phrase above into concrete actions
         callback(null, 'pending');
       });

    Then('I can access that item from the grocery list', function (callback) {
         // Write code here that turns the phrase above into concrete actions
         callback(null, 'pending');
       });

}

$ node_modules/.bin/cucumber-js返回

Feature: Shopper can add an item to their Grocery List

    As a grocery shopper
    I want to add an item to my grocery list
    So that I can remember to buy that item at the grocery store

  Scenario: Item added to grocery list
  ? Given I have an empty grocery list
  ? When I add an item to the list
  ? Then The grocery list contains a single item

  Scenario: Item accessible from grocery list
  ? Given I have an empty grocery list
  ? When I add an item to the list
  ? Then I can access that item from the grocery list

Warnings:

1) Scenario: Item added to grocery list - features/add-item.feature:6
   Step: Given I have an empty grocery list - features/add-item.feature:7
   Message:
     Undefined. Implement with the following snippet:

       Given('I have an empty grocery list', function (callback) {
         // Write code here that turns the phrase above into concrete actions
         callback(null, 'pending');
       });

2) Scenario: Item added to grocery list - features/add-item.feature:6
   Step: When I add an item to the list - features/add-item.feature:8
   Message:
     Undefined. Implement with the following snippet:

       When('I add an item to the list', function (callback) {
         // Write code here that turns the phrase above into concrete actions
         callback(null, 'pending');
       });

3) Scenario: Item added to grocery list - features/add-item.feature:6
   Step: Then The grocery list contains a single item - features/add-item.feature:9
   Message:
     Undefined. Implement with the following snippet:

       Then('The grocery list contains a single item', function (callback) {
         // Write code here that turns the phrase above into concrete actions
         callback(null, 'pending');
       });

4) Scenario: Item accessible from grocery list - features/add-item.feature:11
   Step: Given I have an empty grocery list - features/add-item.feature:12
   Message:
     Undefined. Implement with the following snippet:

       Given('I have an empty grocery list', function (callback) {
         // Write code here that turns the phrase above into concrete actions
         callback(null, 'pending');
       });

5) Scenario: Item accessible from grocery list - features/add-item.feature:11
   Step: When I add an item to the list - features/add-item.feature:13
   Message:
     Undefined. Implement with the following snippet:

       When('I add an item to the list', function (callback) {
         // Write code here that turns the phrase above into concrete actions
         callback(null, 'pending');
       });

6) Scenario: Item accessible from grocery list - features/add-item.feature:11
   Step: Then I can access that item from the grocery list - features/add-item.feature:14
   Message:
     Undefined. Implement with the following snippet:

       Then('I can access that item from the grocery list', function (callback) {
         // Write code here that turns the phrase above into concrete actions
         callback(null, 'pending');
       });

2 scenarios (2 undefined)
6 steps (6 undefined)
0m00.000s

目前还没有包含 world.js 文件。

@dunlop-ben 请参阅更新日志,了解 1.0 和 2.0 候选版本之间的区别。 定义步骤的方式发生了变化,您的示例似乎使用了 1.0 语法。

@jeffmath对我来说没什么问题。 我们有功能测试来验证类似于您的简单示例的功能。 在 mac 上复制,它按我的预期工作。 您正在运行什么版本的节点? 从路径看来您在 Windows 机器上,是这样吗?

@charlierudolph - 谢谢。 我已经研究过这个并尝试了一些具有类似结果的不同示例。 我也尝试在这里使用功能和步骤定义: http :

我觉得好像我必须遗漏一个步骤和/或文件。 如果有任何地方可以指导我,那就太好了。 我没有多少运气阅读 Github 上的文档。

Windows 7 上的@charlierudolph节点 6.4.0

@charlierudolph我有类似的问题。 我完全遵循https://github.com/cucumber/cucumber-js/blob/master/docs/nodejs_example.md并且问题仍然存在。

我的设置是

  • 节点 7.4.0
  • 操作系统:Windows 10
  • 黄瓜 2.0.0 -rc.6

@jeffmath @TheAdamW抱歉,我没有 Windows 机器,因此无法真正追踪到这一点。 我们正在使用 appveyor 在 Windows 机器上运行我们的测试,我不知道有什么区别。 你们中的一个人可以尝试深入研究吗? 我很乐意提供我能提供的任何帮助

对我来说,如果我正在编写./node_modules/.bin/cucumberjs但如果我只编写全局安装的cucumberjs (相同版本2.0.0-rc.6 )则

同样的错误:黄瓜无法找到步骤定义。 在 linux 中具有相同的文件夹结构。

  • 节点。 4.4.0 & 4.6
  • 操作系统:Windows 10
  • 黄瓜 2.0.0 -rc.6

有没有办法强制步骤定义路径/文件夹/模式?
顺便说一句, @MartinDelille的伎俩对我不起作用。

我突然面临同样的问题,直到今天早上,黄瓜都可以很好地执行脚本。 我不知道它有什么问题。

我通过右键单击 ruby​​ mine 并从命令行使用命令cucumber -r features/chatstep.feature运行该功能,我得到了相同的结果。

任何人都可以帮忙

Given(/^I launch "([^"]*)"$/) do |arg1|
pending # 在这里写代码,把上面的短语变成具体的动作
结尾

我不得不降级到 1.3.1 才能让它工作。
它甚至不适用于 centos。

2017 年 1 月 25 日 21:32,“Mrityunjeyan S”通知@github.com 写道:

我突然面临同样的问题,黄瓜很好
执行脚本直到今天早上。 我不知道出了什么问题
用它。

我通过右键单击 ruby​​ mine 并从命令行使用
命令黄瓜 -r features/chatstep.feature 我得到相同的结果。

任何人都可以帮忙


您收到此消息是因为您发表了评论。
直接回复本邮件,在GitHub上查看
https://github.com/cucumber/cucumber-js/issues/718#issuecomment-275224435
或静音线程
https://github.com/notifications/unsubscribe-auth/ALjDKMNdggk1Gh-x_q3KRuvlpN5f_0cOks5rV7E9gaJpZM4LhJZO
.

@jbgraug :你降级了什么? 降级前你之前的版本是什么

我尝试过 RC 2.0.6 和 RC 2.0.0。 我无法让它在 linux 或
windows asucumber 无法找到步骤定义,甚至没有
-r 选项。

2017 年 1 月 26 日星期四上午 6:18, Mrityunjeyan S [email protected]
写道:

@jbgraug https://github.com/jbgraug :你降级了什么? 什么是乌尔
降级前的旧版本


你收到这个是因为你被提到了。
直接回复本邮件,在GitHub上查看
https://github.com/cucumber/cucumber-js/issues/718#issuecomment-275310070
或静音线程
https://github.com/notifications/unsubscribe-auth/ALjDKGbyYXvsjlLv-dlz6bnHMfPK7t05ks5rWCypgaJpZM4LhJZO
.

@jbgraug我猜黄瓜已经弃用了 2.0 版中与它捆绑在一起的gherkin ? 我目前正在安装 1.3.14,它说fetching gherkin在安装 2.0 以上的版本时我从未注意到

@reach2jeyan基于您的命令行和测试片段,我相信您是在谈论-ruby 。 虽然这两个项目在尝试为各自的语言实现黄瓜框架方面是相关的,但它们有许多不同之处。

@charlierudolph感谢您的

顺便说一下,它现在可以工作了!

@charlierudolph正是@MartinDelille所说的,通过npm -S./node_modules/.bin/cucumber.js ,但不能通过npm -g/usr/local/bin/cucumber.js找到并执行步骤,但是不知何故后来忘记了他们。

macOS + Node.js 7.5.0 + Cucumber.js 2.0.0-rc.7
windows specific标签具有误导性

@kozhevnikov感谢您的更新。 到目前为止,所有报告都在 Windows 上(并且我无法使用本地安装在本地 mac 上重新创建)很难想象全局安装在某种程度上有所不同,但至少现在我可以进行调查。

我在 Windows 上使用命令提示符通过 cygwin 遇到了同样的问题,也许这就是问题所在? 我什至直接从示例中复制了代码。

特点:简单的数学
为了做数学
作为开发者
我想增加变量

场景:简单的数学
给定一个变量设置为 1
当我将变量增加 1 时
那么变量应该包含 2

场景大纲:更复杂的东西
给定一个变量设置为

Examples:
  | var | increment | result |
  | 100 |         5 |    105 |
  |  99 |      1234 |   1333 |
  |  12 |         5 |     18 |

Cucumber.defineSupportCode(function(context) {
var setWorldConstructor = context.setWorldConstructor;
var Given = context.Give
var When = context.When
var Then = context.Then

///// 你的世界 /////
//
// 调用“setWorldConstructor”到您的自定义世界(可选)
//

var CustomWorld = function() {};

CustomWorld.prototype.variable = 0;

CustomWorld.prototype.setTo = function(number) {
this.variable = parseInt(number);
};

CustomWorld.prototype.incrementBy = function(number) {
this.variable += parseInt(number);
};

setWorldConstructor(CustomWorld);

///// 您的步骤定义 /////
//
// 使用“Given”、“When”和“Then”来声明步骤定义
//

Given(/^a 变量设置为 (\d+)$/, function(number) {
this.setTo(number);
});

当(/^I 将变量增加 (\d+)$/, function(number) {
this.incrementBy(number);
});

然后(/^变量应该包含 (\d+)$/, function(number) {
if (this.variable != parseInt(number))
throw new Error('变量应该包含'+数字+
' 但它包含 ' + this.variable + '.');
});
})

我收到此错误“ReferenceError: Given is not defined”
我用特定的文件名运行cucumber.js 和~/node_modules/.bin/cucumber.js,它总是抛出一个错误。 我可以用 ruby​​ 运行黄瓜没有问题,但我无法让黄瓜运行

好吧。 我相信我想通了这一点。

当使用全局安装的 Cucumber 运行功能测试时,您仍然需要通过本地安装 Cucumber 来定义支持代码。 这是两个不同的实例,因此不会加载任何步骤定义。

是否有任何原因不能使用本地安装来运行命令? 我在 mac 上通过将“./node_modules/.bin”添加到我的路径来做到这一点。

我不喜欢通过让全局安装尝试要求本地安装来获得步骤定义来尝试使这项工作的想法。 我也不喜欢全局安装,因为它使您必须通过绝对路径指定诸如编译模块之类的内容。

如果这在 Windows 上是可能的,我宁愿只更新文档以提及这不能用作全局安装。

关于删除全局安装的两个小点:

  1. 所有文档都需要更新,不仅仅是官方文档,还有许多其他流行的框架构建在上面。 当我遇到这个问题时,我一直在跟进,对于刚开始使用 JavaScript/Node.js/Cucumber.js 的人来说,调试令人沮丧

  2. 许多 IDE 和插件被硬编码为指向全局。 在 macOS 上添加 bash 配置文件之外的环境变量(即通过 Spotlight 启动 WebStorm)也很复杂,并且会随着版本的变化而变化。

唔。 我们可以添加一个 CLI 选项,允许用户指定本地黄瓜实例在哪里? 我们可以将默认值设为process.cwd() + /node_modules/cucumber ,这应该涵盖大多数情况。

@charlierudolph在最初报告错误后,我意识到无论如何我都需要使用 Cucumber-JVM,因为我需要测试的代码是用 Scala.js 编写的。 但是当我接下来需要测试 Javascript 时,您的解决方法听起来很容易实现。 感谢您解决问题。

这会很棘手,据我所知,Node 有很多边缘情况,所以至少它必须遍历父级,直到找到本地包或点击驱动器根目录,而且我不知道使用全局文件夹有多流行在 Node 社区。

@jeffmath @kozhevnikov这是我对此的最新想法

  1. 我对允许用户指定本地黄瓜路径的 cli 选项感到满意。 如果未提供,则使用已执行的实例。
  2. 使用此选项将进行版本验证以确保本地/全局安装是完全相同的版本,否则事情可能无法按预期工作。 如果没有,它会抛出一个错误,告诉用户请对齐版本。
  3. 此选项将立即被弃用,并带有停止使用全局安装的警告,而仅使用本地安装。
  4. 此选项将在下一个主要版本中删除。

我在尝试构建通用测试库时发现了类似的问题。 我的想法是在不同的项目中重用测试。

我已经构建了一个名为 _roi-functional-tests_ 的通用测试包,我想在其他项目中使用它。 到目前为止,我已将包与 _npm link_链接,然后尝试将 Cucumber 指向 _roi-functional-tests_ 中的功能,如下所示:

./node_modules/.bin/cucumberjs ./node_modules/roi-functional-tests/features

但我明白了:

Warnings:

1) Scenario: Curl request - node_modules/roi-functional-tests/features/home.feature:3
   Step: Given I make curl request to "/" - node_modules/roi-functional-tests/features/home.feature:4
   Message:
     Undefined. Implement with the following snippet:

       Given('I make curl request to {arg1:stringInDoubleQuotes}', function (arg1, callback) {
         // Write code here that turns the phrase above into concrete actions
         callback(null, 'pending');
       });

Cucumber 可以从工作目录之外导入定义吗?

@jramcast是的。 但是您需要使用--require cli 标志。

已经尝试使用--require标志:

./node_modules/.bin/cucumberjs --require ./node_modules/roi-functional-tests/features ./node_modules/roi-functional-tests/features

但我得到了同样的结果。

npm link可能导致黄瓜找不到定义?

所以我在没有npm link情况下做了同样的测试。 我发布并安装了这个包,一切正常,只需调用:

./node_modules/.bin/cucumberjs ./node_modules/roi-functional-tests/features

我的猜测是,在链接包时, require会加载安装在链接包目录 ( node_modules/roi-functional-tests/node_modules/cucumber ) 内的黄瓜。 但是,如果安装了 _roi-functional-tests_,npm 会展平依赖关系树并使用安装在项目 node_modules ( node_modules/cucumber ) 下的 Cucumber 实例

有同样的问题。

黄瓜版本:“黄瓜”:“^2.0.0-rc.9”
视窗版本:10
节点版本:6.10.3 LTS & 7.10

在 IDE 或 Cucumber 的全局实例中不起作用,即使它们正确实现,也永远找不到步骤。

如果我从项目的 node_modules 目录中调用cucumber.js,那么它就可以工作。

有人对如何解决此问题有任何建议吗?

这也在 MacOS Sierra 10.12.4 上发生
节点版本 7.10

回到 Cucumber 版本 1.3.0,现在工作正常,使用全局实例通过 cmd 调用它并且在 IDE 中工作。

@charlierudolph
我在下一步。 (从第 60 期移到这里)
我仅使用本地实例运行,但在运行功能文件时无法识别我的步骤定义
image
特征文件和步骤定义文件在正确的位置(附截图)
为什么我的步骤定义没有被识别?

我在 package-lock.json 中引用了 1.X 版本的 Cucumber
“wdio-cucumber-framework”:

    "cucumber": {
      "version": "1.3.3",
      "resolved": "https://registry.npmjs.org/cucumber/-/cucumber-1.3.3.tgz",
      "integrity": "sha1-Za+2Xy+T9y2teN8qterPFGCf7C8=",
      "dev": true
    },

"webdriverio-cucumber-js":  

   "cucumber": {
      "version": "1.2.2",
      "resolved": "https://registry.npmjs.org/cucumber/-/cucumber-1.2.2.tgz",
      "integrity": "sha1-KEZJEf8arfwOZjTXY2q69mHGaJI="
    },

我把这两个版本改成了 2,3,1
我的节点版本是 8.0.0
但仍然无法识别步骤定义
任何的想法?

@vvechalam

如果您将 webdriverIO 与其测试运行程序一起使用,而不是在独立模式下,您将需要使用 Cucumber-js 版本 1.x,最好是 1.3.3 而不是 2.x,因为测试运行程序不支持新版本和实施步骤定义的新方式。

我回到了旧的[email protected]因为我正在使用页面对象模型所需的测试运行程序并确保所有命令同步运行。

@GeeChao
谢谢回复。 我已经恢复并运行测试仍然无法识别步骤定义

当我使用 -r 尝试该命令时,在功能文件中出现语法错误...
C:/webdriverio-test/node_modules/.bin/cucumber-js -r features/customer-validation.feature

C:\webdriverio-testfeatures\customer-validation。 特点:2
作为业主
^^

语法错误:意外的标识符
在 createScript (vm.js:74:10)
在 Object.runInThisContext (vm.js:116:10)
在 Module._compile (module.js:533:28)
在 Object.Module._extensions..js (module.js:580:10)
在 Module.load (module.js:503:32)
在 tryModuleLoad (module.js:466:12)
在 Function.Module._load (module.js:458:3)
在 Module.require (module.js:513:17)
在要求(内部/module.js:11:18)
在 C:\webdriverio-test\node_modules\cucumberlib\cliindex.js:135:16
在 Array.forEach(本机)
在 Cli.getSupportCodeLibrary (C:\webdriverio-test\node_modules\cucumberlib\cliindex.js:134:24)
在 Cli。(C:\webdriverio-test\node_modules\cucumberlib\cliindex.js:144:39)
在 Generator.next ()
在 Generator.tryCatcher (C:\webdriverio-test\node_modules\bluebird\js\release\util.js:16:23)
在 PromiseSpawn._promiseFulfilled (C:\webdriverio-test\node_modules\bluebird\js\release\generators.js:97:49)

@vvechalam您能否创建并将其推送到您的存储库,以便我可以复制它?

@vvechalam

将您的 step_definitions 文件夹移动到您的 features 文件夹中,它将开始工作并识别步骤定义。 之后抛出的唯一问题是您的 txtdetails.txt 文件位置不正确。

@GeeChao
啊,对不起,我没有推动那个改变。
如果你看到我上面的截图,步骤定义只在 features 文件夹中。
希望内容是:
Content = fs.readFileSync('../../txndetails.txt', 'utf8');

那么为什么我无法将步骤定义粘贴到我本地的特征文件中呢?
我的设置不正确吗?

@vvechalam

这个对我有用。

我所做的唯一一件事就是将 step_definitions 移到 features 目录下

也是我做了一个NPM安装[email protected] --save-dev的

然后运行 ​​node_modules/cucumber/bin/cucumber.js features/

@GeeChao一切正常。 我在创建问题的 Then 语句中做了一个 For 循环。 是否可以在步骤定义中使用 For 循环?
我不想使用示例,因为我必须根据文本文件数据运行 Then 步骤(数据驱动测试)
我知道这个问题与这个主题无关,只是想我是否可以获得一些帮助。

@GeeChao好吧,又是我的愚蠢错误。 循环变量设置不正确(忘记比较长度)。 再次感谢您的支持

对我来说,作为新手,错误是由于我将字符串而不是正则表达式传递到步骤中引起的;

Then('I log in as (.*)', ...)

而不是

Then(/I log in as (.*)', ...)

简单的错误,但希望对刚开始的其他人有用。

@vvechalam你解决了吗? 能否请你帮忙

@sancy2017我从 WDIO 转移到 TestCafe 工具。 所以我可能无法立即告诉你解决方案。 让我知道您面临的问题究竟是什么

此错误的简单的解决办法是安装NPM [email protected] --save-dev的
然后运行你的测试 ..npm test

我遇到了同样的问题“无法识别黄瓜步骤定义”。 将目录名称从“steps”更改为“step_definitions”对我有用。

我看到大多数人在 features 目录下都有 step_definitions,我相信更好的做法是将步骤实现放在单独的目录中,即 src/step_definitions 而不是将步骤保留在 features 目录下。

此页面是否有帮助?
0 / 5 - 0 等级