Create a Page Object Model (POM) framework using Cucumber, Selenium, and Java

Create a Page Object Model (POM) framework using Cucumber, Selenium, and Java


Creating a Page Object Model (POM) framework using Cucumber, Selenium, and Java is a structured way to organize your test automation code for better maintainability and scalability. Below is a simplified example of a POM framework using Cucumber, Selenium, and Java:


Let's assume you're testing a simple web application with two pages: a login page and a home page.


1. **Create Your Page Classes:**


Create two Java classes representing the login page and the home page. These classes should contain WebElement locators and methods to interact with the elements on those pages.


**LoginPage.java:**


```java

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.WebElement;

import org.openqa.selenium.By;


public class LoginPage {

    private WebDriver driver;


    public LoginPage(WebDriver driver) {

        this.driver = driver;

    }


    private WebElement usernameField() {

        return driver.findElement(By.id("username"));

    }


    private WebElement passwordField() {

        return driver.findElement(By.id("password"));

    }


    private WebElement loginButton() {

        return driver.findElement(By.id("loginBtn"));

    }


    public void enterUsername(String username) {

        usernameField().sendKeys(username);

    }


    public void enterPassword(String password) {

        passwordField().sendKeys(password);

    }


    public void clickLoginButton() {

        loginButton().click();

    }

}

```


**HomePage.java:**


``java

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.WebElement;

import org.openqa.selenium.By;


public class HomePage {

    private WebDriver driver;


    public HomePage(WebDriver driver) {

        this.driver = driver;

    }


    private WebElement welcomeMessage() {

        return driver.findElement(By.id("welcomeMsg"));

    }


    public String getWelcomeMessage() {

        return welcomeMessage().getText();

    }

}

```


2. **Create Your Cucumber Feature File:**


Write a Cucumber feature file that describes your test scenario. Here's an example feature file:


**login.feature:**


```gherkin

Feature: User Login


  Scenario: Valid user login

    Given I am on the login page

    When I enter username "testuser" and password "password123"

    And I click the login button

    Then I should see the welcome message "Welcome, testuser!"

```


3. **Create Step Definitions:**


Create step definition classes that map the Gherkin steps to Java code. These classes will use the Page Object classes to interact with the web elements.


**LoginStepDefinitions.java:**


``java

import io.cucumber.java.en.Given;

import io.cucumber.java.en.When;

import io.cucumber.java.en.Then;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.chrome.ChromeDriver;


public class LoginStepDefinitions {

    private WebDriver driver;

    private LoginPage loginPage;

    private HomePage homePage;


    @Given("I am on the login page")

    public void i_am_on_the_login_page() {

        System.setProperty("webdriver.chrome.driver", "path/to/chromedriver.exe");

        driver = new ChromeDriver();

        driver.get("http://example.com/login");

        loginPage = new LoginPage(driver);

    }


    @When("I enter username {string} and password {string}")

    public void i_enter_username_and_password(String username, String password) {

        loginPage.enterUsername(username);

        loginPage.enterPassword(password);

    }


    @When("I click the login button")

    public void i_click_the_login_button() {

        loginPage.clickLoginButton();

    }


    @Then("I should see the welcome message {string}")

    public void i_should_see_the_welcome_message(String expectedMessage) {

        homePage = new HomePage(driver);

        String actualMessage = homePage.getWelcomeMessage();

        driver.quit();


        // Add assertions here to compare actualMessage with expectedMessage

    }

}

```


4. **Run Your Cucumber Tests:**


You'll need to set up your Cucumber runner class to execute your feature files. You can use a testing framework like JUnit or TestNG for this purpose. Run your tests, and Cucumber will execute the steps and interact with the web application using your Page Object classes.


This is a simplified example, and in a real-world scenario, you'd handle things like driver management, test data, and error handling more robustly. However, this should give you a basic understanding of how to structure a POM-based test automation framework using Cucumber, Selenium, and Java.

Comments

Popular posts from this blog

Log4j2 configuration for Selenium Framework

Building Cucumber BDD Framework from Scratch using Selenium and TestNG