Monday, December 28, 2015

Cucumber Hooks/TestRunner(Glue Code)

Glue Code is the code that interacts directly with your application. There are two kinds of Glue code – Step Definitions and Hooks.

Cucumber provides a number of hooks which allow us to run blocks at various points in the Cucumber test cycle. You can put them in your support/env.java file or any other file under the support directory, for example in a file called support/hooks.java. There is no association between where the hook is defined and which scenario/step it is run for, but you can use tagged hooks (see below) if you want more fine grained control.

All defined hooks are run whenever the relevant event occurs


Hooks.java look like,


package com.shasta.StepDefinitions;
import org.junit.runner.RunWith;

import cucumber.api.CucumberOptions;
import cucumber.api.junit.Cucumber;



@RunWith(Cucumber.class)
@CucumberOptions(
features = {"resource/"},
plugin = {"pretty", "html:target/cucumber-html-report","json:target/cucumber.json"},
tags = {"@login,@testing,@closeBrowser"}
  )

public class HooksFile{
}


Scenario hooks

  • Before hooks will be run before the first step of each scenario. They will run in the same order of which they are registered.


/*@Before
public void setUp() throws Exception{
# Do something before each scenario. 
}

# The +scenario+ argument is optional, but if you use it, you can get the title, # description, or name (title + description) of the scenario that is about to be # executed.

  • After hooks will be run after the last step of each scenario, even when there are failing, undefined, pending or skipped steps. They will run in the opposite order of which they are registered.
@After
public void tearDown() throws Exception{
closeBrowser(driver);
}
 # Do something after each scenario. # The +scenario+ argument is optional, but # if you use it, you can inspect status with # the #failed?, #passed? and #exception methods.

  • Around hooks will run "around" a scenario. This can be used to wrap the execution of a scenario in a block. The Around hook receives a scenario object and a block (Proc) object. The scenario will be executed when you invoke block.call.
Tagged hooks

Sometimes you may want a certain hook to run only for certain scenarios. This can be achieved by associating a Before, After, Around or AfterStep hook with one or more tags. You can OR and AND tags in much the same way as you can when running Cucumber from the command line. 
Think twice before you use this feature, as whatever happens in hooks is invisible to people who only read the features. You should consider using background as a more explicit alternative if the setup should be readable by non-technical people.

Global hooks


If you want something to happen once before any scenario is run - just put that code at the top-level in your env.java file (or any other file in your features/support directory.

AfterConfiguration
You may also provide an AfterConfiguration hook that will be run after Cucumber has been configured.