Skip to content

Commit e37f4e4

Browse files
Merge pull request #786 from TikhomirovSergey/alizelzele-finger-print
Additional changes of the #473
2 parents 7efa6c3 + eb4116f commit e37f4e4

File tree

5 files changed

+175
-1
lines changed

5 files changed

+175
-1
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ public class MobileCommand {
6868
protected static final String IS_KEYBOARD_SHOWN;
6969
protected static final String IS_LOCKED;
7070
protected static final String LONG_PRESS_KEY_CODE;
71+
protected static final String FINGER_PRINT;
7172
protected static final String OPEN_NOTIFICATIONS;
7273
protected static final String PRESS_KEY_CODE;
7374
protected static final String PUSH_FILE;
@@ -116,6 +117,7 @@ public class MobileCommand {
116117
IS_KEYBOARD_SHOWN = "isKeyboardShown";
117118
IS_LOCKED = "isLocked";
118119
LONG_PRESS_KEY_CODE = "longPressKeyCode";
120+
FINGER_PRINT = "fingerPrint";
119121
OPEN_NOTIFICATIONS = "openNotifications";
120122
PRESS_KEY_CODE = "pressKeyCode";
121123
PUSH_FILE = "pushFile";
@@ -170,6 +172,7 @@ public class MobileCommand {
170172
commandRepository.put(IS_LOCKED, postC("/session/:sessionId/appium/device/is_locked"));
171173
commandRepository.put(LONG_PRESS_KEY_CODE,
172174
postC("/session/:sessionId/appium/device/long_press_keycode"));
175+
commandRepository.put(FINGER_PRINT, postC("/session/:sessionId/appium/device/finger_print"));
173176
commandRepository.put(OPEN_NOTIFICATIONS,
174177
postC("/session/:sessionId/appium/device/open_notifications"));
175178
commandRepository.put(PRESS_KEY_CODE,

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public class AndroidDriver<T extends WebElement>
4747
extends AppiumDriver<T>
4848
implements PressesKeyCode, HasNetworkConnection, PushesFiles, StartsActivity,
4949
FindsByAndroidUIAutomator<T>, LocksAndroidDevice, HasAndroidSettings, HasDeviceDetails,
50-
HasSupportedPerformanceDataType {
50+
HasSupportedPerformanceDataType, AuthenticatesByFinger {
5151

5252
private static final String ANDROID_PLATFORM = MobilePlatform.ANDROID;
5353

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,19 @@ public class AndroidMobileCommandHelper extends MobileCommand {
184184
IS_LOCKED, ImmutableMap.<String, Object>of());
185185
}
186186

187+
/**
188+
* This method forms a {@link java.util.Map} of parameters for the
189+
* finger print authentication invocation.
190+
*
191+
* @param fingerPrintId finger prints stored in Android Keystore system (from 1 to 10)
192+
* @return a key-value pair. The key is the command name. The value is a
193+
* {@link java.util.Map} command arguments.
194+
*/
195+
public static Map.Entry<String, Map<String, ?>> fingerPrintCommand(int fingerPrintId) {
196+
return new AbstractMap.SimpleEntry<>(FINGER_PRINT,
197+
prepareArguments("fingerprintId", fingerPrintId));
198+
}
199+
187200
/**
188201
* This method forms a {@link java.util.Map} of parameters for the
189202
* notification opening.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package io.appium.java_client.android;
2+
3+
import static io.appium.java_client.android.AndroidMobileCommandHelper.fingerPrintCommand;
4+
5+
import io.appium.java_client.CommandExecutionHelper;
6+
import io.appium.java_client.ExecutesMethod;
7+
8+
public interface AuthenticatesByFinger extends ExecutesMethod {
9+
10+
/**
11+
* Authenticate users by using their finger print scans on supported emulators.
12+
*
13+
* @param fingerPrintId finger prints stored in Android Keystore system (from 1 to 10)
14+
*/
15+
default void fingerPrint(int fingerPrintId) {
16+
CommandExecutionHelper.execute(this, fingerPrintCommand(fingerPrintId));
17+
}
18+
}
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
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 static io.appium.java_client.MobileBy.AndroidUIAutomator;
20+
import static java.util.concurrent.TimeUnit.SECONDS;
21+
import static org.openqa.selenium.By.id;
22+
23+
import io.appium.java_client.remote.MobileCapabilityType;
24+
import io.appium.java_client.service.local.AppiumDriverLocalService;
25+
import org.junit.After;
26+
import org.junit.AfterClass;
27+
import org.junit.Assert;
28+
import org.junit.Before;
29+
import org.junit.BeforeClass;
30+
import org.junit.Test;
31+
import org.openqa.selenium.NoSuchElementException;
32+
import org.openqa.selenium.remote.DesiredCapabilities;
33+
34+
public class FingerPrintTest {
35+
private static AppiumDriverLocalService service;
36+
private static AndroidDriver<AndroidElement> driver;
37+
38+
private static void initDriver() {
39+
DesiredCapabilities capabilities = new DesiredCapabilities();
40+
capabilities.setCapability(MobileCapabilityType.DEVICE_NAME, "Android Emulator");
41+
capabilities.setCapability("appPackage", "com.android.settings");
42+
capabilities.setCapability("appActivity", "Settings");
43+
driver = new AndroidDriver<>(service.getUrl(), capabilities);
44+
driver.manage().timeouts().implicitlyWait(15, SECONDS);
45+
}
46+
47+
/**
48+
* initialization.
49+
*/
50+
@BeforeClass public static void beforeClass() {
51+
service = AppiumDriverLocalService.buildDefaultService();
52+
service.start();
53+
54+
if (service == null || !service.isRunning()) {
55+
throw new ExceptionInInitializerError("An appium server node is not started!");
56+
}
57+
}
58+
59+
/**
60+
* finishing.
61+
*/
62+
@AfterClass public static void afterClass() {
63+
if (service != null) {
64+
service.stop();
65+
}
66+
}
67+
68+
private AndroidElement findElementByText(String text) {
69+
return driver.findElements(id("android:id/title")).stream().filter(androidElement ->
70+
text.equals(androidElement.getText())).findFirst()
71+
.orElseThrow(() ->
72+
new NoSuchElementException(String.format("There is no element with the text '%s'", text)));
73+
}
74+
75+
private void clickNext() {
76+
driver.findElementById("com.android.settings:id/next_button").click();
77+
}
78+
79+
private void clickOKInPopup() {
80+
driver.findElementById("android:id/button1").click();
81+
}
82+
83+
private void enterPasswordAndContinue() {
84+
driver.findElementById("com.android.settings:id/password_entry")
85+
.sendKeys("1234\n");
86+
}
87+
88+
private void clickOnSecurity() {
89+
driver.findElement(AndroidUIAutomator("new UiScrollable(new UiSelector()"
90+
+ ".scrollable(true)).scrollIntoView("
91+
+ "new UiSelector().text(\"Security\"));")).click();
92+
}
93+
94+
/**
95+
* enable system security which is required for finger print activation.
96+
*/
97+
@Before public void before() throws Exception {
98+
initDriver();
99+
clickOnSecurity();
100+
findElementByText("Screen lock").click();
101+
findElementByText("PIN").click();
102+
enterPasswordAndContinue();
103+
enterPasswordAndContinue();
104+
clickNext();
105+
}
106+
107+
/**
108+
* add a new finger print to security.
109+
*/
110+
@Test public void fingerPrintTest() {
111+
findElementByText("Fingerprint").click();
112+
clickNext();
113+
enterPasswordAndContinue();
114+
clickNext();
115+
116+
driver.fingerPrint(2);
117+
try {
118+
clickNext();
119+
} catch (Exception e) {
120+
Assert.fail("fingerprint command fail to execute");
121+
}
122+
}
123+
124+
/**
125+
* disabling pin lock mode.
126+
*/
127+
@After public void after() throws InterruptedException {
128+
driver.quit();
129+
130+
initDriver();
131+
clickOnSecurity();
132+
133+
findElementByText("Screen lock").click();
134+
135+
enterPasswordAndContinue();
136+
findElementByText("None").click();
137+
clickOKInPopup();
138+
driver.quit();
139+
}
140+
}

0 commit comments

Comments
 (0)