From 6efba9ee02d9768be56f6aa07711be88eae8887c Mon Sep 17 00:00:00 2001 From: kp-ruth-o Date: Tue, 31 Oct 2023 15:48:52 +0800 Subject: [PATCH] PIA-636: Add new test for ServerLists Add new test for serverlist that checks for valid region, invalid region and connecting from serverlist page. --- .../screens/objects/MainScreenPageObjects.kt | 3 ++ .../screens/objects/RegionListsPageObjects.kt | 9 ++++ .../screens/steps/RegionListsStepObjects.kt | 36 ++++++++++++++ .../java/tests/RegionListsTests.kt | 47 +++++++++++++++++++ 4 files changed, 95 insertions(+) create mode 100644 app/src/androidTest/java/screens/objects/RegionListsPageObjects.kt create mode 100644 app/src/androidTest/java/screens/steps/RegionListsStepObjects.kt create mode 100644 app/src/androidTest/java/tests/RegionListsTests.kt diff --git a/app/src/androidTest/java/screens/objects/MainScreenPageObjects.kt b/app/src/androidTest/java/screens/objects/MainScreenPageObjects.kt index e9e05ea..59964d7 100644 --- a/app/src/androidTest/java/screens/objects/MainScreenPageObjects.kt +++ b/app/src/androidTest/java/screens/objects/MainScreenPageObjects.kt @@ -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") + } \ No newline at end of file diff --git a/app/src/androidTest/java/screens/objects/RegionListsPageObjects.kt b/app/src/androidTest/java/screens/objects/RegionListsPageObjects.kt new file mode 100644 index 0000000..12af6a9 --- /dev/null +++ b/app/src/androidTest/java/screens/objects/RegionListsPageObjects.kt @@ -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") +} \ No newline at end of file diff --git a/app/src/androidTest/java/screens/steps/RegionListsStepObjects.kt b/app/src/androidTest/java/screens/steps/RegionListsStepObjects.kt new file mode 100644 index 0000000..93ee1c5 --- /dev/null +++ b/app/src/androidTest/java/screens/steps/RegionListsStepObjects.kt @@ -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() + + 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] + } +} \ No newline at end of file diff --git a/app/src/androidTest/java/tests/RegionListsTests.kt b/app/src/androidTest/java/tests/RegionListsTests.kt new file mode 100644 index 0000000..ac3663a --- /dev/null +++ b/app/src/androidTest/java/tests/RegionListsTests.kt @@ -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()) + } + } +} \ No newline at end of file