Skip to content

Commit

Permalink
[plugin-web-app] Fix field clearing using keyboard on MacOS (#3724)
Browse files Browse the repository at this point in the history
  • Loading branch information
valfirst authored Mar 17, 2023
1 parent 8fa4bb1 commit 21b93fc
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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, () ->
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -38,5 +38,7 @@ public interface IGenericWebDriverManager

boolean isAndroid();

boolean isMacOs();

Capabilities getCapabilities();
}
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -146,7 +146,7 @@ void testPerformActionInNativeContextSwitchNotNeeded()
verify(contextSwitchingDriver, never()).getContextHandles();
}

static Stream<Arguments> mobileArguments()
static Stream<Arguments> platformsData()
{
// CHECKSTYLE:OFF
return Stream.of(
Expand All @@ -167,13 +167,19 @@ static Stream<Arguments> mobileArguments()
arguments((Predicate<GenericWebDriverManager>) GenericWebDriverManager::isTvOS, MobilePlatform.TVOS, true),
arguments((Predicate<GenericWebDriverManager>) GenericWebDriverManager::isTvOS, MobilePlatform.IOS, false),
arguments((Predicate<GenericWebDriverManager>) GenericWebDriverManager::isTvOS, Platform.IOS, false),
arguments((Predicate<GenericWebDriverManager>) GenericWebDriverManager::isIOS, null, false)
arguments((Predicate<GenericWebDriverManager>) GenericWebDriverManager::isIOS, null, false),
arguments((Predicate<GenericWebDriverManager>) GenericWebDriverManager::isMacOs, Platform.MAC, true),
arguments((Predicate<GenericWebDriverManager>) GenericWebDriverManager::isMacOs, Platform.WINDOWS, false),
arguments((Predicate<GenericWebDriverManager>) GenericWebDriverManager::isMacOs, Platform.LINUX, false),
arguments((Predicate<GenericWebDriverManager>) GenericWebDriverManager::isMacOs, Platform.UNIX, false),
arguments((Predicate<GenericWebDriverManager>) GenericWebDriverManager::isMacOs, Platform.IOS, false),
arguments((Predicate<GenericWebDriverManager>) GenericWebDriverManager::isMacOs, null, false)
);
// CHECKSTYLE:ON
}

@ParameterizedTest
@MethodSource("mobileArguments")
@MethodSource("platformsData")
void testIsPlatform(Predicate<GenericWebDriverManager> test, Object platform, boolean expected)
{
mockWebDriver(platform);
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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()))))
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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;
Expand Down Expand Up @@ -100,7 +103,10 @@ public void clearFieldUsingKeyboard(WebElement field)
{
if (field != null)
{
field.sendKeys(Keys.chord(Keys.CONTROL, "a") + Keys.BACK_SPACE);
Entry<Keys, String> 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);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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";
Expand All @@ -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()
{
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 21b93fc

Please sign in to comment.