Skip to content

Commit 4e5353f

Browse files
authored
Merge pull request #70 from AutomateThePlanet/navramov_MM
StaleElementReference enhancements
2 parents f5f9f1a + 0565039 commit 4e5353f

File tree

2 files changed

+22
-5
lines changed

2 files changed

+22
-5
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: 20 additions & 4 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,7 +899,8 @@ private void clickInternal() {
893899
try {
894900
wait.until(x -> tryClick());
895901
} catch (TimeoutException e) {
896-
toBeVisible().toBeClickable().findElement().click();
902+
Log.info("Click has timed out. Trying with JS click()...");
903+
javaScriptService.execute("arguments[0].click()", findElement());
897904
}
898905
}
899906

@@ -902,9 +909,15 @@ private boolean tryClick() {
902909
toBeVisible().toBeClickable().findElement().click();
903910
return true;
904911
} catch (ElementNotInteractableException e) {
912+
Log.error("ElementNotInteractableException found - retrying with scroll.. ");
913+
scrollToVisible();
914+
return false;
915+
} catch (StaleElementReferenceException e) {
916+
Log.error("StaleElementReference Exception found - retrying with a new Find... ");
917+
findElement();
905918
return false;
906919
} catch (WebDriverException e) {
907-
toBeVisible().toBeClickable().waitToBe();
920+
Log.error("WebDriverException found - trying again... ");
908921
return false;
909922
}
910923
}
@@ -1291,6 +1304,9 @@ private void scrollToVisible(WebElement wrappedElement, boolean shouldWait, Scro
12911304
public boolean isVisible() {
12921305
try {
12931306
return getWrappedElement().isDisplayed();
1307+
} catch (StaleElementReferenceException e) {
1308+
wrappedElement = findElement();
1309+
return false;
12941310
} catch (NotFoundException e) {
12951311
return false;
12961312
}

0 commit comments

Comments
 (0)