Skip to content

Commit

Permalink
[plugin-mobile-app] Add press key step
Browse files Browse the repository at this point in the history
  • Loading branch information
uarlouski committed Mar 23, 2021
1 parent a6585f2 commit 9e64100
Show file tree
Hide file tree
Showing 4 changed files with 128 additions and 9 deletions.
17 changes: 17 additions & 0 deletions docs/modules/plugins/pages/plugin-mobile-app.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -618,4 +618,21 @@ When I switch to native context
Then number of elements found by `xpath(html)` is equal to `0`
----

=== Press key

Presses an https://github.com/appium/appium-xcuitest-driver#mobile-pressbutton[iOS key] or https://appium.github.io/java-client/io/appium/java_client/android/nativekey/AndroidKey.html[Android key]

[source,gherkin]
----
When I press $key key
----

. `$key` - The key to press

.Go to home
[source,gherkin]
----
When I press Home key
----

include::partial$generic-ui-steps.adoc[]
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2019-2020 the original author or authors.
* Copyright 2019-2021 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 @@ -17,24 +17,42 @@
package org.vividus.bdd.mobileapp.steps;

import java.nio.file.Paths;
import java.util.Map;

import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.EnumUtils;
import org.apache.commons.lang3.Validate;
import org.jbehave.core.annotations.When;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.vividus.mobileapp.action.DeviceActions;
import org.vividus.selenium.IWebDriverProvider;
import org.vividus.selenium.manager.GenericWebDriverManager;
import org.vividus.ui.action.JavascriptActions;

import io.appium.java_client.android.nativekey.AndroidKey;
import io.appium.java_client.android.nativekey.KeyEvent;
import io.appium.java_client.android.nativekey.PressesKey;

public class DeviceSteps
{
private static final Logger LOGGER = LoggerFactory.getLogger(DeviceSteps.class);

private final DeviceActions deviceActions;
private final String folderForFileUpload;
private final GenericWebDriverManager genericWebDriverManager;
private final JavascriptActions javascriptActions;
private final IWebDriverProvider webDriverProvider;

public DeviceSteps(String folderForFileUpload, DeviceActions deviceActions)
public DeviceSteps(String folderForFileUpload, DeviceActions deviceActions,
GenericWebDriverManager genericWebDriverManager, JavascriptActions javascriptActions,
IWebDriverProvider webDriverProvider)
{
this.folderForFileUpload = folderForFileUpload;
this.deviceActions = deviceActions;
this.genericWebDriverManager = genericWebDriverManager;
this.javascriptActions = javascriptActions;
this.webDriverProvider = webDriverProvider;
}

/**
Expand All @@ -51,4 +69,28 @@ public void uploadFileToDevice(String filePath)
String fileName = FilenameUtils.getName(filePath);
deviceActions.pushFile(Paths.get(folderForFileUpload, fileName).toString(), filePath);
}

/**
* Presses the key
* <br>
* See <a href="https://github.com/appium/appium-xcuitest-driver#mobile-pressbutton">iOS keys</a> and
* <a href="https://appium.github.io/java-client/io/appium/java_client/android/nativekey/AndroidKey.html">
* Android keys</a> for available values
*
* @param key key to press
*/
@When("I press $key key")
public void pressKey(String key)
{
if (genericWebDriverManager.isIOSNativeApp())
{
javascriptActions.executeScript("mobile: pressButton", Map.of("name", key));
}
else
{
AndroidKey androidKey = EnumUtils.getEnumIgnoreCase(AndroidKey.class, key);
Validate.isTrue(androidKey != null, "Unsupported Android key: %s", key);
webDriverProvider.getUnwrapped(PressesKey.class).pressKey(new KeyEvent(androidKey));
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2019-2020 the original author or authors.
* Copyright 2019-2021 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 @@ -19,10 +19,17 @@
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.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoInteractions;
import static org.mockito.Mockito.verifyNoMoreInteractions;
import static org.mockito.Mockito.when;

import java.nio.file.Paths;
import java.util.List;
import java.util.Map;

import com.github.valfirst.slf4jtest.TestLogger;
import com.github.valfirst.slf4jtest.TestLoggerFactory;
Expand All @@ -31,11 +38,18 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.vividus.mobileapp.action.DeviceActions;
import org.vividus.selenium.IWebDriverProvider;
import org.vividus.selenium.manager.GenericWebDriverManager;
import org.vividus.ui.action.JavascriptActions;

import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import io.appium.java_client.android.nativekey.AndroidKey;
import io.appium.java_client.android.nativekey.KeyEvent;
import io.appium.java_client.android.nativekey.PressesKey;

@SuppressFBWarnings("DMI_HARDCODED_ABSOLUTE_FILENAME")
@ExtendWith({ MockitoExtension.class, TestLoggerFactoryExtension.class })
Expand All @@ -44,14 +58,18 @@ class DeviceStepsTests
private static final String DEVICE_FOLDER = "/device/folder";

@Mock private DeviceActions deviceActions;
@Mock private GenericWebDriverManager genericWebDriverManager;
@Mock private JavascriptActions javascriptActions;
@Mock private IWebDriverProvider webDriverProvider;
private DeviceSteps deviceSteps;

private final TestLogger logger = TestLoggerFactory.getTestLogger(DeviceSteps.class);

@BeforeEach
void init()
{
deviceSteps = new DeviceSteps(DEVICE_FOLDER, deviceActions);
deviceSteps = new DeviceSteps(DEVICE_FOLDER, deviceActions, genericWebDriverManager, javascriptActions,
webDriverProvider);
}

@Test
Expand All @@ -67,4 +85,43 @@ void shoulUploadFileToDevice()
info("Uploading file '{}' to a device at '{}' folder", filePath, DEVICE_FOLDER)
)));
}

