Skip to content

Commit

Permalink
PIA-633 - Add Sign In tests, screen objects and helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
kp-andrei-madescu committed Oct 2, 2023
1 parent ee43a5f commit 4494536
Show file tree
Hide file tree
Showing 8 changed files with 175 additions and 0 deletions.
4 changes: 4 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ repositories {

dependencies {
implementation 'androidx.swiperefreshlayout:swiperefreshlayout:1.0.0'
implementation 'androidx.test.uiautomator:uiautomator:2.2.0'
coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:1.1.5'

implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
Expand Down Expand Up @@ -235,6 +236,9 @@ android {
}
debug {
debuggable true
buildConfigField 'String', 'PIA_VALID_USERNAME', "\"" + System.getenv("PIA_VALID_USERNAME") + "\""
buildConfigField 'String', 'PIA_VALID_PASSWORD', "\"" + System.getenv("PIA_VALID_PASSWORD") + "\""
buildConfigField 'String', 'PIA_VALID_DIP_TOKEN', "\"" + System.getenv("PIA_VALID_DIP_TOKEN") + "\""
}
}
ndkVersion '21.1.6352462'
Expand Down
32 changes: 32 additions & 0 deletions app/src/androidTest/java/core/BaseUiAutomatorClass.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.privateinternetaccess.android.core

import android.content.Context
import android.content.Intent
import androidx.test.platform.app.InstrumentationRegistry
import androidx.test.uiautomator.UiDevice
import org.junit.Before

open class BaseUiAutomatorClass {

private lateinit var device: UiDevice
private lateinit var context: Context

private fun startApp(packageName: String, activityName: String? = null) {
context = InstrumentationRegistry.getInstrumentation().targetContext
val intent = if (activityName == null) {
context.packageManager.getLaunchIntentForPackage(packageName)
} else {
Intent().setClassName(packageName, activityName)
}
intent?.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
intent?.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK) // Clear out any previous instances
context.startActivity(intent)
}

@Before
fun setUp() {
device = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation())
device.pressHome()
startApp("com.privateinternetaccess.android")
}
}
18 changes: 18 additions & 0 deletions app/src/androidTest/java/helpers/ActionHelpers.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.privateinternetaccess.android.helpers

import androidx.test.uiautomator.UiObject

object ActionHelpers {

fun clickIfExists(primaryUiObject : UiObject, secondaryUiObj: UiObject? = null) {
if (primaryUiObject.exists()) {
(secondaryUiObj ?: primaryUiObject).click()
}
}

fun <T> inputTextInField(field: UiObject, data: T? = null) {
field.clearTextField()
field.click()
field.text = data?.toString() ?: ""
}
}
15 changes: 15 additions & 0 deletions app/src/androidTest/java/helpers/LocatorHelper.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.privateinternetaccess.android.helpers

import androidx.test.platform.app.InstrumentationRegistry
import androidx.test.uiautomator.UiDevice
import androidx.test.uiautomator.UiObject
import androidx.test.uiautomator.UiSelector

object LocatorHelper {

private val device: UiDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation())

