I have the following test, using Specflow, Selenium WebDriver and C#:
Scenario Outline: Verify query result
Given I'm logged in
When I enter "<query>"
Then I should see the correct result
Examples:
| query |
| Query1 |
| Query2 |
| Query3 |
After each scenario I save a screenshot to a file which is named based on the ScenarioContext.Current.ScenarioInfo.Title. However, I can't find a good way to differentiate between the iterations, so the screenshots get overwritten. I could add a column in the Examples table, but I want a more generic solution...
Is there a way to know which iteration is being executed?
In your When step definition you could record the current query in ScenarioContext.Current eg
[When(@"I enter (.*)")]
public void WhenIEnter(string query)
{
ScenarioContext.Current["query"] = query;
}
Then in your AfterScenario step you could retrieve this value to identify which Example iteration just run eg
[AfterScenario]
void SaveScreenShot()
{
var queryJustRun = ScenarioContext.Current["query"];
// You could subsequently append queryJustRun to the screenshot filename to
// differentiate between the iterations
//
// e.g. var screenShotFileName = String.Format("{0}_{1}.jpg",
// ScenarioContext.Current.ScenarioInfo.Title,
// queryJustRun );
}
this is not possible from what I can tell; for a clearer example;
Scenario Outline: Add two numbers
Given I have entered <first> into the calculator
And I have entered <last> into the calculator
When I press add
Then the result should be <result> on the screen
Examples:
| name | first | last | result |
| simple | 1 | 1 | 2 |
| zero | 0 | 0 | 0 |
if you look at the generated code then the <name>
is not saved anywhere, it does not appear on either ScenarioContext or FeatureContext
TestExecutionResults in the report generator does seem to have this information, Model.TestExecutionResults[].TestItemResult.TestNode.Description is a composite string that includes the <name>
element in there; but how it gets there is a mystery to me.
This is checked in the spec runner generated code and report generator
The name will not appear unless you have defined in your scenario, which I can see you have not. Your feature could be written like this
Scenario Outline: Performing mathematical operation
Given I am on <name> scenario
And I have entered <first> into the calculator
And I have entered <last> into the calculator
When I do the <operation>
Then the result should be <result> on the screen
Examples:
| name | first | last | result | operation |
| simple | 1 | 1 | 2 | add |
| subs | 6 | 2 | 4 | substract |
The advantage of structuring a above is that you can leverage on same scenario to perform several operations like 'add', 'subtract', 'multiply', etc
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.