Specflow功能,以同样的步骤文件导致多个浏览器实例启动 [英] Specflow Feature files with same steps causing multiple browser instances to launch

查看:314
本文介绍了Specflow功能,以同样的步骤文件导致多个浏览器实例启动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在C#Specflow测试项目中,我有步骤至少3 .feature文件,例如:

I have at least 3 .feature files in my C# Specflow tests project in which I have the step, for instance:

鉴于我我的首页

当我第一次写在文件中的步 Feateure1.feature 并创建了一步的方法,我把它放在一个STEP文件,比方说, Steps1.cs ,它从一个基类继承初始化 FirefoxDriver 。我所有的 StepsXXXX.cs 类从这个基类继承。

When I first wrote the step in the file Feateure1.feature and created the step method, I placed it in a step file, let's say, Steps1.cs, which inherits from a base class that initializes a FirefoxDriver. All my StepsXXXX.cs classes inherit from this base class.

然后,我写了 Feature2.feature ,其中也有步鉴于我的首页。和步骤是automaticaly势必一个在 Steps1.cs

Then, I wrote Feature2.feature, which also has a step Given I am at the Home Page. And the step was automaticaly bound to the one in Steps1.cs

到现在为止,没有任何问题。这几乎是我想要的东西 - 有整个测试项目可重复使用的步骤。但问题是,每当我跑了有diferent步骤 StepsXXXX 文件的情况下,我得到运行的各种浏览器实例。

'Till now, no problem. That's pretty much what I wanted - to have reusable steps throughout the test project. But the problem is, whenever I'm running a scenario that has steps in diferent StepsXXXX files, I get various browser instances running.

==

我敢肯定,这是由于这样的事实,我的 StepsXXXX (绑定类),所有这一切都有自己的IWebDriver这个基类继承,并在步骤称为,其他的一切(包括前/场景后的方法)被调用。但我无法弄清楚如何解决这个问题。

I'm pretty sure this is due to the fact that My StepsXXXX (binding classes) all inherit from this base class that has a IWebDriver of its own, and when the step is called, everything else (including the before/after scenario methods) is called. But I can't figure out how to work around this.

我还是要重复使用的步骤。我试图把在基类中这些步骤,但没有奏效。
我想改变绑定过的,但specflow使用meaningfull字符串这样做了,我不想改变他们误导字符串。

I still want reusable steps. I tried to put these steps in the base class, but it did not work. I thought of changing the bindings too, but specflow uses meaningfull strings to do so, and I don't want to change them to misleading strings.

有整个人迷迷糊糊这样?
任何帮助真的赞赏。

Has anyone stumbled across this? Any help is really appreciated.

推荐答案

的问题是,SpecFlow绑定不尊重继承。所有的自定义属性被认为是全球性的,因此所有SpecFlow确实是搜索类带有[绑定]一个名单,然后建立一个字典全部[鉴于] / [如果] / [然后]如此,它可以对其进行评估的最佳匹配。然后,它会创建类的实例(如果还没有这样做的话),并调用该方法就可以了。

The problem is that SpecFlow bindings don't respect inheritance. All custom attributes are considered global, and so all SpecFlow does is search for a list of classes with a [Binding]then build up a dictionary for all the [Given]/[When]/[Then]s so that it can evaluate them for a best match. It will then create an instance of the class (if it hasn't already done so) and call the method on it.

因此,您的简单的情况下,所有留在Steps1类,因为它第一次完美匹配。你更复杂的情况下启动实例更多的类,因此多个浏览器,而你试图重构将无法工作,因为你的抽象基类不具有[绑定]就可以了。

As a result your simple cases all stay in the Steps1 class, because its the first perfect match. Your more complicated cases start instantiating more classes, hence multiple browsers, And your attempt to refactor won't work because your abstract base class doesn't have a [Binding] on it.

我可能会通过压平所有的步骤类层次结构,成一个大AllSteps.cs类开始。的这似乎适得其反,但你真正做的是安排当前代码绑定是如何出现的SpecFlow功能。的这种方式,你就可以开始重构出不同的GWT绑定之间的重叠

I'd probably start by flattening all your step class hierarchy, into one big AllSteps.cs class. This may seem counter-productive, but all you are really doing is arranging the code just how the current bindings appear to your SpecFlow features. This way you can start to refactor out the overlaps between the different GWT bindings.

目前您的绑定全都围绕着场景的时刻。你将需要做的是重构他们周围的功能。有的域是无论如何它的读?然后再开始,这可能会给你一些好的想法。然后看看共享,数据之间的绑定上在SpecFlow文档,以制定出如何将新的步骤类之间的链接。

At the moment your bindings are arranged around the scenarios. What you will need to do is refactor them around your functionality. Have a read of Whose Domain is it anyway? before you start and this will probably give you some good ideas. Then have a look at Sharing-Data-between-Bindings on the SpecFlow documentation to work out how to link between your new steps classes.

这篇关于Specflow功能,以同样的步骤文件导致多个浏览器实例启动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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