Showing posts with label Hooks in Cucumber. Show all posts
Showing posts with label Hooks in Cucumber. Show all posts

Global Hooks and Tagged Hooks in BDD cucumber


BDD Framework, HOOKs in Cucumber:
Global Hooks
Use @Before for common steps running before the actual test ,ie, precondition.
Use @After for common steps running after the actual test, ie, post condition.
@Before and @After are global hooks.
Example:
Let’s take a feature file:
Note: You can create 1 test case and parameterize with example. Three test cases are creates just to show, three different test cases are run in this.
@sprint7
Feature: Sprint7

@finance
Scenario Outline: TestCase1
#Precondition:User is on the facebook page. After execution, close the session.
    And Enter "<uid_ColumnName>" and "<pwd_ColumnName>" and click login
    Then Validate screen


Examples:
      | uid_ColumnName | pwd_ColumnName |
      | realuid1       | realpwd1       |

@humanResource
 Scenario Outline: TestCase2
#Precondition:User is on the facebook page. After execution, close the session.
    And Enter "<uid_ColumnName>" and "<pwd_ColumnName>" and click login
    Then Validate screen

Examples:
      | uid_ColumnName | pwd_ColumnName |
      | realuid2       | realpwd2       |
     
@marketing
Scenario Outline: TestCase3
#Precondition:User is on the facebook page. After execution, close the session.
    And Enter "<uid_ColumnName>" and "<pwd_ColumnName>" and click login
    Then Validate screen

Examples:
      | uid_ColumnName | pwd_ColumnName |
      | realuid3       | realpwd3      |

Step Definition:

package stepDefinition;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

import cucumber.api.java.After;
import cucumber.api.java.Before;
import cucumber.api.java.en.And;
import cucumber.api.java.en.Then;

public class StepDefinitionClass {

      WebDriver driver;

      @Before
      public void OpenBrowserOpenfacebook() throws Throwable {
            System.setProperty("webdriver.chrome.driver",
                        "C:\\Path\\chromedriver.exe");
            driver = new ChromeDriver();
            driver.manage().window().maximize();
            driver.get("https://facebook.com/");
      }

      @After
      public void Close_Session() {
            driver.quit();
      }

      @And("^Enter \"([^\"]*)\" and \"([^\"]*)\" and click login$")
      public void Enter_and_and_click_login(String userID, String password) throws Throwable {
      driver.findElement(By.xpath("//input[@id='email']")).sendKeys(userID);
      driver.findElement(By.xpath("//input[@id='pass']")).sendKeys(password);
      driver.findElement(By.xpath("//input[@type='submit']")).click();
      }

      @Then("^Validate screen$")
      public void Validate_screen() throws Throwable {
            System.out.println("Screenshot taken");
            Thread.sleep(5000);
      }

}

Test Runner :
package testrunner;

import org.junit.runner.RunWith;
import cucumber.api.junit.Cucumber;

@RunWith(Cucumber.class)
@Cucumber.Options(
features="\\Users\\Badri\\Documents\\Selenium\\CucumberTest\\features\\featureFile.feature",
glue={"stepDefinition"},
format={"pretty"},
monochrome=true
)
public class TestRunner {

}

Tagged Hooks
Hooks for particular testcase or testcases
Feature file:
@sprint7
Feature: Sprint7

      @finance
  Scenario Outline: TestCase1
#Precondition:User is on the facebook page. After execution, close the session.
    And Enter "<uid_ColumnName>" and "<pwd_ColumnName>" and click login
    Then Validate screen


    Examples:
      | uid_ColumnName | pwd_ColumnName |
      | realuid1       | realpwd1       |

      @humanResource
  Scenario: TestCase2     #Outline should be added here to use example
#Hooks: Global Precondition:User is on the facebook page. After execution, close the session.
#Tagged Hooks:Precondition for this test case: User is on forgot pwd page
#Tagged Hooks: Click serach is post condition.
    And Enter mobile number

     
  @marketing
  Scenario Outline: TestCase3    #Outline should be added here to use example
#Precondition:User is on the facebook page. After execution, close the session.
    And Enter "<uid_ColumnName>" and "<pwd_ColumnName>" and click login
    Then Validate screen

    Examples:
      | uid_ColumnName | pwd_ColumnName |
      | realuid3       | realpwd3      |


Step Definition:
package stepDefinition;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

import cucumber.api.java.After;
import cucumber.api.java.Before;
import cucumber.api.java.en.And;
import cucumber.api.java.en.Then;

public class StepDefinitionClass {

      WebDriver driver;

      @Before
      public void OpenBrowserOpenfacebook() throws Throwable {
            System.setProperty("webdriver.chrome.driver",
                  "C:\\Users\\Badri\\Documents\\Selenium\\chromedriver_win32\\chromedriver.exe");
            driver = new ChromeDriver();
            driver.manage().window().maximize();
            driver.get("https://facebook.com/");
      }

      @After
      public void Close_Session() {
            driver.quit();
      }

      @Before("@humanResource")//Tagged Hooks
      public void fogotPassword(){
            driver.findElement(By.xpath("//a[text()='Forgot account?']")).click();
      }
     
      @After("@humanResource")//Tagged Hooks
      public void clickSearch() {
      driver.findElement(By.xpath("//input[@value='Search']")).click();
      }
     
      @And("^Enter mobile number$")
      public void enterMobile() throws Throwable {
      driver.findElement(By.xpath("//input[@id='identify_email']")).sendKeys("123456");
      }


      @And("^Enter \"([^\"]*)\" and \"([^\"]*)\" and click login$")
      public void Enter_and_and_click_login(String userID, String password) throws Throwable {
      driver.findElement(By.xpath("//input[@id='email']")).sendKeys(userID);
      driver.findElement(By.xpath("//input[@id='pass']")).sendKeys(password);
      driver.findElement(By.xpath("//input[@type='submit']")).click();
      }

      @Then("^Validate screen$")
      public void Validate_screen() throws Throwable {
            System.out.println("Screenshot taken");
            Thread.sleep(5000);
      }

}

Test Runner:
package testrunner;

import org.junit.runner.RunWith;
import cucumber.api.junit.Cucumber;

@RunWith(Cucumber.class)
@Cucumber.Options(
features="\\Users\\path\\features\\featureFile.feature",
glue={"stepDefinition"},
format={"pretty"},
monochrome=true
)
public class TestRunner {

}

We can add priority to Hooks: 

@Before(order=0)
@After(order=0)
@Before(order=1) 
@After(order=1)