Practical usage of @Before and @After Cucumber Hooks

I started my automation testing journey with QTP 9.0 in 2009, and one thing that I always found very good in QTP 9.0 and all its future versions was its reporting mechanism. I particularly liked the object level reporting facility, using which we can report out step by step the object level interactions. For example, if you are automating some login screen where you enter username, password and click on Login button, then using QTP you can generate reports which can contain the detailed steps like this –

  • Entered ‘Anish’ in txtUserName field
  • Entered ‘abc123’ in txtPassword field
  • Button btnLogin clicked

Looking for more articles on Cucumber? Head over to our Selenium Cucumber Tutorial main page to check out our other Cucumber related articles

The best part about this type of reporting is that you get a step by step report, looking at which it is easier to figure out if all the steps were executed or if there was any error in any of the steps. When I later switched to Selenium, I wanted to implement this object level reporting in my Selenium Frameworks as well. I was able to achieve this using logging frameworks (Log4J, SLF4J, LogBack etc).

Recently, I used Cucumber in one my test automation projects, and I implemented object level logging in this project as well. With Cucumber, I used many feature files and there were around 5-8 scenarios in each of these feature files. The entire setup worked well, but there was one minor issue related to reporting which I wanted to resolve. With my step by step level reporting, the entire log for all the scenarios was dumped one after another in different lines. This was, it was very difficult to figure out where one scenario ended and next one begun. This also made analyzing the results very tedious, especially when I had lots of scenarios.

This is where I found @Before and @After Cucumber hooks useful. I had always used JUnit or TestNG with Cucumber and I didn’t see the need of using @Before and @After Cucumber hooks, especially when JUnit & TestNG frameworks provide these annotations themselves. So this article is basically show a practical usage of these Cucumber hooks.

Cucumber Hooks – Where to use @Before

@Before, in its most basic usage, allows you to run a block of code before every scenario. Normally in Cucumber, we tend to do the initialization related things – such as object initialization, data setup etc in the Given statement. Because of this a lot of people don’t see the need of using Cucumber’s @Before. But you can use @Before to make in an entry in your reporting that a new scenario is being executed. Since @Before always runs before every scenario, you can use this in your reports to clearly depict when a scenario starts executing.

It’s not necessary that you add @Before in each feature file. Just add it in any one feature file and let Cucumber do its job. Cucumber will figure out where you have saved @Before and then it will use it before all the scenarios. To make your reporting bit more useful and easy to understand, you can actually write the scenario name as well in the report. The Java code for this is given below –

@Before 
public void before(Scenario scenario) { 
    System.out.println("------------------------------"); 
    System.out.println("Starting - " + scenario.getName()); 
    System.out.println("------------------------------"); 
}

Cucumber API provides an Interface called Scenario, using which you can get can instance of this class. In the above code, we have just used the getName() method of this interface to print the scenario name in our log.

Cucumber Hooks – Where to use @After

@After works the same way as @Before, the only difference being that @After runs your block of code at the end of each scenario. What do you think would be the some good usage for @After. Just like @Before, you can obviously use @After to write in your log file that your scenario has ended. But there are some more useful things that you can do with @After cucumber hook.

We had seen the usage of Scenario interface in @Before hook and had used the getName() method. Scenario interface contains many other useful functions, one of which – getStatus(), can give you the status of execution of the scenario. This method, getStatus() returns the most severe status of the Scenario’s Steps. One of “passed”, “undefined”, “pending”, “skipped”, “failed”. You can either directly print the status of each scenario when it ends, or you can collate the results of all the scenarios and then print out combined results once all the scenarios are executed. The basic function of printing the status at the end of the scenario is given below –

@After
public void before(Scenario scenario) {
    System.out.println("------------------------------");
    System.out.println(scenario.getName() + " Status - " + scenario.getStatus());
    System.out.println("------------------------------");
}

If you want to collate all the status for all the scenarios, and then report it out at the end of the test execution then you would have to make use of TestNG or JUnit annotations such as @AfterAll, because Cucumber in the present format doesn’t provide any implementation for @AfterAll. There are few more methods provided by Scenario interface such as isFailed(), getId(), getSourceTagNames() etc. You can use any of these methods depending upon your specific requirements.

This is what I have been using @Before and @After cucumber hooks. Is there anything else which you use it for? Share your experience with us using the comments section.