Source:
public static void loginGooglePlay ( android.app.Instrumentation instrumentation ) throws java.lang.Exception { final android.support.test.uiautomator.UiDevice device = android.support.test.uiautomator.UiDevice.getInstance ( instrumentation ) ; final java.lang.String playStore = "Play Store" ; final java.lang.String email = "pstester1980@gmail.com" ; final java.lang.String password = "pst4lif3" ; com.android.devtools.systemimage.uitest.utils.PlayStoreUtil.resetPlayStore ( instrumentation ) ; device.findObject ( new android.support.test.uiautomator.UiSelector ( ) . text ( playStore ) ) . clickAndWaitForNewWindow ( ) ; new com.android.devtools.systemimage.uitest.watchers.PlayStorePermissionsWatcher ( device ) . checkForCondition ( ) ; boolean hasSearchBox = new com.android.devtools.systemimage.uitest.utils.Wait ( java.util.concurrent.TimeUnit.SECONDS.toMillis ( 20 ) ) . until ( new com.android.devtools.systemimage.uitest.utils.Wait.ExpectedCondition ( ) { @ java.lang.Override public boolean isTrue ( ) throws android.support.test.uiautomator.UiObjectNotFoundException { return ( device.findObject ( new android.support.test.uiautomator.UiSelector ( ) . resourceId ( Res.GOOGLE_PLAY_IDLE_RES ) ) . exists ( ) ) || ( device.findObject ( new android.support.test.uiautomator.UiSelector ( ) . resourceId ( Res.GOOGLE_PLAY_ACTIVE_RES ) ) . exists ( ) ) ; } } ) ; if ( hasSearchBox ) { device.pressHome ( ) ; return ; } boolean needsEmail = new com.android.devtools.systemimage.uitest.utils.Wait ( ) . until ( new com.android.devtools.systemimage.uitest.utils.Wait.ExpectedCondition ( ) { @ java.lang.Override public boolean isTrue ( ) throws android.support.test.uiautomator.UiObjectNotFoundException { if ( ( com.android.devtools.systemimage.uitest.utils.SystemUtil.getApiLevel ( ) ) <= 24 ) { return device.findObject ( new android.support.test.uiautomator.UiSelector ( ) . description ( "Email or phone" ) ) . exists ( ) ; } else { return device.findObject ( new android.support.test.uiautomator.UiSelector ( ) . text ( "Email or phone" ) ) . exists ( ) ; } } } ) ; if ( ! needsEmail ) { boolean needsPassword = new com.android.devtools.systemimage.uitest.utils.Wait ( ) . until ( new com.android.devtools.systemimage.uitest.utils.Wait.ExpectedCondition ( ) { @ java.lang.Override public boolean isTrue ( ) throws android.support.test.uiautomator.UiObjectNotFoundException { if ( ( com.android.devtools.systemimage.uitest.utils.SystemUtil.getApiLevel ( ) ) <= 24 ) { return device.findObject ( new android.support.test.uiautomator.UiSelector ( ) . description ( ( "Sign in " + email ) ) ) . exists ( ) ; } else { return device.findObject ( new android.support.test.uiautomator.UiSelector ( ) . text ( ( "Sign in " + email ) ) ) . exists ( ) ; } } } ) ; if ( ! needsPassword ) { device.pressHome ( ) ; return ; } android.support.test.uiautomator.UiObject inputPasswordField = device.findObject ( new android.support.test.uiautomator.UiSelector ( ) . resourceId ( "password" ) ) ; inputPasswordField.clearTextField ( ) ; inputPasswordField.setText ( password ) ; device.findObject ( new android.support.test.uiautomator.UiSelector ( ) . description ( "NEXT" ) ) . clickAndWaitForNewWindow ( ) ; com.android.devtools.systemimage.uitest.utils.PlayStoreUtil.logInWithPassword ( instrumentation , password ) ; } else { android.support.test.uiautomator.UiObject inputEmailField ; if ( ( com.android.devtools.systemimage.uitest.utils.SystemUtil.getApiLevel ( ) ) <= 24 ) { inputEmailField = device.findObject ( new android.support.test.uiautomator.UiSelector ( ) . description ( "Email or phone" ) ) ; } else { inputEmailField = device.findObject ( new android.support.test.uiautomator.UiSelector ( ) . text ( "Email or phone" ) ) ; } inputEmailField.clearTextField ( ) ; inputEmailField.setText ( email ) ; device.findObject ( new android.support.test.uiautomator.UiSelector ( ) . description ( "NEXT" ) ) . clickAndWaitForNewWindow ( ) ; com.android.devtools.systemimage.uitest.utils.PlayStoreUtil.logInWithPassword ( instrumentation , password ) ; } new com.android.devtools.systemimage.uitest.watchers.PlayStorePermissionsWatcher ( device ) . checkForCondition ( ) ; new com.android.devtools.systemimage.uitest.watchers.PlayStoreConfirmationWatcher ( device ) . checkForCondition ( ) ; device.pressHome ( ) ; device.findObject ( new android.support.test.uiautomator.UiSelector ( ) . text ( playStore ) ) . clickAndWaitForNewWindow ( ) ; new com.android.devtools.systemimage.uitest.watchers.PlayStorePermissionsWatcher ( device ) . checkForCondition ( ) ; device.pressHome ( ) ; return ; }

