Multiple Step Definitions match error in Cucumber

只谈情不闲聊 提交于 2019-12-25 11:05:22

问题


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

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?

Reproducible example is here in this GIT.


回答1:


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.

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

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.

Edit

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

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).

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.



来源:https://stackoverflow.com/questions/46664254/multiple-step-definitions-match-error-in-cucumber

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!