Skip to content

Commit

Permalink
PIA-637: Add new tests for DedicatedIP
Browse files Browse the repository at this point in the history
Created an xpath finder  helper called XPathUtil.kt for us to be able to
validate toast notification in the app. This function can be used not only for
toast but for any elements you need to find that has only xpath available.
  • Loading branch information
kp-ruth-o committed Oct 25, 2023
1 parent b14124b commit 6da743b
Show file tree
Hide file tree
Showing 9 changed files with 86 additions and 13 deletions.
2 changes: 2 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ dependencies {
androidTestImplementation 'androidx.test:runner:1.4.0'
androidTestImplementation 'androidx.test:core:1.4.0'
androidTestUtil 'androidx.test:orchestrator:1.4.2'
implementation 'org.dom4j:dom4j:2.1.4'


// EventBus
implementation 'org.greenrobot:eventbus:3.1.1'
Expand Down
3 changes: 1 addition & 2 deletions app/src/androidTest/java/core/BaseUiAutomatorClass.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import org.junit.Before

open class BaseUiAutomatorClass {

private lateinit var device: UiDevice
protected val device: UiDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation())
private lateinit var context: Context

private fun startApp(packageName: String, activityName: String? = null) {
Expand All @@ -25,7 +25,6 @@ open class BaseUiAutomatorClass {

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

import androidx.test.uiautomator.UiObject
import androidx.test.uiautomator.UiSelector
import com.privateinternetaccess.android.core.BaseUiAutomatorClass
import java.io.File
import org.dom4j.Document
import org.dom4j.io.SAXReader

object XPathUtil : BaseUiAutomatorClass() {

fun findElementByXPath(xPathQuery : String) : UiObject? {
val dumpFile = File.createTempFile("uiDump", ".xml")
device.dumpWindowHierarchy(dumpFile)
//parse - translating info what we are getting to something workable and manageable
val reader = SAXReader()
val document:Document = reader.read(dumpFile)
val node = document.selectSingleNode(xPathQuery) ?: return null

if(node != null){
val resourceID = node.valueOf("@resource-id")
val contentDesc = node.valueOf("@content-desc")
val textValue = node.valueOf("@text")
val className = node.valueOf("@class")
val selector = UiSelector()
if(resourceID.isNotEmpty()) selector.resourceId(resourceID)
if(contentDesc.isNotEmpty()) selector.description(contentDesc)
if(textValue.isNotEmpty()) selector.text(textValue)
if(className.isNotEmpty()) selector.className(className)
return device.findObject(selector)
}
return null
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package com.privateinternetaccess.android.screens.objects

import com.privateinternetaccess.android.helpers.LocatorHelper
import com.privateinternetaccess.android.helpers.XPathUtil

class DedicatedIPPageObjects {
val dedicatedIPField = LocatorHelper.findByResourceId("com.privateinternetaccess.android:id/snippet_dip_entry_field")
val activateButton = LocatorHelper.findByResourceId("com.privateinternetaccess.android:id/snippet_dip_activate_button")
val serverFlag = LocatorHelper.findByResourceId("com.privateinternetaccess.android:id/list_server_flag")
val serverName = LocatorHelper.findByResourceId("com.privateinternetaccess.android:id/list_server_name")
val serverIPAddress = LocatorHelper.findByResourceId("com.privateinternetaccess.android:id/list_server_ping")
val toastErrorMessage = XPathUtil.findElementByXPath("//android.widget.Toast[@text='Your token is invalid. Please make sure you have entered the token correctly.']" )
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import com.privateinternetaccess.android.helpers.LocatorHelper

class SideMenuPageObjects {

val dedicateIP = LocatorHelper.findByText("Dedicated IP")
val dedicatedIP = LocatorHelper.findByText("Dedicated IP")
val settings = LocatorHelper.findByText("Settings")
val logout = LocatorHelper.findByText("Log out")
val logoutConfirm = LocatorHelper.findByResourceId("android:id/button1")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.privateinternetaccess.android.screens.steps

import com.privateinternetaccess.android.BuildConfig
import com.privateinternetaccess.android.helpers.ActionHelpers.inputTextInField
import com.privateinternetaccess.android.screens.objects.DedicatedIPPageObjects
import com.privateinternetaccess.android.core.BaseUiAutomatorClass.Companion.defaultTimeOut
Expand All @@ -10,7 +9,7 @@ class DedicatedIPStepObjects {

val dedicatedIPPageObjects = DedicatedIPPageObjects()

fun enterDedicatedIP(DIPToken : String = BuildConfig.PIA_VALID_DIP_TOKEN ) {
fun enterDedicatedIPToken(DIPToken : String) {
inputTextInField(dedicatedIPPageObjects.dedicatedIPField, DIPToken)
dedicatedIPPageObjects.activateButton.click()
dedicatedIPPageObjects.activateButton.waitForExists(defaultTimeOut)
Expand Down
4 changes: 0 additions & 4 deletions app/src/androidTest/java/screens/steps/SideMenuStepObjects.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,6 @@ class SideMenuStepObjects {

private val sideMenuPageObjects = SideMenuPageObjects()

fun clickOnDedicatedIP(){
sideMenuPageObjects.dedicateIP.click()
}

fun clickOnSettings() {
sideMenuPageObjects.settings.click()
}
Expand Down
41 changes: 41 additions & 0 deletions app/src/androidTest/java/tests/DedicatedIPTests.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.privateinternetaccess.android.tests

import com.privateinternetaccess.android.core.BaseUiAutomatorClass
import com.privateinternetaccess.android.helpers.ActionHelpers.successfulLogin
import com.privateinternetaccess.android.helpers.ActionHelpers.goToSideMenu
import com.privateinternetaccess.android.screens.objects.DedicatedIPPageObjects
import com.privateinternetaccess.android.screens.objects.SideMenuPageObjects
import com.privateinternetaccess.android.screens.steps.DedicatedIPStepObjects
import com.privateinternetaccess.android.BuildConfig
import org.junit.Test
import org.junit.Assert

class DedicatedIPTests : BaseUiAutomatorClass() {

private val dedicatedIPStepObjects = DedicatedIPStepObjects()
private val sideMenuPageObjects = SideMenuPageObjects()

@Test
fun oneDIPTokenAccepted() {
successfulLogin()
goToSideMenu(sideMenuPageObjects.dedicatedIP)
dedicatedIPStepObjects.enterDedicatedIPToken(BuildConfig.PIA_VALID_DIP_TOKEN )
val conditions = listOf(
{ !DedicatedIPPageObjects().dedicatedIPField.exists() },
{ DedicatedIPPageObjects().serverFlag.exists() },
{ DedicatedIPPageObjects().serverName.exists() },
{ DedicatedIPPageObjects().serverIPAddress.exists() }
)
for (condition in conditions) {
Assert.assertTrue(condition())
}
}

@Test
fun invalidDIPTokenValidation() {
successfulLogin()
goToSideMenu(sideMenuPageObjects.dedicatedIP)
dedicatedIPStepObjects.enterDedicatedIPToken("invalidToken")
DedicatedIPPageObjects().toastErrorMessage?.let { assert(it.exists()) }
}
}
8 changes: 4 additions & 4 deletions app/src/androidTest/java/tests/SignOutTests.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import com.privateinternetaccess.android.helpers.ActionHelpers.returnOnMainScree
import com.privateinternetaccess.android.helpers.ActionHelpers.goToSideMenu
import com.privateinternetaccess.android.screens.objects.*
import com.privateinternetaccess.android.screens.steps.*

import com.privateinternetaccess.android.BuildConfig
import org.junit.Test

class SignOutTests : BaseUiAutomatorClass() {
Expand All @@ -32,12 +32,12 @@ class SignOutTests : BaseUiAutomatorClass() {
@Test
fun validateDIPSettingsAtLogout() {
successfulLogin()
goToSideMenu(sideMenuPageObjects.dedicateIP)
dedicatedIPStepObjects.enterDedicatedIP()
goToSideMenu(sideMenuPageObjects.dedicatedIP)
dedicatedIPStepObjects.enterDedicatedIPToken(BuildConfig.PIA_VALID_DIP_TOKEN)
returnOnMainScreen()
successfulLogout()
successfulLogin()
goToSideMenu(sideMenuPageObjects.dedicateIP)
goToSideMenu(sideMenuPageObjects.dedicatedIP)

val conditions = listOf(
{ DedicatedIPPageObjects().dedicatedIPField.text.equals("Paste your token here") },
Expand Down

0 comments on commit 6da743b

Please sign in to comment.