aquality.selenium.core.elements.DefaultElementStateProvider Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of aquality-selenium-core Show documentation
Show all versions of aquality-selenium-core Show documentation
Library with core functions simplifying work with Selenium-controlled applications.
package aquality.selenium.core.elements;
import aquality.selenium.core.elements.interfaces.IElementFinder;
import aquality.selenium.core.elements.interfaces.ILogElementState;
import aquality.selenium.core.waitings.IConditionalWait;
import org.openqa.selenium.By;
import java.time.Duration;
import java.util.function.BooleanSupplier;
/**
* DefaultElementStateProvider class.
*
* @author lenovo
* @version $Id: $Id
*/
public class DefaultElementStateProvider extends ElementStateProvider {
private final By locator;
private final IConditionalWait conditionalWait;
private final IElementFinder elementFinder;
/**
* Constructor for DefaultElementStateProvider.
*
* @param locator a {@link org.openqa.selenium.By} object.
* @param conditionalWait a {@link aquality.selenium.core.waitings.IConditionalWait} object.
* @param elementFinder a {@link aquality.selenium.core.elements.interfaces.IElementFinder} object.
* @param logger a {@link aquality.selenium.core.elements.interfaces.ILogElementState} object.
*/
public DefaultElementStateProvider(By locator, IConditionalWait conditionalWait, IElementFinder elementFinder,
ILogElementState logger) {
super(logger);
this.locator = locator;
this.conditionalWait = conditionalWait;
this.elementFinder = elementFinder;
}
/** {@inheritDoc} */
@Override
public boolean isClickable() {
return waitForIsClickable(Duration.ZERO, true);
}
/** {@inheritDoc} */
@Override
public void waitForClickable(Duration timeout) {
try {
waitForIsClickable(timeout, false);
} catch (Exception e) {
logElementState("loc.wait.for.state.failed", elementClickable().getStateName());
throw e;
}
}
private boolean waitForIsClickable(Duration timeout, boolean catchTimeoutException) {
DesiredState desiredState = elementClickable();
desiredState = catchTimeoutException ? desiredState.withCatchingTimeoutException() : desiredState;
return isElementInDesiredCondition(desiredState, timeout);
}
private boolean isElementInDesiredCondition(DesiredState elementStateCondition, Duration timeout) {
return doAndLogWaitForState(
() -> !elementFinder.findElements(locator, elementStateCondition, timeout).isEmpty(),
elementStateCondition.getStateName(),
timeout);
}
/** {@inheritDoc} */
@Override
public boolean isDisplayed() {
return waitForDisplayed(Duration.ZERO);
}
/** {@inheritDoc} */
@Override
public boolean waitForDisplayed(Duration timeout) {
return doAndLogWaitForState(
() -> isAnyElementFound(timeout, ElementState.DISPLAYED),
"displayed",
timeout);
}
private boolean isAnyElementFound(Duration timeout, ElementState state) {
return !elementFinder.findElements(locator, state, timeout).isEmpty();
}
/** {@inheritDoc} */
@Override
public boolean waitForNotDisplayed(Duration timeout) {
return doAndLogWaitForState(
() -> conditionalWait.waitFor(() -> !isDisplayed(), timeout),
"not.displayed",
timeout);
}
/** {@inheritDoc} */
@Override
public boolean isExist() {
return waitForExist(Duration.ZERO);
}
/** {@inheritDoc} */
@Override
public boolean waitForExist(Duration timeout) {
return doAndLogWaitForState(
() -> isAnyElementFound(timeout, ElementState.EXISTS_IN_ANY_STATE),
"exist",
timeout);
}
/** {@inheritDoc} */
@Override
public boolean waitForNotExist(Duration timeout) {
return doAndLogWaitForState(
() -> conditionalWait.waitFor(() -> !isExist(), timeout),
"not.exist",
timeout);
}
/** {@inheritDoc} */
@Override
public boolean isEnabled() {
return waitForEnabled(Duration.ZERO);
}
/** {@inheritDoc} */
@Override
public boolean waitForEnabled(Duration timeout) {
return isElementInDesiredCondition(elementEnabled(), timeout);
}
/** {@inheritDoc} */
@Override
public boolean waitForNotEnabled(Duration timeout) {
return isElementInDesiredCondition(elementNotEnabled(), timeout);
}
private boolean doAndLogWaitForState(BooleanSupplier waitingAction, String conditionKeyPart, Duration timeout)
{
if (Duration.ZERO.equals(timeout))
{
return waitingAction.getAsBoolean();
}
logElementState("loc.wait.for.state", conditionKeyPart);
boolean result = waitingAction.getAsBoolean();
if (!result)
{
logElementState("loc.wait.for.state.failed", conditionKeyPart);
}
return result;
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy