Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get the Scenario Outline Example iteration number

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?

like image 820
user3642874 Avatar asked Oct 31 '22 23:10

user3642874


1 Answers

In your When step definition you could record the current query in ScenarioContext.Current e.g.

[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 e.g.

[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 ); 
}
like image 157
Ben Smith Avatar answered Nov 15 '22 05:11

Ben Smith