Hooks and Tags are most interesting topic on Cucucmber-JVM.
Hooks is a technique to run set of code before and after the whole Feature or Scenarios. So we need to use
@Before
and
@After
annotation for hook feature of Cucumber-JVM. We need to be sure when we use those above mentioned annotation then we should import below mentioned packages on Step generation or Glue Code file
import cucumber.api.java.After;
import cucumber.api.java.Before;
It is possible to define a hook so that it is only executed before or after scenarios that are tagged with a specified annotation
We will define this on Feature file and pass this tag value on @Before or @After method on step file. It should be looked like below:
@Before("@add")
or
@Before(value={"@add"})
@After(value={"@add"}))
Feature file should be :
Feature: This is a sample feature file
@add
Scenario: This is a scenario for Adding two number.
Given Two number as 1 and 6
When We add this two given number
Then Sum Result should be 7
We can use AND/OR logic on hooks also.
Syntax for AND logic is:
@Before(value={"@add","@sub"})
@After(value={"@add","@sub"})
Syntax for OR logic is :
@Before(value={"@add,@sub"})
@After(value={"@add,@sub"})
So AND logic means hooks will be executed when all tags are matched with the feature file tags and OR logic means when any of the tag matched with feature file.
We can also mention the execution order when we will use multiple hooks. Syntax looks like below:
@Before(order=5)
@After(order =5)
If we use multiple @Before with different order then lower order number execute first but behavior of @After method is opposite, upper order number execute first then execute the lower order number.
Complete Code:
Feature File :
Feature: This is a sample feature file
@tag1
Scenario: This is a scenario for Adding two number.
Given Two number as 1 and 6
When We add this two given number
Then Sum Result should be 7
@tag2 @tag3
Scenario: This is a scenario for sub two number.
Given Two number as 8 and 1
When We substract this two given number
Then Sub Result should be 7
@tag4 @tag5
Scenario: This is a scenario for sub two number.
Given Two number as 6 and 1
When We add this two given number
Then Sum Result should be 7
And We substract this two given number
Then Sub Result should be 5
Glue Code :
import org.junit.Assert;
import cucumber.api.java.After;
import cucumber.api.java.Before;
import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;
import cucumber.api.java.en.When;
public class SumFeatureTest {
@Before(order=1,value={"@tag1"})
public void beforeMethod(){
System.out.println("This will be executed When Tag Matched With The Following Tag Matched :@tag1. ");
}
@Before(order=5,value={"@tag1"})
public void beforeMethodForOrderSingle(){
System.out.println("Upper Order Value Execute Later For @Before Method. This Is Executed From Single Tag Method : beforeMethodForOrderSingle ");
}
@Before(order=1,value={"@tag4","@tag5",})
public void beforeMethodAND(){
System.out.println("This will be executed When Both Of The Following Tag Matched :@tag4 AND @tag5. ");
}
@Before(order=5,value={"@tag4","@tag5"})
public void beforeMethodForOrderAND(){
System.out.println("Upper Order Value Execute Later For @Before Method. This Is Executed From AND Tag Method : beforeMethodForOrderAND ");
}
@Before(order=1,value={"@tag2,@tag3"})
public void beforeMethodOR(){
System.out.println("This will be executed When Any Of The Following Tag Matched : @tag2 OR @tag3. ");
}
@Before(order=5,value={"@tag2,@tag3"})
public void beforeMethodforOrderOR(){
System.out.println("Upper Order Value Execute Later For @Before Method.This Is Executed From OR Tag Method : beforeMethodforOrderOR ");
}
@After(order=1)
public void afterMethod(){
System.out.println("Lower Order Value Execute Later For @After Method.");
}
@After(order =5)
public void afterMethod1(){
System.out.println("Upper Order Value Execute First For @After Method.");
}
int input1,input2,sumResult,subResult;
@Given("^Two number as (\\d+) and (\\d+)$")
public void twoNumberGivenAsAndAnotherIs(int firstInput, int secondInput) throws Throwable {
input1 = firstInput;
input2 = secondInput;
}
@When("^We add this two given number$")
public void weAddThisTwoGivenNumber() throws Throwable {
sumResult = input1+input2;
System.out.println("sum");
}
@When("^We substract this two given number$")
public void weSubstractThisTwoGivenNumber() throws Throwable {
subResult = input1-input2;
System.out.println("sub");
}
@Then("^Sum Result should be (\\d+)$")
public void sumResultShouldBe(int expectedTotal) throws Throwable {
Assert.assertEquals("Not Matched: ", expectedTotal, sumResult);
}
@Then("^Sub Result should be (\\d+)$")
public void subResultShouldBe(int expectedTotal) throws Throwable {
Assert.assertEquals("Not Matched: ", expectedTotal, subResult);
}
}
Runner Class:
import org.junit.runner.RunWith;
import cucumber.api.CucumberOptions;
import cucumber.api.SnippetType;
import cucumber.api.junit.Cucumber;
@RunWith(Cucumber.class)
@CucumberOptions(
plugin = {
"html:target/cucumber-html-report",
"json:target/cucumber.json",
"pretty:target/cucumber-pretty.txt",
"usage:target/cucumber-usage.json",
"junit:target/cucumber-results.xml",
"progress:target/cucumber-progress.txt"
},features ={"./sample.feature"},
glue ={"com/automation/steps"},
monochrome = true, snippets= SnippetType.CAMELCASE)
public class Runner {
}
Hooks is a technique to run set of code before and after the whole Feature or Scenarios. So we need to use
@Before
and
@After
annotation for hook feature of Cucumber-JVM. We need to be sure when we use those above mentioned annotation then we should import below mentioned packages on Step generation or Glue Code file
import cucumber.api.java.After;
import cucumber.api.java.Before;
It is possible to define a hook so that it is only executed before or after scenarios that are tagged with a specified annotation
We will define this on Feature file and pass this tag value on @Before or @After method on step file. It should be looked like below:
@Before("@add")
or
@Before(value={"@add"})
@After(value={"@add"}))
Feature file should be :
Feature: This is a sample feature file
@add
Scenario: This is a scenario for Adding two number.
Given Two number as 1 and 6
When We add this two given number
Then Sum Result should be 7
We can use AND/OR logic on hooks also.
Syntax for AND logic is:
@Before(value={"@add","@sub"})
@After(value={"@add","@sub"})
Syntax for OR logic is :
@Before(value={"@add,@sub"})
@After(value={"@add,@sub"})
So AND logic means hooks will be executed when all tags are matched with the feature file tags and OR logic means when any of the tag matched with feature file.
We can also mention the execution order when we will use multiple hooks. Syntax looks like below:
@Before(order=5)
@After(order =5)
If we use multiple @Before with different order then lower order number execute first but behavior of @After method is opposite, upper order number execute first then execute the lower order number.
Complete Code:
Feature File :
Feature: This is a sample feature file
@tag1
Scenario: This is a scenario for Adding two number.
Given Two number as 1 and 6
When We add this two given number
Then Sum Result should be 7
@tag2 @tag3
Scenario: This is a scenario for sub two number.
Given Two number as 8 and 1
When We substract this two given number
Then Sub Result should be 7
@tag4 @tag5
Scenario: This is a scenario for sub two number.
Given Two number as 6 and 1
When We add this two given number
Then Sum Result should be 7
And We substract this two given number
Then Sub Result should be 5
Glue Code :
import org.junit.Assert;
import cucumber.api.java.After;
import cucumber.api.java.Before;
import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;
import cucumber.api.java.en.When;
public class SumFeatureTest {
@Before(order=1,value={"@tag1"})
public void beforeMethod(){
System.out.println("This will be executed When Tag Matched With The Following Tag Matched :@tag1. ");
}
@Before(order=5,value={"@tag1"})
public void beforeMethodForOrderSingle(){
System.out.println("Upper Order Value Execute Later For @Before Method. This Is Executed From Single Tag Method : beforeMethodForOrderSingle ");
}
@Before(order=1,value={"@tag4","@tag5",})
public void beforeMethodAND(){
System.out.println("This will be executed When Both Of The Following Tag Matched :@tag4 AND @tag5. ");
}
@Before(order=5,value={"@tag4","@tag5"})
public void beforeMethodForOrderAND(){
System.out.println("Upper Order Value Execute Later For @Before Method. This Is Executed From AND Tag Method : beforeMethodForOrderAND ");
}
@Before(order=1,value={"@tag2,@tag3"})
public void beforeMethodOR(){
System.out.println("This will be executed When Any Of The Following Tag Matched : @tag2 OR @tag3. ");
}
@Before(order=5,value={"@tag2,@tag3"})
public void beforeMethodforOrderOR(){
System.out.println("Upper Order Value Execute Later For @Before Method.This Is Executed From OR Tag Method : beforeMethodforOrderOR ");
}
@After(order=1)
public void afterMethod(){
System.out.println("Lower Order Value Execute Later For @After Method.");
}
@After(order =5)
public void afterMethod1(){
System.out.println("Upper Order Value Execute First For @After Method.");
}
int input1,input2,sumResult,subResult;
@Given("^Two number as (\\d+) and (\\d+)$")
public void twoNumberGivenAsAndAnotherIs(int firstInput, int secondInput) throws Throwable {
input1 = firstInput;
input2 = secondInput;
}
@When("^We add this two given number$")
public void weAddThisTwoGivenNumber() throws Throwable {
sumResult = input1+input2;
System.out.println("sum");
}
@When("^We substract this two given number$")
public void weSubstractThisTwoGivenNumber() throws Throwable {
subResult = input1-input2;
System.out.println("sub");
}
@Then("^Sum Result should be (\\d+)$")
public void sumResultShouldBe(int expectedTotal) throws Throwable {
Assert.assertEquals("Not Matched: ", expectedTotal, sumResult);
}
@Then("^Sub Result should be (\\d+)$")
public void subResultShouldBe(int expectedTotal) throws Throwable {
Assert.assertEquals("Not Matched: ", expectedTotal, subResult);
}
}
Runner Class:
import org.junit.runner.RunWith;
import cucumber.api.CucumberOptions;
import cucumber.api.SnippetType;
import cucumber.api.junit.Cucumber;
@RunWith(Cucumber.class)
@CucumberOptions(
plugin = {
"html:target/cucumber-html-report",
"json:target/cucumber.json",
"pretty:target/cucumber-pretty.txt",
"usage:target/cucumber-usage.json",
"junit:target/cucumber-results.xml",
"progress:target/cucumber-progress.txt"
},features ={"./sample.feature"},
glue ={"com/automation/steps"},
monochrome = true, snippets= SnippetType.CAMELCASE)
public class Runner {
}