One way of doing this is to make use of the BeforeClass and AfterClass
methods of either junit or testng for the runner. The problem is there can be only one scenariooutline in the feature file and it will need a unique runner class for it.
Having a method run only for the first scenario run off a scenario outline is pretty easy. Create a before hook to run once only using a static flag. Modify the Before hook to run for certain tags etc..
private static boolean skipFlag = false;
@Before
public void beforeHook() {
if(!skipFlag) {
do stuff
skipFlag=true;
}
}
Though this works well with one scenario outline in a feature file. Else you will need to replicate flags fro each scenariooutline. For parallel running it may get messy.
A better but a little complex solution which requires to split the examples table into 3 and give the top and bottom part tags.
@AftBef
Scenario Outline:
When User Selects <Origin>, <Destination>
@StartIt
Examples:
| Origin | Destination |
| London | New York |
Examples:
| Origin | Destination |
| Munich | Moscow |
| Rome | Shanghai |
@EndIt
Examples:
| Origin | Destination |
| Miami | San Francisco |
Add the below hook methods with tags. You can also add tag(s) to the middle part
if there is any custom logic.
@Before(value={"@StartIt"})
public void startItAll() {
System.out.println("-----START IT BIRTH BIRTH----");
}
@After(value={"@EndIt"})
public void endItAll() {
System.out.println("-----END IT DIE DIE---------");
}
If you have only one example just add both tags to the examples like below.
@StartIt @EndIt
Examples:
Need to be careful in creating the table to make sure only one example row is present for the @StartIt and @EndIt tags.