Skip to content

Commit

Permalink
Add Google Pay tests
Browse files Browse the repository at this point in the history
  • Loading branch information
samer-stripe committed Aug 29, 2024
1 parent 52f21eb commit be1137a
Show file tree
Hide file tree
Showing 7 changed files with 156 additions and 1 deletion.
18 changes: 18 additions & 0 deletions bitrise.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ stages:
- run-cardscan-instrumentation-tests: { }
- run-paymentsheet-end-to-end-tests: { }
- run-paymentsheet-screenshot-tests: { }
- run-google-pay-tests: { }
- check-dependencies: { }
stage-build-connections-debug:
workflows:
Expand Down Expand Up @@ -104,6 +105,23 @@ workflows:
- deploy-to-bitrise-io@2:
inputs:
- pipeline_intermediate_files: "$BITRISE_APK_PATH:BITRISE_APK_PATH"
run-google-pay-tests:
before_run:
- prepare_all
after_run:
- conclude_all
steps:
- android-build-for-ui-testing:
inputs:
- module: paymentsheet-example
- variant: debug
- virtual-device-testing-for-android:
inputs:
- test_type: instrumentation
- auto_google_login: "true"
- inst_test_targets: class com.stripe.android.lpm.TestGooglePay
- num_flaky_test_attempts: 3
- test_devices: 'NexusLowRes,28,en,portrait'
run-connections-e2e-payments-tests:
envs:
- MAESTRO_TAGS: testmode-payments
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@ import androidx.test.platform.app.InstrumentationRegistry
import androidx.test.uiautomator.UiDevice
import com.stripe.android.test.core.PlaygroundTestDriver
import com.stripe.android.utils.TestRules
import leakcanary.LeakCanary
import org.junit.After
import org.junit.Before
import org.junit.Rule
import shark.LibraryLeakReferenceMatcher
import shark.ReferencePattern

