Appium Automating Gesture Actions in Appium Estimated reading: 5 minutes 251 views In the previous guide, we learned about Appium UI Locator Strategies. Now, let’s take it a step further by automating gesture actions. Gestures such as swipes, scrolls, and taps are essential for comprehensive mobile app testing.Common Gesture ActionsAlright, let’s dive into Gesture Actions in Appium. These actions are essential for simulating complex interactions with your mobile app, mimicking how a real user would engage with it. Whether you need to swipe, pinch, zoom, or perform a drag-and-drop action, Appium’s gesture commands have you covered. These gestures are critical for testing user interfaces that rely heavily on touch interactions, ensuring your app behaves as expected in the hands of actual users.Create Gestures Util ClassLet’s dive right in and enhance our Appium project by adding a new class to handle gesture methods efficiently. To begin, we’ll create a new class called GesturesUtil which will serve as a repository for various gesture methods. This step is essential to keep our code organized and maintainable. First, navigate to your project directory and create a new package namedcom.helpers. This package will be the home for our GesturesUtilclass. Organizing your code into packages not only improves readability but also aligns with best practices in software development, making your project more modular and easier to manage.Import DependenciesIn the com.helpers package, create the GesturesUtil class. This class will encapsulate all the gesture methods you need for your Appium tests, such as swipe, tap, and scroll. To get started, the first step is importing the necessary dependencies. In your project, add the following imports:GesturesUtil.classimport io.appium.java_client.AppiumDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.interactions.PointerInput; import org.openqa.selenium.interactions.Sequence; import java.time.Duration; import java.util.Arrays; import java.util.Collections;With these imports in place, you can now proceed to define your gesture methods without encountering any compilation issues. This setup ensures that your code is well-structured and ready to handle a variety of automated testing scenarios, making your Appium testing framework robust and efficient.Simulating Tap or Long Press A tap is simply a quick touch on the screen, often used for selecting items, while a long press involves holding down on an element for a longer duration. This is typically used for actions like bringing up a context menu or initiating a special function. Appium’s capabilities allow you to simulate both, ensuring that these interactions are tested thoroughly.Gesture: tapElementpublic void tapElement(WebElement element) { PointerInput finger = new PointerInput(PointerInput.Kind.TOUCH, "finger"); Sequence tap = new Sequence(finger, 1) .addAction(finger.createPointerMove(Duration.ZERO, PointerInput.Origin.viewport(), element.getLocation().x + element.getSize().width / 2, element.getLocation().y + element.getSize().height / 2)) .addAction(finger.createPointerDown(PointerInput.MouseButton.LEFT.asArg())) .addAction(finger.createPointerUp(PointerInput.MouseButton.LEFT.asArg())); driver.perform(Collections.singletonList(tap)); }TapThe tapElement method simulates a tap on a web element by: Creating a touch input named finger. Creating a sequence of actions that: - Moves the touch pointer to the center of the specified element. - Presses down on the element. - Releases the press. Perform the sequence of actions using the driver.Gesture: Long Presspublic void longPressElement(WebElement element, Duration duration) { PointerInput finger = new PointerInput(PointerInput.Kind.TOUCH, "finger"); Sequence longPress = new Sequence(finger, 1) .addAction(finger.createPointerMove(Duration.ZERO, PointerInput.Origin.viewport(), element.getLocation().x + element.getSize().width / 2, element.getLocation().y + element.getSize().height / 2)) .addAction(finger.createPointerDown(PointerInput.MouseButton.LEFT.asArg())) .addAction(finger.createPointerMove(duration, PointerInput.Origin.viewport(), element.getLocation().x,element.getLocation().y)) .addAction(finger.createPointerUp(PointerInput.MouseButton.LEFT.asArg())); driver.perform(Collections.singletonList(longPress)); }Long PressThe longPressElement method simulates a long press on a mobile or web element by: Moving the virtual finger to the center of the element. Pressing down on the element. Holding the press for a specified duration. Releasing the press.Simulating Swipe GesturesIn Appium, swiping is straightforward but powerful, allowing you to simulate the user’s finger moving across the screen.To implement a swipe, you define the start and end coordinates, which tells Appium where the swipe should begin and end. By fine-tuning these coordinates, you can create swipes that mimic natural user behavior, ensuring your test cases are as realistic as possible.Gesture: Swipepublic void swipe(int startX, int startY, int endX, int endY, Duration duration) { PointerInput finger = new PointerInput(PointerInput.Kind.TOUCH, "finger"); Sequence swipe = new Sequence(finger, 1) .addAction(finger.createPointerMove(Duration.ZERO, PointerInput.Origin.viewport(), startX, startY)) .addAction(finger.createPointerDown(PointerInput.MouseButton.LEFT.asArg())) .addAction(finger.createPointerMove(duration, PointerInput.Origin.viewport(), endX, endY)) .addAction(finger.createPointerUp(PointerInput.MouseButton.LEFT.asArg())); driver.perform(Collections.singletonList(swipe)); }SwipeThe swipe function simulates a swipe gesture on a touch screen. First, it moves the touch pointer from a starting point (startX, startY) Next it moves to the ending point (endX, endY) over a given duration. The process involves moving the pointer to the start position, touching down, moving to the end position, and then lifting up All of these actions are coordinated through a sequence of actions performed by the driver.Simulating a Zoom InNext we have pinch and zoom gestures, which are indispensable for applications that involve maps, images, or any form of visual content that users might need to zoom in and out of.Gesture: Zoom Inpublic void zoomIn(WebElement element) { int centerX = element.getLocation().getX() + element.getSize().getWidth() / 2; int centerY = element.getLocation().getY() + element.getSize().getHeight() / 2; int endX1 = centerX - 100; int endY1 = centerY - 100; int endX2 = centerX + 100; int endY2 = centerY + 100; PointerInput finger1 = new PointerInput(PointerInput.Kind.TOUCH, "finger1"); Sequence pinch1 = new Sequence(finger1, 1) .addAction(finger1.createPointerMove(Duration.ZERO, PointerInput.Origin.viewport(), centerX, centerY)) .addAction(finger1.createPointerDown(PointerInput.MouseButton.LEFT.asArg())) .addAction(finger1.createPointerMove(Duration.ofMillis(600), PointerInput.Origin.viewport(), endX1, endY1)) .addAction(finger1.createPointerUp(PointerInput.MouseButton.LEFT.asArg())); PointerInput finger2 = new PointerInput(PointerInput.Kind.TOUCH, "finger2"); Sequence pinch2 = new Sequence(finger2, 1) .addAction(finger2.createPointerMove(Duration.ZERO, PointerInput.Origin.viewport(), centerX, centerY)) .addAction(finger2.createPointerDown(PointerInput.MouseButton.LEFT.asArg())) .addAction(finger2.createPointerMove(Duration.ofMillis(600), PointerInput.Origin.viewport(), endX2, endY2)) .addAction(finger2.createPointerUp(PointerInput.MouseButton.LEFT.asArg())); driver.perform(Arrays.asList(pinch1, pinch2)); }PinchingPinching involves bringing two fingers together on the screen, while zooming involves spreading them apart. Appium handles these multi-touch gestures by specifying the touch points and their movements, giving you precise control over the interactions. Calculate the center coordinates (centerX, centerY)of the element. Define end coordinates for the pinch gesture (endX1, endY1, endX2, endY2). Create the first finger input sequence pinch1to: - Move to the center of the element. - Press down. - Move to the end coordinates over 600 ms. - Lift up the finger. Create the seond finger input sequence pinch2 to: - Move to the center of the element. - Press down. - Move to the end coordinates over 600 ms. - Lift up the finger. Execute both sequences simultaneously to perform the pinch gesture.Simulating a Zoom OutPerform a zoom out gesture by reversing the pinch gesture:Gesture: Zoom Outpublic void zoomOut(WebElement element) { int centerX = element.getLocation().getX() + element.getSize().getWidth() / 2; int centerY = element.getLocation().getY() + element.getSize().getHeight() / 2; int startX1 = centerX - 100; int startY1 = centerY - 100; int startX2 = centerX + 100; int startY2 = centerY + 100; PointerInput finger1 = new PointerInput(PointerInput.Kind.TOUCH, "finger1"); Sequence pinchIn1 = new Sequence(finger1, 1) .addAction(finger1.createPointerMove(Duration.ZERO, PointerInput.Origin.viewport(), startX1, startY1)) .addAction(finger1.createPointerDown(PointerInput.MouseButton.LEFT.asArg())) .addAction(finger1.createPointerMove(Duration.ofMillis(600), PointerInput.Origin.viewport(), centerX, centerY)) .addAction(finger1.createPointerUp(PointerInput.MouseButton.LEFT.asArg())); PointerInput finger2 = new PointerInput(PointerInput.Kind.TOUCH, "finger2"); Sequence pinchIn2 = new Sequence(finger2, 1) .addAction(finger2.createPointerMove(Duration.ZERO, PointerInput.Origin.viewport(), startX2, startY2)) .addAction(finger2.createPointerDown(PointerInput.MouseButton.LEFT.asArg())) .addAction(finger2.createPointerMove(Duration.ofMillis(600), PointerInput.Origin.viewport(), centerX, centerY)) .addAction(finger2.createPointerUp(PointerInput.MouseButton.LEFT.asArg())); driver.perform(Arrays.asList(pinchIn1, pinchIn2)); }ZoomingPinching involves bringing two fingers together on the screen, while zooming involves spreading them apart. Appium handles these multi-touch gestures by specifying the touch points and their movements, giving you precise control over the interactions. Calculate the center coordinates of the element. Define starting points for two touch actions, positioned diagonally around the center. Create a touch action sequence for the first finger, moving from the starting point to the center. Create a touch action sequence for the second finger, moving from the starting point to the center. Execute both sequences simultaneously to perform the pinch-in gesture.ConclusionIncorporating gesture actions into your Appium tests enhances functionality and verifies a seamless user experience. By simulating real user gestures, you can uncover issues that traditional testing may miss, leading to a more robust and user-friendly app.Whether you’re testing a simple or complex app, mastering these gestures will improve your testing outcomes. In the next guide, you’ll learn about Capturing Screenshots and Logs in Appium.Tagged:Appium Appium - Previous Understanding Appium UI Locator Strategies Next - Appium Capturing Screenshots and Logs In Appium