From f5dbfbf6775f65c9f878be5f7c4aa4a5b5867222 Mon Sep 17 00:00:00 2001 From: AB Date: Mon, 16 Jun 2025 09:47:46 +0200 Subject: [PATCH] Improve ``ImprovedRemoteWebElement`` --- CHANGELOG.md | 6 +++ .../remote/ImprovedRemoteWebElement.java | 37 +++++++++++++++---- 2 files changed, 36 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0a3dc02..027747b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,9 @@ +# 1.0.2 +* ``ImprovedRemoteWebElement`` + * Make it possible to disable auto scroll + * Don't throw an exception when scrolling into view is somehow not working + * Improve logger initialization + # 1.0.1 * Correctly declare ``software.xdev:testcontainers-selenium`` as scope ``test`` diff --git a/selenium-elements/src/main/java/software/xdev/selenium/elements/remote/ImprovedRemoteWebElement.java b/selenium-elements/src/main/java/software/xdev/selenium/elements/remote/ImprovedRemoteWebElement.java index ebe204d..a709839 100644 --- a/selenium-elements/src/main/java/software/xdev/selenium/elements/remote/ImprovedRemoteWebElement.java +++ b/selenium-elements/src/main/java/software/xdev/selenium/elements/remote/ImprovedRemoteWebElement.java @@ -18,6 +18,7 @@ import org.openqa.selenium.ElementNotInteractableException; import org.openqa.selenium.WebDriver; import org.openqa.selenium.remote.RemoteWebElement; +import org.slf4j.Logger; import org.slf4j.LoggerFactory; import software.xdev.selenium.elements.CanFindElements; @@ -34,13 +35,30 @@ @SuppressWarnings("java:S2160") public class ImprovedRemoteWebElement extends RemoteWebElement implements CanFindElements { - protected String waitForServerLoadToFinishFunction; + protected Logger logger; + protected final String waitForServerLoadToFinishFunction; + protected boolean autoScrollIntoView = true; public ImprovedRemoteWebElement(final String waitForServerLoadToFinishFunction) { this.waitForServerLoadToFinishFunction = waitForServerLoadToFinishFunction; } + public ImprovedRemoteWebElement withAutoScrollIntoView(final boolean autoScrollIntoView) + { + this.autoScrollIntoView = autoScrollIntoView; + return this; + } + + protected Logger logger() + { + if(this.logger == null) + { + this.logger = LoggerFactory.getLogger(this.getClass()); + } + return this.logger; + } + @Override public WebDriver getWebDriver() { @@ -57,8 +75,7 @@ public void click() } catch(final ElementNotInteractableException ex) { - LoggerFactory.getLogger(this.getClass()) - .warn( + this.logger().warn( "Element can't be clicked via UI - executing JS click. " + "Please manually check if the element is accessible. " + "If the element is accessible consider calling performJsClick directly.", ex); @@ -94,9 +111,16 @@ public void prepareForOperation() public void scrollIntoViewIfRequired() { - if(!this.isDisplayed()) + try + { + if(this.autoScrollIntoView && !this.isDisplayed()) + { + this.executeScript("arguments[0].scrollIntoView(true);", this); + } + } + catch(final ElementNotInteractableException ex) { - this.executeScript("arguments[0].scrollIntoView(true);", this); + this.logger().warn("Element can't be scrolled into view", ex); } } @@ -115,8 +139,7 @@ public void waitForServerLoadToFinish() final Boolean retVal = (Boolean)this.executeScript(this.waitForServerLoadToFinishFunction); if(retVal == null) { - LoggerFactory.getLogger(this.getClass()) - .warn("waitForLoadToFinishFunction returned null! It should either return true or false"); + this.logger().warn("waitForLoadToFinishFunction returned null! It should either return true or false"); } finished = Boolean.TRUE.equals(retVal); }