All Downloads are FREE. Search and download functionalities are using the official Maven repository.

web.WebElementHelper Maven / Gradle / Ivy

There is a newer version: 1.3.12
Show newest version
package web;

import core.internal.EnvironmentInterface;
import org.openqa.selenium.*;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.FluentWait;

import java.time.Duration;
import java.util.concurrent.TimeUnit;
import java.util.function.Function;

/**
 * Created by Ismail on 6/2/2018.
 */
public class WebElementHelper {

    /*************** Class Variables Section ***************/
    // This variable to save old WebElement border color
    private static String elementOldBorderColor = "white";
    // This variable to save old WebElement border width
    private static String elementOldBorderWidth = "1px";
    // This variable to save old WebElement border style
    private static String elementOldBorderStyle = "solid";

    /*************** Class Methods Section ***************/
    // This method to color WebElement with red border to set focus when start action
    protected static void coloringWebElement(WebDriver webDriver, WebElement webElement) {
        try {
            // Save current WebElement border style
            elementOldBorderColor = webElement.getCssValue("border-bottom-color");
            elementOldBorderWidth = webElement.getCssValue("border-width");
            elementOldBorderStyle = webElement.getCssValue("border-style");

            // Highlight the WebElement with red border
            ((JavascriptExecutor) webDriver).executeScript("arguments[0].setAttribute('style','border: solid 2px red');", webElement);
        } catch (Throwable throwable) {
            // Do nothing
        }
    }

    // This method to remove WebElement color when applied while starting action after finish the action
    protected static void removeColoringWebElement(WebDriver webDriver, WebElement webElement) {
        try {
            // Check if element isn't null
            if (webElement != null && webDriver != null)
                // Remove red border color
                ((JavascriptExecutor) webDriver).executeScript("arguments[0].setAttribute('style','border: " + elementOldBorderWidth + " " + elementOldBorderStyle + " " + elementOldBorderColor + "');", webElement);
        } catch (Throwable throwable) {
            // Do nothing
        }
    }

    // This method is generic fluentWait that accepts ExpectedConditions as parameter and return condition results
    public static Object fluentWait(WebDriver webDriver, Function expectedConditions) {
        return new FluentWait<>(webDriver).withTimeout(Duration.ofMillis(EnvironmentInterface.EXPLICIT_WAIT_TIMEOUT)).pollingEvery(Duration.ofMillis(50)).ignoring(StaleElementReferenceException.class, NoSuchElementException.class).until(expectedConditions);
    }

    // This method is a fluent wait that returns WebElement object
    protected static WebElement fluentWait(WebDriver webDriver, By by) {
        return (WebElement) fluentWait(webDriver, ExpectedConditions.visibilityOfElementLocated(by));
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy