After Scenario Outline hook in Cucumber

Solarsoul picture Solarsoul · Apr 27, 2018 · Viewed 8.7k times · Source

I'm using Java and Cucumber. I need to do some actions after every scenario outline. I know there are @Before and @After hooks, but they are applicable to every scenario in Scenario Outline. Is there any possibility to launch some actions exactly after all scenarios in outline and not after every scenario?

Example:

   Scenario Outline: Some scenario
   Given given actions
   When when actions
   Then display <value>
Examples:
|value|
|a    |
|b    |

I want to make execution in following way:

@Before actions

a value

b value

@After actions

@Before actions

//another scenario outline output

@After actions

Answer

GaurZilla picture GaurZilla · Apr 27, 2018

Yes, you can use Tags.

   @tag1
   Scenario Outline: Some scenario
   Given given actions
   When when actions
   Then display <value>
Examples:
|value|
|a    |
|b    |

Here we have specified tag1 as a tag.

Now in step definition, you can use something like this:

@Before("@tag1")
public void testSetup(){
    ...
} 

@After("@tag1")
public void testEnd(){
    ...
} 

These @before and @after are specific to tag1 now.' I hope this helps.