top of page
Search

How to create a Selenium Framework using POM in JAVA

  • Writer: Dev Raj Sinha
    Dev Raj Sinha
  • Nov 3, 2023
  • 2 min read

Building an advanced Selenium framework involves several components and best practices to ensure reusability, maintainability, and scalability of automated tests. Here's a step-by-step guide to creating an advanced Selenium framework using Java, TestNG, Maven, and Page Object Model (POM):


Prerequisites:

1. Java Development Kit (JDK): Make sure you have JDK installed on your system.

2. Integrated Development Environment (IDE): You can use IntelliJ IDEA, Eclipse, or any other IDE of your choice.

3. Build Tool: Maven is used in this example for dependency management and build automation.


Step 1: Set Up a New Maven Project

Create a new Maven project in your IDE or using the command line:



mvn archetype:generate -DgroupId=com.example -DartifactId=selenium-framework -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

Step 2: Add Dependencies to `pom.xml`

Add Selenium WebDriver, TestNG, and other dependencies to your `pom.xml` file:


<dependencies>
    <!-- Selenium WebDriver -->
    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-java</artifactId>
        <version>3.141.59</version>
    </dependency>
    <!-- TestNG -->
    <dependency>
        <groupId>org.testng</groupId>
        <artifactId>testng</artifactId>
        <version>7.1.0</version>
    </dependency>
    <!-- Add other dependencies if needed -->
</dependencies>

Step 3: Create Base Test Class

Create a `BaseTest` class that initializes the WebDriver, sets up common configurations, and handles before/after test methods.


import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;

public class BaseTest {
    protected WebDriver driver;

    @BeforeMethod
    public void setUp() {
        System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");
        driver = new ChromeDriver();
    }

    @AfterMethod
    public void tearDown() {
        if (driver != null) {
            driver.quit();
        }
    }
}

Step 4: Implement Page Object Model (POM)

Create Page Object classes representing the web pages of your application. Each class should contain locators and methods to interact with the page.


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

public class LoginPage {
    private WebDriver driver;

    @FindBy(id = "username")
    private WebElement usernameInput;

    @FindBy(id = "password")
    private WebElement passwordInput;

    @FindBy(id = "loginButton")
    private WebElement loginButton;

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

    public void enterUsername(String username) {
        usernameInput.sendKeys(username);
    }

    public void enterPassword(String password) {
        passwordInput.sendKeys(password);
    }

    public void clickLoginButton() {
        loginButton.click();
    }
}

Step 5: Create Test Classes Using POM

Create test classes that extend the `BaseTest` class and use the Page Object Model to interact with web elements.


import org.testng.annotations.Test;

public class LoginTest extends BaseTest {
    @Test
    public void testLogin() {
        driver.get("https://example.com/login");
        LoginPage loginPage = new LoginPage(driver);
        loginPage.enterUsername("your_username");
        loginPage.enterPassword("your_password");
        loginPage.clickLoginButton();
        // Add assertions and further test steps
    }
}

Step 6: Run Your Tests

You can run your tests using your IDE or by using Maven from the command line:

mvn clean test

This will execute your TestNG tests using the configured WebDriver, and the results will be displayed in the console.


This basic framework can be further enhanced with features such as logging, reporting, configuration management, and parallel execution. Additionally, consider using a version control system like Git to manage your codebase and collaborate with team members effectively.

 
 
 

Recent Posts

See All

Comments


Never Miss a Post. Subscribe Now!

Find new exciting ways to dominate the IT Automation industry

Thanks for submitting!

bottom of page