Common Page Actions

In a Cucumber Selenium Framework, especially one using the Page Object Model (POM) design pattern, common action pages refer to Java classes that encapsulate reusable user actions or UI interactions (like click(), enterText(), selectDropdown()), abstracted across multiple pages.

The Page Object Model (POM) is a design pattern in test automation that promotes the separation of test logic from the user interface (UI) elements. In essence, each web page in the application is represented as a class, encapsulating the elements and actions associated with that page. This structure allows for a more organized and modular approach to writing tests.

The main purpose of POM is to improve maintainability and readability of test scripts. By centralizing the UI interactions within page classes, any changes to the UI only require updates in one place, rather than in every test case. This significantly reduces the risk of errors and the time spent on maintenance. For Jamie, adopting POM means her test suite will be easier to manage, more scalable, and less prone to breaking with UI changes.

Reusability ---->Reduces code duplication for repeated actions across test cases
Abstraction---->Separates logic for UI interactions from step definitions
Maintainability----> Easy to update locators or logic in one place
Clean Framework---->Keeps Step Definition files readable and business-focused


[Cucumber Runner]
               |
               v
       Reads Feature File: WebElements Check
               |
               v
   --------------------------------------------
     |  @Before Hook (HookSteps.java)  |
   |  - Launch Chrome                          |
     |  - Maximize window                       |
   --------------------------------------------
               |
               v
    Executes Scenario: "Item Counting"
               |
               v
   [Step: Given I am in Ebay Shopping Homepage]
               |
               v
     --> Launches eBay Homepage (driver.get)
               |
               v
   [Step: When I am searching for 'iphone13']
               |
               v
       --> WebElementActions.searchForItem()
           - Enters text into search field
           - Clicks on search button
               |
               v
 [Step: Then I am validating the itemcount atleast 1500]
               |
               v
 --> WebElementActions.getItemCount()
     - Reads item count text
     - Parses count to integer
     - Compares count with expected value
     - Pass or fail the test
               |
               v
   --------------------------------------------
    |  @After Hook (HookSteps.java)  |
    |      - Quit browser                            |
     |  - Print teardown messages          |
   --------------------------------------------
                   |
                   v
               [Test Ends]
===================================

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


  • @Wb1 is a tag used to filter this scenario for execution.
  • The scenario will open eBay, search for "iphone13", and verify that the result count is at least 1500.



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

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);

}

}


Defines the elements (fields/buttons) on the eBay homepage using Page Factory. @FindBy annotations are used to identify UI elements. PageFactory.initElements() binds the WebElements to actual DOM elements when the class is initialized.


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

package Actions;


import org.openqa.selenium.WebDriver;

import org.openqa.selenium.WebElement;


public class BaseActions {


protected WebDriver driver;


public BaseActions(WebDriver driver) {

this.driver = driver;

}


public void navigateTo(String url) {

driver.get(url);

}


public void enterText(WebElement element, String text) {

element.clear();

element.sendKeys(text);

}


public void clickElement(WebElement element) {

element.click();

}


public String getElementText(WebElement element) {

return element.getText().trim();

}


public int convertToInt(String str) {

return Integer.parseInt(str.replaceAll(",", "").trim());

}

}


Provides generic reusable actions to:
Navigate to a URL
Enter text
Click elements
Extract element text
Convert string with commas (e.g., "1,500") to integer

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

package Actions;


import org.openqa.selenium.WebDriver;


import Steps.CommonSteps;


import WebElements.WebElementElements;


public class WebElementActions extends BaseActions {


WebDriver driver;

WebElementElements webElements;


public WebElementActions(CommonSteps commonsteps) {

super(this.driver = commonsteps.getDriver());

this.webElements = new WebElementElements(driver);

}


public void searchForItem(String searchQuery) {

enterText(webElements.searchAnitem,searchQuery);

clickElement(webElements.searchButton);

}


public int getItemCount() {

String strCount = webElements.itemCountText.getText().trim();

return Integer.parseInt(strCount.replace(",", ""));

}

}


Extends BaseActions to reuse methods like enterText() and clickElement(). Uses WebElementElements to interact with the actual web elements.

Defines business actions: searchForItem("iphone13") types and searches. getItemCount() reads the count from result text and parses it into an integer.


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


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(CommonSteps hook_steps) {

this.driver = hook_steps.getDriver();

this.webActions = new WebElementActions(hook_steps);

}


@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);

}

}

}


Maps Gherkin steps to Java code using @Given, @When, @Then. Uses HookSteps to get WebDriver instance. Calls WebElementActions to perform UI actions and verifications.


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

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 CommonSteps {


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;

}

}