Page Factory (WebElement Repository)(Example)

 project-root/
├── Features/
│   └── WebElementValidation.feature
├── Steps/
│   ├── WebElementSteps.java
│   └── HookSteps.java
├── Actions/
│   └── WebElementActions.java
├── WebElements/
│   └── WebElementElements.java
├── testRunner/
│   └── ParameterRunner.java


Explanation & Key Points



Page Factory-------------- Centralized locator definitions
Actions ------------Business logic methods like searchForItem()
Steps---------------Glue code connecting Gherkin to actions
Hooks---------------Setup & teardown using @Before/@After
======================================

Gherkin Scenario ➝ Step Definitions ➝ Web Actions ➝ Page Factory (WebElements) ➝ WebDriver (via Hook)

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



Feature: WebElements Check


@Wb1

Scenario: Item Counting

Given WebElement I am in Ebay Shopping Homepage

When WebElement I am seaching for 'iphone13'

Then WebElement I am validating the itemcount atleast 1500



Cucumber starts executing each step in order:
Given → When → Then


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


package Steps;


import static org.testng.Assert.fail;


import org.openqa.selenium.WebDriver;


import Actions.WebElementActions;

import io.cucumber.java.en.Given;

import io.cucumber.java.en.Then;

import io.cucumber.java.en.When;


public class WebElementSteps {


WebDriver driver;

WebElementActions webActions;


public WebElementSteps(HookSteps hook_steps) {

this.driver = hook_steps.getDriver();

this.webActions = new WebElementActions(hook_steps);

}


//The constructor receives the shared WebDriver from HookSteps.

//Calls the WebElementActions class to perform test actions.



@Given("WebElement I am in Ebay Shopping Homepage")

public void i_am_in_ebay_shopping_homepage() throws InterruptedException {

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

Thread.sleep(2000);

}


@When("WebElement I am seaching for {string}")

public void i_am_seaching_for(String product) {

webActions.searchForItem(product);

}


@Then("WebElement I am validating the itemcount atleast {int}")

public void i_am_validating_the_itemcount_atleast(Integer minCount) {

int actualCount = webActions.getItemCount();

if (actualCount < minCount) {

fail("Item count is less than expected. Found: " + actualCount + ", Expected: " + minCount);

}

}

}


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

package Actions;
import org.openqa.selenium.WebDriver;
import Steps.HookSteps;
import WebElements.WebElementElements;

public class WebElementActions {
WebDriver driver;
WebElementElements webElements;

public WebElementActions(HookSteps hook_steps) {
this.driver = hook_steps.getDriver();
this.webElements = new WebElementElements(driver);
}

public void searchForItem(String searchQuery) {
webElements.searchAnitem.sendKeys(searchQuery);
webElements.searchButton.click();
}

public int getItemCount() {
String strCount = webElements.itemCountText.getText().trim();
return Integer.parseInt(strCount.replace(",", ""));
}
}


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

package WebElements;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;

public class WebElementElements {
WebDriver driver;

@FindBy(id = "gh-ac")
public WebElement searchAnitem;

@FindBy(className = "gh-search-button__label") // Corrected from className to id for search button
public WebElement searchButton;

@FindBy(css = "div[class='srp-controls__control srp-controls__count'] span:nth-child(1)")
public WebElement itemCountText;

public WebElementElements(WebDriver driver) {
this.driver = driver;
PageFactory.initElements(driver, this);
}
}

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


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(order =0)

public void setup() {

driver = new ChromeDriver();

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

System.out.println("global BEFORE got executed");

}

@Before(value="@setcookies", order =1)

public void setcookies() {

System.out.println("Scenario specific hook - setcookies got executed");

}

@After(order =1)

public void teardown() throws InterruptedException {

driver.quit();

System.out.println("global AFTER got executed");

Thread.sleep(2000);

}


@After(value="@test", order =0)

public void afterHook() throws InterruptedException {

System.out.println("Scenario specific hook - test got executed");

}

public WebDriver getDriver() {

return driver;

}

}



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

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 = "@Wb1",

monochrome = true,

plugin = {"pretty", "html:target/cucumber-report.html"}

)

public class ParameterRunner {

}


features points to your .feature file (i.e., test cases). glue tells Cucumber where to find step definitions (in Steps package). @Wb1 is a tag used to filter which scenario to run.


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


Explanation WebElementElements code WebDriver driver; Declares a WebDriver instance to control the browser. @FindBy(id = "gh-ac") Finds the search input box on the webpage using its HTML id attribute "gh-ac". The corresponding element is stored in searchAnitem. @FindBy(className = "gh-search-button__label") Finds the search button element using its CSS class name. This element is stored in searchButton. @FindBy(css = "div[class='srp-controls__control srp-controls__count'] span:nth-child(1)") Finds a specific span element within a div, identified using a CSS selector. This element displays the count of items found and is stored in itemCountText. Constructor public WebElementElements(WebDriver driver) Takes a WebDriver object as a parameter. Initializes the driver for this class. Calls PageFactory.initElements(driver, this); to bind the WebElement fields to their actual elements on the web page. ====================== Explanation WebElementActions code WebDriver driver;: The browser driver used to control the web browser. WebElementElements webElements;: An object that likely holds references to specific web elements on the page (such as search box, button, item count text). Constructor: public WebElementActions(HookSteps hook_steps): Initializes an instance by: Obtaining the WebDriver instance from hook_steps. Creating an instance of WebElementElements, passing the driver to it, so it can locate web elements. Methods: searchForItem(String searchQuery): Performs a search operation by: Entering the search query into the search input field (searchAnitem). Clicking the search button (searchButton). getItemCount(): Retrieves the number of items displayed on the page by: Getting the text from a web element (itemCountText). Removing any whitespace and commas from the text. Converting the cleaned string to an integer, and returning it. ====================== WebElementSteps Code WebDriver driver; — Manages the web browser session. WebElementActions webActions; — Encapsulates actions performed on web elements, like searching or getting item counts. Constructor: Accepts an object HookSteps, which provides the WebDriver instance. Initializes webActions using the same HookSteps object. Methods: i_am_in_ebay_shopping_homepage(): Opens the eBay homepage (https://www.ebay.com/). Waits for 2 seconds to ensure the page loads completely. i_am_seaching_for(String product): Uses webActions to search for a product on eBay.

i_am_validating_the_itemcount_atleast(Integer minCount): Retrieves the number of search results. Checks if the count is at least a specified minimum. If not, causes the test to fail with a message indicating the actual and expected counts.