问题
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.
回答1:
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.
回答2:
I don't believe that you can do this in specflow (or in any gherkin language implementation). As mentioned in the related question you can have a background to provide a table, but I'm not certain that this can be used to provide examples in scenario outlines, at least I've never seen this used and I'm not sure how it could work.
Possible (dirty) solutions I can think of would be along the lines of having a script which scanned your feature files and updated the examples when you add a new browser (I can't imagine this is very often), or having a code snippet to add the examples text if the typing it in every scenario outline is the problem.
回答3:
SpecFlow advises to use tags for these kind of scenarios. Maybe you did the Bookstore tutorial, then you'll recognize it from the tags that indicated if a scenario was a system or a browser test.
@Chrome @Firefox @IE # <- feature wide, applicable for all scenarios
Feature: Open Google
Scenario: Open Google in the browser
Given the browser is active
When I navigate to "https://www.google.co.uk/"
Then the title should be 'Google'
@Lynx # <- additional browser for a specific scenario
Scenario: There is a Search button
Given the browser is active
When I navigate to "https://www.google.co.uk/"
Then I should see a button with label "Search"
Now you can run the testrunner for each tag/category.
来源:https://stackoverflow.com/questions/33349847/specflow-maintain-one-examples-table-for-many-scenario-outlines