-
-
Notifications
You must be signed in to change notification settings - Fork 84
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[plugin-web-app-playwright] Add support of visibility attribute in lo…
…cators (#5126)
- Loading branch information
Showing
14 changed files
with
410 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
vividus-extension-ui/src/main/java/org/vividus/ui/action/search/ElementVisibility.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 <T extends Enum<T> & ElementVisibility> T getElementType(String input, Class<T> 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 <T extends Enum<T> & ElementVisibility> String getExpectedVisibilityMessage(Class<T> enumClass) | ||
{ | ||
return Stream.of(enumClass.getEnumConstants()).map(Enum::name) | ||
.map(String::toLowerCase) | ||
.map(type -> StringUtils.wrap(type, '\'')) | ||
.collect(Collectors.joining(", ", "Expected one of ", "")); | ||
} | ||
} |
58 changes: 58 additions & 0 deletions
58
vividus-extension-ui/src/test/java/org/vividus/ui/action/search/ElementVisibilityTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
...in-web-app-playwright/src/main/java/org/vividus/ui/web/playwright/locator/Visibility.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |
Oops, something went wrong.