From b17daf9463425cf0a8a45abf1e4847b695ceffdb Mon Sep 17 00:00:00 2001 From: Uladzislau Arlouski Date: Fri, 13 Dec 2024 12:08:47 +0200 Subject: [PATCH] bump --- .../plugins/partials/generic-ui-steps.adoc | 18 ----------------- .../partials/ui-wait-element-state-steps.adoc | 18 +++++++++++++++++ .../PlaywrightLocatorAssertions.java | 9 +++++++++ .../ui/web/playwright/steps/WaitSteps.java | 20 +++++++++++++++++++ .../story/integration/ScrollStepsTests.story | 5 ++--- 5 files changed, 49 insertions(+), 21 deletions(-) diff --git a/docs/modules/plugins/partials/generic-ui-steps.adoc b/docs/modules/plugins/partials/generic-ui-steps.adoc index 1fb8500a0c..95edff82e3 100644 --- a/docs/modules/plugins/partials/generic-ui-steps.adoc +++ b/docs/modules/plugins/partials/generic-ui-steps.adoc @@ -165,24 +165,6 @@ Then element located by `$locator` exists for `$duration` duration Then element located by `id(banner)` exists for `PT5S` duration ---- -=== Wait until element has text matching regular expression - -Waits until an element with the specified locator has text that matches the provided https://www.regular-expressions.info/[regular expression]. - -[source,gherkin] ----- -When I wait until element located by `$locator` has text matching `$regex` ----- - -* `$locator` - The <<_locator>> of the element which text to check. -* `$regex` - The https://www.regular-expressions.info/[regular expression] used to validate the text of the element. - -.The element should have text consisting of numbers only -[source,gherkin] ----- -When I wait until element located by `id(indicator)` has text matching `\d+` ----- - === Verify elements order Gets a collection of elements by locator and checks that they are sorted by diff --git a/docs/modules/plugins/partials/ui-wait-element-state-steps.adoc b/docs/modules/plugins/partials/ui-wait-element-state-steps.adoc index 9fcf28982b..f9d1da88ef 100644 --- a/docs/modules/plugins/partials/ui-wait-element-state-steps.adoc +++ b/docs/modules/plugins/partials/ui-wait-element-state-steps.adoc @@ -58,3 +58,21 @@ When I wait `$duration` with `$pollingDuration` polling until element located by ---- When I wait `PT10S` with `PT1S` polling until element located by `id(element-to-hide)` becomes not visible ---- + +=== Wait until element has text matching regular expression + +Waits until an element with the specified locator has text that matches the provided https://www.regular-expressions.info/[regular expression]. + +[source,gherkin] +---- +When I wait until element located by `$locator` has text matching `$regex` +---- + +* `$locator` - The <<_locator>> of the element which text to check. +* `$regex` - The https://www.regular-expressions.info/[regular expression] used to validate the text of the element. + +.The element should have text consisting of numbers only +[source,gherkin] +---- +When I wait until element located by `id(indicator)` has text matching `\d+` +---- diff --git a/vividus-plugin-web-app-playwright/src/main/java/org/vividus/ui/web/playwright/assertions/PlaywrightLocatorAssertions.java b/vividus-plugin-web-app-playwright/src/main/java/org/vividus/ui/web/playwright/assertions/PlaywrightLocatorAssertions.java index 17c5e4c1dc..a2f802e92d 100644 --- a/vividus-plugin-web-app-playwright/src/main/java/org/vividus/ui/web/playwright/assertions/PlaywrightLocatorAssertions.java +++ b/vividus-plugin-web-app-playwright/src/main/java/org/vividus/ui/web/playwright/assertions/PlaywrightLocatorAssertions.java @@ -18,6 +18,8 @@ import static org.vividus.ui.web.playwright.PlaywrightAssertionConfiguration.ASSERTION_NO_WAIT_TIMEOUT; +import java.util.regex.Pattern; + import com.microsoft.playwright.Locator; import com.microsoft.playwright.assertions.LocatorAssertions; import com.microsoft.playwright.assertions.PlaywrightAssertions; @@ -28,6 +30,13 @@ private PlaywrightLocatorAssertions() { } + public static void assertElementHasTextMatchingRegex(Locator locator, Pattern pattern, boolean waitForState) + { + LocatorAssertions.ContainsTextOptions options = waitForState ? null + : new LocatorAssertions.ContainsTextOptions().setTimeout(ASSERTION_NO_WAIT_TIMEOUT); + getLocatorAssertions(locator).containsText(pattern, options); + } + public static void assertElementVisible(Locator locator, boolean waitForState) { LocatorAssertions.IsVisibleOptions options = waitForState ? null : new LocatorAssertions.IsVisibleOptions() diff --git a/vividus-plugin-web-app-playwright/src/main/java/org/vividus/ui/web/playwright/steps/WaitSteps.java b/vividus-plugin-web-app-playwright/src/main/java/org/vividus/ui/web/playwright/steps/WaitSteps.java index 3e1015f33e..60413c5d9b 100644 --- a/vividus-plugin-web-app-playwright/src/main/java/org/vividus/ui/web/playwright/steps/WaitSteps.java +++ b/vividus-plugin-web-app-playwright/src/main/java/org/vividus/ui/web/playwright/steps/WaitSteps.java @@ -19,6 +19,7 @@ import java.time.Duration; import java.util.function.BooleanSupplier; import java.util.function.Supplier; +import java.util.regex.Pattern; import com.microsoft.playwright.Locator; import com.microsoft.playwright.Page; @@ -32,6 +33,7 @@ import org.vividus.ui.web.playwright.BrowserContextProvider; import org.vividus.ui.web.playwright.UiContext; import org.vividus.ui.web.playwright.action.WaitActions; +import org.vividus.ui.web.playwright.assertions.PlaywrightLocatorAssertions; import org.vividus.ui.web.playwright.locator.PlaywrightLocator; import org.vividus.ui.web.playwright.locator.Visibility; import org.vividus.util.wait.DurationBasedWaiter; @@ -160,6 +162,24 @@ public void waitDurationWithPollingTillElementState(Duration duration, Duration isElementInState); } + /** + * Waits until an element with the specified locator has text that matches the provided regular expression. + * + * @param locator The locator of the element which text to check + * @param regex The regular expression used to validate the text of the element + */ + @When("I wait until element located by `$locator` has text matching `$regex`") + public void waitUntilElementHasTextMatchingRegex(PlaywrightLocator locator, Pattern regex) + { + Supplier conditionDescription = () -> "The element located by `%s` has text matching regex %s" + .formatted(locator, regex); + waitActions.runWithTimeoutAssertion(conditionDescription, () -> + { + Locator element = uiContext.locateElement(locator); + PlaywrightLocatorAssertions.assertElementHasTextMatchingRegex(element, regex, true); + }); + } + private void waitForElementStateValidatingVisibility(PlaywrightLocator locator, ElementState state) { diff --git a/vividus-tests/src/main/resources/story/integration/ScrollStepsTests.story b/vividus-tests/src/main/resources/story/integration/ScrollStepsTests.story index 4d9d548392..dbcfe05736 100644 --- a/vividus-tests/src/main/resources/story/integration/ScrollStepsTests.story +++ b/vividus-tests/src/main/resources/story/integration/ScrollStepsTests.story @@ -27,7 +27,7 @@ When I scroll context to TOP edge When I reset context When I wait until element located by `id(current-vertical):a` contains text `0` -Scenario: Verify steps: "When I scroll element located by `$locator` into view", "Then element located by `$locator` $presence visible in viewport" +Scenario: Verify steps: "When I scroll element located by `$locator` into view", "Then element located by `$locator` $presence visible in viewport", "When I wait until element located by `$locator` has text matching `$regex`" Meta: @requirementId 436 @playwrightSupported @@ -35,8 +35,7 @@ Given I am on page with URL `${vividus-test-site-url}/scrollableElements.html` Then element located by `xpath(//a[text()="Contact"])` is not visible in viewport When I scroll element located by `xpath(//a[text()="Contact"])` into view Then element located by `xpath(//a[text()="Contact"])` is visible in viewport -When I save text of element located by `id(current-vertical):a` to scenario variable `currentVertical` -Then `${currentVertical}` matches `\d+` +When I wait until element located by `id(current-vertical):a` has text matching `\d+` Scenario: Verify deprecated steps: "When I scroll element located `$locator` into view", "Then page is scrolled to element located `$locator`" When I refresh page