Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PIA-636: Add new test for ServerLists #46

Merged
merged 1 commit into from
Nov 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,7 @@ class MainScreenPageObjects {
val hamburgerMenu = LocatorHelper.findByContentDesc("Open")
val quickConnectFlag1 = LocatorHelper.findByResourceId("com.privateinternetaccess.android:id/quick_server_flag_1")
val quickConnectText1 = LocatorHelper.findByResourceId("com.privateinternetaccess.android:id/quick_server_name_1")
val currentRegion = LocatorHelper.findByResourceId("com.privateinternetaccess.android:id/fragment_connect_flag_area")
val regionNameSelected = LocatorHelper.findByResourceId("com.privateinternetaccess.android:id/fragment_connect_server_name")

}
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 RegionListsPageObjects {
val searchField = LocatorHelper.findByResourceId("com.privateinternetaccess.android:id/search")
val regionName = LocatorHelper.findByResourceId("com.privateinternetaccess.android:id/list_server_name")
val regionNoResultImage = LocatorHelper.findByResourceId("com.privateinternetaccess.android:id/server_select_no_results")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.privateinternetaccess.android.screens.steps

import androidx.test.uiautomator.By
import com.privateinternetaccess.android.core.BaseUiAutomatorClass
import com.privateinternetaccess.android.helpers.ActionHelpers.inputTextInField
import com.privateinternetaccess.android.screens.objects.RegionListsPageObjects
import com.privateinternetaccess.android.helpers.LocatorHelper.findByText

class RegionListsStepObjects : BaseUiAutomatorClass() {
private val serverListPageObjects = RegionListsPageObjects()

fun regionInput (region : String) {
inputTextInField(serverListPageObjects.searchField, region)
}

/* This function will select a random region within the range of the list we extracted.
This is to make our test less flaky, instead of relying on a hard coded region name since
region lists depends on where you are currently located.
*/

fun selectFromVisibleRegionName() : String {
val visibleTextElements = device.findObjects(By.clazz("android.widget.TextView").res("com.privateinternetaccess.android:id/list_server_name"))
val visibleTextList = mutableListOf<String>()

for(textElement in visibleTextElements) {
val text = textElement.text
if(text != null) visibleTextList.add(text)
}

val listSize = visibleTextList.size
val randomIndex = kotlin.random.Random.nextInt(listSize)
findByText(visibleTextList[randomIndex]).clickAndWaitForNewWindow(defaultTimeOut)

return visibleTextList[randomIndex]
}
}
47 changes: 47 additions & 0 deletions app/src/androidTest/java/tests/RegionListsTests.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.privateinternetaccess.android.tests

import com.privateinternetaccess.android.core.BaseUiAutomatorClass
import com.privateinternetaccess.android.helpers.ActionHelpers.successfulLogin
import com.privateinternetaccess.android.screens.objects.MainScreenPageObjects
import com.privateinternetaccess.android.screens.objects.RegionListsPageObjects
import com.privateinternetaccess.android.screens.steps.RegionListsStepObjects
import org.junit.Test
import org.junit.Assert

class RegionListsTests : BaseUiAutomatorClass() {

val mainScreenPageObjects = MainScreenPageObjects()
val regionListsStepObjects = RegionListsStepObjects()
val regionListsPageObjects = RegionListsPageObjects()

@Test
fun searchValidRegion () {
successfulLogin()
mainScreenPageObjects.currentRegion.click()
regionListsStepObjects.regionInput("Belgium")
assert(regionListsPageObjects.regionName.text.equals("Belgium"))
}

@Test
fun searchInvalidRegion () {
successfulLogin()
mainScreenPageObjects.currentRegion.click()
regionListsStepObjects.regionInput("invalidRegion")
assert(regionListsPageObjects.regionNoResultImage.exists())
}

@Test
fun connectToRegion() {
successfulLogin()
mainScreenPageObjects.currentRegion.click()
val selectedRegion = regionListsStepObjects.selectFromVisibleRegionName()

val conditions = listOf(
{mainScreenPageObjects.regionNameSelected.text.equals(selectedRegion)},
{mainScreenPageObjects.connectButton.contentDescription.equals("VPN Connected")},
)
for (condition in conditions) {
Assert.assertTrue(condition())
}
}
}