@Test
void shouldPressIOSKey()
{
String key = "Home";

when(genericWebDriverManager.isIOSNativeApp()).thenReturn(true);

deviceSteps.pressKey(key);

verify(javascriptActions).executeScript("mobile: pressButton", Map.of("name", key));
verifyNoInteractions(webDriverProvider);
}

@Test
void shouldPressAndroidKey()
{
ArgumentCaptor<KeyEvent> keyCaptor = ArgumentCaptor.forClass(KeyEvent.class);
PressesKey pressesKey = mock(PressesKey.class);
when(genericWebDriverManager.isIOSNativeApp()).thenReturn(false);
when(webDriverProvider.getUnwrapped(PressesKey.class)).thenReturn(pressesKey);

deviceSteps.pressKey(AndroidKey.SPACE.name());

verify(pressesKey).pressKey(keyCaptor.capture());

assertEquals(Map.of("keycode", 62), keyCaptor.getValue().build());
verifyNoMoreInteractions(webDriverProvider, genericWebDriverManager);
}

@Test
void shouldNotPressUnsupportedAndroidKey()
{
when(genericWebDriverManager.isIOSNativeApp()).thenReturn(false);
IllegalArgumentException exception = assertThrows(IllegalArgumentException.class,
() -> deviceSteps.pressKey("unsupported key"));
assertEquals("Unsupported Android key: unsupported key", exception.getMessage());
verifyNoMoreInteractions(webDriverProvider, genericWebDriverManager);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,14 @@ Given I start mobile application with capabilities:
|app |${app-url}|


Scenario: Verify steps: 'When I press $key key', 'Then number of $state elements found by `$locator` is $comparisonRule `$quantity`' and Text Part Filter and Text Filter
Then number of elements found by `xpath(<textElementXpath>)->filter.text(Home)` is equal to `1`
When I press Home key
Then number of elements found by `xpath(<textElementXpath>)->filter.text(Home)` is equal to `0`
When I activate application with bundle identifier `${main-app}`
When I wait until element located `xpath(<textElementXpath>)->filter.textPart(om)` appears


Scenario: [Android] Verify step: 'When I change Appium session settings:$settings' and Id Locator
Meta:
@targetPlatform android
Expand Down Expand Up @@ -44,11 +52,6 @@ When I change Appium session settings:
Then number of elements found by `xpath(<menuButtonXpath>):a` is equal to `1`


Scenario: Verify step: 'Then number of $state elements found by `$locator` is $comparisonRule `$quantity`' and Text Part Filter and Text Filter
Then number of elements found by `xpath(<textElementXpath>)->filter.textPart(om)` is equal to `1`
Then number of elements found by `xpath(<textElementXpath>)->filter.text(Home)` is equal to `1`


Scenario: Verify step: 'Then number of $state elements found by `$locator` is $comparisonRule `$quantity`' and Accessibility Id Locator
Then number of VISIBLE elements found by `accessibilityId(<togglerAccessibilityId>):a` is equal to `1`

Expand Down

0 comments on commit 9e64100

Please sign in to comment.