Session 48 - Selenium with Java | Hybrid Automation Framework Project | POM | 2024 New Series

3 min read 1 hour ago
Published on Nov 05, 2024 This response is partially generated with the help of AI. It may contain inaccuracies.

Table of Contents

Introduction

This tutorial provides a step-by-step guide on building a Hybrid Automation Framework using Selenium with Java, specifically focusing on the Page Object Model (POM) design pattern. This framework is essential for automating web applications effectively and efficiently. By following this guide, you'll learn how to set up your environment, implement the POM structure, and run automated tests.

Step 1: Set Up Your Development Environment

To begin, you need to prepare your environment for Selenium automation.

  1. Install Java Development Kit (JDK)

    • Download the latest version from the official Oracle website.
    • Follow the installation instructions for your operating system.
  2. Install an Integrated Development Environment (IDE)

    • Use Eclipse or IntelliJ IDEA for Java development.
    • Download and install your chosen IDE.
  3. Set Up Maven

    • Maven is a build automation tool used for Java projects.
    • Create a new Maven project in your IDE.
  4. Add Selenium Dependencies

    • Open the pom.xml file in your Maven project.
    • Add the following dependencies:
      <dependency>
          <groupId>org.seleniumhq.selenium</groupId>
          <artifactId>selenium-java</artifactId>
          <version>4.x.x</version>
      </dependency>
      <dependency>
          <groupId>org.testng</groupId>
          <artifactId>testng</artifactId>
          <version>7.x.x</version>
      </dependency>
      
    • Replace 4.x.x and 7.x.x with the latest versions.

Step 2: Implement the Page Object Model

The Page Object Model is a design pattern that enhances test maintenance and reduces code duplication.

  1. Create Base Page Class

    • Create a class named BasePage that contains common methods for all page objects.
    public class BasePage {
        protected WebDriver driver;
    
        public BasePage(WebDriver driver) {
            this.driver = driver;
        }
    
        public void clickElement(By locator) {
            driver.findElement(locator).click();
        }
    
        public void enterText(By locator, String text) {
            driver.findElement(locator).sendKeys(text);
        }
    }
    
  2. Create Specific Page Classes

    • For each page in your application, create a corresponding page class that extends BasePage.
    • Example for a login page:
    public class LoginPage extends BasePage {
        private By usernameField = By.id("username");
        private By passwordField = By.id("password");
        private By loginButton = By.id("login");
    
        public LoginPage(WebDriver driver) {
            super(driver);
        }
    
        public void login(String username, String password) {
            enterText(usernameField, username);
            enterText(passwordField, password);
            clickElement(loginButton);
        }
    }
    
  3. Set Up Test Classes Using TestNG

    • Create a test class that utilizes the page objects.
    public class LoginTest {
        private WebDriver driver;
        private LoginPage loginPage;
    
        @BeforeClass
        public void setup() {
            driver = new ChromeDriver();
            loginPage = new LoginPage(driver);
        }
    
        @Test
        public void testLogin() {
            driver.get("http://example.com/login");
            loginPage.login("user", "pass");
            // Add assertions to verify successful login
        }
    
        @AfterClass
        public void tearDown() {
            driver.quit();
        }
    }
    

Step 3: Run Your Tests

Once your framework is set up, you can run your tests using TestNG.

  1. Execute Tests in IDE

    • Right-click on your test class and select "Run As" > "TestNG Test".
  2. Check Test Results

    • Review the results in the TestNG results panel to see if the tests passed or failed.
  3. Integrate with CI/CD Tools

    • To automate your tests, consider integrating with Jenkins or GitHub Actions.

Conclusion

You've successfully set up a Hybrid Automation Framework using Selenium with Java and the Page Object Model. This structured approach not only improves the maintainability of your test code but also enhances readability. Next steps may include exploring advanced features of Selenium, optimizing your framework, or integrating additional tools like Docker for containerization. Happy testing!