Invoking browsers with Baseclass.Contrib.Specflow in C# using Browser.Current

喜欢而已 提交于 2019-12-10 18:05:01

问题


I'm currently trying to use Selenium Grid 2 to run automation tests on multiple browsers. During my research I came across using Baseclass.Contrib.Specflow which enables me to use the browsers as tags in the feature files without having to declare it in my main driver class. The problem I have is that one of the blogs I read had the following as the set up code

[SetUp]
public void Test_Setup(){
CurrentDriver = Browser.Current;}

My app config file looks contains the following:

   <components>
  <!-- <component name="Firefox" type="OpenQA.Selenium.Firefox.FirefoxDriver, WebDriver" service="OpenQA.Selenium.IWebDriver, WebDriver" instance-scope="per-dependency">
  </component>-->
  <component name="Firefox" 
             type="Baseclass.Contrib.SpecFlow.Selenium.NUnit.RemoteWebDriver, Baseclass.Contrib.SpecFlow.Selenium.NUnit.SpecFlowPlugin" 
             service="OpenQA.Selenium.IWebDriver, WebDriver" 
            instance-scope="per-dependency">
    <parameters>
      <parameter name="url" value=" http://localhost/wd/hub" />
      <parameter name="browser" value="Firefox" />
    </parameters>
  </component>
  <component name="Safari" type="Baseclass.Contrib.SpecFlow.Selenium.NUnit.RemoteWebDriver, Baseclass.Contrib.SpecFlow.Selenium.NUnit.SpecFlowPlugin" service="OpenQA.Selenium.IWebDriver, WebDriver" instance-scope="per-dependency">
    <parameters>
      <parameter name="url" value=" http://localhost/wd/hub" />
      <parameter name="desiredCapabilities" value="Chrome" />
    </parameters>
  </component>

I get an error when I try to run the script using the above Setup method.

Error:

System.Collections.Generic.KeyNotFoundException : The given key was not present in the dictionary

The blog I got this solution from doesn't seem to answer questions regarding to this so I'm a bit desperate. This will basically allow me to to the following on the feature file and get tests to run based on the tag

@Browser:Firefox
@Browser:Chrome

Hope this is enough information to give me advice.


回答1:


The mistake you are making here is that you are annotating your entire feature file with the tag @Browser.

Baseclass.Contrib.Specflow allows you to annotate scenarios with scenario supporting Browsers. Therefore, you have to annotate each scenario.

If you don't do that, there is no Current Browser set for that test and trying to access Browser.Current will throw System.Collections.Generic.KeyNotFoundException.

You know you're doing it right when the generated Unit Tests will include the Browser name as part of the unit test name like

<Test Name> on <Browser> with: <parameters>

Example:

@Browser:IE
@Browser:Chrome
@Browser:Firefox
Scenario Outline: Add Two Numbers
>Given I navigated to / using
And I have entered <summandOne> into summandOne calculator
And I have entered <summandTwo> into summandTwo calculator
When I press add
Then the result should be <result> on the screen
Scenarios:
| summandOne| summandTwo|result|
| 10 | 20 | 30 |
| 3 | 4 | 7 |


来源:https://stackoverflow.com/questions/31184992/invoking-browsers-with-baseclass-contrib-specflow-in-c-sharp-using-browser-curre

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