From 2b130112f993da9387782aaae9a85df78a059527 Mon Sep 17 00:00:00 2001 From: Valery Yatsynovich Date: Wed, 15 Mar 2023 17:21:16 +0300 Subject: [PATCH] [plugin-web-app] Fix field clearing using keyboard on MacOS --- .../manager/GenericWebDriverManager.java | 8 +++++- .../manager/IGenericWebDriverManager.java | 4 ++- .../manager/GenericWebDriverManagerTests.java | 14 +++++++--- .../vividus/excel/ExcelSheetWriterTests.java | 4 +-- .../vividus/ui/web/action/FieldActions.java | 10 +++++-- .../ui/web/action/FieldActionsTests.java | 27 +++++++++++++++---- 6 files changed, 52 insertions(+), 15 deletions(-) diff --git a/vividus-extension-selenium/src/main/java/org/vividus/selenium/manager/GenericWebDriverManager.java b/vividus-extension-selenium/src/main/java/org/vividus/selenium/manager/GenericWebDriverManager.java index 39605d7be7..a10b4c36fd 100644 --- a/vividus-extension-selenium/src/main/java/org/vividus/selenium/manager/GenericWebDriverManager.java +++ b/vividus-extension-selenium/src/main/java/org/vividus/selenium/manager/GenericWebDriverManager.java @@ -1,5 +1,5 @@ /* - * Copyright 2019-2022 the original author or authors. + * Copyright 2019-2023 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. @@ -147,6 +147,12 @@ public static boolean isAndroid(Capabilities capabilities) return isPlatformName(capabilities, MobilePlatform.ANDROID); } + @Override + public boolean isMacOs() + { + return isPlatformName(getCapabilities(), "MacOS"); + } + private static boolean isPlatformName(Capabilities capabilities, String platformName) { return checkCapabilities(capabilities, () -> diff --git a/vividus-extension-selenium/src/main/java/org/vividus/selenium/manager/IGenericWebDriverManager.java b/vividus-extension-selenium/src/main/java/org/vividus/selenium/manager/IGenericWebDriverManager.java index 3431a007d1..d01ea7d6b3 100644 --- a/vividus-extension-selenium/src/main/java/org/vividus/selenium/manager/IGenericWebDriverManager.java +++ b/vividus-extension-selenium/src/main/java/org/vividus/selenium/manager/IGenericWebDriverManager.java @@ -1,5 +1,5 @@ /* - * Copyright 2019-2022 the original author or authors. + * Copyright 2019-2023 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. @@ -38,5 +38,7 @@ public interface IGenericWebDriverManager boolean isAndroid(); + boolean isMacOs(); + Capabilities getCapabilities(); } diff --git a/vividus-extension-selenium/src/test/java/org/vividus/selenium/manager/GenericWebDriverManagerTests.java b/vividus-extension-selenium/src/test/java/org/vividus/selenium/manager/GenericWebDriverManagerTests.java index 07bc279bb4..a8787687c2 100644 --- a/vividus-extension-selenium/src/test/java/org/vividus/selenium/manager/GenericWebDriverManagerTests.java +++ b/vividus-extension-selenium/src/test/java/org/vividus/selenium/manager/GenericWebDriverManagerTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2019-2022 the original author or authors. + * Copyright 2019-2023 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. @@ -146,7 +146,7 @@ void testPerformActionInNativeContextSwitchNotNeeded() verify(contextSwitchingDriver, never()).getContextHandles(); } - static Stream mobileArguments() + static Stream platformsData() { // CHECKSTYLE:OFF return Stream.of( @@ -167,13 +167,19 @@ static Stream mobileArguments() arguments((Predicate) GenericWebDriverManager::isTvOS, MobilePlatform.TVOS, true), arguments((Predicate) GenericWebDriverManager::isTvOS, MobilePlatform.IOS, false), arguments((Predicate) GenericWebDriverManager::isTvOS, Platform.IOS, false), - arguments((Predicate) GenericWebDriverManager::isIOS, null, false) + arguments((Predicate) GenericWebDriverManager::isIOS, null, false), + arguments((Predicate) GenericWebDriverManager::isMacOs, Platform.MAC, true), + arguments((Predicate) GenericWebDriverManager::isMacOs, Platform.WINDOWS, false), + arguments((Predicate) GenericWebDriverManager::isMacOs, Platform.LINUX, false), + arguments((Predicate) GenericWebDriverManager::isMacOs, Platform.UNIX, false), + arguments((Predicate) GenericWebDriverManager::isMacOs, Platform.IOS, false), + arguments((Predicate) GenericWebDriverManager::isMacOs, null, false) ); // CHECKSTYLE:ON } @ParameterizedTest - @MethodSource("mobileArguments") + @MethodSource("platformsData") void testIsPlatform(Predicate test, Object platform, boolean expected) { mockWebDriver(platform); diff --git a/vividus-plugin-excel/src/test/java/org/vividus/excel/ExcelSheetWriterTests.java b/vividus-plugin-excel/src/test/java/org/vividus/excel/ExcelSheetWriterTests.java index c9542203f7..1e6847ac81 100644 --- a/vividus-plugin-excel/src/test/java/org/vividus/excel/ExcelSheetWriterTests.java +++ b/vividus-plugin-excel/src/test/java/org/vividus/excel/ExcelSheetWriterTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2019-2022 the original author or authors. + * Copyright 2019-2023 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. @@ -76,7 +76,7 @@ private Path createExcelFile() throws IOException return ResourceUtils.createTempFile("test", ".xlsx", null); } - @SuppressWarnings("checkstyle:MultipleStringLiteralsExtended") + @SuppressWarnings({ "checkstyle:MultipleStringLiterals", "checkstyle:MultipleStringLiteralsExtended" }) private void assertDataInSheet(Path path, int index, String name) throws IOException { try (XSSFWorkbook myExcelBook = new XSSFWorkbook(FileUtils.openInputStream(new File(path.toString())))) diff --git a/vividus-plugin-web-app/src/main/java/org/vividus/ui/web/action/FieldActions.java b/vividus-plugin-web-app/src/main/java/org/vividus/ui/web/action/FieldActions.java index 524ae991b7..a2ae2d2342 100644 --- a/vividus-plugin-web-app/src/main/java/org/vividus/ui/web/action/FieldActions.java +++ b/vividus-plugin-web-app/src/main/java/org/vividus/ui/web/action/FieldActions.java @@ -1,5 +1,5 @@ /* - * Copyright 2019-2022 the original author or authors. + * Copyright 2019-2023 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,7 +16,10 @@ package org.vividus.ui.web.action; +import static java.util.Map.entry; + import java.util.List; +import java.util.Map.Entry; import org.openqa.selenium.By; import org.openqa.selenium.Keys; @@ -100,7 +103,10 @@ public void clearFieldUsingKeyboard(WebElement field) { if (field != null) { - field.sendKeys(Keys.chord(Keys.CONTROL, "a") + Keys.BACK_SPACE); + Entry controllingKey = webDriverManager.isMacOs() ? entry(Keys.COMMAND, "Cmd") + : entry(Keys.CONTROL, "Ctrl"); + LOGGER.info("Attempting to clear field with [{} + A, Backspace] keys sequence", controllingKey.getValue()); + field.sendKeys(Keys.chord(controllingKey.getKey(), "a") + Keys.BACK_SPACE); } } diff --git a/vividus-plugin-web-app/src/test/java/org/vividus/ui/web/action/FieldActionsTests.java b/vividus-plugin-web-app/src/test/java/org/vividus/ui/web/action/FieldActionsTests.java index bdd806cfcb..48947abdcb 100644 --- a/vividus-plugin-web-app/src/test/java/org/vividus/ui/web/action/FieldActionsTests.java +++ b/vividus-plugin-web-app/src/test/java/org/vividus/ui/web/action/FieldActionsTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2019-2022 the original author or authors. + * Copyright 2019-2023 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,6 +16,9 @@ package org.vividus.ui.web.action; +import static com.github.valfirst.slf4jtest.LoggingEvent.info; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.is; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.mockito.Mockito.inOrder; import static org.mockito.Mockito.mock; @@ -27,9 +30,14 @@ import java.util.List; +import com.github.valfirst.slf4jtest.TestLogger; +import com.github.valfirst.slf4jtest.TestLoggerFactory; +import com.github.valfirst.slf4jtest.TestLoggerFactoryExtension; + import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; import org.junit.jupiter.params.provider.ValueSource; import org.mockito.InOrder; import org.mockito.InjectMocks; @@ -45,7 +53,7 @@ import org.vividus.softassert.ISoftAssert; import org.vividus.ui.web.util.FormatUtils; -@ExtendWith(MockitoExtension.class) +@ExtendWith({ MockitoExtension.class, TestLoggerFactoryExtension.class }) class FieldActionsTests { private static final String INDEX = "index"; @@ -69,6 +77,8 @@ class FieldActionsTests @Mock private IWebWaitActions waitActions; @InjectMocks private FieldActions fieldActions; + private final TestLogger logger = TestLoggerFactory.getTestLogger(FieldActions.class); + @Test void testSelectItemInDDLSelectPresentOptionExist() { @@ -167,11 +177,18 @@ void testSelectItemInDDLSingleSelectAdditable() .recordFailedAssertion("Multiple selecting is not available to single select drop down"); } - @Test - void testClearFieldUsingKeyboard() + @ParameterizedTest + @CsvSource({ + "false, CONTROL, Ctrl", + "true, COMMAND, Cmd" + }) + void testClearFieldUsingKeyboard(boolean macOs, Keys controllingKey, String controllingKeyName) { + when(webDriverManager.isMacOs()).thenReturn(macOs); fieldActions.clearFieldUsingKeyboard(webElement); - verify(webElement).sendKeys(Keys.chord(Keys.CONTROL, "a") + Keys.BACK_SPACE); + verify(webElement).sendKeys(Keys.chord(controllingKey, "a") + Keys.BACK_SPACE); + assertThat(logger.getLoggingEvents(), is(List.of( + info("Attempting to clear field with [{} + A, Backspace] keys sequence", controllingKeyName)))); } @Test