Two Steps Definitions that are the same, but one for the given and one for Then

Cucumber function files throw an error (declared definitions of several steps) if I try to have two similar definitions of a step, but with different annotations -

@Then("^element with id \"([^\"]*)\" is displayed$") & @Given("^element with id \"([^\"]*)\" is displayed$") 

So far I have tricked him by adding extra space to the @Then definition (take a close look at @Then, after the regex).

But this is not a good practice.

I want @Given because it installs my script, and I definitely need @Then.

How do I pass this?

+1
source share
2 answers

Definition steps must be unique to Cucumber in order to know what to perform. In addition, the keywords โ€œDate / When / Thenโ€ are technically interchangeable. They are intended to be readable by the function file, but not implementation related. Therefore, if both steps (Given and Then) do the same, technically there is no problem; you should be able to use the same step definition from your properties file that is preceded by either the "Given" or "Then" keyword. However, you might want to rewrite the definitions of the step to describe the intended behavior instead of implementation, for example. "Given an element with id xxx"

+1
source

When the "Given" parameter is called, the state must be set and a Then they must make statements on the content. Your assignment is poorly constructed because it is about asserting what is on the page. You must rewrite it to describe WHAT you need to do to make sure that you can see a specific element.

For example, if your assignment matches

Given "element with id 'product_foo' is displayed"

This script should be revised in a read script.

Given 'there is a product foo'

Actually, you cannot implement Given("^element with id \"([^\"]*)\" is displayed$") for general use even in a small application, because it will need to know how to create everything that can be displayed in your application in any context. You can see the product_foo id on the product page, or when it was ordered, or in your basket. The first just requires the product, the second requires the previous order, and the third requires a session with the product in the basket.

0
source

All Articles