internal open class BasePlaygroundTest(disableAnimations: Boolean = true) {
@get:Rule
Expand All @@ -16,6 +19,13 @@ internal open class BasePlaygroundTest(disableAnimations: Boolean = true) {
lateinit var device: UiDevice
lateinit var testDriver: PlaygroundTestDriver

init {
LeakCanary.config = LeakCanary.config.copy(
referenceMatchers = LeakCanary.config.referenceMatchers +
LibraryLeakReferenceMatcher(ReferencePattern.JavaLocalPattern("hh.a"))
)
}

@Before
fun before() {
device = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation())
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.stripe.android.lpm

import androidx.test.ext.junit.runners.AndroidJUnit4
import com.stripe.android.BasePlaygroundTest
import com.stripe.android.paymentsheet.example.playground.settings.Country
import org.junit.Test
import org.junit.runner.RunWith

@RunWith(AndroidJUnit4::class)
internal class TestGooglePay : BasePlaygroundTest() {
@Test
fun testUnitedStates() {
testDriver.confirmWithGooglePay(Country.US)
}

@Test
fun testFrance() {
testDriver.confirmWithGooglePay(Country.FR)
}

@Test
fun testGreatBritain() {
testDriver.confirmWithGooglePay(Country.GB)
}

@Test
fun testAustralia() {
testDriver.confirmWithGooglePay(Country.AU)
}

@Test
fun testBrazil() {
testDriver.confirmWithGooglePay(Country.BR)
}

@Test
fun testJapan() {
testDriver.confirmWithGooglePay(Country.JP)
}

@Test
fun testMexico() {
testDriver.confirmWithGooglePay(Country.MX)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ import com.stripe.android.paymentsheet.example.playground.PaymentSheetPlayground
import com.stripe.android.paymentsheet.example.playground.PlaygroundState
import com.stripe.android.paymentsheet.example.playground.SUCCESS_RESULT
import com.stripe.android.paymentsheet.example.playground.activity.FawryActivity
import com.stripe.android.paymentsheet.example.playground.settings.Country
import com.stripe.android.paymentsheet.example.playground.settings.CountrySettingsDefinition
import com.stripe.android.paymentsheet.example.playground.settings.CustomerSettingsDefinition
import com.stripe.android.paymentsheet.example.playground.settings.CustomerType
import com.stripe.android.paymentsheet.example.playground.settings.PlaygroundConfigurationData
Expand Down Expand Up @@ -412,6 +414,43 @@ internal class PlaygroundTestDriver(
return result
}

fun confirmWithGooglePay(
country: Country
) {
setup(
TestParameters.create(
paymentMethodCode = "card",
) { settings ->
settings[CountrySettingsDefinition] = country
}
)

launchComplete()

Espresso.onIdle()
composeTestRule.waitForIdle()

selectors.googlePayButton.waitForEnabled()
selectors.googlePayButton.click()

composeTestRule.waitForIdle()

selectors.googlePaySheet.waitFor()
selectors.googlePayContinueButton.click()

composeTestRule.waitForIdle()

// Skips the full screen payment animation in `PaymentSheet`
while (currentActivity !is PaymentSheetPlaygroundActivity) {
composeTestRule.mainClock.advanceTimeByFrame()
}

Espresso.onIdle()
composeTestRule.waitForIdle()

teardown()
}

private fun pressMultiStepSelect() {
selectors.multiStepSelect.click()
waitForNotPlaygroundActivity()
Expand Down Expand Up @@ -1227,6 +1266,7 @@ internal class PlaygroundTestDriver(

internal fun teardown() {
application?.unregisterActivityLifecycleCallbacks(activityLifecycleCallbacks)
application = null
playgroundState = null
currentActivity = null
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import com.stripe.android.paymentsheet.example.playground.RELOAD_TEST_TAG
import com.stripe.android.paymentsheet.example.playground.activity.FawryActivity
import com.stripe.android.paymentsheet.example.samples.ui.shared.CHECKOUT_TEST_TAG
import com.stripe.android.paymentsheet.example.samples.ui.shared.PAYMENT_METHOD_SELECTOR_TEST_TAG
import com.stripe.android.paymentsheet.ui.GOOGLE_PAY_BUTTON_TEST_TAG
import com.stripe.android.test.core.AuthorizeAction
import com.stripe.android.test.core.DEFAULT_UI_TIMEOUT
import com.stripe.android.test.core.HOOKS_PAGE_LOAD_TIMEOUT
Expand Down Expand Up @@ -104,6 +105,22 @@ internal class Selectors(
hasTestTag(FawryActivity.FAILED_BUTTON_TEST_TAG)
)

val googlePayButton = ComposeButton(
composeTestRule,
hasTestTag(GOOGLE_PAY_BUTTON_TEST_TAG)
)

val googlePaySheet = UiAutomatorWindow(
device,
"com.google.android.gms"
)

val googlePayContinueButton = UiAutomatorText(
"Continue",
"android.widget.Button",
device
)

val playgroundBuyButton = ComposeButton(composeTestRule, hasTestTag(CHECKOUT_TEST_TAG))

val addPaymentMethodButton = AddPaymentMethodButton(device)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.stripe.android.test.core.ui

import androidx.test.uiautomator.UiDevice
import com.stripe.android.test.core.DEFAULT_UI_TIMEOUT

class UiAutomatorWindow(
private val device: UiDevice,
private val packageName: String
) {
fun waitFor() {
device.waitForWindowUpdate(packageName, DEFAULT_UI_TIMEOUT.inWholeMilliseconds)
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.stripe.android.paymentsheet.ui

import androidx.annotation.RestrictTo
import androidx.compose.foundation.isSystemInDarkTheme
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxWidth
Expand All @@ -10,6 +11,8 @@ import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.platform.LocalInspectionMode
import androidx.compose.ui.platform.testTag
import androidx.compose.ui.res.colorResource
import androidx.compose.ui.semantics.onClick
import androidx.compose.ui.semantics.semantics
import com.google.pay.button.ButtonTheme
import com.google.pay.button.ButtonType
import com.google.pay.button.PayButton
Expand Down Expand Up @@ -60,6 +63,13 @@ internal fun GooglePayButton(
is PrimaryButton.State.Ready -> PayButton(
modifier = modifier
.fillMaxWidth()
.semantics {
onClick {
onPressed()

true
}
}
.testTag(GOOGLE_PAY_BUTTON_TEST_TAG),
allowedPaymentMethods = allowedPaymentMethods,
type = buttonType.toComposeButtonType(),
Expand Down Expand Up @@ -132,5 +142,7 @@ private fun GooglePayButtonType.toComposeButtonType(): ButtonType {
}
}

internal const val GOOGLE_PAY_BUTTON_TEST_TAG = "google-pay-button"
@RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
const val GOOGLE_PAY_BUTTON_TEST_TAG = "google-pay-button"

internal const val GOOGLE_PAY_PRIMARY_BUTTON_TEST_TAG = "google-pay-primary-button"

0 comments on commit be1137a

Please sign in to comment.