2
votes

So at my job we have a core SpecFlow library that our different teams can use for their automation. This library has some declared steps.

For example, the library might have something like this:

When I click the button

However, let's say I want to define my own step declaration that uses that exact same wording. Is it possible to override it?

3

3 Answers

3
votes

As @Grasshopper wrote, the step definition are global.
But you could use Scopes to overwrite it.
See http://www.specflow.org/documentation/Scoped-Bindings/

In this case do not forget to specify on every scenario the tag or the original step definition will be called.

3
votes

It would be a very bad idea to do this, as any scenario that uses this step and fails will be very much harder to understand and debug.

In general using generic library steps in scenarios is also not such a good idea. Scenarios should not contain generic steps or descriptions of HOW things are done. Instead they should contain steps specific to your business context, and these should describe WHAT is being done and WHY its being done.

So instead of

When I click on sign in
   And I fill in my email with ...
   ...

we get the much simpler and more abstract

 When I sign in

which is all about WHAT we are doing, and nothing about HOW we are doing it.

0
votes

You will get a DuplicateStepException if you have a same step (in your case - When I click the button) twice either in the same step definition file or another one. Even if you use a given or then annotation. This is because the step definitions are loaded globally thus resulting in conflict.

Also you cannot extend a stepdefinition or hook containing file as cucumber will throw an error that this is not acceptable. Thus no way you can override behaviour by inheritance.

You will need to write a different step all together, or if possible pass the button as a parameter to the existing step and put in the logic if you are allowed to modify the library code.