-
tanish20-coder authoredc05518eb
// Author: kundan.kumar@bluealatair.com
package utils;
import java.time.Duration;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.function.Function;
import org.openqa.selenium.Alert;
import org.openqa.selenium.InvalidSelectorException;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.Keys;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.StaleElementReferenceException;
import org.openqa.selenium.TimeoutException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.FluentWait;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.WebDriverWait;
public class ElementUtils extends myLogs {
private WebDriver driver;
private WebDriverWait wait;
public ElementUtils(WebDriver driver) {
this.driver = driver;
}
// Method to select any dropdown option by their Values:
public void selectFromDropdownByValue(WebElement element, String elementName, String value,
long durationInSeconds) {
try {
WebElement webElement = fluentWaitForElement(element, elementName, durationInSeconds);
Select dropdown = new Select(webElement);
dropdown.selectByValue(value);
logInfo("Selected " + elementName + " with value: " + value);
} catch (Exception e) {
logError("Failed to select " + elementName + " with value: " + value, e);
throw e;
}
}
// Method to Enter any Data into the Fields with Validations:
public void setText(WebElement element, String elementName, String textToBeTyped, long durationInSeconds) {
try {
WebElement webElement = fluentWaitForElement(element, elementName, durationInSeconds);
// Validation 1: Clear the field before entering new text
webElement.clear();
// Validation 2: Ensure the text doesn't exceed maximum length
if (textToBeTyped.length() <= getMaxFieldLength(webElement)) {
// Perform the text entry
webElement.sendKeys(textToBeTyped);
// Validation 3: Verify that the entered text matches the expected text
if (isTextEnteredSuccessfully(webElement, textToBeTyped)) {
logInfo("Entered values in " + elementName + " Field successfully: " + textToBeTyped);
// logInfo("Entered values in element" +elementName+ "successfully: " +
// textToBeTyped);
} else {
logError("Failed to enter values in " + elementName + textToBeTyped + " Verification failed",
null);
}
7172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
} else {
logError("Exceeds maximum field length", null);
}
} catch (Exception e) {
logError("Failed to enter values: " + elementName + textToBeTyped, e);
throw e;
}
}
// Method to check if any text entered matches with the expected text:
private boolean isTextEnteredSuccessfully(WebElement element, String expectedText) {
String actualText = element.getAttribute("value");
return actualText.equals(expectedText);
}
private int getMaxFieldLength(WebElement element) {
// logic to determine the maximum length allowed for the field
// In this, we assume a maximum length of 100 characters
return 100;
}
// Method to select a value from a dropdown by Visible text:
public void selectFromDropdown(WebElement element, String elementName, String optionText, long durationInSeconds) {
try {
WebElement dropdownElement = fluentWaitForElement(element, elementName, durationInSeconds);
Select dropdown = new Select(dropdownElement);
dropdown.selectByVisibleText(optionText);
logInfo("Selected option '" + optionText + "' from " + elementName);
} catch (Exception e) {
logError("Failed to select option from " + elementName, e);
throw e;
}
}
// Method to checkmark on a Checkbox:
public void checkCheckbox(WebElement element, String elementName, long durationInSeconds) {
try {
WebElement checkboxElement = fluentWaitForElement(element, elementName, durationInSeconds);
if (!checkboxElement.isSelected()) {
checkboxElement.click();
logInfo("Checked " + elementName);
} else {
logInfo(elementName + " is already checked.");
}
} catch (Exception e) {
logError("Failed to check " + elementName, e);
throw e;
}
}
// Method to Select any Radio Buttons:
public void selectRadioButton(WebElement element, String elementName, long durationInSeconds) {
try {
WebElement radioButtonElement = fluentWaitForElement(element, elementName, durationInSeconds);
if (!radioButtonElement.isSelected()) {
radioButtonElement.click();
logInfo("Selected " + elementName);
} else {
logInfo(elementName + " is already selected.");
}
} catch (Exception e) {
logError("Failed to select " + elementName, e);
throw e;
}
}
// Method to Click on Element using fluent wait:
public void clickOnElement(WebElement element, String elementName, long durationInSeconds) {
try {
WebElement webElement = fluentWaitForElement(element, elementName, durationInSeconds);