Cucumber 中的多个步骤定义匹配错误 [英] Multiple Step Definitions match error in Cucumber

查看:34
本文介绍了Cucumber 中的多个步骤定义匹配错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近开始使用 Cucumber.我正在尝试实现 Cucumber+Protractor+TypeScript,使用这个 link 作为基线.

I recently got started with Cucumber. I am trying to implement Cucumber+Protractor+TypeScript, using this link as the baseline.

我正在尝试遵循这种结构,<代码>C:.|├────.circleci|├────.vscode|├───e2e│ ├───特色|||--sample.feature|||--sample2.feature||│ └───步骤|||--pageobject1_step.ts|||--pageobject2_step.ts|||--common_step.ts我在 sample 和 sample2 功能文件中都有一个简单的功能.但是,当我尝试运行测试时,我得到了

I am trying to follow this structure, C:. | ├───.circleci | ├───.vscode | ├───e2e │ ├───features | | |--sample.feature | | |--sample2.feature | | │ └───steps | | |--pageobject1_step.ts | | |--pageobject2_step.ts | | |--common_step.ts I have a simple feature inside both sample and sample2 feature files. However when I try running the tests, I get

 `Given I am on the angular.io site
   Multiple step definitions match:
     /I am on the angular.io site$/ - YadavDocumentsangular-protractor-cucumber
ode_modulescucumbersrcsupport_code_library_builderdefine_helpers.js:90
     I am on the angular.io site`

我读了这个 链接 ,这让我相信这是不建议.如果这是正确的,我该如何使用页面对象的强大功能,更重要的是,解决多步骤定义问题?

I read this link which states which makes me believe that this is not recommended. If this is correct, how can I use the power of Page Objects and more importantly, resolve the multiple step definitions issue?

GIT 中有可重现的示例.

推荐答案

当 2 个步骤在其步骤定义中共享相同的正则表达式/字符串时,就会发生歧义错误,因为跑步者必须决定使用哪一个.

The Ambiguous error happens when 2 steps share the same regex/string in their step definition, as the runner has to decide which one of them to use.

如果多个功能和场景之间存在共享步骤,最好将它们分成通用文件,以便于跟踪.

If there is a shared step between multiple features and scenarios, it's best to separate these into common files, to make them easier to track.

查看我对问题什么是组织功能文件的最佳方式?"查看我如何组织我的项目.

See my answer to the question "What's the best way to organize feature files?" to see how I organise my projects.

在伪代码(Ruby 实现)中,我将举一个例子.

In pseudo-code (the Ruby implementation), I'll give an example.

示例

作为测试人员,您希望能够浏览您拥有的网页,而无需在功能文件中明确说明网址,以防网址在未来某个时间点发生变化.

As a tester, you'll want to be able to navigate through the webpages that you have, without expressly stating the URL in the feature file, in the case that the URL changes at some point in the future.

urls = Project.urls # Class with url method that returns a map, where pages are the keys and urls are the values

Given 'I navigate to the {string} page' do | page |
    driver.navigate.to(urls[page.downcase])
end

这意味着在功能文件中,您将能够这样做:

This will mean in the feature file, you'll be able to do this:

Given I navigate to the "Home" page

这使步骤保持动态、易于重复使用和将来可维护,无需复制步骤定义.

Which keeps the step dynamic, easily reusable and maintainable for the future, with no need to duplicate the step definition.

编辑

在评论中,要求提供带有黄瓜的页面对象模型的工作示例.这是一个关于 Git 的工作示例.

In the comments, a working example for the Page Object Model with cucumber has been requested. Here's a working example on Git.

用 Ruby 编写,这是一个非常简单的框架版本,表示目录结构以及我个人如何使用 POM(注意:有很多正确"的方法可以做到这一点).

Written in Ruby, this is an extremely bare-bones version of the framework, denoting directory structure and how I personally work with the POM (note: there are many "right" ways to do this).

这个例子对 POM 的要求比我在上面的例子中建议的要严格得多,而是在 elements 目录的urls"文件中存储一个go_to"方法,但如前所述,有很多对" 使用 POM 的方法.

This example is a lot stricter with the POM than I suggested to use in the example above, instead storing a "go_to" method within the "urls" file in the elements directory, but as stated previously, there are many "right" ways to go about utilising the POM.

这篇关于Cucumber 中的多个步骤定义匹配错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