Hooks(Example)

 Feature: Advanced Search

@Logo
Scenario: AdvancedSearch Logo search
Given Iam in advance seach page
When click on the Ebay Logo
Then I am navigating to Ebay Homepage Again

@Itemscount
Scenario: search items count
Given Iam in Ebay Homepage
When I am searching for iphone11
Then I am validating atleast 1100 search items present


==========================

package testRunner;


import org.junit.runner.RunWith;

import io.cucumber.junit.Cucumber;

import io.cucumber.junit.CucumberOptions;


@RunWith(Cucumber.class)

@CucumberOptions(

features = "src/test/java/Features",

glue = "Steps",

tags = "@Logo or @Itemscount",

plugin = {

"pretty",

"html:target/cucumber-reports.html",

"json:target/cucumber.json"

},

monochrome = true

)

public class HookTestRunner {

}


Purpose: Entry point for Cucumber tests using JUnit.
@RunWith(Cucumber.class): Integrates JUnit with Cucumber.
@CucumberOptions:
  • features: Path to .feature files.
  • glue: Step definition package.
  • tags: Only scenarios with @Logo or @Itemscount will run.
  • plugin: Reporting formats – HTML, JSON.
  • monochrome=true: Clean console output.

==============================


package Steps;


import org.openqa.selenium.WebDriver;

import org.openqa.selenium.chrome.ChromeDriver;

import io.cucumber.java.After;

import io.cucumber.java.Before;


public class HookSteps {


private WebDriver driver;


@Before

public void setup() {

driver = new ChromeDriver();

driver.manage().window().maximize();

}


@After

public void teardown() throws InterruptedException {

driver.quit();

Thread.sleep(2000);

}


public WebDriver getDriver() {

return driver;

}

}


Hooks file for @Before and @After scenario lifecycle:

@Before: Initializes Chrome browser and maximizes window.
@After: Closes the browser after test execution.

Provides a getDriver() method to share the WebDriver instance with step classes like ItemCountHooks.
getDriver(): Provides access to the WebDriver instance to other classes.


==============================


package Steps;


import static org.testng.Assert.fail;


import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import io.cucumber.java.en.Given;

import io.cucumber.java.en.Then;

import io.cucumber.java.en.When;


public class itemCountHooks {


private WebDriver driver;


public itemCountHooks(HookSteps hookSteps) {

this.driver = hookSteps.getDriver();

}


@Given("I am on the advanced search page")

public void iAmOnTheAdvancedSearchPage() throws InterruptedException {

driver.get("https://www.ebay.com/");

Thread.sleep(2000);

driver.findElement(By.linkText("Advanced")).click();

Thread.sleep(2000);

}


@When("I click on the Ebay Logo")

public void iClickOnTheEbayLogo() {

driver.findElement(By.id("gh-logo")).click();

}


@Then("I am navigated to the Ebay Homepage again")

public void iAmNavigatedToTheEbayHomepageAgain() {

String expectedUrl = "https://www.ebay.com/";

String actualUrl = driver.getCurrentUrl();

if (!expectedUrl.equals(actualUrl)) {

fail("Not navigated to Ebay homepage");

}

}


@Given("I am on the Ebay Homepage")

public void iAmOnTheEbayHomepage() throws InterruptedException {

driver.get("https://www.ebay.com/");

Thread.sleep(2000);

}


@When("I am searching for iphone11")

public void IamSearching_ForIphone11() {

driver.findElement(By.id("gh-ac")).sendKeys("iphone11");

driver.findElement(By.className("gh-search-button__label")).click();

}


@Then("I am validating atleast 1100 search items present")

public void iValidating_AtLeast_1100Search_ItemsArePresent() {

String itemCountText = driver.findElement(By.cssSelector("div.srp-controls__control.srp-controls__count span:nth-child(1)")).getText().trim();

int itemCount = Integer.parseInt(itemCountText.replace(",", ""));

if (itemCount < 1100) {

fail("Item count is less than 1100");

}

}

}



  • iAmOnTheAdvancedSearchPage(): Navigates to eBay and clicks "Advanced" search.
  • iClickOnTheEbayLogo(): Clicks the eBay logo to go back to homepage.
  • iAmNavigatedToTheEbayHomepageAgain(): Checks if the URL is the homepage URL.
  • iAmOnTheEbayHomepage(): Opens the eBay homepage.
  • iSearchForIphone11(): Performs a search for "iphone11."
  • iValidateAtLeast1100SearchItemsArePresent(): Checks if the search results show at least 1100 items.
  • Implementation details: Uses Selenium WebDriver to interact with the web page elements like links, search boxes, and buttons. Validates conditions using assertions (fail() if not met).


===============================