Skip to content

Commit

Permalink
feat: Add swipe support in Compose mode (#850)
Browse files Browse the repository at this point in the history
  • Loading branch information
mykola-mokhnach committed Jan 16, 2023
1 parent b9a2fd9 commit 97d2064
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 15 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,8 @@ In order to change between subdrivers use the [driver](#settings-api) setting. S
- click, isDisplayed, isEnabled, clear, getText, sendKeys, getElementRect, getValue, isSelected: These commands should properly support compose elements.
- getAttribute: Accepts and returns Compose-specific element attributes. See [Compose Element Attributes](#compose-element-attributes) for the full list of supported Compose element attributes.
- getElementScreenshot: Fetches a screenshot of the given Compose element. Available since driver version *2.14.0*
- `mobile: swipe`: Performs swipe gesture on the given element in the given direction.
The `swiper` argument is not supported in Compose mode. Available since driver version *2.15.0*

Calling other driver element-specific APIs not listed above would most likely throw an exception as Compose and Espresso elements are being stored in completely separated internal caches and must not be mixed.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,17 @@

package io.appium.espressoserver.lib.handlers

import androidx.compose.ui.test.performGesture
import androidx.compose.ui.test.swipeDown

import androidx.compose.ui.test.swipeLeft
import androidx.compose.ui.test.swipeRight
import androidx.compose.ui.test.swipeUp
import androidx.test.espresso.UiController
import androidx.test.espresso.action.GeneralSwipeAction
import androidx.test.espresso.action.ViewActions.*
import io.appium.espressoserver.lib.handlers.exceptions.AppiumException
import io.appium.espressoserver.lib.handlers.exceptions.InvalidArgumentException
import io.appium.espressoserver.lib.helpers.AndroidLogger
import io.appium.espressoserver.lib.helpers.getNodeInteractionById
import io.appium.espressoserver.lib.model.EspressoElement
import io.appium.espressoserver.lib.model.MobileSwipeParams
import io.appium.espressoserver.lib.model.MobileSwipeParams.Direction.*
Expand All @@ -31,35 +36,38 @@ import io.appium.espressoserver.lib.viewaction.ViewGetter

class MobileSwipe : RequestHandler<MobileSwipeParams, Void?> {

@Throws(AppiumException::class)
override fun handleInternal(params: MobileSwipeParams): Void? {
override fun handleEspresso(params: MobileSwipeParams): Void? {
// Get a reference to the view and call onData. This will automatically scroll to the view.
val viewInteraction = EspressoElement.getViewInteractionById(params.elementId)

if (params.direction != null) {
AndroidLogger.info("Performing swipe action with direction '${params.direction}'")
when (params.direction) {
UP -> viewInteraction.perform(swipeUp())
DOWN -> viewInteraction.perform(swipeDown())
LEFT -> viewInteraction.perform(swipeLeft())
RIGHT -> viewInteraction.perform(swipeRight())
else -> throw InvalidArgumentException("Direction cannot be ${params.direction}")
UP -> viewInteraction.perform(androidx.test.espresso.action.ViewActions.swipeUp())
DOWN -> viewInteraction.perform(androidx.test.espresso.action.ViewActions.swipeDown())
LEFT -> viewInteraction.perform(androidx.test.espresso.action.ViewActions.swipeLeft())
RIGHT -> viewInteraction.perform(androidx.test.espresso.action.ViewActions.swipeRight())
else -> throw InvalidArgumentException(
"Unknown swipe direction '${params.direction}'. " +
"Only the following values are supported: " +
values().joinToString(",") { x -> x.name.lowercase() }
)
}
} else if (params.swiper != null) {

val runnable = object : UiControllerRunnable<Void?> {
override fun run(uiController: UiController): Void? {
val swipeAction = GeneralSwipeAction(
params.swiper,
params.startCoordinates,
params.endCoordinates,
params.precisionDescriber
params.swiper,
params.startCoordinates,
params.endCoordinates,
params.precisionDescriber
)
AndroidLogger.info("""
Performing general swipe action with parameters
swiper=[${params.swiper}] startCoordinates=[${params.startCoordinates}]
endCoordinates=[${params.endCoordinates}] precisionDescriber=[${params.precisionDescriber}]
""".trimIndent())
""".trimIndent()
)
swipeAction.perform(uiController, ViewGetter().getView(viewInteraction))
return null
}
Expand All @@ -69,4 +77,22 @@ class MobileSwipe : RequestHandler<MobileSwipeParams, Void?> {

return null
}

override fun handleCompose(params: MobileSwipeParams): Void? {
val nodeInteractions = getNodeInteractionById(params.elementId)

AndroidLogger.info("Performing swipe action with direction '${params.direction}'")
when (params.direction) {
UP -> nodeInteractions.performGesture { swipeUp() }
DOWN -> nodeInteractions.performGesture { swipeDown() }
LEFT -> nodeInteractions.performGesture { swipeLeft() }
RIGHT -> nodeInteractions.performGesture { swipeRight() }
else -> throw InvalidArgumentException(
"Unknown swipe direction '${params.direction}'. " +
"Only the following values are supported: " +
values().joinToString(",") { x -> x.name.lowercase() }
)
}
return null
}
}

0 comments on commit 97d2064

Please sign in to comment.