From fb35b6eba83d50d8a85ade477ad714b43a3d356b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alaksiej=20Miale=C5=A1ka?= Date: Tue, 10 Sep 2024 00:07:18 +0200 Subject: [PATCH] Update to use Selenium 4.24.0 +semver:feature Use new FindElements logic from aquality-selenium-core to avoid possible StaleElementReference while iterating WebElements list Add Clear method to ITextBox interface Fix error on clearAndType #145 --- pom.xml | 6 ++--- .../aquality/selenium/elements/TextBox.java | 24 ++++++++++++++++++- .../elements/interfaces/ITextBox.java | 5 ++++ .../java/tests/integration/ActionTests.java | 17 +++++++++++++ .../devtools/DeviceEmulationTest.java | 4 ++-- .../devtools/NetworkSpeedEmulationTest.java | 2 +- .../devtools/OverrideUserAgentTest.java | 2 +- 7 files changed, 52 insertions(+), 8 deletions(-) diff --git a/pom.xml b/pom.xml index 6dc75bf..489e27a 100644 --- a/pom.xml +++ b/pom.xml @@ -82,17 +82,17 @@ com.github.aquality-automation aquality-selenium-core - 4.0.4 + 4.1.0 org.apache.commons commons-lang3 - 3.14.0 + 3.17.0 com.fasterxml.jackson.core jackson-databind - 2.17.0 + 2.17.2 org.slf4j diff --git a/src/main/java/aquality/selenium/elements/TextBox.java b/src/main/java/aquality/selenium/elements/TextBox.java index 93a4243..cf1b16f 100644 --- a/src/main/java/aquality/selenium/elements/TextBox.java +++ b/src/main/java/aquality/selenium/elements/TextBox.java @@ -1,10 +1,14 @@ package aquality.selenium.elements; import aquality.selenium.core.elements.ElementState; +import aquality.selenium.core.utilities.IElementActionRetrier; import aquality.selenium.elements.interfaces.ITextBox; import org.openqa.selenium.By; +import org.openqa.selenium.JavascriptException; import org.openqa.selenium.Keys; +import java.util.ArrayList; + /** * The class that describes an input field */ @@ -37,6 +41,15 @@ public void clearAndType(final String value) { clearAndType(value, false); } + @Override + public void clear() { + logElementAction(LOG_CLEARING); + getJsActions().highlightElement(); + doWithRetry(() -> { + getElement().clear(); + }); + } + @Override public void clearAndTypeSecret(final String value) { clearAndType(value, true); @@ -62,7 +75,16 @@ public void unfocus() { doWithRetry(() -> getElement().sendKeys(Keys.TAB)); } - private void type(final String value, final boolean maskValueInLog) { + @Override + protected void doWithRetry(Runnable action) { + IElementActionRetrier retrier = getElementActionRetrier(); + ArrayList> handledExceptions = new ArrayList<>(retrier.getHandledExceptions()); + handledExceptions.add(JavascriptException.class); + retrier.doWithRetry(action, handledExceptions); + } + + private void type(final String value, final boolean maskValueInLog) + { logElementAction(LOG_TYPING, maskValueInLog ? logMaskedValue : value); getJsActions().highlightElement(); doWithRetry(() -> getElement().sendKeys(value)); diff --git a/src/main/java/aquality/selenium/elements/interfaces/ITextBox.java b/src/main/java/aquality/selenium/elements/interfaces/ITextBox.java index 77e9dc7..335f5df 100644 --- a/src/main/java/aquality/selenium/elements/interfaces/ITextBox.java +++ b/src/main/java/aquality/selenium/elements/interfaces/ITextBox.java @@ -15,6 +15,11 @@ public interface ITextBox extends IElement { */ void typeSecret(String value); + /** + * Clears element text. + */ + void clear(); + /** * Clears input and enters text in the box, inputted value isn't logging * diff --git a/src/test/java/tests/integration/ActionTests.java b/src/test/java/tests/integration/ActionTests.java index cfdfa69..5072900 100644 --- a/src/test/java/tests/integration/ActionTests.java +++ b/src/test/java/tests/integration/ActionTests.java @@ -4,6 +4,7 @@ import aquality.selenium.elements.interfaces.ILabel; import aquality.selenium.elements.interfaces.ILink; import aquality.selenium.elements.interfaces.ITextBox; +import org.apache.commons.lang3.StringUtils; import org.openqa.selenium.Keys; import org.testng.Assert; import org.testng.annotations.BeforeMethod; @@ -131,6 +132,22 @@ public void testSetFocus() { Assert.assertEquals(textBox.getValue(), expectedText, "One character should be removed from " + expectedText); } + @Test + public void testClear() { + FormAuthenticationForm form = new FormAuthenticationForm(); + getBrowser().goTo(form.getUrl()); + ITextBox textBox = form.getTxbUsername(); + textBox.clear(); + Assert.assertTrue(StringUtils.isEmpty(textBox.getValue()), "Clear should work when was initially empty"); + textBox.type("anything"); + textBox.clear(); + Assert.assertTrue(StringUtils.isEmpty(textBox.getValue()), "Clear should work when was not empty"); + textBox.type("anything"); + textBox.clear(); + textBox.clear(); + Assert.assertTrue(StringUtils.isEmpty(textBox.getValue()), "Clear should work called twice"); + } + @Test public void testSetValue() { final String expectedValue = "2"; diff --git a/src/test/java/tests/usecases/devtools/DeviceEmulationTest.java b/src/test/java/tests/usecases/devtools/DeviceEmulationTest.java index 5b223fa..54ff008 100644 --- a/src/test/java/tests/usecases/devtools/DeviceEmulationTest.java +++ b/src/test/java/tests/usecases/devtools/DeviceEmulationTest.java @@ -3,8 +3,8 @@ import aquality.selenium.browser.AqualityServices; import aquality.selenium.browser.devtools.EmulationHandling; import com.google.common.collect.ImmutableMap; -import org.openqa.selenium.devtools.v127.emulation.Emulation; -import org.openqa.selenium.devtools.v127.emulation.model.DisplayFeature; +import org.openqa.selenium.devtools.v128.emulation.Emulation; +import org.openqa.selenium.devtools.v128.emulation.model.DisplayFeature; import org.testng.Assert; import org.testng.annotations.BeforeMethod; import org.testng.annotations.Test; diff --git a/src/test/java/tests/usecases/devtools/NetworkSpeedEmulationTest.java b/src/test/java/tests/usecases/devtools/NetworkSpeedEmulationTest.java index 44fb24d..59f88c3 100644 --- a/src/test/java/tests/usecases/devtools/NetworkSpeedEmulationTest.java +++ b/src/test/java/tests/usecases/devtools/NetworkSpeedEmulationTest.java @@ -2,7 +2,7 @@ import aquality.selenium.browser.AqualityServices; import org.openqa.selenium.TimeoutException; -import org.openqa.selenium.devtools.v127.network.model.ConnectionType; +import org.openqa.selenium.devtools.v128.network.model.ConnectionType; import org.testng.Assert; import org.testng.annotations.Test; import tests.BaseTest; diff --git a/src/test/java/tests/usecases/devtools/OverrideUserAgentTest.java b/src/test/java/tests/usecases/devtools/OverrideUserAgentTest.java index e8fbc09..b94354a 100644 --- a/src/test/java/tests/usecases/devtools/OverrideUserAgentTest.java +++ b/src/test/java/tests/usecases/devtools/OverrideUserAgentTest.java @@ -8,7 +8,7 @@ import manytools.BrowserLanguageForm; import manytools.UserAgentForm; import org.openqa.selenium.devtools.idealized.Network; -import org.openqa.selenium.devtools.v127.emulation.Emulation; +import org.openqa.selenium.devtools.v128.emulation.Emulation; import org.testng.Assert; import org.testng.annotations.BeforeMethod; import org.testng.annotations.Test;