Skip to content

Commit

Permalink
#72 added catch of StaleElementReferenceException in ElementStateProv… (
Browse files Browse the repository at this point in the history
#73)

* #72 added catch of StaleElementReferenceException in ElementStateProvider
* #72 updated version in read.me
* #72 removed unused imports
  • Loading branch information
knysh authored and DmitryBogatko committed Jan 9, 2020
1 parent a2fae13 commit 89dcc91
Show file tree
Hide file tree
Showing 8 changed files with 114 additions and 55 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ We use interfaces where is possible, so you can implement your own version of ta
<dependency>
<groupId>com.github.aquality-automation</groupId>
<artifactId>aquality-selenium</artifactId>
<version>1.2.0</version>
<version>1.2.1</version>
</dependency>
```

Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>com.github.aquality-automation</groupId>
<artifactId>aquality-selenium</artifactId>
<version>1.2.0</version>
<version>1.2.1</version>
<packaging>jar</packaging>

<name>Aquality Selenium</name>
Expand Down
5 changes: 1 addition & 4 deletions src/main/java/aquality/selenium/browser/Browser.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,9 @@
import org.openqa.selenium.WebDriver.Navigation;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.support.ui.ExpectedCondition;

import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Collections;
import java.util.concurrent.TimeUnit;
import java.util.function.Supplier;

Expand Down Expand Up @@ -165,8 +163,7 @@ public void waitForPageToLoad() {
ConditionalWait.waitFor(condition,
timeouts.getPageLoad(),
timeouts.getPollingInterval(),
String.format(getLocManager().getValue("loc.browser.page.is.not.loaded"), timeouts.getPageLoad()),
Collections.emptyList());
String.format(getLocManager().getValue("loc.browser.page.is.not.loaded"), timeouts.getPageLoad()));
}

/**
Expand Down
13 changes: 4 additions & 9 deletions src/main/java/aquality/selenium/elements/ElementFinder.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@
import aquality.selenium.logger.Logger;
import aquality.selenium.waitings.ConditionalWait;
import org.openqa.selenium.*;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.function.Predicate;
Expand Down Expand Up @@ -60,24 +58,21 @@ public List<WebElement> findElements(By locator, long timeout, ElementState stat
}
}

List<WebElement> findElements(By locator, long timeout, DesiredState desiredState)
{
List<WebElement> findElements(By locator, long timeout, DesiredState desiredState) {
List<WebElement> foundElements = new ArrayList<>();
List<WebElement> resultElements = new ArrayList<>();
long zeroTimeout = 0L;
getBrowser().setImplicitWaitTimeout(zeroTimeout);
try{

try {
ConditionalWait.waitFor(driver ->
{
List<WebElement> allFoundElements = driver.findElements(locator);
foundElements.addAll(allFoundElements);
List<WebElement> filteredElements = filterByState(allFoundElements, desiredState.getDesiredStatePredicate());
resultElements.addAll(filteredElements);
return !filteredElements.isEmpty();
}, timeout, getTimeoutConfiguration().getPollingInterval(),
desiredState.getMessage(), Collections.emptyList());
}catch (TimeoutException e){
}, timeout, getTimeoutConfiguration().getPollingInterval(), desiredState.getMessage());
} catch (TimeoutException e) {
applyResult(locator, desiredState, foundElements);
}
getBrowser().setImplicitWaitTimeout(getTimeoutConfiguration().getImplicit());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.TimeoutException;
import org.openqa.selenium.WebElement;

import java.util.Collections;
import java.util.List;
import java.util.Objects;

Expand Down Expand Up @@ -97,8 +95,7 @@ public boolean waitForNotExist(long timeout) {
return ConditionalWait.waitFor(y -> findElements(zeroTimeout).isEmpty(),
timeout,
getTimeoutConfiguration().getPollingInterval(),
message,
Collections.emptyList());
message);
}catch (TimeoutException e){
getLogger().debug(getDesiredStateMessage("NOT EXIST", timeout));
return false;
Expand Down
18 changes: 18 additions & 0 deletions src/main/java/aquality/selenium/waitings/ConditionalWait.java
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,24 @@ public static <T> T waitFor(ExpectedCondition<T> condition, String message) {
Collections.singleton(StaleElementReferenceException.class));
}

/**
* Waits for function will be true or return some except false.
* StaleElementReferenceException will be handled by default
* @param condition Function for waiting {@link Function}.,
* @param timeOutInSeconds Time-out in seconds
* @param pollingIntervalInMilliseconds interval in milliseconds between checks whether condition match
* @param message the message that will be added to an error in case if the condition is not matched during the timeout
* @param <T> Type of object which is waiting
* @return Object which waiting for or null - is exceptions occurred
*/
public static <T> T waitFor(ExpectedCondition<T> condition, long timeOutInSeconds, long pollingIntervalInMilliseconds, String message) {
return waitFor(condition,
timeOutInSeconds,
pollingIntervalInMilliseconds,
message,
Collections.singleton(StaleElementReferenceException.class));
}

/**
* Waits for function will be true or return some except false.
*
Expand Down
121 changes: 87 additions & 34 deletions src/test/java/aquality/selenium/waitings/ConditionalWaitTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@
import aquality.selenium.configuration.ITimeoutConfiguration;
import org.openqa.selenium.StaleElementReferenceException;
import org.testng.Assert;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.Test;
import utils.DurationSample;
import utils.Timer;

import java.util.Collections;
import java.util.concurrent.TimeoutException;

import static org.testng.Assert.assertFalse;
import static org.testng.Assert.assertTrue;

Expand Down Expand Up @@ -88,8 +87,6 @@ public void testTimeoutExceptionShouldBeThrownIfDriverConditionIsNotMetAndDefaul
} catch (org.openqa.selenium.TimeoutException e) {
DurationSample durationSample = new DurationSample(timer.duration(), getTimeoutConfig().getCondition(), defaultDeviation);
assertTrue(durationSample.isDurationBetweenLimits(), durationSample.toString());
} finally {
BrowserManager.getBrowser().quit();
}
}

Expand All @@ -102,71 +99,122 @@ public void testTimeoutExceptionShouldBeThrownIfDriverConditionIsNotMetAndTimeou
timer.start();
return false;
}, waitForTimeoutCondition, waitForTimeoutPolling,
"Conditional should be true", Collections.singleton(StaleElementReferenceException.class));
"Conditional should be true");

} catch (org.openqa.selenium.TimeoutException e) {
DurationSample durationSample = new DurationSample(timer.duration(), waitForTimeoutCondition, defaultDeviation);
assertTrue(durationSample.isDurationBetweenLimits(), durationSample.toString());
} finally {
BrowserManager.getBrowser().quit();
}
}

@Test
public void testTimeoutExceptionShouldNotBeThrownIfDriverConditionIsMetAndDefaultTimeoutIsNotOver() {
public void testTimeoutExceptionShouldBeThrownIfDriverConditionIsNotMetAndTimeoutIsOverWithIgnoredExceptions() {
Timer timer = new Timer();
try {
ConditionalWait.waitFor((driver) ->
{
timer.start();
return true;
},
"Conditional should be true");
DurationSample durationSample = new DurationSample(timer.duration(), getTimeoutConfig().getCondition());
assertTrue(durationSample.getDuration() < getTimeoutConfig().getCondition());
} finally {
BrowserManager.getBrowser().quit();
return false;
}, waitForTimeoutCondition, waitForTimeoutPolling,
"Conditional should be true", Collections.emptyList());

} catch (org.openqa.selenium.TimeoutException e) {
DurationSample durationSample = new DurationSample(timer.duration(), waitForTimeoutCondition, defaultDeviation);
assertTrue(durationSample.isDurationBetweenLimits(), durationSample.toString());
}
}

@Test
public void testExceptionShouldBeCaughtConditionIsMetAndDefaultTimeoutIsNotOver(){
public void testTimeoutExceptionShouldNotBeThrownIfDriverConditionIsMetAndDefaultTimeoutIsNotOver() {
Timer timer = new Timer();

ConditionalWait.waitFor((driver) ->
{
timer.start();
return true;
},
"Conditional should be true");
DurationSample durationSample = new DurationSample(timer.duration(), getTimeoutConfig().getCondition());
assertTrue(durationSample.getDuration() < getTimeoutConfig().getCondition());
}

@Test
public void testTimeoutExceptionShouldNotBeThrownIfDriverConditionIsMetAndTimeoutIsNotOverWithIgnoredExceptions() {
Timer timer = new Timer();
boolean conditionResult = ConditionalWait.waitFor((driver) ->
{
timer.start();
return true;
}, waitForTimeoutCondition, waitForTimeoutPolling,
"Conditional should be true");
DurationSample durationSample = new DurationSample(timer.duration(), waitForTimeoutCondition);
assertTrue(durationSample.getDuration() < waitForTimeoutCondition);
assertTrue(conditionResult, "Condition result should be true");
}

@Test
public void testTimeoutExceptionShouldNotBeThrownIfDriverConditionIsMetAndTimeoutIsNotOver() {
Timer timer = new Timer();
boolean conditionResult = ConditionalWait.waitFor((driver) ->
{
timer.start();
return true;
}, waitForTimeoutCondition, waitForTimeoutPolling,
"Conditional should be true", Collections.singleton(IllegalArgumentException.class));
DurationSample durationSample = new DurationSample(timer.duration(), waitForTimeoutCondition);
assertTrue(durationSample.getDuration() < waitForTimeoutCondition);
assertTrue(conditionResult, "Condition result should be true");
}

@Test
public void testExceptionShouldBeCaughtConditionIsMetAndTimeoutIsNotOver() {
Timer timer = new Timer();
try{
try {
ConditionalWait.waitFor((driver) ->
{
timer.start();
throw new IllegalArgumentException("I am exception");
}, waitForTimeoutCondition, waitForTimeoutPolling,
"Conditional should be true", Collections.singleton(IllegalArgumentException.class));
} catch (org.openqa.selenium.TimeoutException e){
} catch (org.openqa.selenium.TimeoutException e) {
DurationSample durationSample = new DurationSample(timer.duration(), waitForTimeoutCondition, defaultDeviation);
assertTrue(durationSample.isDurationBetweenLimits(), durationSample.toString());
} finally {
BrowserManager.getBrowser().quit();
}
}

@Test
public void testTimeoutExceptionShouldNotBeThrownIfDriverConditionIsMetAndTimeoutIsNotOver() {
public void testStaleElementReferenceExceptionShouldBeCaughtConditionIsMetAndTimeoutIsNotOver() {
Timer timer = new Timer();
try {
boolean conditionResult = ConditionalWait.waitFor((driver) ->
ConditionalWait.waitFor((driver) ->
{
timer.start();
return true;
throw new StaleElementReferenceException("I am StaleElementReferenceException");
}, waitForTimeoutCondition, waitForTimeoutPolling,
"Conditional should be true", Collections.singleton(IllegalArgumentException.class));
DurationSample durationSample = new DurationSample(timer.duration(), waitForTimeoutCondition);
assertTrue(durationSample.getDuration() < waitForTimeoutCondition);
assertTrue(conditionResult, "Condition result should be true");
} finally {
BrowserManager.getBrowser().quit();
"Conditional should be true");
} catch (org.openqa.selenium.TimeoutException e) {
DurationSample durationSample = new DurationSample(timer.duration(), waitForTimeoutCondition, defaultDeviation);
assertTrue(durationSample.isDurationBetweenLimits(), durationSample.toString());
}
}

@Test
public void testStaleElementReferenceExceptionShouldBeCaughtConditionIsMetAndDefaultTimeoutIsNotOver() {
Timer timer = new Timer();
try {
ConditionalWait.waitFor((driver) ->
{
timer.start();
throw new StaleElementReferenceException("I am StaleElementReferenceException");
}, "Conditional should be true");
} catch (org.openqa.selenium.TimeoutException e) {
DurationSample durationSample = new DurationSample(timer.duration(), getTimeoutConfig().getCondition(), defaultDeviation);
assertTrue(durationSample.isDurationBetweenLimits(), durationSample.toString());
}
}

@Test
public void testTrueShouldNotBeReturnedIfConditionIsMetAndTimeoutIsNotOver(){
public void testTrueShouldBeReturnedIfConditionIsMetAndTimeoutIsNotOver() {
Timer timer = new Timer();
boolean conditionResult = ConditionalWait.waitFor(() ->
{
Expand All @@ -179,7 +227,7 @@ public void testTrueShouldNotBeReturnedIfConditionIsMetAndTimeoutIsNotOver(){
}

@Test
public void testFalseShouldBeReturnedIfConditionIsNotMetAndTimeoutIsOver(){
public void testFalseShouldBeReturnedIfConditionIsNotMetAndTimeoutIsOver() {
Timer timer = new Timer();
boolean conditionResult = ConditionalWait.waitFor(() ->
{
Expand All @@ -192,7 +240,7 @@ public void testFalseShouldBeReturnedIfConditionIsNotMetAndTimeoutIsOver(){
}

@Test
public void testTrueShouldBeReturnedIfConditionIsMetAndDefaultTimeoutIsNotOver(){
public void testTrueShouldBeReturnedIfConditionIsMetAndDefaultTimeoutIsNotOver() {
Timer timer = new Timer();
boolean conditionResult = ConditionalWait.waitFor(() ->
{
Expand All @@ -205,7 +253,7 @@ public void testTrueShouldBeReturnedIfConditionIsMetAndDefaultTimeoutIsNotOver()
}

@Test
public void testFalseShouldBeReturnedIfConditionIsNotMetAndDefaultTimeoutIsOver(){
public void testFalseShouldBeReturnedIfConditionIsNotMetAndDefaultTimeoutIsOver() {
Timer timer = new Timer();
boolean conditionResult = ConditionalWait.waitFor(() ->
{
Expand All @@ -217,7 +265,12 @@ public void testFalseShouldBeReturnedIfConditionIsNotMetAndDefaultTimeoutIsOver(
assertFalse(conditionResult, "Condition result should be false");
}

private ITimeoutConfiguration getTimeoutConfig(){
@AfterMethod
public void after() {
BrowserManager.getBrowser().quit();
}

private ITimeoutConfiguration getTimeoutConfig() {
return Configuration.getInstance().getTimeoutConfiguration();
}
}
3 changes: 1 addition & 2 deletions src/test/java/tests/usecases/BrowserFactoryTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,7 @@ public void testShouldBePossibleToOverrideDownloadDirectory() throws IOException

BrowserManager.getBrowser().goTo(urlXlsSample);
File fileDownloaded = new File(downloadDirFactoryInitialized + fileName);
boolean isFileDownloaded = ConditionalWait.waitFor(driver -> fileDownloaded.exists(), 120, 300, "File should be downloaded", Collections.singleton(StaleElementReferenceException.class));

boolean isFileDownloaded = ConditionalWait.waitFor(driver -> fileDownloaded.exists(), 120, 300, "File should be downloaded");
Assert.assertTrue(isFileDownloaded, "Downloaded file exists");
}

Expand Down

0 comments on commit 89dcc91

Please sign in to comment.