LocalHelper
package utilities;
import java.awt.AWTException;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.datatransfer.StringSelection;
import java.awt.event.KeyEvent;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.time.Duration;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Properties;
import java.util.Random;
import java.util.regex.Pattern;
import org.openqa.selenium.*;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.Wait;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import constants.Constants;
public class LocalHelper {
public static int timeOutInSeconds = 30;
public static int minTime = 10;
public static int avgTime = 30;
protected static final Logger logger = LoggerFactory.getLogger(LocalHelper.class);
protected static List<WebElement> elementList;
public void hoverAndClick(By locator, WebDriver driver) {
try {
WebElement element = driver.findElement(locator);
// Try using Actions class to hover and click
try {
Actions actions = new Actions(driver);
actions.moveToElement(element).click().perform();
System.out.println("Hovered and clicked using Actions class.");
return;
} catch (Exception e) {
System.err.println("Actions hover+click failed: " + e.getMessage());
}
// Fallback: scroll into view and click using JavaScript
try {
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("arguments[0].scrollIntoView(true);", element);
js.executeScript("arguments[0].click();", element);
System.out.println("Hovered and clicked using JavaScript fallback.");
} catch (Exception jsEx) {
System.err.println("JavaScript hover+click failed: " + jsEx.getMessage());
}
} catch (NoSuchElementException e) {
System.err.println("Element not found: " + e.getMessage());
} catch (Exception e) {
System.err.println("Unexpected exception: " + e.getMessage());
}
}
// public String extractTextFromYopMail(String text) {
// if (text == null || text.isEmpty()) {
// return "";
// }
//
// // Look for "Password:" in the string and extract what's after
// int index = text.indexOf("Password:");
// if (index != -1) {
// // Get substring after "Password:"
// String after = text.substring(index + "Password:".length()).trim();
//
// // If "after" contains space, return the first word (in case it's followed by extra text)
// String[] parts = after.split("\s+");
// return parts[0];
// }
//
// return ""; // If "Password:" not found
// }
public static String extractTextFromYopMail(String input) {
// Split the input by ":" and trim to get the email part
if (input != null && input.contains(":")) {
return input.split(":", 2)[1].trim();
}
return ""; // or return null if you prefer
}
public static String extractPassword(String input) {
if (input != null && input.contains(":")) {
return input.split(":", 2)[1].trim();
}
return "";
}
public void clickElement(By element, WebDriver driver){
WebDriverWait wait= new WebDriverWait(driver,Duration.ofSeconds(avgTime));
wait.until(ExpectedConditions.visibilityOfElementLocated(element));
wait.until(ExpectedConditions.presenceOfElementLocated(element));
wait.until(ExpectedConditions.elementToBeClickable(element));
try {
scrollIntoView(element,driver);
driver.findElement(element).click();
}catch (NoSuchElementException e){
logger.error("Error found on click Element.");
logger.error(e.getMessage());
wait.until(ExpectedConditions.visibilityOfElementLocated(element));
wait.until(ExpectedConditions.elementToBeClickable(element));
driver.findElement(element).click();
}catch (ElementClickInterceptedException e){
logger.error("Error found on click Element : ElementClickInterceptedException");
logger.info("Element : ");
logger.info(String.valueOf(element));
jsExecutorClick(element,driver);
}
}
public void clearTextBox(By element, WebDriver driver) {
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
WebElement textBox = wait.until(ExpectedConditions.visibilityOfElementLocated(element));
try {
textBox.clear(); // standard way
logger.info("Text box cleared successfully.");
} catch (Exception e) {
logger.warn("Standard clear failed. Trying with JavaScript.");
// Fallback: use JavaScript if .clear() doesn't work
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("arguments[0].value='';", textBox);
}
}
public void zoomOut(WebDriver driver, int zoomPercent) {
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("document.body.style.zoom='" + zoomPercent + "%'");
}
public void scrollIntoViewAndClick(By element, WebDriver driver) {
try {
WebElement webElement = driver.findElement(element);
JavascriptExecutor js = (JavascriptExecutor) driver;
// Scroll the element into view (centered vertically)
js.executeScript("arguments[0].scrollIntoView({block: 'center'});", webElement);
// Click the element using JavaScript
js.executeScript("arguments[0].click();", webElement);
logger.info("Element clicked successfully using JavaScriptExecutor.");
} catch (Exception e) {
logger.error("Failed to scroll and click the element: " + element.toString());
logger.error(e.getMessage());
}
}
public List<String> getAllText(By element, WebDriver driver) {
WebDriverWait wait= new WebDriverWait(driver,Duration.ofSeconds(timeOutInSeconds));
wait.until(ExpectedConditions.visibilityOfElementLocated(element));
wait.until(ExpectedConditions.presenceOfAllElementsLocatedBy(element));
List<WebElement> elements= driver.findElements(element);
List<String> allTexts=new LinkedList<String>();
for (WebElement webElement : elements) {
allTexts.add(webElement.getText());
}
return allTexts;
}
public void clickParticularValue(By element,String year, WebDriver driver) {
WebDriverWait wait= new WebDriverWait(driver,Duration.ofSeconds(timeOutInSeconds));
wait.until(ExpectedConditions.visibilityOfElementLocated(element));
wait.until(ExpectedConditions.presenceOfAllElementsLocatedBy(element));
List<WebElement> elements= driver.findElements(element);
for (WebElement webElement : elements) {
if(webElement.getText().contains(year)) {
webElement.click();
break;
}
}
}
public void clearElement(By element_by,WebDriver driver){
WebDriverWait wait= new WebDriverWait(driver,Duration.ofSeconds(timeOutInSeconds));
WebElement element = driver.findElement(element_by);
wait.until(ExpectedConditions.visibilityOfElementLocated(element_by));
element.clear();
}
public void clearElementAndSendKeysWithJsExecutor(By element_by,String text,WebDriver driver) {
WebDriverWait wait= new WebDriverWait(driver,Duration.ofSeconds(timeOutInSeconds));
WebElement element = driver.findElement(element_by);
wait.until(ExpectedConditions.visibilityOfElementLocated(element_by));
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("arguments[0].value='';", element);
// Send new values to the textbox
element.sendKeys(text);
}
public boolean verifyElement(By element,WebDriver driver){
try{
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(avgTime));
return driver.findElement(element).isDisplayed();
}catch (NoSuchElementException e){
return false;
}
}
public boolean verifyElement(By element, int Time, WebDriver driver){
try{
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(Time));
return driver.findElement(element).isDisplayed();
}catch (NoSuchElementException e){
return false;
}
}
public void sendKeys(By element_by,String text,WebDriver driver){
WebDriverWait wait= new WebDriverWait(driver,Duration.ofSeconds(timeOutInSeconds));
try {
WebElement element = driver.findElement(element_by);
try {
wait.until(ExpectedConditions.visibilityOfElementLocated(element_by));
} catch (NoSuchElementException e) {
logger.error("No such element exception or Time out exception on sendKeys");
wait.until(ExpectedConditions.visibilityOfElementLocated(element_by));
logger.info("We found the element on second time");
}
try {
wait.until(ExpectedConditions.elementToBeClickable(element_by));
element.click();
} catch (ElementClickInterceptedException e) {
WebDriverWait wait2 = new WebDriverWait(driver,Duration.ofSeconds(minTime));
wait2.until(ExpectedConditions.visibilityOfElementLocated(element_by));
wait2.until(ExpectedConditions.elementToBeClickable(element_by));
scrollIntoView(element_by, driver);
jsExecutorClick(element_by, driver);
}
try {
element.clear();
element.sendKeys(text);
} catch (NoSuchElementException e) {
logger.error("Time out Exception or no such element exception found on ");
logger.info(String.valueOf(element_by));
wait.until(ExpectedConditions.visibilityOfElementLocated(element_by));
element.clear();
element.sendKeys(text);
}
}catch (NoSuchElementException e){
logger.info("Send Keys - No such element exception");
WebElement element = driver.findElement(element_by);
wait.until(ExpectedConditions.visibilityOfElementLocated(element_by));
element.click();
element.clear();
element.sendKeys(text);
}
}
public void simpleSendWithoutClear(By element_by,String text,WebDriver driver) {
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(timeOutInSeconds));
try {
WebElement element = driver.findElement(element_by);
element.sendKeys(text);
} catch (NoSuchElementException e) {
logger.error("No such element exception or Time out exception on sendKeys");
wait.until(ExpectedConditions.visibilityOfElementLocated(element_by));
logger.info("We found the element on second time");
}
}
public void clearUsingJsExecutor(By element_by, WebDriver driver) {
WebElement element = driver.findElement(element_by);
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("arguments[0].value = '';", element);
}
public void sendKeysAndPressEnter(By element_by,String text,WebDriver driver){
WebDriverWait wait= new WebDriverWait(driver,Duration.ofSeconds(timeOutInSeconds));
try {
WebElement element = driver.findElement(element_by);
try {
wait.until(ExpectedConditions.visibilityOfElementLocated(element_by));
} catch (NoSuchElementException e) {
logger.error("No such element exception or Time out exception on sendKeys");
wait.until(ExpectedConditions.visibilityOfElementLocated(element_by));
logger.info("We found the element on second time");
}
try {
wait.until(ExpectedConditions.elementToBeClickable(element_by));
element.click();
} catch (ElementClickInterceptedException e) {
WebDriverWait wait2 = new WebDriverWait(driver,Duration.ofSeconds(minTime));
wait2.until(ExpectedConditions.visibilityOfElementLocated(element_by));
wait2.until(ExpectedConditions.elementToBeClickable(element_by));
scrollIntoView(element_by, driver);
jsExecutorClick(element_by, driver);
}
try {
element.clear();
element.sendKeys(text);
} catch (NoSuchElementException e) {
logger.error("Time out Exception or no such element exception found on ");
logger.info(String.valueOf(element_by));
wait.until(ExpectedConditions.visibilityOfElementLocated(element_by));
element.clear();
element.sendKeys(text+Keys.ENTER);
}
}catch (NoSuchElementException e){
logger.info("Send Keys - No such element exception");
WebElement element = driver.findElement(element_by);
wait.until(ExpectedConditions.visibilityOfElementLocated(element_by));
element.click();
element.clear();
element.sendKeys(text+Keys.ENTER);
}
}
public String getText(By element_by,WebDriver driver) {
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(timeOutInSeconds));
try{
WebElement element = driver.findElement(element_by);
wait.until(ExpectedConditions.visibilityOfElementLocated(element_by));
wait.until(ExpectedConditions.presenceOfElementLocated(element_by));
return element.getText();
}catch(NoSuchElementException e) {
logger.error("Time out exception or No such element exception");
WebElement element = driver.findElement(element_by);
WebDriverWait wait2 = new WebDriverWait(driver, Duration.ofSeconds(avgTime));
wait2.until(ExpectedConditions.visibilityOfElementLocated(element_by));
wait2.until(ExpectedConditions.presenceOfElementLocated(element_by));
return element.getText();
}
}
public String getTextButSplitFirstLine(By element_by,WebDriver driver) {
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(timeOutInSeconds));
try{
WebElement element = driver.findElement(element_by);
wait.until(ExpectedConditions.visibilityOfElementLocated(element_by));
wait.until(ExpectedConditions.presenceOfElementLocated(element_by));
String text = element.getText();
String[] lines = text.split("\\r?\\n", 2);
String firstLine=lines[0];
return firstLine;
}catch(NoSuchElementException e) {
logger.error("Time out exception or No such element exception");
WebElement element = driver.findElement(element_by);
WebDriverWait wait2 = new WebDriverWait(driver, Duration.ofSeconds(avgTime));
wait2.until(ExpectedConditions.visibilityOfElementLocated(element_by));
wait2.until(ExpectedConditions.presenceOfElementLocated(element_by));
return element.getText();
}
}
public String getTextButSplitBySpace(By element_by,WebDriver driver) {
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(timeOutInSeconds));
try{
WebElement element = driver.findElement(element_by);
wait.until(ExpectedConditions.visibilityOfElementLocated(element_by));
wait.until(ExpectedConditions.presenceOfElementLocated(element_by));
String text = element.getText();
String[] firstIndex = text.split(" ");
String firstIndexValue=firstIndex[1];
return firstIndexValue;
}catch(NoSuchElementException e) {
logger.error("Time out exception or No such element exception");
WebElement element = driver.findElement(element_by);
WebDriverWait wait2 = new WebDriverWait(driver, Duration.ofSeconds(avgTime));
wait2.until(ExpectedConditions.visibilityOfElementLocated(element_by));
wait2.until(ExpectedConditions.presenceOfElementLocated(element_by));
return element.getText();
}
}
public String extractEmailFromElement(By locator, WebDriver driver) {
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(timeOutInSeconds));
try {
wait.until(ExpectedConditions.presenceOfElementLocated(locator));
wait.until(ExpectedConditions.visibilityOfElementLocated(locator));
WebElement element = driver.findElement(locator);
String text = element.getText();
logger.info(text);
// Assuming the format is always "Label: value"
if (text.contains(":")) {
return text.split(":")[1].trim(); // Gets the part after the colon and trims whitespace
} else {
logger.warn("Text does not contain ':' to split: " + text);
return null;
}
} catch (NoSuchElementException e) {
logger.error("Element not found: " + locator, e);
return null;
}
}
public String extractPasswordFromElement(By locator, WebDriver driver) {
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(timeOutInSeconds));
try {
wait.until(ExpectedConditions.presenceOfElementLocated(locator));
wait.until(ExpectedConditions.visibilityOfElementLocated(locator));
WebElement element = driver.findElement(locator);
String text = element.getText();
if (text.contains(":")) {
return text.split(":", 2)[1].trim(); // Split only on the first colon and trim
} else {
logger.warn("No colon ':' found in text: " + text);
return null;
}
} catch (NoSuchElementException e) {
logger.error("Element not found: " + locator, e);
return null;
}
}
public String getTextButInteger(By element_by,WebDriver driver) {
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(timeOutInSeconds));
try{
WebElement element = driver.findElement(element_by);
wait.until(ExpectedConditions.visibilityOfElementLocated(element_by));
wait.until(ExpectedConditions.presenceOfElementLocated(element_by));
wait.until(ExpectedConditions.textMatches(element_by,Pattern.compile("\\b([1-9]|[1-9][0-9]+)\\b")));
return element.getText();
}catch(NoSuchElementException u) {
logger.error("Time out exception or No such element exception");
WebElement element = driver.findElement(element_by);
WebDriverWait wait2 = new WebDriverWait(driver, Duration.ofSeconds(avgTime));
wait2.until(ExpectedConditions.visibilityOfElementLocated(element_by));
wait.until(ExpectedConditions.presenceOfElementLocated(element_by));
wait.until(ExpectedConditions.textMatches(element_by,Pattern.compile("\\b([1-9]|[1-9][0-9]+)\\b")));
return element.getText();
}
}
public String getTextButIntegerWithZeroExpected(By element_by,WebDriver driver) {
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(timeOutInSeconds));
try{
WebElement element = driver.findElement(element_by);
wait.until(ExpectedConditions.visibilityOfElementLocated(element_by));
wait.until(ExpectedConditions.presenceOfElementLocated(element_by));
wait.until(ExpectedConditions.textToBe(element_by,"0"));
return element.getText();
}catch(NoSuchElementException u) {
logger.error("Time out exception or No such element exception");
WebElement element = driver.findElement(element_by);
WebDriverWait wait2 = new WebDriverWait(driver, Duration.ofSeconds(avgTime));
wait2.until(ExpectedConditions.visibilityOfElementLocated(element_by));
wait.until(ExpectedConditions.presenceOfElementLocated(element_by));
wait.until(ExpectedConditions.textToBe(element_by,"0"));
return element.getText();
}
}
public void dropDownSelectByText(By element_by, String text, WebDriver driver){
WebElement element = driver.findElement(element_by);
Actions actions = new Actions(driver);
element.click();
actions.sendKeys(text).sendKeys(Keys.chord(Keys.ENTER)).perform();
}
public void checkDropdownValues(By element_by, String dropdownValues, WebDriver driver) {
WebElement element = driver.findElement(element_by);
Select select = new Select(element);
String[] valueArray = dropdownValues.split(",");
List<WebElement> options = select.getOptions();
for (WebElement value : options) {
for (String s : valueArray) {
if (!value.getText().equals(s)) {
logger.info("Value not found in dropdown :");
logger.info(String.valueOf(value));
}
}
}
}
public void waitForPageURL(String url, WebDriver driver){
new WebDriverWait(driver,Duration.ofSeconds(timeOutInSeconds)).until(ExpectedConditions.urlContains(url));
}
public void jsExecutorClick1(By elementBy, WebDriver driver) {
JavascriptExecutor js = (JavascriptExecutor) driver;
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(timeOutInSeconds));
try {
wait.until(ExpectedConditions.presenceOfElementLocated(elementBy));
wait.until(ExpectedConditions.visibilityOfElementLocated(elementBy));
wait.until(ExpectedConditions.elementToBeClickable(elementBy));
WebElement element = driver.findElement(elementBy);
// Ensure it's a real DOM element and not stale
if (element.isDisplayed() && element.isEnabled()) {
js.executeScript("arguments[0].scrollIntoView(true);", element);
js.executeScript("arguments[0].click();", element); // note the semicolon at end
logger.info("Button clicked using JS Executor");
} else {
logger.warn("Element found but not clickable via JS");
}
} catch (StaleElementReferenceException e) {
logger.warn("StaleElementReferenceException caught. Retrying JS click...");
WebElement element = driver.findElement(elementBy);
js.executeScript("arguments[0].scrollIntoView(true);", element);
js.executeScript("arguments[0].click();", element);
logger.info("Retry JS click successful after stale element");
} catch (JavascriptException je) {
logger.error("JavaScript execution failed: " + je.getMessage());
} catch (Exception e) {
logger.error("Unexpected error during jsExecutorClick: " + e.getMessage());
}
}
public void clickBasedOnText(By primaryXPath, By enableButton, WebDriver driver) {
logger.info("inside Click Based on Text function");
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
try {
WebElement primaryElement = wait.until(ExpectedConditions.visibilityOfElementLocated(primaryXPath));
String actualText = primaryElement.getText().trim();
logger.info(actualText);
if (actualText.equalsIgnoreCase("Disable")) {
WebElement alt1 = wait.until(ExpectedConditions.elementToBeClickable(enableButton));
alt1.click();
}
} catch (Exception e) {
System.err.println("Error in clickBasedOnText(): " + e.getMessage());
}
}
public void jsExecutorClick(By element_by,WebDriver driver){
Wait<WebDriver> wait = new WebDriverWait(driver, Duration.ofSeconds(timeOutInSeconds));
wait.until(ExpectedConditions.visibilityOfElementLocated(element_by));
wait.until(ExpectedConditions.elementToBeClickable(element_by));
JavascriptExecutor js = (JavascriptExecutor) driver;
WebElement element=driver.findElement(element_by);
js.executeScript("arguments[0].scrollIntoView(true);", element);
js.executeScript("arguments[0].click()",element);
logger.info("Button clicked using Js Executor");
}
public boolean checkButtonisPresent(By element_by,WebDriver driver) {
jsExecutorHighlight(element_by, driver);
return driver.findElement(element_by).isEnabled();
}
public void jsExecutorHighlight(By element_by,WebDriver driver){
WebDriverWait wait= new WebDriverWait(driver,Duration.ofSeconds(timeOutInSeconds));
wait.until(ExpectedConditions.visibilityOfElementLocated(element_by));
wait.until(ExpectedConditions.presenceOfElementLocated(element_by));
wait.until(ExpectedConditions.elementToBeClickable(element_by));
JavascriptExecutor js = (JavascriptExecutor) driver;
WebElement element=driver.findElement(element_by);
js.executeScript("arguments[0].setAttribute('style', 'border: 4px solid red;');", element);
// logger.info("Button Highlighted using Js Executor");
}
public void uploadUsingRobot(String location,WebDriver driver,By element) throws AWTException, InterruptedException {
StringSelection selection=new StringSelection(location);
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(selection, null);
Robot robot=new Robot();
//robot.keyPress(KeyEvent.VK_ENTER);
// robot.keyRelease(KeyEvent.VK_ENTER);
Thread.sleep(2000);
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);
// WebDriverWait wait= new WebDriverWait(driver,Duration.ofSeconds(timeOutInSeconds));
// wait.until(ExpectedConditions.presenceOfElementLocated(element));
}
public void uploadDirectlyMissingField(By element,By afterUpload,WebDriver driver) throws AWTException {
String location=System.getProperty("user.dir")+"\\portfolio_format.xlsx";
sendKeys(element, location, driver);
jsExecutorHighlight(element, driver);
WebDriverWait wait= new WebDriverWait(driver,Duration.ofSeconds(timeOutInSeconds));
wait.until(ExpectedConditions.presenceOfElementLocated(afterUpload));
}
public void clearUsingRobot() throws AWTException {
Robot robot=new Robot();
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_A);
robot.keyRelease(KeyEvent.VK_A);
robot.keyRelease(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_DELETE);
robot.keyRelease(KeyEvent.VK_DELETE);
}
public void jsExecutorSendValue(By element_by,String Value,WebDriver driver){
WebDriverWait wait= new WebDriverWait(driver,Duration.ofSeconds(timeOutInSeconds));
WebElement element=driver.findElement(element_by);
wait.until(ExpectedConditions.visibilityOfElementLocated(element_by));
element.sendKeys(Value);
}
public void multipleClicks(By element_by, WebDriver driver){
Wait<WebDriver> wait = new WebDriverWait(driver, Duration.ofSeconds(timeOutInSeconds));
wait.until(ExpectedConditions.visibilityOfAllElementsLocatedBy(element_by));
wait.until(ExpectedConditions.elementToBeClickable(element_by));
List<WebElement> element= driver.findElements(element_by);
for(WebElement ClickElement:element){
try {
((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoViewIfNeeded();", ClickElement);
ClickElement.click();
}catch(ElementClickInterceptedException e) {
((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoViewIfNeeded();", ClickElement);
((JavascriptExecutor) driver).executeScript("arguments[0].click()",ClickElement);
}
}
}
public void selectDropdownByVisibleText(By element_by,String Value,WebDriver driver){
WebDriverWait wait= new WebDriverWait(driver,Duration.ofSeconds(timeOutInSeconds));
wait.until(ExpectedConditions.visibilityOfElementLocated(element_by));
WebElement element=driver.findElement(element_by);
Select select = new Select(element);
select.selectByVisibleText(Value);
}
public boolean verifyEnabledElement(By element_by,WebDriver driver) throws TimeoutException{
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(avgTime));
return driver.findElement(element_by).isEnabled();
}
public void pressTabKey(By element_by, WebDriver driver){
WebElement element = driver.findElement(element_by);
element.click();
new Actions(driver).sendKeys(Keys.chord(Keys.TAB)).perform();
}
public void scrollIntoView(By element_by, WebDriver driver) {
WebDriverWait wait= new WebDriverWait(driver,Duration.ofSeconds(timeOutInSeconds));
wait.until(ExpectedConditions.visibilityOfElementLocated(element_by));
WebElement element = driver.findElement(element_by);
((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoViewIfNeeded();", element);
wait.until(ExpectedConditions.elementToBeClickable(element_by));
}
public void scrollInsideTable(By element_by, WebDriver driver) {
WebElement element = driver.findElement(element_by);
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("arguments[0].scrollLeft = arguments[0].scrollWidth", element);
}
public void scrollIntoViewVertical(By element_by, WebDriver driver) {
WebDriverWait wait= new WebDriverWait(driver,Duration.ofSeconds(timeOutInSeconds));
wait.until(ExpectedConditions.visibilityOfElementLocated(element_by));
WebElement element = driver.findElement(element_by);
((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);", element);
wait.until(ExpectedConditions.elementToBeClickable(element_by));
}
public void scrollHorizontallyView(By element_by, WebDriver driver) {
WebDriverWait wait= new WebDriverWait(driver,Duration.ofSeconds(timeOutInSeconds));
wait.until(ExpectedConditions.visibilityOfElementLocated(element_by));
((JavascriptExecutor) driver).executeScript("window.scrollBy(-500,0)");
wait.until(ExpectedConditions.elementToBeClickable(element_by));
}
public String getAttributeValue(By element_by,WebDriver driver) {
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(timeOutInSeconds));
wait.until(ExpectedConditions.visibilityOfElementLocated(element_by));
return driver.findElement(element_by).getAttribute("value");
}
public List<String> getAllAttributeValue(List<WebElement> elements,WebDriver driver,String attribute) {
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(timeOutInSeconds));
wait.until(ExpectedConditions.visibilityOfAllElements(elements));
List<String> allAttributes=new LinkedList<String>();
for (WebElement webElement : elements) {
String attribute2 = webElement.getAttribute(attribute);
allAttributes.add(attribute2);
}
return allAttributes;
}
public void sendTextByCharacter(By element_by, String sendChar, WebDriver driver){
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(timeOutInSeconds));
wait.until(ExpectedConditions.visibilityOfElementLocated(element_by));
for (int i=0;i<=sendChar.length()-1;i++){
StringBuilder sb = new StringBuilder();
driver.findElement(element_by).sendKeys(sb.append(sendChar.charAt(i)).toString());
}
}
public void clickAllElements(By elements,WebDriver driver) throws InterruptedException{
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(timeOutInSeconds));
wait.until(ExpectedConditions.visibilityOfElementLocated(elements));
List<WebElement> findElements = driver.findElements(elements);
for (WebElement webElement : findElements) {
try {
Thread.sleep(2000);
webElement.click();
}catch (StaleElementReferenceException e) {
List<WebElement> findElementsAgain = driver.findElements(elements);
for (WebElement webElementAgain : findElementsAgain) {
try {
webElementAgain.click();
}catch (StaleElementReferenceException qq) {
List<WebElement> findElementsAgainAgain = driver.findElements(elements);
for (WebElement webElementAgainAgain : findElementsAgainAgain) {
try {
webElementAgainAgain.click();
}catch (StaleElementReferenceException q) {
List<WebElement> findElementsAgainAgainAgain = driver.findElements(elements);
for (WebElement webElementAgainAgainAgain : findElementsAgainAgainAgain) {
webElementAgainAgainAgain.click();
}
}
}
}
}
}catch(ElementClickInterceptedException z) {
((JavascriptExecutor) driver).executeScript("arguments[0].click()",webElement);
}
}
Thread.sleep(2000);
}
public WebElement getRandomElement(By element_by, WebDriver driver){
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(timeOutInSeconds));
wait.until(ExpectedConditions.visibilityOfElementLocated(element_by));
elementList = driver.findElements(element_by);
int eleSize = elementList.size();
// logger.info("Element size: "+String.valueOf(eleSize));
if(eleSize==1){
if(elementList.get(0).getText().equalsIgnoreCase("No options")||elementList.get(0).getText().contains("Loading")){
logger.info("No option or loading found in the drop-down");
wait.until(ExpectedConditions.visibilityOfAllElementsLocatedBy(element_by));
elementList = driver.findElements(element_by);
}
logger.info("The dropdown is showing only one value");
return elementList.get(new Random().nextInt(eleSize));
}
logger.info("Get one random element");
return elementList.get(new Random().nextInt(eleSize));
}
public Boolean checkLoader(By element_by, WebDriver driver, String loader_txt) {
try{
WebDriverWait wait = new WebDriverWait(driver,(Duration.ofSeconds(timeOutInSeconds+timeOutInSeconds)));
return wait.until(ExpectedConditions.invisibilityOfElementWithText(element_by,loader_txt));
}catch (NoSuchElementException e){
return true;
}
}
public Boolean checkLoader(By element_by,WebDriver driver){
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(timeOutInSeconds));
try{
return wait.until(ExpectedConditions.invisibilityOfElementLocated(element_by));
}catch (TimeoutException e){
logger.error("The page takes very long time to load");
WebDriverWait wait2 = new WebDriverWait(driver,(Duration.ofSeconds(timeOutInSeconds+timeOutInSeconds)));
return wait2.until(ExpectedConditions.invisibilityOfElementLocated(element_by));
}
catch (NoSuchElementException e){
return true;
}
}
public void get(String url, WebDriver driver){
driver.get(url);
}
public void switchTab(WebDriver driver){
List<String> tabs2 = new ArrayList<String>(driver.getWindowHandles());
driver.switchTo().window(tabs2.get(0));
}
public void closeTab(WebDriver driver){
driver.close();
}
public void hardRefresh(WebDriver driver){
driver.manage().deleteAllCookies();
((JavascriptExecutor) driver).executeScript("location.reload(true);");
logger.info("Cache removed");
driver.navigate().refresh();
}
public String readPropertyFile(String key) throws IOException {
FileInputStream stream=new FileInputStream(new File("./src/test/resources/config.properties"));
Properties properties=new Properties();
properties.load(stream);
return properties.getProperty(key);
}
public void loadProperties(String url) throws IOException {
FileInputStream stream=new FileInputStream(new File("./src/test/resources/config.properties"));
Properties properties=new Properties();
properties.load(stream);
//Constants.App_url=properties.getProperty("appurl1");
Constants.App_url=url;
Constants.Browser=properties.getProperty("browser");
Constants.firstname=properties.getProperty("firstname");
Constants.lastname=properties.getProperty("lastname");
Constants.emailid=properties.getProperty("emailid");
Constants.dob=properties.getProperty("dob");
Constants.postalcode=properties.getProperty("postalcode");
Constants.phonenum=properties.getProperty("phonenum");
Constants.useremailid=properties.getProperty("useremailid");
Constants.commonpassword=properties.getProperty("commonpassword");
Constants.username=properties.getProperty("username");
Constants.yopmail=properties.getProperty("yopmail");
Constants.admin_url=properties.getProperty("admin_url");
Constants.institute_url=properties.getProperty("institute_url");
Constants.institute_user=properties.getProperty("institute_user");
Constants.mailinator=properties.getProperty("mailinator");
Constants.academyAdmin_url=properties.getProperty("academyAdmin_url");
Constants.IapUsername=properties.getProperty("IapUsername");
Constants.stp_url=properties.getProperty("stp_url");
Constants.stpuserpassword=properties.getProperty("stpuserpassword");
Constants.IapUserpassword=properties.getProperty("IapUserpassword");
Constants.adminPassword=properties.getProperty("adminPassword");
}
public void robotZoomout() throws AWTException {
Robot robot=new Robot();
for(int i=0;i<5;i++) {
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_SUBTRACT);
robot.keyRelease(KeyEvent.VK_SUBTRACT);
robot.keyRelease(KeyEvent.VK_CONTROL);
}
}
public void zoomoutUsingJS(WebDriver driver) {
String zoomJS;
JavascriptExecutor js = (JavascriptExecutor) driver;
zoomJS = "document.body.style.zoom='0.70'";
js.executeScript(zoomJS);
}
public void zoomoutUsingChourd(WebDriver driver) {
WebElement html = driver.findElement(By.tagName("html"));
html.sendKeys(Keys.chord(Keys.COMMAND, Keys.SUBTRACT));
html.sendKeys(Keys.chord(Keys.COMMAND, Keys.SUBTRACT));
html.sendKeys(Keys.chord(Keys.COMMAND, Keys.SUBTRACT));
}
public void callRobot() throws AWTException, InterruptedException {
Thread.sleep(2000);
Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_DOWN);
robot.keyRelease(KeyEvent.VK_DOWN);
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);
}
public void callRobotWithTiming08Hours() throws AWTException, InterruptedException {
Thread.sleep(2000);
Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_NUMPAD0);
robot.keyRelease(KeyEvent.VK_NUMPAD0);
robot.keyPress(KeyEvent.VK_NUMPAD8);
robot.keyRelease(KeyEvent.VK_NUMPAD8);
robot.keyPress(KeyEvent.VK_NUMPAD0);
robot.keyRelease(KeyEvent.VK_NUMPAD0);
robot.keyPress(KeyEvent.VK_NUMPAD0);
robot.keyRelease(KeyEvent.VK_NUMPAD0);
}
public void callRobotWithTiming02Hours() throws AWTException, InterruptedException {
Thread.sleep(2000);
Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_NUMPAD0);
robot.keyRelease(KeyEvent.VK_NUMPAD0);
robot.keyPress(KeyEvent.VK_NUMPAD2);
robot.keyRelease(KeyEvent.VK_NUMPAD2);
robot.keyPress(KeyEvent.VK_NUMPAD0);
robot.keyRelease(KeyEvent.VK_NUMPAD0);
robot.keyPress(KeyEvent.VK_NUMPAD0);
robot.keyRelease(KeyEvent.VK_NUMPAD0);
}
public boolean checkFileintheDownload(String fileNameToBeCheck) {
File file=new File("C:\\Users\\ayyappan.g\\Downloads");
File[] listFiles = file.listFiles();
for (File file2 : listFiles) {
if(file2.getName().contains(fileNameToBeCheck)) {
return true;
}
}
return false;
}
public boolean checkElement(By element,WebDriver driver) {
return driver.findElement(element).isDisplayed();
}
public void SendkeysByJsExecutor(By element,String timeValue,WebDriver driver) {
// Wait for the input element to be visible and interactable
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
WebElement timeInput = wait.until(ExpectedConditions.elementToBeClickable(element));
// Use JavaScript to clear the input field
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("arguments[0].value = '';", timeInput);
// Correctly formatted time value
// Use JavaScript to set the value directly
js.executeScript("arguments[0].value = arguments[1];", timeInput, timeValue);
// Trigger necessary events for Material-UI
js.executeScript("arguments[0].dispatchEvent(new Event('input', { bubbles: true }));", timeInput);
js.executeScript("arguments[0].dispatchEvent(new Event('change', { bubbles: true }));", timeInput);
// Optionally verify the value has been set correctly
String setValue = (String) js.executeScript("return arguments[0].value;", timeInput);
System.out.println("Set value: " + setValue);
}
public void clearUsingSendKeys(By element_by, WebDriver driver) {
WebDriverWait wait= new WebDriverWait(driver,Duration.ofSeconds(timeOutInSeconds));
wait.until(ExpectedConditions.visibilityOfElementLocated(element_by));
WebElement element = driver.findElement(element_by);
element.sendKeys(Keys.CONTROL + "a");
element.sendKeys(Keys.DELETE);
}
public void switchToFrame(By closeButtonFrameId, WebDriver driver) {
driver.switchTo().frame(driver.findElement(closeButtonFrameId));
}
public void switchToDefaultContent(WebDriver driver) {
driver.switchTo().defaultContent();
}
public static int getCurrentDay() {
LocalDate currentDate = LocalDate.now();
int currDate = currentDate.getDayOfMonth();
// return String.valueOf(currentDate.getDayOfMonth());
return currDate;
}
public int getAbsoluteDifference(int n1, int n2) {
return Math.abs(n1 - n2);
}
public static void type10WithRobot(By locator,WebDriver driver) throws AWTException, InterruptedException {
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(timeOutInSeconds));
// Wait until element is clickable
WebElement inputField = wait.until(ExpectedConditions.elementToBeClickable(locator));
inputField.click(); // Focus on the input field
Thread.sleep(500); // Wait briefly after click
Robot robot = new Robot();
// Type '1'
robot.keyPress(KeyEvent.VK_1);
robot.keyRelease(KeyEvent.VK_1);
Thread.sleep(100); // Short pause
// Type '0'
robot.keyPress(KeyEvent.VK_0);
robot.keyRelease(KeyEvent.VK_0);
}
public void setTimeAndSave(WebDriver driver) throws Exception {
Actions actions = new Actions(driver);
Robot robot = new Robot();
// Step 1: Double click on Hours
WebElement hoursElement = driver.findElement(By.xpath("//table//span[@aria-label='Hours']"));
actions.moveToElement(hoursElement).doubleClick().perform();
Thread.sleep(500);
logger.info("Double click is performed in the Hours");
// Step 2: Press 5
logger.info("Entering 5 through keyborad");
robot.keyPress(KeyEvent.VK_5);
robot.keyRelease(KeyEvent.VK_5);
Thread.sleep(500);
logger.info("Entered 5 through keyborad");
// Step 3: Focus moves to Minutes automatically; press 5
WebElement minutesElement = driver.findElement(By.xpath("//table//span[@aria-label='Minutes']"));
// minutesElement.click(); // Optional: explicitly focus on minutes
logger.info("Moved to minutes");
Thread.sleep(300);
robot.keyPress(KeyEvent.VK_5);
robot.keyRelease(KeyEvent.VK_5);
Thread.sleep(500);
// Step 4: Double click on Hours again
logger.info("clicking Double click is performed in the Hours");
actions.moveToElement(hoursElement).doubleClick().perform();
Thread.sleep(500);
logger.info("Double click is performed in the Hours");
// Step 5: Press 5 again
logger.info("Entering 5 through keyborad");
robot.keyPress(KeyEvent.VK_5);
robot.keyRelease(KeyEvent.VK_5);
Thread.sleep(1000);
logger.info("Entered 5 through keyborad");
// Step 6: Wait for and click "Save Changes"
WebElement saveButton = driver.findElement(By.xpath("//button[normalize-space()='Save Changes']"));
saveButton.click();
}
}