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

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

问题描述

我最近开始使用Cucumber.我正在尝试使用此链接来实现Cucumber + Protractor + TypeScript.基线.

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$/ - Yadav\Documents\angular-protractor-cucumber\node_modules\cucumber\src\support_code_library_builder\define_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.

示例

作为测试人员,您希望能够在拥有的网页中导航,而无需在功能文件中明确说明URL,以防URL在将来的某个时刻发生更改.

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.

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

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