From fd1e67d14e5cc392377be1fad54d4619e5444b2b Mon Sep 17 00:00:00 2001 From: NikolayAvramov Date: Tue, 1 Apr 2025 15:46:42 +0300 Subject: [PATCH 1/2] hunting down StaleElementReference Exception --- .../bellatrix/web/components/WebComponent.java | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/bellatrix.web/src/main/java/solutions/bellatrix/web/components/WebComponent.java b/bellatrix.web/src/main/java/solutions/bellatrix/web/components/WebComponent.java index 0668f5ed..de91fb75 100644 --- a/bellatrix.web/src/main/java/solutions/bellatrix/web/components/WebComponent.java +++ b/bellatrix.web/src/main/java/solutions/bellatrix/web/components/WebComponent.java @@ -893,7 +893,15 @@ private void clickInternal() { try { wait.until(x -> tryClick()); } catch (TimeoutException e) { + Log.info("Timeout Exception found - retrying... "); toBeVisible().toBeClickable().findElement().click(); + } catch (StaleElementReferenceException e) { + Log.info("StaleElementReference Exception found - retrying with scroll.. "); + browserService.scrollToTop(); + tryClick(); + } catch (Exception e) { + Log.info("Exception found - retrying with scroll.. "); + findElement().click(); } } @@ -903,6 +911,9 @@ private boolean tryClick() { return true; } catch (ElementNotInteractableException e) { return false; + } catch (StaleElementReferenceException e) { + findElement(); + return false; } catch (WebDriverException e) { toBeVisible().toBeClickable().waitToBe(); return false; From 05650392c5b865aa5d43dd36d3dc2fb988d20a23 Mon Sep 17 00:00:00 2001 From: NikolayAvramov Date: Thu, 3 Apr 2025 13:32:53 +0300 Subject: [PATCH 2/2] hunting down StaleElementReference Exception --- .../plugins/screenshots/ScreenshotPlugin.java | 3 +- .../web/components/WebComponent.java | 29 +++++++++++-------- 2 files changed, 19 insertions(+), 13 deletions(-) diff --git a/bellatrix.plugins.screenshots/src/main/java/plugins/screenshots/ScreenshotPlugin.java b/bellatrix.plugins.screenshots/src/main/java/plugins/screenshots/ScreenshotPlugin.java index c6a4e880..c28b90c2 100644 --- a/bellatrix.plugins.screenshots/src/main/java/plugins/screenshots/ScreenshotPlugin.java +++ b/bellatrix.plugins.screenshots/src/main/java/plugins/screenshots/ScreenshotPlugin.java @@ -17,6 +17,7 @@ import solutions.bellatrix.core.plugins.EventListener; import solutions.bellatrix.core.plugins.Plugin; import solutions.bellatrix.core.plugins.TestResult; +import solutions.bellatrix.core.plugins.TimeRecord; import java.lang.reflect.Method; import java.nio.file.Paths; @@ -37,7 +38,7 @@ public ScreenshotPlugin(boolean isEnabled) { @Override @SneakyThrows - public void postAfterTest(TestResult testResult, Method memberInfo, Throwable failedTestException) { + public void preAfterTest(TestResult testResult, TimeRecord timeRecord, Method memberInfo) { if (!isEnabled || testResult == TestResult.SUCCESS) return; diff --git a/bellatrix.web/src/main/java/solutions/bellatrix/web/components/WebComponent.java b/bellatrix.web/src/main/java/solutions/bellatrix/web/components/WebComponent.java index de91fb75..9d5865ff 100644 --- a/bellatrix.web/src/main/java/solutions/bellatrix/web/components/WebComponent.java +++ b/bellatrix.web/src/main/java/solutions/bellatrix/web/components/WebComponent.java @@ -124,8 +124,14 @@ public WebElement getWrappedElement() { } else { return findElement(); } - } catch (Exception ex ) { + } catch (ElementNotInteractableException ex ) { + scrollToVisible(); return findElement(); + } catch (StaleElementReferenceException ex ) { + return findElement(); + } catch (WebDriverException ex ) { + toExist().waitToBe(); + return wrappedElement; } } @@ -844,7 +850,7 @@ private boolean inShadowContext() { } public WebElement findElement() { - if (waitStrategies.size() == 0) { + if (waitStrategies.isEmpty()) { waitStrategies.add(Wait.to().exist(webSettings.getTimeoutSettings().getElementWaitTimeout(), webSettings.getTimeoutSettings().getSleepInterval())); } @@ -893,15 +899,8 @@ private void clickInternal() { try { wait.until(x -> tryClick()); } catch (TimeoutException e) { - Log.info("Timeout Exception found - retrying... "); - toBeVisible().toBeClickable().findElement().click(); - } catch (StaleElementReferenceException e) { - Log.info("StaleElementReference Exception found - retrying with scroll.. "); - browserService.scrollToTop(); - tryClick(); - } catch (Exception e) { - Log.info("Exception found - retrying with scroll.. "); - findElement().click(); + Log.info("Click has timed out. Trying with JS click()..."); + javaScriptService.execute("arguments[0].click()", findElement()); } } @@ -910,12 +909,15 @@ private boolean tryClick() { toBeVisible().toBeClickable().findElement().click(); return true; } catch (ElementNotInteractableException e) { + Log.error("ElementNotInteractableException found - retrying with scroll.. "); + scrollToVisible(); return false; } catch (StaleElementReferenceException e) { + Log.error("StaleElementReference Exception found - retrying with a new Find... "); findElement(); return false; } catch (WebDriverException e) { - toBeVisible().toBeClickable().waitToBe(); + Log.error("WebDriverException found - trying again... "); return false; } } @@ -1302,6 +1304,9 @@ private void scrollToVisible(WebElement wrappedElement, boolean shouldWait, Scro public boolean isVisible() { try { return getWrappedElement().isDisplayed(); + } catch (StaleElementReferenceException e) { + wrappedElement = findElement(); + return false; } catch (NotFoundException e) { return false; }