From a42ec4c8048bae67fb1a90b05b4aafcf41847560 Mon Sep 17 00:00:00 2001 From: avinBar Date: Thu, 13 Jun 2024 16:28:55 +0300 Subject: [PATCH] [plugin-web-app-playwright] Add support of visibility attribute in locators (#5126) --- .../pages/plugin-web-app-playwright.adoc | 32 ++++++++ .../vividus/ui/action/search/Visibility.java | 28 +------ .../ui/action/search/ElementVisibility.java | 47 +++++++++++ .../action/search/ElementVisibilityTests.java | 58 ++++++++++++++ .../vividus/ui/web/playwright/UiContext.java | 8 +- .../playwright/locator/PlaywrightLocator.java | 18 ++++- .../locator/PlaywrightLocatorConverter.java | 29 ++++++- .../ui/web/playwright/locator/Visibility.java | 30 +++++++ .../ui/web/playwright/UiContextTests.java | 22 +++++- .../PlaywrightLocatorConverterTests.java | 79 +++++++++++++++++++ .../locator/PlaywrightLocatorTests.java | 10 ++- .../playwright/locator/VisibilityTests.java | 65 +++++++++++++++ .../playwright/steps/ElementStepsTests.java | 16 ++-- .../web/playwright/steps/WaitStepsTests.java | 13 +-- 14 files changed, 410 insertions(+), 45 deletions(-) create mode 100644 vividus-extension-ui/src/main/java/org/vividus/ui/action/search/ElementVisibility.java create mode 100644 vividus-extension-ui/src/test/java/org/vividus/ui/action/search/ElementVisibilityTests.java create mode 100644 vividus-plugin-web-app-playwright/src/main/java/org/vividus/ui/web/playwright/locator/Visibility.java create mode 100644 vividus-plugin-web-app-playwright/src/test/java/org/vividus/ui/web/playwright/locator/PlaywrightLocatorConverterTests.java create mode 100644 vividus-plugin-web-app-playwright/src/test/java/org/vividus/ui/web/playwright/locator/VisibilityTests.java diff --git a/docs/modules/plugins/pages/plugin-web-app-playwright.adoc b/docs/modules/plugins/pages/plugin-web-app-playwright.adoc index 121dc7c8b7..e3d30734b9 100644 --- a/docs/modules/plugins/pages/plugin-web-app-playwright.adoc +++ b/docs/modules/plugins/pages/plugin-web-app-playwright.adoc @@ -47,6 +47,19 @@ Playwright captures a set of complete DOM snapshots for each action. == Locator +---- +By.(): +---- + +[IMPORTANT] + +By. prefix is optional. + +. `locatorType` - *[mandatory]* type of the locator +. `locatorValue` - *[mandatory]* value of the locator +. `visibility` - *[optional]* visibility of element (visible by default) + + === Locator Types [cols="1,3,2", options="header"] @@ -70,6 +83,25 @@ Playwright captures a set of complete DOM snapshots for each action. |=== +=== Visibility types + +[cols="1,1,3", options="header"] +|=== + +|Visibility type +|Usage example +|Description + +|VISIBLE +|xpath(//a) +|Default visibility option. Only visible elements will be found + +|all +|xpath(//a):a +|Either visible and invisible elements will be found + +|=== + == Steps include::plugins:partial$common-web-app-steps.adoc[] include::plugins:partial$ui-context-management-steps.adoc[] diff --git a/vividus-extension-selenium/src/main/java/org/vividus/ui/action/search/Visibility.java b/vividus-extension-selenium/src/main/java/org/vividus/ui/action/search/Visibility.java index 653b97a3e9..2b281e184d 100644 --- a/vividus-extension-selenium/src/main/java/org/vividus/ui/action/search/Visibility.java +++ b/vividus-extension-selenium/src/main/java/org/vividus/ui/action/search/Visibility.java @@ -1,5 +1,5 @@ /* - * Copyright 2019-2021 the original author or authors. + * Copyright 2019-2024 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,14 +16,9 @@ package org.vividus.ui.action.search; -import java.util.stream.Collectors; -import java.util.stream.Stream; - -import org.apache.commons.lang3.StringUtils; -import org.apache.commons.lang3.Validate; import org.vividus.ui.State; -public enum Visibility +public enum Visibility implements ElementVisibility { VISIBLE(State.VISIBLE, "visible"), INVISIBLE(State.NOT_VISIBLE, "invisible"), @@ -50,23 +45,6 @@ public String getDescription() public static Visibility getElementType(String input) { - Validate.isTrue(StringUtils.isNotBlank(input), "Visibility type can not be empty. %s", - getExpectedVisibilityMessage()); - - String inputInUpperCase = input.toUpperCase().trim(); - return Stream.of(values()) - .filter(v -> v.name().startsWith(inputInUpperCase)) - .findFirst() - .orElseThrow(() -> - new IllegalArgumentException( - String.format("Illegal visibility type '%s'. %s", input, getExpectedVisibilityMessage()))); - } - - private static String getExpectedVisibilityMessage() - { - return Stream.of(values()).map(Visibility::name) - .map(String::toLowerCase) - .map(type -> StringUtils.wrap(type, '\'')) - .collect(Collectors.joining(", ", "Expected one of ", "")); + return ElementVisibility.getElementType(input, Visibility.class); } } diff --git a/vividus-extension-ui/src/main/java/org/vividus/ui/action/search/ElementVisibility.java b/vividus-extension-ui/src/main/java/org/vividus/ui/action/search/ElementVisibility.java new file mode 100644 index 0000000000..078434790a --- /dev/null +++ b/vividus-extension-ui/src/main/java/org/vividus/ui/action/search/ElementVisibility.java @@ -0,0 +1,47 @@ +/* + * Copyright 2019-2024 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.vividus.ui.action.search; + +import java.util.stream.Collectors; +import java.util.stream.Stream; + +import org.apache.commons.lang3.StringUtils; +import org.apache.commons.lang3.Validate; + +public interface ElementVisibility +{ + static & ElementVisibility> T getElementType(String input, Class enumClass) + { + Validate.isTrue(StringUtils.isNotBlank(input), "Visibility type can not be empty. %s", + getExpectedVisibilityMessage(enumClass)); + + String inputInUpperCase = input.toUpperCase().trim(); + return Stream.of(enumClass.getEnumConstants()) + .filter(v -> v.name().startsWith(inputInUpperCase)) + .findFirst() + .orElseThrow(() -> new IllegalArgumentException(String.format( + "Illegal visibility type '%s'. %s", input, getExpectedVisibilityMessage(enumClass)))); + } + + private static & ElementVisibility> String getExpectedVisibilityMessage(Class enumClass) + { + return Stream.of(enumClass.getEnumConstants()).map(Enum::name) + .map(String::toLowerCase) + .map(type -> StringUtils.wrap(type, '\'')) + .collect(Collectors.joining(", ", "Expected one of ", "")); + } +} diff --git a/vividus-extension-ui/src/test/java/org/vividus/ui/action/search/ElementVisibilityTests.java b/vividus-extension-ui/src/test/java/org/vividus/ui/action/search/ElementVisibilityTests.java new file mode 100644 index 0000000000..11299397b3 --- /dev/null +++ b/vividus-extension-ui/src/test/java/org/vividus/ui/action/search/ElementVisibilityTests.java @@ -0,0 +1,58 @@ +/* + * Copyright 2019-2024 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.vividus.ui.action.search; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; + +import org.apache.commons.lang3.StringUtils; +import org.junit.jupiter.api.Test; + +class ElementVisibilityTests +{ + enum TestVisibility implements ElementVisibility + { + VISIBLE, HIDDEN, ANY + } + + @Test + void shouldGetElementType() + { + TestVisibility visibility = ElementVisibility.getElementType("visible", TestVisibility.class); + assertEquals(TestVisibility.VISIBLE, visibility); + } + + @Test + void shouldThrowExceptionWhenInputIsEmpty() + { + IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, + () -> ElementVisibility.getElementType(StringUtils.EMPTY, TestVisibility.class)); + String expectedMessage = "Visibility type can not be empty. Expected one of 'visible', 'hidden', 'any'"; + assertEquals(expectedMessage, exception.getMessage()); + } + + @Test + void shouldThrowExceptionWhenIllegalVisibilityType() + { + String illegalVisibilityType = "illegal"; + IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, + () -> ElementVisibility.getElementType(illegalVisibilityType, TestVisibility.class)); + String expectedMessage = String.format( + "Illegal visibility type '%s'. Expected one of 'visible', 'hidden', 'any'", illegalVisibilityType); + assertEquals(expectedMessage, exception.getMessage()); + } +} diff --git a/vividus-plugin-web-app-playwright/src/main/java/org/vividus/ui/web/playwright/UiContext.java b/vividus-plugin-web-app-playwright/src/main/java/org/vividus/ui/web/playwright/UiContext.java index c4254f0c2f..ab09c99cbc 100644 --- a/vividus-plugin-web-app-playwright/src/main/java/org/vividus/ui/web/playwright/UiContext.java +++ b/vividus-plugin-web-app-playwright/src/main/java/org/vividus/ui/web/playwright/UiContext.java @@ -1,5 +1,5 @@ /* - * Copyright 2019-2023 the original author or authors. + * Copyright 2019-2024 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -24,6 +24,7 @@ import org.vividus.testcontext.TestContext; import org.vividus.ui.web.playwright.locator.PlaywrightLocator; +import org.vividus.ui.web.playwright.locator.Visibility; public class UiContext { @@ -57,7 +58,10 @@ public void reset() public Locator locateElement(PlaywrightLocator playwrightLocator) { String locator = playwrightLocator.getLocator(); - return getInCurrentContext(context -> context.locator(locator), page -> page.locator(locator)); + Locator locatorInContext = getInCurrentContext(context -> context.locator(locator), + page -> page.locator(locator)); + return (playwrightLocator.getVisibility() == Visibility.VISIBLE) + ? locatorInContext.locator("visible=true") : locatorInContext; } public Locator getCurrentContexOrPageRoot() diff --git a/vividus-plugin-web-app-playwright/src/main/java/org/vividus/ui/web/playwright/locator/PlaywrightLocator.java b/vividus-plugin-web-app-playwright/src/main/java/org/vividus/ui/web/playwright/locator/PlaywrightLocator.java index 2cc6266f6e..9541747d79 100644 --- a/vividus-plugin-web-app-playwright/src/main/java/org/vividus/ui/web/playwright/locator/PlaywrightLocator.java +++ b/vividus-plugin-web-app-playwright/src/main/java/org/vividus/ui/web/playwright/locator/PlaywrightLocator.java @@ -22,6 +22,7 @@ public class PlaywrightLocator { private final String locatorType; private final String locatorValue; + private Visibility visibility = Visibility.VISIBLE; public PlaywrightLocator(String locatorType, String locatorValue) { @@ -34,6 +35,16 @@ public String getLocator() return locatorType + "=" + locatorValue; } + public Visibility getVisibility() + { + return visibility; + } + + public void setVisibility(Visibility visibility) + { + this.visibility = visibility; + } + @Override public boolean equals(Object o) { @@ -46,18 +57,19 @@ public boolean equals(Object o) return false; } PlaywrightLocator that = (PlaywrightLocator) o; - return Objects.equals(locatorType, that.locatorType) && Objects.equals(locatorValue, that.locatorValue); + return Objects.equals(locatorType, that.locatorType) && Objects.equals(locatorValue, + that.locatorValue) && Objects.equals(visibility, that.visibility); } @Override public int hashCode() { - return Objects.hash(locatorType, locatorValue); + return Objects.hash(locatorType, locatorValue, visibility); } @Override public String toString() { - return locatorType + '(' + locatorValue + ')'; + return locatorType + '(' + locatorValue + ')' + " with visibility: " + visibility.toString().toLowerCase(); } } diff --git a/vividus-plugin-web-app-playwright/src/main/java/org/vividus/ui/web/playwright/locator/PlaywrightLocatorConverter.java b/vividus-plugin-web-app-playwright/src/main/java/org/vividus/ui/web/playwright/locator/PlaywrightLocatorConverter.java index 763b50f17c..41f061fa08 100644 --- a/vividus-plugin-web-app-playwright/src/main/java/org/vividus/ui/web/playwright/locator/PlaywrightLocatorConverter.java +++ b/vividus-plugin-web-app-playwright/src/main/java/org/vividus/ui/web/playwright/locator/PlaywrightLocatorConverter.java @@ -1,5 +1,5 @@ /* - * Copyright 2019-2023 the original author or authors. + * Copyright 2019-2024 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -17,11 +17,21 @@ package org.vividus.ui.web.playwright.locator; import java.util.Set; +import java.util.regex.Matcher; +import java.util.regex.Pattern; import org.vividus.ui.locator.LocatorParser; public final class PlaywrightLocatorConverter { + private static final int ATTRIBUTE_TYPE_GROUP = 1; + private static final int SEARCH_VALUE_GROUP = 2; + private static final int ELEMENT_TYPE_GROUP = 3; + private static final int VISIBILITY_GROUP = 4; + private static final char CLOSING_BRACKET = ']'; + private static final String LOCATOR_FORMAT = "(?:By\\.)?([a-zA-Z-]+)\\((.*)\\)(:(.*))?"; + private static final Pattern LOCATOR_PATTERN = Pattern.compile(LOCATOR_FORMAT); + private PlaywrightLocatorConverter() { } @@ -33,7 +43,22 @@ public static Set convertToLocatorSet(String locatorsAsString public static PlaywrightLocator convertToLocator(String locatorAsString) { - return LocatorParser.parseSingleLocator(locatorAsString, PlaywrightLocatorConverter::convertToLocator); + Matcher matcher = LOCATOR_PATTERN.matcher(locatorAsString); + if (matcher.matches()) + { + String elementActionType = matcher.group(ATTRIBUTE_TYPE_GROUP).toLowerCase(); + String searchValue = matcher.group(SEARCH_VALUE_GROUP); + String elementType = matcher.group(ELEMENT_TYPE_GROUP); + PlaywrightLocator convertedLocator = convertToLocator(elementActionType, searchValue); + if (elementType != null) + { + String visibilityType = matcher.group(VISIBILITY_GROUP); + convertedLocator.setVisibility(Visibility.getElementType(visibilityType)); + } + return convertedLocator; + } + throw new IllegalArgumentException("Invalid locator format. Expected matches [" + LOCATOR_FORMAT + + CLOSING_BRACKET + " Actual: [" + locatorAsString + CLOSING_BRACKET); } private static PlaywrightLocator convertToLocator(String type, String value) diff --git a/vividus-plugin-web-app-playwright/src/main/java/org/vividus/ui/web/playwright/locator/Visibility.java b/vividus-plugin-web-app-playwright/src/main/java/org/vividus/ui/web/playwright/locator/Visibility.java new file mode 100644 index 0000000000..e12a8c5f73 --- /dev/null +++ b/vividus-plugin-web-app-playwright/src/main/java/org/vividus/ui/web/playwright/locator/Visibility.java @@ -0,0 +1,30 @@ +/* + * Copyright 2019-2024 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.vividus.ui.web.playwright.locator; + +import org.vividus.ui.action.search.ElementVisibility; + +public enum Visibility implements ElementVisibility +{ + VISIBLE, + ALL; + + public static Visibility getElementType(String input) + { + return ElementVisibility.getElementType(input, Visibility.class); + } +} diff --git a/vividus-plugin-web-app-playwright/src/test/java/org/vividus/ui/web/playwright/UiContextTests.java b/vividus-plugin-web-app-playwright/src/test/java/org/vividus/ui/web/playwright/UiContextTests.java index 7d6fc712ed..9d0df062f9 100644 --- a/vividus-plugin-web-app-playwright/src/test/java/org/vividus/ui/web/playwright/UiContextTests.java +++ b/vividus-plugin-web-app-playwright/src/test/java/org/vividus/ui/web/playwright/UiContextTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2019-2023 the original author or authors. + * Copyright 2019-2024 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -30,6 +30,7 @@ import org.mockito.junit.jupiter.MockitoExtension; import org.vividus.testcontext.SimpleTestContext; import org.vividus.ui.web.playwright.locator.PlaywrightLocator; +import org.vividus.ui.web.playwright.locator.Visibility; @SuppressWarnings("PMD.CloseResource") @ExtendWith(MockitoExtension.class) @@ -38,6 +39,11 @@ class UiContextTests private static final PlaywrightLocator PLAYWRIGHT_LOCATOR = new PlaywrightLocator("xpath", "//div"); private final UiContext uiContext = new UiContext(new SimpleTestContext()); + static + { + PLAYWRIGHT_LOCATOR.setVisibility(Visibility.ALL); + } + @Test void shouldSetAndGetPage() { @@ -118,4 +124,18 @@ void shouldReturnPageRootWhenNoContextIsSet() Locator actual = uiContext.getCurrentContexOrPageRoot(); assertSame(root, actual); } + + @Test + void shouldLocateVisibleElement() + { + var playwrightLocator = new PlaywrightLocator("css", "div"); + Page page = mock(); + uiContext.setCurrentPage(page); + Locator locator = mock(); + Locator visibleLocator = mock(); + when(page.locator(playwrightLocator.getLocator())).thenReturn(locator); + when(locator.locator("visible=true")).thenReturn(visibleLocator); + Locator actual = uiContext.locateElement(playwrightLocator); + assertSame(visibleLocator, actual); + } } diff --git a/vividus-plugin-web-app-playwright/src/test/java/org/vividus/ui/web/playwright/locator/PlaywrightLocatorConverterTests.java b/vividus-plugin-web-app-playwright/src/test/java/org/vividus/ui/web/playwright/locator/PlaywrightLocatorConverterTests.java new file mode 100644 index 0000000000..bd2007ab01 --- /dev/null +++ b/vividus-plugin-web-app-playwright/src/test/java/org/vividus/ui/web/playwright/locator/PlaywrightLocatorConverterTests.java @@ -0,0 +1,79 @@ +/* + * Copyright 2019-2024 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.vividus.ui.web.playwright.locator; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; + +import java.util.stream.Stream; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; + +public class PlaywrightLocatorConverterTests +{ + static Stream actionAttributeSource() + { + return Stream.of( + Arguments.of(createExpectedLocator(Visibility.ALL), + "By.xpath(//div):all"), + Arguments.of(createExpectedLocator(Visibility.VISIBLE), + "By.xpath(//div):v"), + Arguments.of(createExpectedLocator(Visibility.VISIBLE), + "By.xpath(//div)"), + Arguments.of(createExpectedLocator(Visibility.VISIBLE), + "xpath(//div)")); + } + + @ParameterizedTest + @MethodSource("actionAttributeSource") + void shouldConvertToLocator(PlaywrightLocator expected, String testValue) + { + assertEquals(expected, PlaywrightLocatorConverter.convertToLocator(testValue)); + } + + @Test + void testConvertToLocatorInvalidVisibilityType() + { + IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, + () -> PlaywrightLocatorConverter.convertToLocator("By.css(div):invalid")); + assertEquals("Illegal visibility type 'invalid'. Expected one of 'visible', 'all'", + exception.getMessage()); + } + + @Test + void shouldThrowExceptionWhenInvalidLocatorFormat() + { + String locatorAsString = "InvalidLocatorFormat"; + Exception exception = assertThrows(IllegalArgumentException.class, () -> { + PlaywrightLocatorConverter.convertToLocator(locatorAsString); + }); + String expectedMessage = "Invalid locator format. Expected matches [(?:By\\.)?([a-zA-Z-]+)\\((.*)\\)(:(.*))?]" + + " Actual: [" + locatorAsString + "]"; + String actualMessage = exception.getMessage(); + assertEquals(expectedMessage, actualMessage); + } + + private static PlaywrightLocator createExpectedLocator(Visibility visibility) + { + PlaywrightLocator locator = new PlaywrightLocator("xpath", "//div"); + locator.setVisibility(visibility); + return locator; + } +} diff --git a/vividus-plugin-web-app-playwright/src/test/java/org/vividus/ui/web/playwright/locator/PlaywrightLocatorTests.java b/vividus-plugin-web-app-playwright/src/test/java/org/vividus/ui/web/playwright/locator/PlaywrightLocatorTests.java index 3842fd8017..f4119e9033 100644 --- a/vividus-plugin-web-app-playwright/src/test/java/org/vividus/ui/web/playwright/locator/PlaywrightLocatorTests.java +++ b/vividus-plugin-web-app-playwright/src/test/java/org/vividus/ui/web/playwright/locator/PlaywrightLocatorTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2019-2023 the original author or authors. + * Copyright 2019-2024 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -31,6 +31,14 @@ void shouldGeneratePlaywrightLocator() assertEquals("xpath=//div", locator); } + @Test + void testToString() + { + var locator = new PlaywrightLocator("css", "div"); + locator.setVisibility(Visibility.VISIBLE); + assertEquals("css(div) with visibility: visible", locator.toString()); + } + @Test void testHashCodeAndEquals() { diff --git a/vividus-plugin-web-app-playwright/src/test/java/org/vividus/ui/web/playwright/locator/VisibilityTests.java b/vividus-plugin-web-app-playwright/src/test/java/org/vividus/ui/web/playwright/locator/VisibilityTests.java new file mode 100644 index 0000000000..2876352861 --- /dev/null +++ b/vividus-plugin-web-app-playwright/src/test/java/org/vividus/ui/web/playwright/locator/VisibilityTests.java @@ -0,0 +1,65 @@ +/* + * Copyright 2019-2024 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.vividus.ui.web.playwright.locator; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; + +import java.util.stream.Stream; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; + +class VisibilityTests +{ + @ParameterizedTest + @MethodSource("elementTypeSource") + void shouldParseStringIntoElementType(String toParse, Visibility expected) + { + assertEquals(expected, Visibility.getElementType(toParse)); + } + + @Test + void shouldThrowExceptionInCaseOfInvalidVisibility() + { + IllegalArgumentException illegalArgumentException = assertThrows(IllegalArgumentException.class, + () -> Visibility.getElementType("invalid")); + assertEquals("Illegal visibility type 'invalid'. Expected one of 'visible', 'all'", + illegalArgumentException.getMessage()); + } + + @Test + void shouldThrowExceptionInCaseOfEmptyVisibility() + { + IllegalArgumentException illegalArgumentException = assertThrows(IllegalArgumentException.class, + () -> Visibility.getElementType("")); + assertEquals("Visibility type can not be empty. Expected one of 'visible', 'all'", + illegalArgumentException.getMessage()); + } + + static Stream elementTypeSource() + { + return Stream.of( + Arguments.of("v", Visibility.VISIBLE), + Arguments.of("all", Visibility.ALL), + Arguments.of("ALL", Visibility.ALL), + Arguments.of("aLl", Visibility.ALL), + Arguments.of(" aLl ", Visibility.ALL)); + } +} diff --git a/vividus-plugin-web-app-playwright/src/test/java/org/vividus/ui/web/playwright/steps/ElementStepsTests.java b/vividus-plugin-web-app-playwright/src/test/java/org/vividus/ui/web/playwright/steps/ElementStepsTests.java index 576c048f22..4daa80ae64 100644 --- a/vividus-plugin-web-app-playwright/src/test/java/org/vividus/ui/web/playwright/steps/ElementStepsTests.java +++ b/vividus-plugin-web-app-playwright/src/test/java/org/vividus/ui/web/playwright/steps/ElementStepsTests.java @@ -57,12 +57,14 @@ class ElementStepsTests @InjectMocks private ElementSteps steps; @ParameterizedTest + // CHECKSTYLE:OFF @CsvSource(quoteCharacter = '`', value = { - ".//a, 3, 3, The number of elements found by 'xpath(.//a)' is 3", - ".//div, 4, 3, `The number of elements found by 'xpath(.//div)' is 4, but 3 were expected`", - "/h1, 3, 1, `The number of elements found by 'xpath(/h1)' is 3, but 1 was expected`", - "/h2, 1, 0, `The number of elements found by 'xpath(/h2)' is 1, but 0 were expected`" + ".//a, 3, 3, The number of elements found by 'xpath(.//a) with visibility: visible' is 3", + ".//div, 4, 3, `The number of elements found by 'xpath(.//div) with visibility: visible' is 4, but 3 were expected`", + "/h1, 3, 1, `The number of elements found by 'xpath(/h1) with visibility: visible' is 3, but 1 was expected`", + "/h2, 1, 0, `The number of elements found by 'xpath(/h2) with visibility: visible' is 1, but 0 were expected`" }) + // CHECKSTYLE:ON void shouldRecordAssertionOnValidationIfNumberOfElementIsEqualToExpected(String locatorValue, int actualNumber, int expectedNumber, String assertionMessage) { @@ -75,10 +77,12 @@ void shouldRecordAssertionOnValidationIfNumberOfElementIsEqualToExpected(String } @ParameterizedTest + // CHECKSTYLE:OFF @CsvSource(quoteCharacter = '`', value = { - "a, 3, 2, true, `The number of elements found by 'css(a)' is 3, it is greater than 2`", - "div, 3, 4, false, `The number of elements found by 'css(div)' is 3, but it is not greater than 4`" + "a, 3, 2, true, `The number of elements found by 'css(a) with visibility: visible' is 3, it is greater than 2`", + "div, 3, 4, false, `The number of elements found by 'css(div) with visibility: visible' is 3, but it is not greater than 4`" }) + // CHECKSTYLE:ON void shouldRecordAssertionOnValidationIfNumberOfElementMatchesToExpected(String locatorValue, int actualNumber, int expectedNumber, boolean passed, String assertionMessage) { diff --git a/vividus-plugin-web-app-playwright/src/test/java/org/vividus/ui/web/playwright/steps/WaitStepsTests.java b/vividus-plugin-web-app-playwright/src/test/java/org/vividus/ui/web/playwright/steps/WaitStepsTests.java index 144c7aae57..695befe5bf 100644 --- a/vividus-plugin-web-app-playwright/src/test/java/org/vividus/ui/web/playwright/steps/WaitStepsTests.java +++ b/vividus-plugin-web-app-playwright/src/test/java/org/vividus/ui/web/playwright/steps/WaitStepsTests.java @@ -86,7 +86,8 @@ void shouldWaitForNumberOfElements() BooleanSupplier condition = conditionCaptor.getValue(); Assertions.assertTrue(condition.getAsBoolean()); verify(softAssert).recordPassedAssertion( - "Passed wait condition: number of elements located by 'css(div)' to be equal to 5"); + "Passed wait condition: number of elements located by 'css(div) with visibility: visible'" + + " to be equal to 5"); verifyNoMoreInteractions(softAssert); } @@ -112,8 +113,8 @@ void shouldRecordFailedAssertionOnWaitNumberOfElementsTimeout() doThrow(timeoutError).when(context).waitForCondition(any(BooleanSupplier.class)); waitSteps.waitForElementNumber(playwrightLocator, ComparisonRule.EQUAL_TO, 3); verify(softAssert).recordFailedAssertion( - "Failed wait condition: number of elements located by 'css(div)' to be equal to 3. " - + TIMEOUT_ERROR_MESSAGE, timeoutError); + "Failed wait condition: number of elements located by 'css(div) with visibility: visible'" + + " to be equal to 3. " + TIMEOUT_ERROR_MESSAGE, timeoutError); verifyNoMoreInteractions(softAssert); } @@ -122,7 +123,8 @@ private void testWaitForElementStateStep(Consumer step, WaitF when(uiContext.locateElement(playwrightLocator)).thenReturn(locator); step.accept(playwrightLocator); verify(locator).waitFor(argThat(arg -> arg.state == selectorState)); - String assertionMessage = String.format("Passed wait condition: element located by 'css(div)' to be %s", + String assertionMessage = String.format( + "Passed wait condition: element located by 'css(div) with visibility: visible' to be %s", selectorState.toString().toLowerCase()); verify(softAssert).recordPassedAssertion(assertionMessage); verifyNoMoreInteractions(softAssert); @@ -137,7 +139,8 @@ private void testStepRecordFailedAssertionOnElementStateTimeout(Consumer arg.state == selectorState)); step.accept(playwrightLocator); - String assertionMessage = String.format("Failed wait condition: element located by 'css(div)' to be %s. %s", + String assertionMessage = String.format( + "Failed wait condition: element located by 'css(div) with visibility: visible' to be %s. %s", selectorState.toString().toLowerCase(), TIMEOUT_ERROR_MESSAGE); verify(softAssert).recordFailedAssertion(assertionMessage, timeoutError); }