Skip to content

Commit

Permalink
Conditionally click the payment selection if needed.
Browse files Browse the repository at this point in the history
  • Loading branch information
samer-stripe committed May 24, 2024
1 parent 1dd94a6 commit c085514
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ import com.stripe.android.paymentsheet.example.playground.settings.CheckoutMode
import com.stripe.android.paymentsheet.example.playground.settings.CheckoutModeSettingsDefinition
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.PaymentMethodOrderSettingsDefinition
import com.stripe.android.paymentsheet.example.playground.settings.PlaygroundConfigurationData
import com.stripe.android.paymentsheet.example.playground.utils.commaSeparatedToList
import com.stripe.android.paymentsheet.ui.PAYMENT_SHEET_ERROR_TEXT_TEST_TAG
import com.stripe.android.paymentsheet.ui.SAVED_PAYMENT_METHOD_CARD_TEST_TAG
import com.stripe.android.test.core.ui.BrowserUI
Expand Down Expand Up @@ -331,7 +333,9 @@ internal class PlaygroundTestDriver(

launchCustomerSheet()

selectors.paymentSelection.click()
clickPaymentSelection()

selectors.formElement.waitFor()

val fieldPopulator = FieldPopulator(
selectors,
Expand Down Expand Up @@ -798,6 +802,20 @@ internal class PlaygroundTestDriver(
.performClick()
}

private fun clickPaymentSelection() {
val paymentMethodOrder = testParameters.playgroundSettingsSnapshot[PaymentMethodOrderSettingsDefinition]
.commaSeparatedToList().ifEmpty {
listOf("card")
}

if (paymentMethodOrder.first() == testParameters.paymentMethodCode) {
selectors.paymentSelection.click()
}

Espresso.onIdle()
composeTestRule.waitForIdle()
}

/**
* Here we wait for an activity different from the playground to be in view. We
* don't specifically look for PaymentSheetActivity or PaymentOptionsActivity because
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.stripe.android.test.core.ui

import androidx.compose.ui.test.ExperimentalTestApi
import androidx.compose.ui.test.hasTestTag
import androidx.compose.ui.test.junit4.ComposeTestRule
import com.stripe.android.paymentsheet.ui.FORM_ELEMENT_TEST_TAG
import com.stripe.android.test.core.DEFAULT_UI_TIMEOUT

class FormElement(
private val composeTestRule: ComposeTestRule
) {
@OptIn(ExperimentalTestApi::class)
fun waitFor() {
composeTestRule.waitUntilExactlyOneExists(
hasTestTag(FORM_ELEMENT_TEST_TAG),
DEFAULT_UI_TIMEOUT.inWholeMilliseconds
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ internal class Selectors(
testParameters.paymentMethodCode
)

val formElement = FormElement(composeTestRule)

val mandateText = composeTestRule.onNodeWithTag(MANDATE_TEST_TAG)

val buyButton = BuyButton(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import com.stripe.android.customersheet.CustomerSheet
import com.stripe.android.customersheet.ExperimentalCustomerSheetApi
import com.stripe.android.paymentsheet.PaymentSheet
import com.stripe.android.paymentsheet.example.playground.PlaygroundState
import com.stripe.android.paymentsheet.example.playground.utils.commaSeparatedToList

internal object PaymentMethodOrderSettingsDefinition :
PlaygroundSettingDefinition<String>,
Expand All @@ -27,7 +28,7 @@ internal object PaymentMethodOrderSettingsDefinition :
configurationData: PlaygroundSettingDefinition.PaymentSheetConfigurationData
) {
if (value.isNotEmpty()) {
configurationBuilder.paymentMethodOrder(value.split(",").map { it.trim() })
configurationBuilder.paymentMethodOrder(value.commaSeparatedToList())
}
}

Expand All @@ -39,7 +40,7 @@ internal object PaymentMethodOrderSettingsDefinition :
configurationData: PlaygroundSettingDefinition.CustomerSheetConfigurationData,
) {
if (value.isNotEmpty()) {
configurationBuilder.paymentMethodOrder(value.split(",").map { it.trim() })
configurationBuilder.paymentMethodOrder(value.commaSeparatedToList())
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.stripe.android.paymentsheet.example.playground.utils

fun String.commaSeparatedToList(): List<String> {
return split(",").map { it.trim() }
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import androidx.compose.ui.focus.onFocusChanged
import androidx.compose.ui.input.pointer.PointerEventType
import androidx.compose.ui.input.pointer.pointerInput
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.platform.testTag
import androidx.compose.ui.res.dimensionResource
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
Expand Down Expand Up @@ -116,6 +117,7 @@ internal fun FormElement(

Box(
modifier = Modifier
.testTag(FORM_ELEMENT_TEST_TAG)
.pointerInput("AddPaymentMethod") {
awaitEachGesture {
val gesture = awaitPointerEvent()
Expand Down Expand Up @@ -184,3 +186,5 @@ internal fun LinkElement(
}
}
}

const val FORM_ELEMENT_TEST_TAG = "FORM_ELEMENT_UI"

0 comments on commit c085514

Please sign in to comment.