Skip to content
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import org.openqa.selenium.remote.RemoteWebDriver;

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

/**
* Interface of any application controlled by Selenium WebDriver API
Expand All @@ -22,7 +22,6 @@ public interface IApplication {
/**
* Sets implicit wait timeout to Selenium WebDriver.
* @param value timeout value to set.
* @param timeUnit timeUnit of timeout value.
*/
void setImplicitWaitTimeout(long value, TimeUnit timeUnit);
void setImplicitWaitTimeout(Duration value);
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package aquality.selenium.core.configurations;

import java.time.Duration;

/**
* Describes retry configuration.
*/
Expand All @@ -17,5 +19,5 @@ public interface IRetryConfiguration {
*
* @return Polling interval for retry.
*/
long getPollingInterval();
Duration getPollingInterval();
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package aquality.selenium.core.configurations;

import java.time.Duration;

/**
* Provides timeouts configuration.
*/
Expand All @@ -10,26 +12,26 @@ public interface ITimeoutConfiguration {
*
* @return ImplicitWait timeout.
*/
long getImplicit();
Duration getImplicit();

/**
* Gets default ConditionalWait timeout.
*
* @return ConditionalWait timeout.
*/
long getCondition();
Duration getCondition();

/**
* Gets ConditionalWait polling interval.
*
* @return polling interval.
*/
long getPollingInterval();
Duration getPollingInterval();

/**
* Gets Command timeout.
*
* @return Command timeout.
*/
long getCommand();
Duration getCommand();
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@
import aquality.selenium.core.utilities.ISettingsFile;
import com.google.inject.Inject;

import java.time.Duration;

public class RetryConfiguration implements IRetryConfiguration {
private final int number;
private final long pollingInterval;
private final Duration pollingInterval;

@Inject
public RetryConfiguration(ISettingsFile settingsFile) {
this.number = Integer.parseInt(settingsFile.getValue("/retry/number").toString());
this.pollingInterval = Long.parseLong(settingsFile.getValue("/retry/pollingInterval").toString());
this.pollingInterval = Duration.ofMillis(Long.parseLong(settingsFile.getValue("/retry/pollingInterval").toString()));
}

@Override
Expand All @@ -19,7 +21,7 @@ public int getNumber() {
}

@Override
public long getPollingInterval() {
public Duration getPollingInterval() {
return pollingInterval;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,40 +3,46 @@
import aquality.selenium.core.utilities.ISettingsFile;
import com.google.inject.Inject;

public class TimeoutConfiguration implements ITimeoutConfiguration{
import java.time.Duration;

public class TimeoutConfiguration implements ITimeoutConfiguration {

private final ISettingsFile settingsFile;
private final long condition;
private final long pollInterval;
private final long implicit;
private final long command;
private final Duration condition;
private final Duration pollInterval;
private final Duration implicit;
private final Duration command;

@Inject
public TimeoutConfiguration(ISettingsFile settingsFile) {
this.settingsFile = settingsFile;
condition = getTimeout(TIMEOUT.CONDITION);
pollInterval = getTimeout(TIMEOUT.POLL_INTERVAL);
implicit = getTimeout(TIMEOUT.IMPLICIT);
command = getTimeout(TIMEOUT.COMMAND);
condition = getDurationFromSeconds(TIMEOUT.CONDITION);
pollInterval = Duration.ofMillis(getTimeout(TIMEOUT.POLL_INTERVAL));
implicit = getDurationFromSeconds(TIMEOUT.IMPLICIT);
command = getDurationFromSeconds(TIMEOUT.COMMAND);
}

private long getTimeout(TIMEOUT timeout){
private long getTimeout(TIMEOUT timeout) {
return Long.valueOf(settingsFile.getValue(timeout.getKey()).toString());
}

public long getImplicit(){
private Duration getDurationFromSeconds(TIMEOUT timeout) {
return Duration.ofSeconds(getTimeout(timeout));
}

public Duration getImplicit() {
return implicit;
}

public long getCondition(){
public Duration getCondition() {
return condition;
}

public long getPollingInterval(){
public Duration getPollingInterval() {
return pollInterval;
}

public long getCommand(){
public Duration getCommand() {
return command;
}

Expand All @@ -47,11 +53,12 @@ private enum TIMEOUT {
COMMAND("/timeouts/timeoutCommand");

private String key;
TIMEOUT(String key){

TIMEOUT(String key) {
this.key = key;
}

private String getKey(){
private String getKey() {
return key;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
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;
Expand Down Expand Up @@ -42,7 +43,7 @@ protected boolean tryInvokeFunction(Predicate<WebElement> predicate) {

protected boolean tryInvokeFunction(Predicate<WebElement> predicate, List<Class<? extends Exception>> handledExceptions) {
try {
return predicate.test(elementCacheHandler.getElement(getZeroTImeout(), ElementState.EXISTS_IN_ANY_STATE));
return predicate.test(elementCacheHandler.getElement(Duration.ZERO, ElementState.EXISTS_IN_ANY_STATE));
} catch (Exception exception) {
if (handledExceptions.contains(exception.getClass())) {
return false;
Expand All @@ -51,10 +52,10 @@ protected boolean tryInvokeFunction(Predicate<WebElement> predicate, List<Class<
}
}

protected boolean waitForCondition(BooleanSupplier condition, String conditionName, Long timeout) {
boolean result = conditionalWait.waitFor(condition, timeout, null);
protected boolean waitForCondition(BooleanSupplier condition, String conditionName, Duration timeout) {
boolean result = conditionalWait.waitFor(condition, timeout);
if (!result) {
String timeoutString = timeout == null ? "" : String.format("%1$s s.", timeout);
String timeoutString = timeout == null ? "" : String.format("%1$s s.", timeout.getSeconds());
localizedLogger.warn("loc.element.not.in.state", locator, conditionName.toUpperCase(), timeoutString);
}
return result;
Expand All @@ -66,7 +67,7 @@ public boolean isClickable() {
}

@Override
public void waitForClickable(Long timeout) {
public void waitForClickable(Duration timeout) {
String errorMessage = String.format("Element %1$s has not become clickable after timeout.", locator);
try {
conditionalWait.waitForTrue(this::isClickable, timeout, null, errorMessage);
Expand All @@ -82,12 +83,12 @@ public boolean isDisplayed() {
}

@Override
public boolean waitForDisplayed(Long timeout) {
public boolean waitForDisplayed(Duration timeout) {
return waitForCondition(() -> tryInvokeFunction(WebElement::isDisplayed), ElementState.DISPLAYED.toString(), timeout);
}

@Override
public boolean waitForNotDisplayed(Long timeout) {
public boolean waitForNotDisplayed(Duration timeout) {
return waitForCondition(() -> !isDisplayed(), "invisible or absent", timeout);
}

Expand All @@ -97,12 +98,12 @@ public boolean isExist() {
}

@Override
public boolean waitForExist(Long timeout) {
public boolean waitForExist(Duration timeout) {
return waitForCondition(() -> tryInvokeFunction(element -> true), ElementState.EXISTS_IN_ANY_STATE.toString(), timeout);
}

@Override
public boolean waitForNotExist(Long timeout) {
public boolean waitForNotExist(Duration timeout) {
return waitForCondition(() -> !isExist(), "absent", timeout);
}

Expand All @@ -112,12 +113,12 @@ public boolean isEnabled() {
}

@Override
public boolean waitForEnabled(Long timeout) {
public boolean waitForEnabled(Duration timeout) {
return waitForCondition(this::isEnabled, elementEnabled().getStateName(), timeout);
}

@Override
public boolean waitForNotEnabled(Long timeout) {
public boolean waitForNotEnabled(Duration timeout) {
return waitForCondition(() -> !isEnabled(), elementNotEnabled().getStateName(), timeout);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import aquality.selenium.core.waitings.IConditionalWait;
import org.openqa.selenium.By;

import java.time.Duration;

public class DefaultElementStateProvider extends ElementStateProvider {

private final By locator;
Expand All @@ -18,70 +20,70 @@ public DefaultElementStateProvider(By locator, IConditionalWait conditionalWait,

@Override
public boolean isClickable() {
return waitForIsClickable(getZeroTImeout(), true);
return waitForIsClickable(Duration.ZERO, true);
}

@Override
public void waitForClickable(Long timeout) {
public void waitForClickable(Duration timeout) {
waitForIsClickable(timeout, false);
}

private boolean waitForIsClickable(Long timeout, boolean catchTimeoutException) {
private boolean waitForIsClickable(Duration timeout, boolean catchTimeoutException) {
DesiredState desiredState = elementClickable();
desiredState = catchTimeoutException ? desiredState.withCatchingTimeoutException() : desiredState;
return isElementInDesiredCondition(desiredState, timeout);
}

private boolean isElementInDesiredCondition(DesiredState elementStateCondition, Long timeout) {
private boolean isElementInDesiredCondition(DesiredState elementStateCondition, Duration timeout) {
return !elementFinder.findElements(locator, elementStateCondition, timeout).isEmpty();
}

@Override
public boolean isDisplayed() {
return waitForDisplayed(getZeroTImeout());
return waitForDisplayed(Duration.ZERO);
}

@Override
public boolean waitForDisplayed(Long timeout) {
public boolean waitForDisplayed(Duration timeout) {
return isAnyElementFound(timeout, ElementState.DISPLAYED);
}

private boolean isAnyElementFound(Long timeout, ElementState state) {
private boolean isAnyElementFound(Duration timeout, ElementState state) {
return !elementFinder.findElements(locator, state, timeout).isEmpty();
}

@Override
public boolean waitForNotDisplayed(Long timeout) {
return conditionalWait.waitFor(() -> !isDisplayed(), timeout, null);
public boolean waitForNotDisplayed(Duration timeout) {
return conditionalWait.waitFor(() -> !isDisplayed(), timeout);
}

@Override
public boolean isExist() {
return waitForExist(getZeroTImeout());
return waitForExist(Duration.ZERO);
}

@Override
public boolean waitForExist(Long timeout) {
public boolean waitForExist(Duration timeout) {
return isAnyElementFound(timeout, ElementState.EXISTS_IN_ANY_STATE);
}

@Override
public boolean waitForNotExist(Long timeout) {
return conditionalWait.waitFor(() -> !isExist(), timeout, null);
public boolean waitForNotExist(Duration timeout) {
return conditionalWait.waitFor(() -> !isExist(), timeout);
}

@Override
public boolean isEnabled() {
return waitForEnabled(getZeroTImeout());
return waitForEnabled(Duration.ZERO);
}

@Override
public boolean waitForEnabled(Long timeout) {
public boolean waitForEnabled(Duration timeout) {
return isElementInDesiredCondition(elementEnabled(), timeout);
}

@Override
public boolean waitForNotEnabled(Long timeout) {
public boolean waitForNotEnabled(Duration timeout) {
return isElementInDesiredCondition(elementNotEnabled(), timeout);
}
}
3 changes: 2 additions & 1 deletion src/main/java/aquality/selenium/core/elements/Element.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.openqa.selenium.WebDriverException;
import org.openqa.selenium.remote.RemoteWebElement;

import java.time.Duration;
import java.util.function.Supplier;

public abstract class Element implements IElement {
Expand Down Expand Up @@ -73,7 +74,7 @@ public IElementStateProvider state() {
}

@Override
public RemoteWebElement getElement(Long timeout) {
public RemoteWebElement getElement(Duration timeout) {
try {
return getElementCacheConfiguration().isEnabled()
? getCache().getElement(timeout)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import org.openqa.selenium.By;
import org.openqa.selenium.remote.RemoteWebElement;

import java.time.Duration;

/**
* Implementation of {@link IElementCacheHandler}.
*/
Expand Down Expand Up @@ -44,7 +46,7 @@ public boolean wasCached() {
}

@Override
public RemoteWebElement getElement(Long timeout, ElementState customState) {
public RemoteWebElement getElement(Duration timeout, ElementState customState) {
ElementState requiredState = customState == null ? state : customState;
if (isRefreshNeeded(requiredState)) {
remoteElement = (RemoteWebElement) finder.findElement(locator, requiredState, timeout);
Expand Down
Loading