fun findByResourceId(id: String, instance: Int = 0): UiObject {
return device.findObject(UiSelector().resourceId(id).instance(instance))
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.privateinternetaccess.android.screens.objects

import com.privateinternetaccess.android.helpers.LocatorHelper

class MainScreenPageObjects {

val connectButton = LocatorHelper.findByResourceId("com.privateinternetaccess.android:id/connection_background")

}
20 changes: 20 additions & 0 deletions app/src/androidTest/java/screens/objects/SignInPageObjects.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.privateinternetaccess.android.screens.objects

import com.privateinternetaccess.android.helpers.LocatorHelper

class SignInPageObjects {

val notificationPrompt = LocatorHelper.findByResourceId("com.android.permissioncontroller:id/permission_message")
val allowNotifications = LocatorHelper.findByResourceId("com.android.permissioncontroller:id/permission_allow_button")
val denyNotifications = LocatorHelper.findByResourceId("com.android.permissioncontroller:id/permission_deny_button")
val reachLoginScreenButton = LocatorHelper.findByResourceId("com.privateinternetaccess.android:id/login")
val usernameField = LocatorHelper.findByResourceId("com.privateinternetaccess.android:id/piaxEditText")
val passwordField = LocatorHelper.findByResourceId("com.privateinternetaccess.android:id/piaxEditText", instance = 1)
val loginButton = LocatorHelper.findByResourceId("com.privateinternetaccess.android:id/fragment_login_button")
val vpnProfileOkButton = LocatorHelper.findByResourceId("com.privateinternetaccess.android:id/activity_vpn_permissions_button")
val androidOkButton = LocatorHelper.findByResourceId("android:id/button1")

val loginWithReceipt = LocatorHelper.findByResourceId("com.privateinternetaccess.android:id/fragment_login_receipt")

val noUsernameOrPasswordError = LocatorHelper.findByResourceId("com.privateinternetaccess.android:id/piaxEditTextError")
}
35 changes: 35 additions & 0 deletions app/src/androidTest/java/screens/steps/SignInStepObjects.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.privateinternetaccess.android.screens.steps

import com.privateinternetaccess.android.helpers.ActionHelpers.clickIfExists
import com.privateinternetaccess.android.helpers.ActionHelpers.inputTextInField
import com.privateinternetaccess.android.screens.objects.SignInPageObjects

class SignInStepObjects {

private val signInPageObjects = SignInPageObjects()

fun allowNotifications() {
clickIfExists(signInPageObjects.notificationPrompt, signInPageObjects.allowNotifications)
}

fun reachSignInScreen() {
signInPageObjects.reachLoginScreenButton.clickAndWaitForNewWindow(5000)
}

fun enterUsername(username : String?) {
inputTextInField(signInPageObjects.usernameField, username)
}

fun enterPassword(password : String?) {
inputTextInField(signInPageObjects.passwordField, password)
}

fun clickOnLoginButton() {
signInPageObjects.loginButton.clickAndWaitForNewWindow(5000)
}

fun allowVpnProfileCreation() {
clickIfExists(signInPageObjects.vpnProfileOkButton)
clickIfExists(signInPageObjects.androidOkButton)
}
}
42 changes: 42 additions & 0 deletions app/src/androidTest/java/tests/SignInTests.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.privateinternetaccess.android.tests

import com.privateinternetaccess.android.BuildConfig
import com.privateinternetaccess.android.core.BaseUiAutomatorClass
import com.privateinternetaccess.android.screens.objects.MainScreenPageObjects
import com.privateinternetaccess.android.screens.objects.SignInPageObjects
import com.privateinternetaccess.android.screens.steps.SignInStepObjects
import org.junit.Test

class SignInTests : BaseUiAutomatorClass() {

private val stepObjects = SignInStepObjects()

@Test
fun successfulLoginWithValidCredentials() {
stepObjects.allowNotifications()
stepObjects.reachSignInScreen()
stepObjects.enterUsername(BuildConfig.PIA_VALID_USERNAME)
stepObjects.enterPassword(BuildConfig.PIA_VALID_PASSWORD)
stepObjects.clickOnLoginButton()
stepObjects.allowVpnProfileCreation()
assert(MainScreenPageObjects().connectButton.exists())
}

@Test
fun incorrectCredentialsReturnToSplashScreen() {
stepObjects.allowNotifications()
stepObjects.reachSignInScreen()
stepObjects.enterUsername("PLACEHOLDER")
stepObjects.enterPassword("PLACEHOLDER")
stepObjects.clickOnLoginButton()
assert(SignInPageObjects().reachLoginScreenButton.exists())
}

@Test
fun errorMessageIfNoCredentialsAreProvided() {
stepObjects.allowNotifications()
stepObjects.reachSignInScreen()
stepObjects.clickOnLoginButton()
assert(SignInPageObjects().noUsernameOrPasswordError.exists())
}
}

0 comments on commit 4494536

Please sign in to comment.