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

aquality.selenium.core.elements.CachedElementStateProvider Maven / Gradle / Ivy

Go to download

Library with core functions simplifying work with Selenium-controlled applications.

There is a newer version: 4.2.0
Show newest version
package aquality.selenium.core.elements;

import aquality.selenium.core.elements.interfaces.IElementCacheHandler;
import aquality.selenium.core.elements.interfaces.ILogElementState;
import aquality.selenium.core.waitings.IConditionalWait;
import org.openqa.selenium.By;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.StaleElementReferenceException;
import org.openqa.selenium.WebElement;

import java.time.Duration;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.TimeoutException;
import java.util.function.BooleanSupplier;
import java.util.function.Predicate;

/**
 * Provides functions to retrive the state for cached element.
 *
 * @author lenovo
 * @version $Id: $Id
 */
public class CachedElementStateProvider extends ElementStateProvider {

    private final By locator;
    private final IConditionalWait conditionalWait;
    private final IElementCacheHandler elementCacheHandler;

    /**
     * 

Constructor for CachedElementStateProvider.

* * @param locator a {@link org.openqa.selenium.By} object. * @param conditionalWait a {@link aquality.selenium.core.waitings.IConditionalWait} object. * @param elementCacheHandler a {@link aquality.selenium.core.elements.interfaces.IElementCacheHandler} object. * @param logger a {@link aquality.selenium.core.elements.interfaces.ILogElementState} object. */ public CachedElementStateProvider(By locator, IConditionalWait conditionalWait, IElementCacheHandler elementCacheHandler, ILogElementState logger) { super(logger); this.locator = locator; this.conditionalWait = conditionalWait; this.elementCacheHandler = elementCacheHandler; } /** *

getHandledExceptions.

* * @return a {@link java.util.List} object. */ protected List> getHandledExceptions() { return Arrays.asList(StaleElementReferenceException.class, NoSuchElementException.class); } /** *

tryInvokeFunction.

* * @param predicate a {@link java.util.function.Predicate} object. * @return a boolean. */ protected boolean tryInvokeFunction(Predicate predicate) { return tryInvokeFunction(predicate, getHandledExceptions()); } /** *

tryInvokeFunction.

* * @param predicate a {@link java.util.function.Predicate} object. * @param handledExceptions a {@link java.util.List} object. * @return a boolean. */ protected boolean tryInvokeFunction(Predicate predicate, List> handledExceptions) { try { return predicate.test(elementCacheHandler.getElement(Duration.ZERO, ElementState.EXISTS_IN_ANY_STATE)); } catch (Exception exception) { if (handledExceptions.contains(exception.getClass())) { return false; } throw exception; } } /** *

waitForCondition.

* * @param condition a {@link java.util.function.BooleanSupplier} object. * @param conditionKeyPart a {@link java.lang.String} object. * @param timeout a {@link java.time.Duration} object. * @return a boolean. */ protected boolean waitForCondition(BooleanSupplier condition, String conditionKeyPart, Duration timeout) { logElementState("loc.wait.for.state", conditionKeyPart); boolean result = conditionalWait.waitFor(condition, timeout); if (!result) { logElementState("loc.wait.for.state.failed", conditionKeyPart); } return result; } /** {@inheritDoc} */ @Override public boolean isClickable() { return tryInvokeFunction(elementClickable().getElementStateCondition()); } /** {@inheritDoc} */ @Override public void waitForClickable(Duration timeout) { String errorMessage = String.format("Element %1$s has not become clickable after timeout.", locator); String conditionKeyPart = elementClickable().getStateName(); try { logElementState("loc.wait.for.state", conditionKeyPart); conditionalWait.waitForTrue(this::isClickable, timeout, null, errorMessage); } catch (TimeoutException e) { logElementState("loc.wait.for.state.failed", conditionKeyPart); throw new org.openqa.selenium.TimeoutException(e.getMessage(), e); } } /** {@inheritDoc} */ @Override public boolean isDisplayed() { return !elementCacheHandler.isStale() && tryInvokeFunction(WebElement::isDisplayed); } /** {@inheritDoc} */ @Override public boolean waitForDisplayed(Duration timeout) { return waitForCondition(() -> tryInvokeFunction(WebElement::isDisplayed), "displayed", timeout); } /** {@inheritDoc} */ @Override public boolean waitForNotDisplayed(Duration timeout) { return waitForCondition(() -> !isDisplayed(), "not.displayed", timeout); } /** {@inheritDoc} */ @Override public boolean isExist() { return !elementCacheHandler.isStale() && tryInvokeFunction(element -> true); } /** {@inheritDoc} */ @Override public boolean waitForExist(Duration timeout) { return waitForCondition(() -> tryInvokeFunction(element -> true), "exist", timeout); } /** {@inheritDoc} */ @Override public boolean waitForNotExist(Duration timeout) { return waitForCondition(() -> !isExist(), "not.exist", timeout); } /** {@inheritDoc} */ @Override public boolean isEnabled() { return tryInvokeFunction(elementEnabled().getElementStateCondition(), Collections.singletonList(StaleElementReferenceException.class)); } /** {@inheritDoc} */ @Override public boolean waitForEnabled(Duration timeout) { return waitForCondition(this::isEnabled, elementEnabled().getStateName(), timeout); } /** {@inheritDoc} */ @Override public boolean waitForNotEnabled(Duration timeout) { return waitForCondition(() -> !isEnabled(), elementNotEnabled().getStateName(), timeout); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy