Session 48 - Selenium with Java | Hybrid Automation Framework Project | POM | 2024 New Series
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.
-
Install Java Development Kit (JDK)
- Download the latest version from the official Oracle website.
- Follow the installation instructions for your operating system.
-
Install an Integrated Development Environment (IDE)
- Use Eclipse or IntelliJ IDEA for Java development.
- Download and install your chosen IDE.
-
Set Up Maven
- Maven is a build automation tool used for Java projects.
- Create a new Maven project in your IDE.
-
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
and7.x.x
with the latest versions.
- Open the
Step 2: Implement the Page Object Model
The Page Object Model is a design pattern that enhances test maintenance and reduces code duplication.
-
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); } }
- Create a class named
-
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); } }
- For each page in your application, create a corresponding page class that extends
-
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.
-
Execute Tests in IDE
- Right-click on your test class and select "Run As" > "TestNG Test".
-
Check Test Results
- Review the results in the TestNG results panel to see if the tests passed or failed.
-
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!