|
| 1 | +package com.frontegg.demo |
| 2 | + |
| 3 | +import androidx.test.uiautomator.By |
| 4 | +import com.frontegg.demo.utils.Env |
| 5 | +import com.frontegg.demo.utils.UiTestInstrumentation |
| 6 | +import com.frontegg.demo.utils.delay |
| 7 | +import com.frontegg.demo.utils.logout |
| 8 | +import com.frontegg.demo.utils.tapLoginButton |
| 9 | +import org.junit.Assume.assumeTrue |
| 10 | +import org.junit.Before |
| 11 | +import org.junit.Test |
| 12 | +import java.util.regex.Pattern |
| 13 | + |
| 14 | +/** |
| 15 | + * P2: Verifies Google social login via Chrome Custom Tabs. |
| 16 | + * |
| 17 | + * Mirrors frontegg-android-kotlin's LoginViaGoogleTest. The flow: |
| 18 | + * 1. Tap Login on the Ionic login page. |
| 19 | + * 2. On the Frontegg hosted login, tap the Google social button. |
| 20 | + * 3. Chrome Custom Tab opens with Google's sign-in. |
| 21 | + * 4. Authenticate with Google test credentials. |
| 22 | + * 5. Return to the app — Logout button appears. |
| 23 | + * |
| 24 | + * Requires GOOGLE_EMAIL and GOOGLE_PASSWORD instrumentation arguments. |
| 25 | + * Skipped if credentials are not provided. |
| 26 | + */ |
| 27 | +class GoogleSocialLoginTest { |
| 28 | + private lateinit var instrumentation: UiTestInstrumentation |
| 29 | + |
| 30 | + @Before |
| 31 | + fun setUp() { |
| 32 | + instrumentation = UiTestInstrumentation() |
| 33 | + instrumentation.openApp() |
| 34 | + } |
| 35 | + |
| 36 | + @Test |
| 37 | + fun success_login_with_google() { |
| 38 | + // Skip if Google credentials not provided. |
| 39 | + assumeTrue( |
| 40 | + "GOOGLE_EMAIL not set — skipping Google social login test", |
| 41 | + Env.googleEmail.isNotEmpty() |
| 42 | + ) |
| 43 | + assumeTrue( |
| 44 | + "GOOGLE_PASSWORD not set — skipping Google social login test", |
| 45 | + Env.googlePassword.isNotEmpty() |
| 46 | + ) |
| 47 | + |
| 48 | + // 1. Open the Frontegg hosted login. |
| 49 | + instrumentation.tapLoginButton() |
| 50 | + |
| 51 | + // 2. Tap the Google social login button on the Frontegg hosted page. |
| 52 | + // The button may be labeled "Continue with Google" or show a Google icon. |
| 53 | + val googleButton = instrumentation.waitForView( |
| 54 | + By.text(Pattern.compile(".*google.*", Pattern.CASE_INSENSITIVE)), |
| 55 | + timeout = 10_000 |
| 56 | + ) ?: throw Exception("Google social login button not found on hosted login page") |
| 57 | + googleButton.click() |
| 58 | + |
| 59 | + // 3. Chrome Custom Tab opens. Wait for Google's sign-in page. |
| 60 | + delay(3_000) // allow Chrome Custom Tab to open |
| 61 | + |
| 62 | + // Handle "Choose an account" / sign-in form. |
| 63 | + // First, try to find an email input field. |
| 64 | + val emailField = instrumentation.waitForView( |
| 65 | + By.clazz(android.widget.EditText::class.java), |
| 66 | + timeout = 10_000 |
| 67 | + ) |
| 68 | + if (emailField != null) { |
| 69 | + emailField.text = Env.googleEmail |
| 70 | + instrumentation.clickByText("Next", timeout = 5_000) |
| 71 | + |
| 72 | + delay(2_000) |
| 73 | + |
| 74 | + // Password field. |
| 75 | + val passwordField = instrumentation.waitForView( |
| 76 | + By.clazz(android.widget.EditText::class.java), |
| 77 | + timeout = 10_000 |
| 78 | + ) |
| 79 | + if (passwordField != null) { |
| 80 | + passwordField.text = Env.googlePassword |
| 81 | + instrumentation.clickByText("Next", timeout = 5_000) |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + // 4. Handle potential consent screens ("Accept & continue", "No thanks"). |
| 86 | + delay(2_000) |
| 87 | + instrumentation.clickByText("Accept & continue", timeout = 3_000) |
| 88 | + delay(1_000) |
| 89 | + instrumentation.clickByText("No thanks", timeout = 3_000) |
| 90 | + |
| 91 | + // 5. Verify authenticated state — Logout button appears. |
| 92 | + val logoutPattern = Pattern.compile("logout", Pattern.CASE_INSENSITIVE) |
| 93 | + instrumentation.waitForView(By.text(logoutPattern), timeout = 30_000) |
| 94 | + ?: throw Exception("Google login did not complete — Logout button not found") |
| 95 | + |
| 96 | + // 6. Cleanup. |
| 97 | + instrumentation.logout() |
| 98 | + } |
| 99 | +} |
0 commit comments