1616
1717package io .appium .java_client .android ;
1818
19+ import static io .appium .java_client .MobileBy .AndroidUIAutomator ;
1920import static java .util .concurrent .TimeUnit .SECONDS ;
21+ import static org .openqa .selenium .By .id ;
2022
2123import io .appium .java_client .remote .MobileCapabilityType ;
2224import io .appium .java_client .service .local .AppiumDriverLocalService ;
2628import org .junit .Before ;
2729import org .junit .BeforeClass ;
2830import org .junit .Test ;
29- import org .openqa .selenium .By ;
3031import org .openqa .selenium .NoSuchElementException ;
3132import org .openqa .selenium .remote .DesiredCapabilities ;
3233
33- /**
34- * It is necessary to make this test passing
35- * - emulator API Level 23 Nexus device
36- * - perform 'adb emu finger touch' 1 on Windows
37- */
3834public class FingerPrintTest {
39-
40- private static final String PASSWORD_INPUT_ID = "com.android.settings:id/password_entry" ;
41- private static final String FIRST_IN_LIST_XPATH = "//android.widget.ListView[1]/android.widget.LinearLayout[1]" ;
4235 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+ }
4346
4447 /**
4548 * initialization.
@@ -62,77 +65,76 @@ public class FingerPrintTest {
6265 }
6366 }
6467
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+
6594 /**
6695 * enable system security which is required for finger print activation.
6796 */
6897 @ Before public void before () throws Exception {
69- final AndroidDriver driver = getAndroidDriver ("ChooseLockGeneric" );
70- // clicking the pin lock mode
71- driver .findElement (By .xpath ("//android.widget.LinearLayout[4]" )).click ();
72- try {
73- // line below will throw exception if secure startup window is popped up
74- driver .findElementById (PASSWORD_INPUT_ID );
75- } catch (NoSuchElementException e ) {
76- // in secure startup window
77- driver .findElementById ("com.android.settings:id/encrypt_require_password" ).click ();
78- SECONDS .sleep (2 );
79- clickOKInPopup (driver );
80- clickNext (driver );
81- }
82- enterPasswordAndContinue (driver );
83- enterPasswordAndContinue (driver );
84- clickNext (driver );
85- driver .quit ();
98+ initDriver ();
99+ clickOnSecurity ();
100+ findElementByText ("Screen lock" ).click ();
101+ findElementByText ("PIN" ).click ();
102+ enterPasswordAndContinue ();
103+ enterPasswordAndContinue ();
104+ clickNext ();
86105 }
87106
88107 /**
89108 * add a new finger print to security.
90109 */
91- @ Test public void pressKeyCodeTest () throws InterruptedException {
92- final AndroidDriver driver = getAndroidDriver (".fingerprint.FingerprintSettings" );
93- enterPasswordAndContinue (driver );
94- // click add fingerprint
95- driver .findElementByXPath (FIRST_IN_LIST_XPATH ).click ();
110+ @ Test public void fingerPrintTest () {
111+ findElementByText ("Fingerprint" ).click ();
112+ clickNext ();
113+ enterPasswordAndContinue ();
114+ clickNext ();
115+
96116 driver .fingerPrint (2 );
97117 try {
98- clickNext ( driver );
118+ driver . findElementById ( "com.android.settings:id/next_button" ). click ( );
99119 } catch (Exception e ) {
100120 Assert .fail ("fingerprint command fail to execute" );
101- } finally {
102- driver .quit ();
103121 }
104122 }
105123
106124 /**
107125 * disabling pin lock mode.
108126 */
109127 @ After public void after () throws InterruptedException {
110- final AndroidDriver driver = getAndroidDriver ("ChooseLockGeneric" );
111- enterPasswordAndContinue (driver );
112- driver .findElementByXPath (FIRST_IN_LIST_XPATH ).click ();
113- clickOKInPopup (driver );
114128 driver .quit ();
115- }
116-
117- private static AndroidDriver getAndroidDriver (String activity ) {
118- DesiredCapabilities capabilities = new DesiredCapabilities ();
119- capabilities .setCapability (MobileCapabilityType .DEVICE_NAME , "Android Emulator" );
120- capabilities .setCapability ("appPackage" , "com.android.settings" );
121- capabilities .setCapability ("appActivity" , activity );
122- AndroidDriver <AndroidElement > driver = new AndroidDriver <AndroidElement >(service .getUrl (), capabilities );
123- driver .manage ().timeouts ().implicitlyWait (15 , SECONDS );
124- return driver ;
125- }
126129
127- private void enterPasswordAndContinue (AndroidDriver driver ) throws InterruptedException {
128- driver .findElementById (PASSWORD_INPUT_ID ).sendKeys ("1234\n " );
129- }
130+ initDriver ();
131+ clickOnSecurity ();
130132
131- private void clickNext (AndroidDriver driver ) throws InterruptedException {
132- driver .findElementById ("com.android.settings:id/next_button" ).click ();
133- }
133+ findElementByText ("Screen lock" ).click ();
134134
135- private void clickOKInPopup (AndroidDriver driver ) throws InterruptedException {
136- driver .findElementById ("android:id/button1" ).click ();
135+ enterPasswordAndContinue ();
136+ findElementByText ("None" ).click ();
137+ clickOKInPopup ();
138+ driver .quit ();
137139 }
138- }
140+ }
0 commit comments