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