Skip to content

Commit 89dcc91

Browse files
knyshDmitryBogatko
authored andcommitted
#72 added catch of StaleElementReferenceException in ElementStateProv… (#73)
* #72 added catch of StaleElementReferenceException in ElementStateProvider * #72 updated version in read.me * #72 removed unused imports
1 parent a2fae13 commit 89dcc91

File tree

8 files changed

+114
-55
lines changed

8 files changed

+114
-55
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ We use interfaces where is possible, so you can implement your own version of ta
1818
<dependency>
1919
<groupId>com.github.aquality-automation</groupId>
2020
<artifactId>aquality-selenium</artifactId>
21-
<version>1.2.0</version>
21+
<version>1.2.1</version>
2222
</dependency>
2323
```
2424

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>com.github.aquality-automation</groupId>
88
<artifactId>aquality-selenium</artifactId>
9-
<version>1.2.0</version>
9+
<version>1.2.1</version>
1010
<packaging>jar</packaging>
1111

1212
<name>Aquality Selenium</name>

src/main/java/aquality/selenium/browser/Browser.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,9 @@
1313
import org.openqa.selenium.WebDriver.Navigation;
1414
import org.openqa.selenium.remote.RemoteWebDriver;
1515
import org.openqa.selenium.support.ui.ExpectedCondition;
16-
1716
import java.io.File;
1817
import java.io.IOException;
1918
import java.nio.charset.StandardCharsets;
20-
import java.util.Collections;
2119
import java.util.concurrent.TimeUnit;
2220
import java.util.function.Supplier;
2321

@@ -165,8 +163,7 @@ public void waitForPageToLoad() {
165163
ConditionalWait.waitFor(condition,
166164
timeouts.getPageLoad(),
167165
timeouts.getPollingInterval(),
168-
String.format(getLocManager().getValue("loc.browser.page.is.not.loaded"), timeouts.getPageLoad()),
169-
Collections.emptyList());
166+
String.format(getLocManager().getValue("loc.browser.page.is.not.loaded"), timeouts.getPageLoad()));
170167
}
171168

172169
/**

src/main/java/aquality/selenium/elements/ElementFinder.java

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,7 @@
99
import aquality.selenium.logger.Logger;
1010
import aquality.selenium.waitings.ConditionalWait;
1111
import org.openqa.selenium.*;
12-
1312
import java.util.ArrayList;
14-
import java.util.Collections;
1513
import java.util.List;
1614
import java.util.Objects;
1715
import java.util.function.Predicate;
@@ -60,24 +58,21 @@ public List<WebElement> findElements(By locator, long timeout, ElementState stat
6058
}
6159
}
6260

63-
List<WebElement> findElements(By locator, long timeout, DesiredState desiredState)
64-
{
61+
List<WebElement> findElements(By locator, long timeout, DesiredState desiredState) {
6562
List<WebElement> foundElements = new ArrayList<>();
6663
List<WebElement> resultElements = new ArrayList<>();
6764
long zeroTimeout = 0L;
6865
getBrowser().setImplicitWaitTimeout(zeroTimeout);
69-
try{
70-
66+
try {
7167
ConditionalWait.waitFor(driver ->
7268
{
7369
List<WebElement> allFoundElements = driver.findElements(locator);
7470
foundElements.addAll(allFoundElements);
7571
List<WebElement> filteredElements = filterByState(allFoundElements, desiredState.getDesiredStatePredicate());
7672
resultElements.addAll(filteredElements);
7773
return !filteredElements.isEmpty();
78-
}, timeout, getTimeoutConfiguration().getPollingInterval(),
79-
desiredState.getMessage(), Collections.emptyList());
80-
}catch (TimeoutException e){
74+
}, timeout, getTimeoutConfiguration().getPollingInterval(), desiredState.getMessage());
75+
} catch (TimeoutException e) {
8176
applyResult(locator, desiredState, foundElements);
8277
}
8378
getBrowser().setImplicitWaitTimeout(getTimeoutConfiguration().getImplicit());

src/main/java/aquality/selenium/elements/ElementStateProvider.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@
1010
import org.openqa.selenium.NoSuchElementException;
1111
import org.openqa.selenium.TimeoutException;
1212
import org.openqa.selenium.WebElement;
13-
14-
import java.util.Collections;
1513
import java.util.List;
1614
import java.util.Objects;
1715

@@ -97,8 +95,7 @@ public boolean waitForNotExist(long timeout) {
9795
return ConditionalWait.waitFor(y -> findElements(zeroTimeout).isEmpty(),
9896
timeout,
9997
getTimeoutConfiguration().getPollingInterval(),
100-
message,
101-
Collections.emptyList());
98+
message);
10299
}catch (TimeoutException e){
103100
getLogger().debug(getDesiredStateMessage("NOT EXIST", timeout));
104101
return false;

src/main/java/aquality/selenium/waitings/ConditionalWait.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,24 @@ public static <T> T waitFor(ExpectedCondition<T> condition, String message) {
118118
Collections.singleton(StaleElementReferenceException.class));
119119
}
120120

121+
/**
122+
* Waits for function will be true or return some except false.
123+
* StaleElementReferenceException will be handled by default
124+
* @param condition Function for waiting {@link Function}.,
125+
* @param timeOutInSeconds Time-out in seconds
126+
* @param pollingIntervalInMilliseconds interval in milliseconds between checks whether condition match
127+
* @param message the message that will be added to an error in case if the condition is not matched during the timeout
128+
* @param <T> Type of object which is waiting
129+
* @return Object which waiting for or null - is exceptions occurred
130+
*/
131+
public static <T> T waitFor(ExpectedCondition<T> condition, long timeOutInSeconds, long pollingIntervalInMilliseconds, String message) {
132+
return waitFor(condition,
133+
timeOutInSeconds,
134+
pollingIntervalInMilliseconds,
135+
message,
136+
Collections.singleton(StaleElementReferenceException.class));
137+
}
138+
121139
/**
122140
* Waits for function will be true or return some except false.
123141
*

src/test/java/aquality/selenium/waitings/ConditionalWaitTests.java

Lines changed: 87 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,12 @@
55
import aquality.selenium.configuration.ITimeoutConfiguration;
66
import org.openqa.selenium.StaleElementReferenceException;
77
import org.testng.Assert;
8+
import org.testng.annotations.AfterMethod;
89
import org.testng.annotations.Test;
910
import utils.DurationSample;
1011
import utils.Timer;
11-
1212
import java.util.Collections;
1313
import java.util.concurrent.TimeoutException;
14-
1514
import static org.testng.Assert.assertFalse;
1615
import static org.testng.Assert.assertTrue;
1716

@@ -88,8 +87,6 @@ public void testTimeoutExceptionShouldBeThrownIfDriverConditionIsNotMetAndDefaul
8887
} catch (org.openqa.selenium.TimeoutException e) {
8988
DurationSample durationSample = new DurationSample(timer.duration(), getTimeoutConfig().getCondition(), defaultDeviation);
9089
assertTrue(durationSample.isDurationBetweenLimits(), durationSample.toString());
91-
} finally {
92-
BrowserManager.getBrowser().quit();
9390
}
9491
}
9592

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

107104
} catch (org.openqa.selenium.TimeoutException e) {
108105
DurationSample durationSample = new DurationSample(timer.duration(), waitForTimeoutCondition, defaultDeviation);
109106
assertTrue(durationSample.isDurationBetweenLimits(), durationSample.toString());
110-
} finally {
111-
BrowserManager.getBrowser().quit();
112107
}
113108
}
114109

115110
@Test
116-
public void testTimeoutExceptionShouldNotBeThrownIfDriverConditionIsMetAndDefaultTimeoutIsNotOver() {
111+
public void testTimeoutExceptionShouldBeThrownIfDriverConditionIsNotMetAndTimeoutIsOverWithIgnoredExceptions() {
117112
Timer timer = new Timer();
118113
try {
119114
ConditionalWait.waitFor((driver) ->
120115
{
121116
timer.start();
122-
return true;
123-
},
124-
"Conditional should be true");
125-
DurationSample durationSample = new DurationSample(timer.duration(), getTimeoutConfig().getCondition());
126-
assertTrue(durationSample.getDuration() < getTimeoutConfig().getCondition());
127-
} finally {
128-
BrowserManager.getBrowser().quit();
117+
return false;
118+
}, waitForTimeoutCondition, waitForTimeoutPolling,
119+
"Conditional should be true", Collections.emptyList());
120+
121+
} catch (org.openqa.selenium.TimeoutException e) {
122+
DurationSample durationSample = new DurationSample(timer.duration(), waitForTimeoutCondition, defaultDeviation);
123+
assertTrue(durationSample.isDurationBetweenLimits(), durationSample.toString());
129124
}
130125
}
131126

132127
@Test
133-
public void testExceptionShouldBeCaughtConditionIsMetAndDefaultTimeoutIsNotOver(){
128+
public void testTimeoutExceptionShouldNotBeThrownIfDriverConditionIsMetAndDefaultTimeoutIsNotOver() {
129+
Timer timer = new Timer();
130+
131+
ConditionalWait.waitFor((driver) ->
132+
{
133+
timer.start();
134+
return true;
135+
},
136+
"Conditional should be true");
137+
DurationSample durationSample = new DurationSample(timer.duration(), getTimeoutConfig().getCondition());
138+
assertTrue(durationSample.getDuration() < getTimeoutConfig().getCondition());
139+
}
140+
141+
@Test
142+
public void testTimeoutExceptionShouldNotBeThrownIfDriverConditionIsMetAndTimeoutIsNotOverWithIgnoredExceptions() {
143+
Timer timer = new Timer();
144+
boolean conditionResult = ConditionalWait.waitFor((driver) ->
145+
{
146+
timer.start();
147+
return true;
148+
}, waitForTimeoutCondition, waitForTimeoutPolling,
149+
"Conditional should be true");
150+
DurationSample durationSample = new DurationSample(timer.duration(), waitForTimeoutCondition);
151+
assertTrue(durationSample.getDuration() < waitForTimeoutCondition);
152+
assertTrue(conditionResult, "Condition result should be true");
153+
}
154+
155+
@Test
156+
public void testTimeoutExceptionShouldNotBeThrownIfDriverConditionIsMetAndTimeoutIsNotOver() {
157+
Timer timer = new Timer();
158+
boolean conditionResult = ConditionalWait.waitFor((driver) ->
159+
{
160+
timer.start();
161+
return true;
162+
}, waitForTimeoutCondition, waitForTimeoutPolling,
163+
"Conditional should be true", Collections.singleton(IllegalArgumentException.class));
164+
DurationSample durationSample = new DurationSample(timer.duration(), waitForTimeoutCondition);
165+
assertTrue(durationSample.getDuration() < waitForTimeoutCondition);
166+
assertTrue(conditionResult, "Condition result should be true");
167+
}
168+
169+
@Test
170+
public void testExceptionShouldBeCaughtConditionIsMetAndTimeoutIsNotOver() {
134171
Timer timer = new Timer();
135-
try{
172+
try {
136173
ConditionalWait.waitFor((driver) ->
137174
{
138175
timer.start();
139176
throw new IllegalArgumentException("I am exception");
140177
}, waitForTimeoutCondition, waitForTimeoutPolling,
141178
"Conditional should be true", Collections.singleton(IllegalArgumentException.class));
142-
} catch (org.openqa.selenium.TimeoutException e){
179+
} catch (org.openqa.selenium.TimeoutException e) {
143180
DurationSample durationSample = new DurationSample(timer.duration(), waitForTimeoutCondition, defaultDeviation);
144181
assertTrue(durationSample.isDurationBetweenLimits(), durationSample.toString());
145-
} finally {
146-
BrowserManager.getBrowser().quit();
147182
}
148183
}
149184

150185
@Test
151-
public void testTimeoutExceptionShouldNotBeThrownIfDriverConditionIsMetAndTimeoutIsNotOver() {
186+
public void testStaleElementReferenceExceptionShouldBeCaughtConditionIsMetAndTimeoutIsNotOver() {
152187
Timer timer = new Timer();
153188
try {
154-
boolean conditionResult = ConditionalWait.waitFor((driver) ->
189+
ConditionalWait.waitFor((driver) ->
155190
{
156191
timer.start();
157-
return true;
192+
throw new StaleElementReferenceException("I am StaleElementReferenceException");
158193
}, waitForTimeoutCondition, waitForTimeoutPolling,
159-
"Conditional should be true", Collections.singleton(IllegalArgumentException.class));
160-
DurationSample durationSample = new DurationSample(timer.duration(), waitForTimeoutCondition);
161-
assertTrue(durationSample.getDuration() < waitForTimeoutCondition);
162-
assertTrue(conditionResult, "Condition result should be true");
163-
} finally {
164-
BrowserManager.getBrowser().quit();
194+
"Conditional should be true");
195+
} catch (org.openqa.selenium.TimeoutException e) {
196+
DurationSample durationSample = new DurationSample(timer.duration(), waitForTimeoutCondition, defaultDeviation);
197+
assertTrue(durationSample.isDurationBetweenLimits(), durationSample.toString());
198+
}
199+
}
200+
201+
@Test
202+
public void testStaleElementReferenceExceptionShouldBeCaughtConditionIsMetAndDefaultTimeoutIsNotOver() {
203+
Timer timer = new Timer();
204+
try {
205+
ConditionalWait.waitFor((driver) ->
206+
{
207+
timer.start();
208+
throw new StaleElementReferenceException("I am StaleElementReferenceException");
209+
}, "Conditional should be true");
210+
} catch (org.openqa.selenium.TimeoutException e) {
211+
DurationSample durationSample = new DurationSample(timer.duration(), getTimeoutConfig().getCondition(), defaultDeviation);
212+
assertTrue(durationSample.isDurationBetweenLimits(), durationSample.toString());
165213
}
166214
}
167215

168216
@Test
169-
public void testTrueShouldNotBeReturnedIfConditionIsMetAndTimeoutIsNotOver(){
217+
public void testTrueShouldBeReturnedIfConditionIsMetAndTimeoutIsNotOver() {
170218
Timer timer = new Timer();
171219
boolean conditionResult = ConditionalWait.waitFor(() ->
172220
{
@@ -179,7 +227,7 @@ public void testTrueShouldNotBeReturnedIfConditionIsMetAndTimeoutIsNotOver(){
179227
}
180228

181229
@Test
182-
public void testFalseShouldBeReturnedIfConditionIsNotMetAndTimeoutIsOver(){
230+
public void testFalseShouldBeReturnedIfConditionIsNotMetAndTimeoutIsOver() {
183231
Timer timer = new Timer();
184232
boolean conditionResult = ConditionalWait.waitFor(() ->
185233
{
@@ -192,7 +240,7 @@ public void testFalseShouldBeReturnedIfConditionIsNotMetAndTimeoutIsOver(){
192240
}
193241

194242
@Test
195-
public void testTrueShouldBeReturnedIfConditionIsMetAndDefaultTimeoutIsNotOver(){
243+
public void testTrueShouldBeReturnedIfConditionIsMetAndDefaultTimeoutIsNotOver() {
196244
Timer timer = new Timer();
197245
boolean conditionResult = ConditionalWait.waitFor(() ->
198246
{
@@ -205,7 +253,7 @@ public void testTrueShouldBeReturnedIfConditionIsMetAndDefaultTimeoutIsNotOver()
205253
}
206254

207255
@Test
208-
public void testFalseShouldBeReturnedIfConditionIsNotMetAndDefaultTimeoutIsOver(){
256+
public void testFalseShouldBeReturnedIfConditionIsNotMetAndDefaultTimeoutIsOver() {
209257
Timer timer = new Timer();
210258
boolean conditionResult = ConditionalWait.waitFor(() ->
211259
{
@@ -217,7 +265,12 @@ public void testFalseShouldBeReturnedIfConditionIsNotMetAndDefaultTimeoutIsOver(
217265
assertFalse(conditionResult, "Condition result should be false");
218266
}
219267

220-
private ITimeoutConfiguration getTimeoutConfig(){
268+
@AfterMethod
269+
public void after() {
270+
BrowserManager.getBrowser().quit();
271+
}
272+
273+
private ITimeoutConfiguration getTimeoutConfig() {
221274
return Configuration.getInstance().getTimeoutConfiguration();
222275
}
223276
}

src/test/java/tests/usecases/BrowserFactoryTests.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,7 @@ public void testShouldBePossibleToOverrideDownloadDirectory() throws IOException
8484

8585
BrowserManager.getBrowser().goTo(urlXlsSample);
8686
File fileDownloaded = new File(downloadDirFactoryInitialized + fileName);
87-
boolean isFileDownloaded = ConditionalWait.waitFor(driver -> fileDownloaded.exists(), 120, 300, "File should be downloaded", Collections.singleton(StaleElementReferenceException.class));
88-
87+
boolean isFileDownloaded = ConditionalWait.waitFor(driver -> fileDownloaded.exists(), 120, 300, "File should be downloaded");
8988
Assert.assertTrue(isFileDownloaded, "Downloaded file exists");
9089
}
9190

0 commit comments

Comments
 (0)