Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 980
Ade Bakre Avatar asked May 09 '26 15:05

Ade Bakre


1 Answers

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 |
like image 184
Xena Avatar answered May 12 '26 04:05

Xena



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!