Creating a Selenium Framework from Scratch Using Java and TestNG
Creating a Selenium Framework from Scratch Using Java and TestNG
1. Setup Your Project
a. Create a Maven Project
Use an IDE like IntelliJ IDEA or Eclipse.
Create a new Maven project to manage dependencies easily.
b. Add Dependencies in pom.xml
Add Selenium WebDriver and TestNG dependencies:
xml
Copy
Edit
<dependencies>
<!-- Selenium WebDriver -->
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>4.11.0</version> <!-- Use the latest stable -->
</dependency>
<!-- TestNG -->
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>7.7.1</version>
<scope>test</scope>
</dependency>
<!-- WebDriver Manager to manage browser drivers automatically -->
<dependency>
<groupId>io.github.bonigarcia</groupId>
<artifactId>webdrivermanager</artifactId>
<version>5.5.0</version>
</dependency>
</dependencies>
2. Project Structure
Organize your project like this:
css
Copy
Edit
src
├── main
│ └── java
│ └── com.yourcompany.framework
│ ├── base
│ │ └── BaseTest.java
│ ├── pages
│ │ └── LoginPage.java
│ └── utils
│ └── ConfigReader.java
├── test
└── java
└── com.yourcompany.tests
└── LoginTest.java
3. BaseTest Class
This will initialize and manage the WebDriver lifecycle.
java
Copy
Edit
package com.yourcompany.framework.base;
import io.github.bonigarcia.wdm.WebDriverManager;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.*;
public class BaseTest {
protected WebDriver driver;
@BeforeClass
@Parameters({"browser"})
public void setUp(@Optional("chrome") String browser) {
if (browser.equalsIgnoreCase("chrome")) {
WebDriverManager.chromedriver().setup();
driver = new ChromeDriver();
}
// You can add Firefox, Edge, etc. here
driver.manage().window().maximize();
driver.get("https://example.com"); // You can externalize this URL
}
@AfterClass
public void tearDown() {
if (driver != null) {
driver.quit();
}
}
}
4. Page Object Model (POM) Design
Create a class for each page to encapsulate UI elements and actions.
Example: LoginPage.java
java
Copy
Edit
package com.yourcompany.framework.pages;
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;
// WebElements
@FindBy(id = "username")
private WebElement usernameInput;
@FindBy(id = "password")
private WebElement passwordInput;
@FindBy(id = "loginBtn")
private WebElement loginButton;
// Constructor
public LoginPage(WebDriver driver) {
this.driver = driver;
PageFactory.initElements(driver, this);
}
// Actions
public void enterUsername(String username) {
usernameInput.clear();
usernameInput.sendKeys(username);
}
public void enterPassword(String password) {
passwordInput.clear();
passwordInput.sendKeys(password);
}
public void clickLogin() {
loginButton.click();
}
public void login(String username, String password) {
enterUsername(username);
enterPassword(password);
clickLogin();
}
}
5. Test Class Using TestNG
Create test cases under the test directory.
Example: LoginTest.java
java
Copy
Edit
package com.yourcompany.tests;
import com.yourcompany.framework.base.BaseTest;
import com.yourcompany.framework.pages.LoginPage;
import org.testng.Assert;
import org.testng.annotations.Test;
public class LoginTest extends BaseTest {
@Test
public void validLoginTest() {
LoginPage loginPage = new LoginPage(driver);
loginPage.login("testuser", "testpass");
// Example assertion, replace with actual validation
String expectedUrl = "https://example.com/dashboard";
Assert.assertEquals(driver.getCurrentUrl(), expectedUrl, "User should be redirected to dashboard after login");
}
}
6. Configuration Management
Store configurable values such as URLs, credentials, timeouts, etc., in a properties file.
Example: config.properties
ini
Copy
Edit
baseUrl=https://example.com
username=testuser
password=testpass
Java Class to Read Config:
java
Copy
Edit
package com.yourcompany.framework.utils;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
public class ConfigReader {
private Properties properties = new Properties();
public ConfigReader() {
try {
FileInputStream fis = new FileInputStream("src/main/resources/config.properties");
properties.load(fis);
} catch (IOException e) {
e.printStackTrace();
}
}
public String getProperty(String key) {
return properties.getProperty(key);
}
}
7. Enhancements
Add logging (e.g., Log4j or slf4j)
Implement Waits (Explicit waits with WebDriverWait)
Add Screenshots on test failure
Parameterize tests with TestNG @DataProvider
Integrate with CI tools like Jenkins
Use Page Factory for efficient element initialization (already shown)
8. Running Tests
You can run your tests using:
TestNG XML Suite file (e.g., testng.xml)
IDE test runners
Maven command:
bash
Copy
Edit
mvn clean test
Summary
Set up Maven project and dependencies.
Create a BaseTest to manage WebDriver lifecycle.
Use Page Object Model (POM) to separate UI from test logic.
Write tests with TestNG annotations.
Manage config using properties files.
Add waits, logging, and screenshots for robustness.
Learn Selenium JAVA Training in Hyderabad
Read More
How to Take Screenshots in Selenium Automatically on Failure
Cross-Browser Testing with Selenium WebDriver
Implicit vs Explicit Waits in Selenium – What’s the Difference?
Data-Driven Testing Using Excel Files in Selenium + Java
Visit Our Quality Thought Training in Hyderabad
Comments
Post a Comment