Skip to content

Commit 3f07e40

Browse files
committed
add finger print command to android Driver
1 parent ffc36db commit 3f07e40

File tree

4 files changed

+145
-0
lines changed

4 files changed

+145
-0
lines changed

src/main/java/io/appium/java_client/MobileCommand.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ public class MobileCommand {
6262
protected static final String IS_KEYBOARD_SHOWN = "isKeyboardShown";
6363
protected static final String IS_LOCKED = "isLocked";
6464
protected static final String LONG_PRESS_KEY_CODE = "longPressKeyCode";
65+
protected static final String FINGER_PRINT = "fingerPrint";
6566
protected static final String OPEN_NOTIFICATIONS = "openNotifications";
6667
protected static final String PRESS_KEY_CODE = "pressKeyCode";
6768
protected static final String PUSH_FILE = "pushFile";
@@ -112,6 +113,7 @@ private static Map<String, CommandInfo> createCommandRepository() {
112113
result.put(IS_LOCKED, postC("/session/:sessionId/appium/device/is_locked"));
113114
result.put(LONG_PRESS_KEY_CODE,
114115
postC("/session/:sessionId/appium/device/long_press_keycode"));
116+
result.put(FINGER_PRINT, postC("/session/:sessionId/appium/device/finger_print"));
115117
result.put(OPEN_NOTIFICATIONS,
116118
postC("/session/:sessionId/appium/device/open_notifications"));
117119
result.put(PRESS_KEY_CODE,

src/main/java/io/appium/java_client/PressesKeyCode.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import static io.appium.java_client.MobileCommand.longPressKeyCodeCommand;
2020
import static io.appium.java_client.MobileCommand.pressKeyCodeCommand;
21+
import static io.appium.java_client.android.AndroidMobileCommandHelper.fingerPrintCommand;
2122

2223
public interface PressesKeyCode extends ExecutesMethod {
2324

@@ -41,6 +42,15 @@ default void pressKeyCode(int key, Integer metastate) {
4142
CommandExecutionHelper.execute(this, pressKeyCodeCommand(key, metastate));
4243
}
4344

45+
/**
46+
* Authenticate users by using their finger print scans on supported emulators.
47+
*
48+
* @param fingerPrintId finger prints stored in Android Keystore system (from 1 to 10)
49+
*/
50+
default void fingerPrint(int fingerPrintId) {
51+
CommandExecutionHelper.execute(this, fingerPrintCommand(fingerPrintId));
52+
}
53+
4454
/**
4555
* Send a long key event to the device.
4656
*

src/main/java/io/appium/java_client/android/AndroidMobileCommandHelper.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,19 @@ public class AndroidMobileCommandHelper extends MobileCommand {
166166
LONG_PRESS_KEY_CODE, prepareArguments(parameters, values));
167167
}
168168

169+
/**
170+
* This method forms a {@link java.util.Map} of parameters for the
171+
* finger print authentication invocation.
172+
*
173+
* @param fingerPrintId finger prints stored in Android Keystore system (from 1 to 10)
174+
* @return a key-value pair. The key is the command name. The value is a
175+
* {@link java.util.Map} command arguments.
176+
*/
177+
public static Map.Entry<String, Map<String, ?>> fingerPrintCommand(int fingerPrintId) {
178+
return new AbstractMap.SimpleEntry<>(FINGER_PRINT,
179+
prepareArguments("fingerprintId", fingerPrintId));
180+
}
181+
169182
/**
170183
* This method forms a {@link java.util.Map} of parameters for the
171184
* notification opening.
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
/*
2+
* Licensed under the Apache License, Version 2.0 (the "License");
3+
* you may not use this file except in compliance with the License.
4+
* See the NOTICE file distributed with this work for additional
5+
* information regarding copyright ownership.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.appium.java_client.android;
18+
19+
import io.appium.java_client.remote.MobileCapabilityType;
20+
import io.appium.java_client.service.local.AppiumDriverLocalService;
21+
import org.junit.After;
22+
import org.junit.AfterClass;
23+
import org.junit.Assert;
24+
import org.junit.Before;
25+
import org.junit.BeforeClass;
26+
import org.junit.Test;
27+
import org.openqa.selenium.By;
28+
import org.openqa.selenium.remote.DesiredCapabilities;
29+
30+
import java.util.concurrent.TimeUnit;
31+
32+
public class FingerPrintTest {
33+
private static AppiumDriverLocalService service;
34+
35+
/**
36+
* initialization.
37+
*/
38+
@BeforeClass public static void beforeClass() throws Exception {
39+
service = AppiumDriverLocalService.buildDefaultService();
40+
service.start();
41+
42+
if (service == null || !service.isRunning()) {
43+
throw new ExceptionInInitializerError("An appium server node is not started!");
44+
}
45+
}
46+
47+
/**
48+
* finishing.
49+
*/
50+
@AfterClass public static void afterClass() {
51+
if (service != null) {
52+
service.stop();
53+
}
54+
}
55+
56+
/**
57+
* enable system security which is required for finger print activation.
58+
*/
59+
@Before public void before() throws Exception {
60+
final AndroidDriver driver = getAndroidDriver("ChooseLockGeneric");
61+
TimeUnit.SECONDS.sleep(2);
62+
// clicking the pin lock mode
63+
driver.findElement(By.xpath("//android.widget.ListView[1]/android.widget.LinearLayout[4]"))
64+
.click();
65+
TimeUnit.SECONDS.sleep(2);
66+
driver.findElement(By.id("com.android.settings:id/password_entry")).sendKeys("1234\n");
67+
TimeUnit.SECONDS.sleep(2);
68+
driver.findElement(By.id("com.android.settings:id/password_entry")).sendKeys("1234\n");
69+
TimeUnit.SECONDS.sleep(2);
70+
driver.findElement(By.id("com.android.settings:id/next_button")).click();
71+
TimeUnit.SECONDS.sleep(2);
72+
driver.quit();
73+
}
74+
75+
/**
76+
* add a new finger print to security.
77+
*/
78+
@Test public void pressKeyCodeTest() throws Exception {
79+
try {
80+
final AndroidDriver driver = getAndroidDriver(".fingerprint.FingerprintSettings");
81+
TimeUnit.SECONDS.sleep(2);
82+
driver.findElement(By.id("com.android.settings:id/password_entry")).sendKeys("1234\n");
83+
TimeUnit.SECONDS.sleep(2);
84+
driver.findElement(
85+
By.xpath("//android.widget.ListView[1]/android.widget.LinearLayout[1]")).click();
86+
TimeUnit.SECONDS.sleep(2);
87+
driver.fingerPrint(2);
88+
driver.findElement(By.id("com.android.settings:id/next_button")).click();
89+
TimeUnit.SECONDS.sleep(2);
90+
driver.quit();
91+
} catch (Exception ignored) {
92+
Assert.fail();
93+
}
94+
}
95+
96+
/**
97+
* disabling pin lock mode.
98+
*/
99+
@After public void after() throws Exception {
100+
final AndroidDriver driver = getAndroidDriver("ChooseLockGeneric");
101+
TimeUnit.SECONDS.sleep(2);
102+
driver.findElement(By.id("com.android.settings:id/password_entry")).sendKeys("1234\n");
103+
TimeUnit.SECONDS.sleep(2);
104+
driver.findElement(By.xpath("//android.widget.ListView[1]/android.widget.LinearLayout[1]"))
105+
.click();
106+
TimeUnit.SECONDS.sleep(2);
107+
driver.findElement(By.id("android:id/button1")).click();
108+
TimeUnit.SECONDS.sleep(2);
109+
driver.quit();
110+
}
111+
112+
private AndroidDriver getAndroidDriver(String activity) throws Exception {
113+
DesiredCapabilities capabilities = new DesiredCapabilities();
114+
capabilities.setCapability(MobileCapabilityType.DEVICE_NAME, "Android Emulator");
115+
capabilities.setCapability("appPackage", "com.android.settings");
116+
capabilities.setCapability("appActivity", activity);
117+
return (AndroidDriver) new AndroidDriver<AndroidElement>(service.getUrl(), capabilities);
118+
}
119+
120+
}

0 commit comments

Comments
 (0)