openUrl(String url): Method to open a URL in the browser.
Java Code
public void openUrl(String url) {
driver.get(url);
}
clickElement(By locator): Method to click on a web element identified by its locator.
Java Code
public void clickElement(By locator) {
driver.findElement(locator).click();
}
setText(By locator, String text): Method to set text in an input field.
Java Code
public void setText(By locator, String text) {
driver.findElement(locator).sendKeys(text);
}
getText(By locator): Method to get text from a web element.
Java Code
public String getText(By locator) {
return driver.findElement(locator).getText();
}
isElementDisplayed(By locator): Method to check if an element is displayed.
Java Code
public boolean isElementDisplayed(By locator) {
return driver.findElement(locator).isDisplayed();
}
selectDropdownByVisibleText(By locator, String text): Method to select dropdown option by visible text.
Java Code
public void selectDropdownByVisibleText(By locator, String text) {
Select dropdown = new Select(driver.findElement(locator));
dropdown.selectByVisibleText(text);
}
waitForElementVisible(By locator, int timeoutInSeconds): Method to wait for an element to be visible.
Java Code
public void waitForElementVisible(By locator, int timeoutInSeconds) {
WebDriverWait wait = new WebDriverWait(driver, timeoutInSeconds);
wait.until(ExpectedConditions.visibilityOfElementLocated(locator));
}
waitForElementClickable(By locator, int timeoutInSeconds): Method to wait for an element to be clickable.
Java Code
public void waitForElementClickable(By locator, int timeoutInSeconds) {
WebDriverWait wait = new WebDriverWait(driver, timeoutInSeconds);
wait.until(ExpectedConditions.elementToBeClickable(locator));
}
takeScreenshot(String fileName): Method to take a screenshot.
Java Code
public void takeScreenshot(String fileName) {
File screenshotFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
try {
FileUtils.copyFile(screenshotFile, new File(fileName));
} catch (IOException e) {
e.printStackTrace();
}
}
switchToFrame(By frameLocator): Method to switch to a frame.
Java Code
public void switchToFrame(By frameLocator) {
driver.switchTo().frame(driver.findElement(frameLocator));
}
switchToDefaultContent(): Method to switch back to the default content.
Java Code
public void switchToDefaultContent() {
driver.switchTo().defaultContent();
}
scrollToElement(By locator): Method to scroll to an element.
Java Code
public void scrollToElement(By locator) {
WebElement element = driver.findElement(locator);
((Java CodescriptExecutor) driver).executeScript(“arguments[0].scrollIntoView(true);”, element);
}
scrollToEndOfPage(): Method to scroll to the end of the page.
Java Code
public void scrollToEndOfPage() {
((Java CodescriptExecutor) driver).executeScript(“window.scrollTo(0, document.body.scrollHeight)”);
}
waitForPageLoad(int timeoutInSeconds): Method to wait for the page to load.
Java Code
public void waitForPageLoad(int timeoutInSeconds) {
WebDriverWait wait = new WebDriverWait(driver, timeoutInSeconds);
wait.until(webDriver -> ((Java CodescriptExecutor) webDriver).executeScript(“return document.readyState”).equals(“complete”));
}
switchToWindow(String windowHandle): Method to switch to a window.
Java Code
public void switchToWindow(String windowHandle) {
driver.switchTo().window(windowHandle);
}
closeCurrentWindow(): Method to close the current window.
Java Code
public void closeCurrentWindow() {
driver.close();
}
switchToAlertAndAccept(): Method to switch to an alert and accept it.
Java Code
public void switchToAlertAndAccept() {
Alert alert = driver.switchTo().alert();
alert.accept();
}
switchToAlertAndDismiss(): Method to switch to an alert and dismiss it.
Java Code
public void switchToAlertAndDismiss() {
Alert alert = driver.switchTo().alert();
alert.dismiss();
}
refreshPage(): Method to refresh the page.
Java Code
public void refreshPage() {
driver.navigate().refresh();
}
navigateBack(): Method to navigate back to the previous page.
Java Code
public void navigateBack() {
driver.navigate().back();
}
navigateForward(): Method to navigate forward to the next page.
Java Code
public void navigateForward() {
driver.navigate().forward();
}
waitForElementToBeInvisible(By locator, int timeoutInSeconds): Method to wait for an element to be invisible.
Java Code
public void waitForElementToBeInvisible(By locator, int timeoutInSeconds) {
WebDriverWait wait = new WebDriverWait(driver, timeoutInSeconds);
wait.until(ExpectedConditions.invisibilityOfElementLocated(locator));
}
hoverOverElement(By locator): Method to hover over an element.
Java Code
public void hoverOverElement(By locator) {
Actions actions = new Actions(driver);
actions.moveToElement(driver.findElement(locator)).perform();
}
uploadFile(By locator, String filePath): Method to upload a file.
Java Code
public void uploadFile(By locator, String filePath) {
driver.findElement(locator).sendKeys(filePath);
}
downloadFile(String fileUrl, String downloadPath): Method to download a file.
Java Code
public void downloadFile(String fileUrl, String downloadPath) {
URL url;
try {
url = new URL(fileUrl);
ReadableByteChannel rbc = Channels.newChannel(url.openStream());
FileOutputStream fos = new FileOutputStream(downloadPath);
fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
fos.close();
rbc.close();
} catch (IOException e) {
e.printStackTrace();
}
}
executeJava CodeScript(String script): Method to execute Java CodeScript.
Java Code
public void executeJava CodeScript(String script) {
((Java CodescriptExecutor) driver).executeScript(script);
}
getListOfElements(By locator): Method to get a list of elements.
Java Code
public List getListOfElements(By locator) {
return driver.findElements(locator);
}
getNumberOfElements(By locator): Method to get the number of elements.
Java Code
public int getNumberOfElements(By locator) {
return driver.findElements(locator).size();
}
navigateToUrl(String url): Method to navigate to a URL.
Java Code
public void navigateToUrl(String url) {
driver.navigate().to(url);
}
waitForTitleToBe(String title, int timeoutInSeconds): Method to wait for the title of the page.
Java Code
public void waitForTitleToBe(String title, int timeoutInSeconds) {
WebDriverWait wait = new WebDriverWait(driver, timeoutInSeconds);
wait.until(ExpectedConditions.titleIs(title));
}
private WebDriver driver;
// Constructor to initialize WebDriver
public SeleniumUtils(WebDriver driver) {
this.driver = driver;
}
// Method to click on an element identified by its XPath
public void clickElementByXPath(String xpath) {
WebElement element = driver.findElement(By.xpath(xpath));
element.click();
}
// Method to enter text into a text field identified by its ID
public void enterTextById(String id, String text) {
WebElement element = driver.findElement(By.id(id));
element.sendKeys(text);
}
// Method to verify if an element is present on the page
public boolean isElementPresent(By by) {
try {
driver.findElement(by);
return true;
} catch (org.openqa.selenium.NoSuchElementException e) {
return false;
}
}
// Method to get text of an element identified by its CSS selector
public String getTextByCssSelector(String cssSelector) {
WebElement element = driver.findElement(By.cssSelector(cssSelector));
return element.getText();
}
// Method to wait for a specified number of seconds
public void waitForSeconds(int seconds) {
try {
Thread.sleep(seconds * 1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
// Method to validate if element text matches expected text
public boolean validateElementText(String locator, String expectedText) {
WebElement element = driver.findElement(By.xpath(locator));
String actualText = element.getText();
return actualText.equals(expectedText);
}
// Method to get the attribute value of an element
public String getAttributeValue(String locator, String attribute) {
WebElement element = driver.findElement(By.xpath(locator));
return element.getAttribute(attribute);
}
// Method to scroll to an element
public void scrollToElement(By by) {
WebElement element = driver.findElement(by);
((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);", element);
}
// Method to highlight an element
public void highlightElement(By by) {
WebElement element = driver.findElement(by);
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("arguments[0].setAttribute('style', 'border: 2px solid red;');", element);
}
// Method to reset element styles
public void resetElementStyles(By by) {
WebElement element = driver.findElement(by);
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("arguments[0].setAttribute('style', 'border: 0px solid red;');", element);
}}