Appium

Automating Gesture Actions in Appium

Estimated reading: 8 minutes 159 views
appium-featured-image

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 Actions

Alright, 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 Class

Let’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 Dependencies

In 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:

				
					import 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.

Simulating a Tap

				
					public 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));
    }
				
			

Tap

The 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.

Simulating a Long Press

				
					public 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 Press

The longPressElement method simulates a long press on a mobile or web element by:

  1. Moving the virtual finger to the center of the element.
  2. Pressing down on the element.
  3. Holding the press for a specified duration.
  4. Releasing the press.

Simulating Swipe Gestures

In 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.

Simulating a Swipe

				
					public 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));
    }
				
			

Swipe

The swipe function simulates a swipe gesture on a touch screen.

  1. First, it moves the touch pointer from a starting point (startX, startY)
  2. Next it moves to the ending point (endX, endY) over a given duration.
  3. The process involves moving the pointer to the start position, touching down, moving to the end position, and then lifting up
  4. All of these actions are coordinated through a sequence of actions performed by the driver.

Simulating a Zoom In

Next 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.

Simulating a Zoom In / Pinch Out

				
					 public 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));
    }
				
			

Pinching

Pinching 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.

  1. Calculate the center coordinates (centerX, centerY)of the element.
  2. Define end coordinates for the pinch gesture (endX1, endY1, endX2, endY2).
  3. 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.
  4. 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.
  5. Execute both sequences simultaneously to perform the pinch gesture.

Simulating a Zoom Out

Perform a zoom out gesture by reversing the pinch gesture:

Simulating Zooming Out

				
					public 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));
    }
				
			

Zooming

Pinching 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.

  1. Calculate the center coordinates of the element.
  2. Define starting points for two touch actions, positioned diagonally around the center.
  3. Create a touch action sequence for the first finger, moving from the starting point to the center.
  4. Create a touch action sequence for the second finger, moving from the starting point to the center.
  5. Execute both sequences simultaneously to perform the pinch-in gesture.

Conclusion

Incorporating these gesture actions into your Appium tests is not just about ensuring functionality, but also about verifying that the user experience is seamless and intuitive. By mimicking real user gestures, you can identify potential issues that might not be apparent through traditional testing methods. This approach helps create a more robust and user-friendly app, ultimately leading to higher user satisfaction.

So, whether you are working on a simple app or a complex one with numerous touch interactions, mastering these gestures will significantly improve your testing outcomes.

Leave a Comment

Share this Doc

Automating Gesture Actions in Appium

Or copy link

CONTENTS
Review Your Cart
0
Add Coupon Code
Subtotal
Total Installment Payments
Bundle Discount