Target:
public static void loginGooglePlay ( android.app.Instrumentation instrumentation ) throws java.lang.Exception { final android.support.test.uiautomator.UiDevice device = android.support.test.uiautomator.UiDevice.getInstance ( instrumentation ) ; final java.lang.String email = "pstester1980@gmail.com" ; final java.lang.String password = "pst4lif3" ; com.android.devtools.systemimage.uitest.utils.PlayStoreUtil.resetPlayStore ( instrumentation ) ; com.android.devtools.systemimage.uitest.utils.AppLauncher.launch ( instrumentation , "Play Store" ) ; new com.android.devtools.systemimage.uitest.watchers.PlayStorePermissionsWatcher ( device ) . checkForCondition ( ) ; boolean hasSearchBox = new com.android.devtools.systemimage.uitest.utils.Wait ( java.util.concurrent.TimeUnit.SECONDS.toMillis ( 20 ) ) . until ( new com.android.devtools.systemimage.uitest.utils.Wait.ExpectedCondition ( ) { @ java.lang.Override public boolean isTrue ( ) throws android.support.test.uiautomator.UiObjectNotFoundException { return ( device.findObject ( new android.support.test.uiautomator.UiSelector ( ) . resourceId ( Res.GOOGLE_PLAY_IDLE_RES ) ) . exists ( ) ) || ( device.findObject ( new android.support.test.uiautomator.UiSelector ( ) . resourceId ( Res.GOOGLE_PLAY_ACTIVE_RES ) ) . exists ( ) ) ; } } ) ; if ( hasSearchBox ) { device.pressHome ( ) ; return ; } boolean needsEmail = new com.android.devtools.systemimage.uitest.utils.Wait ( ) . until ( new com.android.devtools.systemimage.uitest.utils.Wait.ExpectedCondition ( ) { @ java.lang.Override public boolean isTrue ( ) throws android.support.test.uiautomator.UiObjectNotFoundException { if ( ( com.android.devtools.systemimage.uitest.utils.SystemUtil.getApiLevel ( ) ) == 24 ) { return device.findObject ( new android.support.test.uiautomator.UiSelector ( ) . description ( "Email or phone" ) ) . exists ( ) ; } else { return device.findObject ( new android.support.test.uiautomator.UiSelector ( ) . text ( "Email or phone" ) ) . exists ( ) ; } } } ) ; if ( ! needsEmail ) { boolean needsPassword = new com.android.devtools.systemimage.uitest.utils.Wait ( ) . until ( new com.android.devtools.systemimage.uitest.utils.Wait.ExpectedCondition ( ) { @ java.lang.Override public boolean isTrue ( ) throws android.support.test.uiautomator.UiObjectNotFoundException { if ( ( com.android.devtools.systemimage.uitest.utils.SystemUtil.getApiLevel ( ) ) == 24 ) { return device.findObject ( new android.support.test.uiautomator.UiSelector ( ) . description ( ( "Sign in " + email ) ) ) . exists ( ) ; } else { return device.findObject ( new android.support.test.uiautomator.UiSelector ( ) . text ( ( "Sign in " + email ) ) ) . exists ( ) ; } } } ) ; if ( ! needsPassword ) { device.pressHome ( ) ; return ; } android.support.test.uiautomator.UiObject inputPasswordField = device.findObject ( new android.support.test.uiautomator.UiSelector ( ) . resourceId ( "password" ) ) ; inputPasswordField.clearTextField ( ) ; inputPasswordField.setText ( password ) ; new com.android.devtools.systemimage.uitest.watchers.PlayStoreConfirmationWatcher ( device ) . checkForCondition ( ) ; com.android.devtools.systemimage.uitest.utils.PlayStoreUtil.logInWithPassword ( instrumentation , password ) ; } else { android.support.test.uiautomator.UiObject inputEmailField ; if ( ( com.android.devtools.systemimage.uitest.utils.SystemUtil.getApiLevel ( ) ) == 24 ) { inputEmailField = device.findObject ( new android.support.test.uiautomator.UiSelector ( ) . description ( "Email or phone" ) ) ; } else { inputEmailField = device.findObject ( new android.support.test.uiautomator.UiSelector ( ) . text ( "Email or phone" ) ) ; } inputEmailField.clearTextField ( ) ; inputEmailField.setText ( email ) ; new com.android.devtools.systemimage.uitest.watchers.PlayStoreConfirmationWatcher ( device ) . checkForCondition ( ) ; com.android.devtools.systemimage.uitest.utils.PlayStoreUtil.logInWithPassword ( instrumentation , password ) ; } new com.android.devtools.systemimage.uitest.watchers.PlayStorePermissionsWatcher ( device ) . checkForCondition ( ) ; new com.android.devtools.systemimage.uitest.watchers.PlayStoreConfirmationWatcher ( device ) . checkForCondition ( ) ; com.android.devtools.systemimage.uitest.utils.PlayStoreUtil.resetPlayStore ( instrumentation ) ; device.pressHome ( ) ; com.android.devtools.systemimage.uitest.utils.AppLauncher.launch ( instrumentation , "Play Store" ) ; new com.android.devtools.systemimage.uitest.watchers.PlayStorePermissionsWatcher ( device ) . checkForCondition ( ) ; new com.android.devtools.systemimage.uitest.watchers.PlayStoreConfirmationWatcher ( device ) . checkForCondition ( ) ; com.android.devtools.systemimage.uitest.utils.PlayStoreUtil.resetPlayStore ( instrumentation ) ; device.pressHome ( ) ; return ; }