Skip to content

Commit

Permalink
[plugin-mobile-app] Add step to press sequence of keys
Browse files Browse the repository at this point in the history
  • Loading branch information
uarlouski committed Mar 30, 2021
1 parent fbdcc40 commit 0aff08f
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 7 deletions.
23 changes: 23 additions & 0 deletions docs/modules/plugins/pages/plugin-mobile-app.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -635,4 +635,27 @@ When I press $key key
When I press Home key
----

=== Press keys

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

[source,gherkin]
----
When I press keys:$keys
----

. `$keys` - The keys to press

.Enter password
[source,gherkin]
----
When I press keys:
|key|
|P |
|a |
|s |
|1 |
|$ |
----

include::partial$generic-ui-steps.adoc[]
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@
package org.vividus.bdd.mobileapp.steps;

import java.nio.file.Paths;
import java.util.List;
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.jbehave.core.model.ExamplesTable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.vividus.mobileapp.action.DeviceActions;
Expand Down Expand Up @@ -76,21 +78,64 @@ public void uploadFileToDevice(String filePath)
* 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
* <br>
* Example:
* <br>
* <code>
* When I press $key key
* </code>
*
* @param key key to press
* @param key the key to press
*/
@When("I press $key key")
public void pressKey(String key)
{
pressKeys(List.of(key));
}

/**
* Presses the keys
* <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
* <br>
* Example:
* <br>
* <code>
* When I press keys:
* <br>
* |key |
* <br>
* |Home|
* </code>
*
* @param keys the keys to press
*/
@When("I press keys:$keys")
public void pressKeys(ExamplesTable keys)
{
pressKeys(keys.getColumn("key"));
}

private void pressKeys(List<String> keys)
{
if (genericWebDriverManager.isIOSNativeApp())
{
javascriptActions.executeScript("mobile: pressButton", Map.of("name", key));
keys.forEach(key -> 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));
PressesKey pressesKey = webDriverProvider.getUnwrapped(PressesKey.class);
keys.stream()
.map(key ->
{
AndroidKey androidKey = EnumUtils.getEnumIgnoreCase(AndroidKey.class, key);
Validate.isTrue(androidKey != null, "Unsupported Android key: %s", key);
return androidKey;
})
.map(KeyEvent::new)
.forEach(pressesKey::pressKey);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import com.github.valfirst.slf4jtest.TestLoggerFactory;
import com.github.valfirst.slf4jtest.TestLoggerFactoryExtension;

import org.jbehave.core.model.ExamplesTable;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
Expand Down Expand Up @@ -101,13 +102,24 @@ void shouldPressIOSKey()

@Test
void shouldPressAndroidKey()
{
performPressAndroidKeyTest(() -> deviceSteps.pressKey(AndroidKey.SPACE.name()));
}

@Test
void shouldPressAndroidKeys()
{
performPressAndroidKeyTest(() -> deviceSteps.pressKeys(new ExamplesTable("|key|\n|SPACE|")));
}

private void performPressAndroidKeyTest(Runnable run)
{
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());
run.run();

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

Expand All @@ -118,10 +130,13 @@ void shouldPressAndroidKey()
@Test
void shouldNotPressUnsupportedAndroidKey()
{
PressesKey pressesKey = mock(PressesKey.class);
when(genericWebDriverManager.isIOSNativeApp()).thenReturn(false);
when(webDriverProvider.getUnwrapped(PressesKey.class)).thenReturn(pressesKey);
IllegalArgumentException exception = assertThrows(IllegalArgumentException.class,
() -> deviceSteps.pressKey("unsupported key"));
assertEquals("Unsupported Android key: unsupported key", exception.getMessage());
verifyNoMoreInteractions(webDriverProvider, genericWebDriverManager);
verifyNoInteractions(pressesKey);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,18 @@ 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
Scenario: Verify steps: 'When I press $key key', 'Then number of $state elements found by `$locator` is $comparisonRule `$quantity`', 'When I press keys:$keys' 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
When I press keys:
|key |
|Home|
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
Expand Down

0 comments on commit 0aff08f

Please sign in to comment.