简体   繁体   English

覆盖SpecFlow中的步骤声明?

[英]Override a step declaration in SpecFlow?

So at my job we have a core SpecFlow library that our different teams can use for their automation. 因此,在我的工作中,我们有一个核心SpecFlow库,我们的不同团队可以将其用于自动化。 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? 是否可以覆盖它?

As @Grasshopper wrote, the step definition are global. 正如@Grasshopper所写,步骤定义是全局的。
But you could use Scopes to overwrite it. 但是您可以使用Scopes覆盖它。
See http://www.specflow.org/documentation/Scoped-Bindings/ 参见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. 在这种情况下,请不要忘记在每种情况下都将指定标签或原始步骤定义。

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. 这与我们正在做的事情有关,而与我们如何做无关。

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. 如果在同一步骤定义文件或另一个步骤定义文件中有相同的步骤(在您的情况下,当我单击按钮时)两次,则将得到DuplicateStepException。 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. 另外,您不能扩展包含文件的stepdefinition或hook,因为黄瓜会抛出错误,这是不可接受的。 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. 您将需要一起编写一个不同的步骤,或者在可能的情况下将按钮作为参数传递给现有步骤,并在允许修改库代码的情况下输入逻辑。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM