Skip to content

Instantly share code, notes, and snippets.

@AlexZhukovich
Last active September 7, 2019 23:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AlexZhukovich/e515c85b041807cd03624836c5192a1b to your computer and use it in GitHub Desktop.
Save AlexZhukovich/e515c85b041807cd03624836c5192a1b to your computer and use it in GitHub Desktop.
Sign In error verification Android UI tests with UiAutomator
@RunWith(AndroidJUnit4::class)
class UiAutomatorSignInTest {
companion object {
const val LAUNCH_TIMEOUT = 2_000L
const val APP_PACKAGE = "com.alex.mapnotes"
}
private val device = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation())
private fun openApp(packageName: String) {
device.pressHome()
// Wait for launcher
val launcherPackage: String = device.launcherPackageName
assertThat(launcherPackage, notNullValue())
device.wait(
Until.hasObject(By.pkg(launcherPackage).depth(0)),
LAUNCH_TIMEOUT
)
// Start an activity
val context = ApplicationProvider.getApplicationContext<Context>()
val intent = context.packageManager.getLaunchIntentForPackage(packageName)
.apply { addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK) }
context.startActivity(intent)
// Wait for the app to appear
device.wait(Until.hasObject(By.pkg(APP_PACKAGE).depth(0)),
LAUNCH_TIMEOUT)
}
private fun navigateToSignIn() {
device.wait(Until.hasObject(By.res("com.alex.mapnotes:id/signIn")), LAUNCH_TIMEOUT)
val login: UiObject2 = device.findObject(By.res("com.alex.mapnotes:id/signIn"))
if (login.isEnabled) {
login.click()
}
device.wait(Until.hasObject(By.res("com.alex.mapnotes:id/signIn")), LAUNCH_TIMEOUT)
}
@Test
fun shouldDisplaySinInErrorWhenEmailIsIncorrect() {
openApp(APP_PACKAGE)
navigateToSignIn()
val incorrectEmail = "test"
val expectedErrorMessage = "An email address should be valid"
val emailInput: UiObject2 = device.findObject(By.res("com.alex.mapnotes:id/email"))
emailInput.text = incorrectEmail
val signInButton: UiObject2 = device.findObject(By.res("com.alex.mapnotes:id/signIn"))
if (signInButton.isEnabled) {
signInButton.click()
}
device.wait(Until.hasObject(By.text(expectedErrorMessage)), LAUNCH_TIMEOUT)
val errorMessage = device.findObject(By.text(expectedErrorMessage))
assertEquals(expectedErrorMessage, errorMessage.text)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment