Skip to content

Commit 0565039

Browse files
author
NikolayAvramov
committed
hunting down StaleElementReference Exception
1 parent fd1e67d commit 0565039

File tree

2 files changed

+19
-13
lines changed

2 files changed

+19
-13
lines changed

bellatrix.plugins.screenshots/src/main/java/plugins/screenshots/ScreenshotPlugin.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import solutions.bellatrix.core.plugins.EventListener;
1818
import solutions.bellatrix.core.plugins.Plugin;
1919
import solutions.bellatrix.core.plugins.TestResult;
20+
import solutions.bellatrix.core.plugins.TimeRecord;
2021

2122
import java.lang.reflect.Method;
2223
import java.nio.file.Paths;
@@ -37,7 +38,7 @@ public ScreenshotPlugin(boolean isEnabled) {
3738

3839
@Override
3940
@SneakyThrows
40-
public void postAfterTest(TestResult testResult, Method memberInfo, Throwable failedTestException) {
41+
public void preAfterTest(TestResult testResult, TimeRecord timeRecord, Method memberInfo) {
4142
if (!isEnabled || testResult == TestResult.SUCCESS)
4243
return;
4344

bellatrix.web/src/main/java/solutions/bellatrix/web/components/WebComponent.java

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,14 @@ public WebElement getWrappedElement() {
124124
} else {
125125
return findElement();
126126
}
127-
} catch (Exception ex ) {
127+
} catch (ElementNotInteractableException ex ) {
128+
scrollToVisible();
128129
return findElement();
130+
} catch (StaleElementReferenceException ex ) {
131+
return findElement();
132+
} catch (WebDriverException ex ) {
133+
toExist().waitToBe();
134+
return wrappedElement;
129135
}
130136
}
131137

@@ -844,7 +850,7 @@ private boolean inShadowContext() {
844850
}
845851

846852
public WebElement findElement() {
847-
if (waitStrategies.size() == 0) {
853+
if (waitStrategies.isEmpty()) {
848854
waitStrategies.add(Wait.to().exist(webSettings.getTimeoutSettings().getElementWaitTimeout(), webSettings.getTimeoutSettings().getSleepInterval()));
849855
}
850856

@@ -893,15 +899,8 @@ private void clickInternal() {
893899
try {
894900
wait.until(x -> tryClick());
895901
} catch (TimeoutException e) {
896-
Log.info("Timeout Exception found - retrying... ");
897-
toBeVisible().toBeClickable().findElement().click();
898-
} catch (StaleElementReferenceException e) {
899-
Log.info("StaleElementReference Exception found - retrying with scroll.. ");
900-
browserService.scrollToTop();
901-
tryClick();
902-
} catch (Exception e) {
903-
Log.info("Exception found - retrying with scroll.. ");
904-
findElement().click();
902+
Log.info("Click has timed out. Trying with JS click()...");
903+
javaScriptService.execute("arguments[0].click()", findElement());
905904
}
906905
}
907906

@@ -910,12 +909,15 @@ private boolean tryClick() {
910909
toBeVisible().toBeClickable().findElement().click();
911910
return true;
912911
} catch (ElementNotInteractableException e) {
912+
Log.error("ElementNotInteractableException found - retrying with scroll.. ");
913+
scrollToVisible();
913914
return false;
914915
} catch (StaleElementReferenceException e) {
916+
Log.error("StaleElementReference Exception found - retrying with a new Find... ");
915917
findElement();
916918
return false;
917919
} catch (WebDriverException e) {
918-
toBeVisible().toBeClickable().waitToBe();
920+
Log.error("WebDriverException found - trying again... ");
919921
return false;
920922
}
921923
}
@@ -1302,6 +1304,9 @@ private void scrollToVisible(WebElement wrappedElement, boolean shouldWait, Scro
13021304
public boolean isVisible() {
13031305
try {
13041306
return getWrappedElement().isDisplayed();
1307+
} catch (StaleElementReferenceException e) {
1308+
wrappedElement = findElement();
1309+
return false;
13051310
} catch (NotFoundException e) {
13061311
return false;
13071312
}

0 commit comments

Comments
 (0)