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

POC of using page object pattern #7017

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions android/test/common/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,5 @@ dependencies {
implementation(libs.kotlin.stdlib)

androidTestUtil(libs.androidx.test.orchestrator)
implementation(kotlin("reflect"))
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package net.mullvad.mullvadvpn.test.common.page

import androidx.test.uiautomator.By
import net.mullvad.mullvadvpn.test.common.extension.findObjectWithTimeout

class ConnectPage internal constructor() : Page() {
override fun assertIsDisplayed() {
uiDevice.findObjectWithTimeout(By.res("connect_card_header_test_tag"))
}

fun clickConnect() {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package net.mullvad.mullvadvpn.test.common.page

import android.widget.Button
import androidx.test.uiautomator.By
import androidx.test.uiautomator.Until
import net.mullvad.mullvadvpn.test.common.constant.DEFAULT_TIMEOUT
import net.mullvad.mullvadvpn.test.common.constant.EXTREMELY_LONG_TIMEOUT
import net.mullvad.mullvadvpn.test.common.extension.findObjectWithTimeout

class LoginPage internal constructor() : Page() {
fun enterAccountNumber(accountNumber: String) {
uiDevice.findObjectWithTimeout(By.clazz("android.widget.EditText")).text = accountNumber
}

fun tapLoginButton() {
val accountTextField = uiDevice.findObjectWithTimeout(By.clazz("android.widget.EditText"))
val loginButton = accountTextField.parent.findObject(By.clazz(Button::class.java))
loginButton.wait(Until.enabled(true), DEFAULT_TIMEOUT)
loginButton.click()
}

fun verifyShowingInvalidAccount() {
uiDevice.findObjectWithTimeout(By.text("Invalid account number"), EXTREMELY_LONG_TIMEOUT)
}

override fun assertIsDisplayed() {
uiDevice.findObjectWithTimeout(By.text("Login"))
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package net.mullvad.mullvadvpn.test.common.page

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

sealed class Page {
protected val uiDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation())

abstract fun assertIsDisplayed()
}

inline fun <reified T : Page> on(scope: T.() -> Unit = {}) {
val page = T::class.constructors.first().call()
page.assertIsDisplayed()
return page.scope()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package net.mullvad.mullvadvpn.test.common.page

import android.os.Build
import androidx.test.uiautomator.By
import androidx.test.uiautomator.Until
import net.mullvad.mullvadvpn.test.common.constant.DEFAULT_TIMEOUT
import net.mullvad.mullvadvpn.test.common.extension.findObjectWithTimeout

class PrivacyPage internal constructor() : Page() {
override fun assertIsDisplayed() {
// uiDevice.findObjectWithTimeout(By.res("connect_card_header_test_tag"))
}

fun clickAgreeOnPrivacyDisclaimer() {
uiDevice.findObjectWithTimeout(By.text("Agree and continue")).click()
}

fun clickAllowOnNotificationPermissionPromptIfApiLevel33AndAbove(
timeout: Long = DEFAULT_TIMEOUT
) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU) {
// Skipping as notification permissions are not shown.
return
}

val selector = By.text("Allow")

uiDevice.wait(Until.hasObject(selector), timeout)

try {
uiDevice.findObjectWithTimeout(selector).click()
} catch (e: IllegalArgumentException) {
throw IllegalArgumentException(
"Failed to allow notification permission within timeout ($timeout)"
)
}
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package net.mullvad.mullvadvpn.test.e2e

import androidx.test.uiautomator.By
import net.mullvad.mullvadvpn.test.common.constant.EXTREMELY_LONG_TIMEOUT
import net.mullvad.mullvadvpn.test.common.extension.clickAgreeOnPrivacyDisclaimer
import net.mullvad.mullvadvpn.test.common.extension.clickAllowOnNotificationPermissionPromptIfApiLevel33AndAbove
import net.mullvad.mullvadvpn.test.common.extension.findObjectWithTimeout
import net.mullvad.mullvadvpn.test.common.page.ConnectPage
import net.mullvad.mullvadvpn.test.common.page.LoginPage
import net.mullvad.mullvadvpn.test.common.page.PrivacyPage
import net.mullvad.mullvadvpn.test.common.page.on
import net.mullvad.mullvadvpn.test.e2e.misc.AccountTestRule
import org.junit.jupiter.api.Disabled
import org.junit.jupiter.api.Test
Expand All @@ -16,30 +15,39 @@ class LoginTest : EndToEndTest(BuildConfig.FLAVOR_infrastructure) {

@Test
fun testLoginWithValidCredentials() {
// Given
val validTestAccountNumber = accountTestRule.validAccountNumber

// When
app.launchAndEnsureLoggedIn(validTestAccountNumber)
app.launch()

on<PrivacyPage> {
clickAgreeOnPrivacyDisclaimer()
clickAllowOnNotificationPermissionPromptIfApiLevel33AndAbove()
}

// Then
app.ensureLoggedIn()
on<LoginPage> {
enterAccountNumber(validTestAccountNumber)
tapLoginButton()
}

on<ConnectPage>()
}

@Test
@Disabled("Failed login attempts are highly rate limited and cause test flakiness")
fun testLoginWithInvalidCredentials() {
// Given
val invalidDummyAccountNumber = accountTestRule.invalidAccountNumber

// When
app.launch()
device.clickAgreeOnPrivacyDisclaimer()
device.clickAllowOnNotificationPermissionPromptIfApiLevel33AndAbove()
app.waitForLoginPrompt()
app.attemptLogin(invalidDummyAccountNumber)

// Then
device.findObjectWithTimeout(By.text("Invalid account number"), EXTREMELY_LONG_TIMEOUT)
on<PrivacyPage> {
clickAgreeOnPrivacyDisclaimer()
clickAllowOnNotificationPermissionPromptIfApiLevel33AndAbove()
}

on<LoginPage> {
enterAccountNumber(invalidDummyAccountNumber)
tapLoginButton()
verifyShowingInvalidAccount()
}
}
}
Loading