Reusable Methods in Java for Selenium Tests
Creating reusable methods in Java for Selenium tests improves code maintainability, readability, and reduces duplication. These methods typically cover common actions such as clicking elements, sending input, waiting for conditions, handling alerts, or capturing screenshots.
Here's a list of commonly used reusable methods in Java for Selenium automation:
๐ Common Reusable Selenium Methods in Java
java
Copy
Edit
import org.openqa.selenium.*;
import org.openqa.selenium.support.ui.*;
import java.time.Duration;
public class SeleniumReusableMethods {
WebDriver driver;
public SeleniumReusableMethods(WebDriver driver) {
this.driver = driver;
}
// Click Element
public void clickElement(By locator) {
waitForElementToBeClickable(locator).click();
}
// Send Keys to Element
public void sendKeysToElement(By locator, String text) {
WebElement element = waitForElementToBeVisible(locator);
element.clear();
element.sendKeys(text);
}
// Wait for Element to be Visible
public WebElement waitForElementToBeVisible(By locator) {
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
return wait.until(ExpectedConditions.visibilityOfElementLocated(locator));
}
// Wait for Element to be Clickable
public WebElement waitForElementToBeClickable(By locator) {
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
return wait.until(ExpectedConditions.elementToBeClickable(locator));
}
// Check if Element is Displayed
public boolean isElementDisplayed(By locator) {
try {
return driver.findElement(locator).isDisplayed();
} catch (NoSuchElementException e) {
return false;
}
}
// Select from Dropdown by Visible Text
public void selectFromDropdown(By locator, String visibleText) {
WebElement dropdownElement = waitForElementToBeVisible(locator);
Select dropdown = new Select(dropdownElement);
dropdown.selectByVisibleText(visibleText);
}
// Get Text from Element
public String getElementText(By locator) {
return waitForElementToBeVisible(locator).getText();
}
// Accept Alert
public void acceptAlert() {
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(5));
wait.until(ExpectedConditions.alertIsPresent()).accept();
}
// Capture Screenshot
public void takeScreenshot(String filePath) {
TakesScreenshot ts = (TakesScreenshot) driver;
File srcFile = ts.getScreenshotAs(OutputType.FILE);
File destFile = new File(filePath);
try {
Files.copy(srcFile.toPath(), destFile.toPath());
} catch (IOException e) {
e.printStackTrace();
}
}
// Scroll to Element
public void scrollToElement(By locator) {
WebElement element = waitForElementToBeVisible(locator);
((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);", element);
}
// Custom Wait (Thread.sleep wrapper)
public void waitInSeconds(int seconds) {
try {
Thread.sleep(seconds * 1000L);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
}
✅ Benefits of Using Reusable Methods
Cleaner test cases – Test scripts are easier to read and maintain.
Centralized updates – Fixes and improvements are made in one place.
Reduced duplication – Avoids repeated blocks of code across tests.
Learn Selenium JAVA Course in Hyderabad
Read More
How to Use Java Streams in Selenium Automation
Reading Data from Properties Files in Java
Writing Your First Selenium Test Case Using Java
Creating Utility Classes for Common Selenium Functions
Visit Our IHUB Talent Training in Hyderabad
Comments
Post a Comment