Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Specflow: maintain one Examples table for many Scenario Outlines

Is there a way of keeping the "Examples" data table in a separate file from the Scenario Outline?

What I am trying to achieve is to run the entire scenario once per browser, one after the other.

I have this working with the following feature:

Feature: OpenGoogleInChrome

Scenario Outline: Open Google in Chrome
    Given a browser '<browser>'
    When the browser points to 'https://www.google.co.uk/'
    Then the title should be 'Google'

Examples:
    | browser |
    | Chrome  |
    | Edge    |
    | Firefox |

But this would mean maintaining the Examples table across every single test if I added another browser. Is it possible to reference a single "Examples" table from each Scenario Outline?

Or call a Scenario Outline, complete with an examples table, from a Step Definition?

(Using Specflow and Selenium WebDriver with NUnit )

While "Background" lets you define a shared table, it appears to be for a single feature with multiple scenarios. What I'm looking for is a way to use the same table in every feature across different (.feature) files.

From experience I know that using tags like @Chrome can work for individual browsers (this is how I have written most of my tests) but using multiple tags results in all the browsers running at the same time, not one after the other.

like image 698
cacheCache Avatar asked Oct 26 '15 15:10

cacheCache


1 Answers

I think the most maintainable approach is to parameterize the browser that gets used by setting an environment variable before running the test. Inside your Given step definition, you can use Environment.GetVariable("...") to retrieve the value at runtime.

Another alternative is to create a special text file that contains the name of the browser to use. Open this file and read the contents in your Given step definition.

If you have an automated build, you could set up a power shell or batch file that sets this text file to the first browser, runs all the tests, then sets the text file to the next browser and reruns the tests. Rinse and repeat for each browser you want to use.

You could throw this into the <appSettings> of the test project in Visual Studio and utilize config transformations. When running the NUnit tests from the command line you can switch the configuration:

nunit-console nunit.tests.csproj /config:Firefox
nunit-console nunit.tests.csproj /config:InternetExplorer
nunit-console nunit.tests.csproj /config:Chrome
nunit-console nunit.tests.csproj /config:Safari

The downside is you create one build configuration for each browser, but it should do the trick.

like image 95
Greg Burghardt Avatar answered Sep 23 '22 02:09

Greg Burghardt