Automate Gestures Using W3 Actions API in Appium | DS

Back to Blog
Feature image of W3 Actions API in Appium blog

Automate Gestures Using W3 Actions API in Appium | DS

Appium is a popular open-source mobile test automation framework that allows users to test native, mobile web, and hybrid applications on Android and iOS devices.

What are W3 Actions Class?

W3 Actions Classes are a set of standardized classes defined by the World Wide Web Consortium (W3C) WebDriver specification. These classes provide a consistent way to simulate user interactions with web and mobile applications. When using Appium, you can leverage these Actions Classes to automate various touch and swipe gestures, enhancing your mobile app testing capabilities.

Introduction Appium Gestures

There are some exclusive events that you can perform only on mobile devices. These gestures are essential for testing touch-based interactions in mobile applications.

Types of Gestures:

Appium provides several types of gestures, we will cover the following types of gestures.

  • Tap
  • LongPress
  • Scroll
  • Swipe
  • Drag & Drop
  • Pinch or zoom

Benefits of Automating Gestures using W3 Actions:

  • It is a standard API, which makes it easier to learn and use.
  • It is supported by Android and iOS both.
  • It is more flexible than using native Appium methods to automate gestures.
  • It helps to automate complex gestures, such as pinch to zoom.

Steps to Automate the Gesture Using W3 Actions:

  • Create a new Appium test case.
  • Get the element that you want to perform the gesture on.
  • Create a new W3 Action object.
  • Add the gesture to the W3 Actions object.
  • Perform the gesture by calling the perform () method on the W3 Actions object.

Good to Read:-  How to Automate Hybrid Apps Using Appium?

Tap Gestures

private void tap (AndroidDriver driver, WebElement element) {
         Point location = element.getLocation();
         Dimension size = element.getSize();
         Point centerOfElement = getCenterOfElement(location, size);
         PointerInput finger1 = new PointerInput(PointerInput.Kind.TOUCH, "finger1");
         Sequence sequence = new Sequence(finger1, 1)
                 .addAction(finger1.createPointerMove(Duration.ZERO, PointerInput.Origin.viewport(), centerOfElement))
                 .addAction(finger1.createPointerDown(PointerInput.MouseButton.LEFT.asArg()))
                 .addAction(new Pause(finger1, Duration.ofMillis(200)))
                 .addAction(finger1.createPointerUp(PointerInput.MouseButton.LEFT.asArg()));
        
         driver.perform(Collections.singletonList(sequence));
}
    
    private Point getCenterOfElement(Point location, Dimension size)
    {
    return new Point(location.getX() + size.getWidth()/2,
            location.getY()+ size.getHeight()/2);
    }

LongPress Gestures

private void longPress (AndroidDriver driver, WebElement element) {
         Point location = element.getLocation();
         Dimension size = element.getSize();
         Point centerOfElement = getCenterOfElement(location, size);
         PointerInput finger1 = new PointerInput(PointerInput.Kind.TOUCH, "finger1");
         Sequence sequence = new Sequence(finger1, 1)
                 .addAction(finger1.createPointerMove (Duration.ZERO, PointerInput.Origin.viewport(), centerOfElement))
                 .addAction(finger1.createPointerDown(PointerInput.MouseButton.LEFT.asArg()))
//waiting for 2 seconds for longPress on the Element
                 .addAction(new Pause(finger1, Duration.ofSeconds(2)))
                 .addAction(finger1.createPointerUp(PointerInput.MouseButton.LEFT.asArg()));
        
         driver.perform(Collections.singletonList(sequence));
}	
    private Point getCenterOfElement(Point location, Dimension size)
    {
    return new Point(location.getX() + size.getWidth()/2,
            location.getY()+ size.getHeight()/2);
    }

Scroll Gestures

Dimension size = driver.manage().window().getSize();
        int startX = size.getWidth() / 2;
        int startY = size.getHeight() / 2;
        int endX = startX;
        int endY = (int) (size.getHeight() * 0.25);
        PointerInput finger1 = new PointerInput (PointerInput.Kind. TOUCH,"finger1");
        Sequence sequence= new Sequence(finger1,1)
        .addAction (finger1.createPointerMove (Duration.ZERO, PointerInput.Origin.viewport(), startX, startY))
        .addAction (finger1.createPointerDown (PointerInput.MouseButton. LEFT.asArg()))
        .addAction (new Pause (finger1, Duration.ofMillis(200)))
        .addAction(finger1.createPointerMove (Duration.ofMillis(100), PointerInput. Origin.viewport(), endX, endY))
        .addAction (finger1.createPointerUp (PointerInput.MouseButton.LEFT.asArg()));
        driver.perform(Collections.singletonList(sequence));

Swipe Gestures

Dimension size = driver.manage().window().getSize();
        int startX = size.getWidth() / 2;
        int startY = size.getHeight() / 2;
        int endX = (int)(size.getWidth() * 0.25);
        int endY = startY;
        PointerInput finger1 = new PointerInput (PointerInput.Kind. TOUCH,"finger1");
        Sequence sequence= new Sequence(finger1,1)
        .addAction (finger1.createPointerMove (Duration.ZERO, PointerInput.Origin.viewport(), startX, startY))
        .addAction (finger1.createPointerDown(PointerInput.MouseButton. LEFT.asArg()))
        .addAction (new Pause (finger1, Duration.ofMillis(200)))
        .addAction(finger1.createPointerMove (Duration.ofMillis(100), PointerInput. Origin.viewport(), endX, endY))
        .addAction (finger1.createPointerUp (PointerInput.MouseButton.LEFT.asArg()));
        driver.perform(Collections.singletonList(sequence));
}

Drag & Drop Gestures

WebElement source = driver.findElement(AppiumBy.id("io.appium.android.apis:id/drag_dot_1"));
WebElement target = driver.findElement(AppiumBy.id("io.appium.android.apis:id/drag_dot_2"));
Point sourceElementCenter = getCenterOfElement(source.getLocation(), source.getSize());
Point targetElementCenter = getCenterOfElement(target.getLocation(), target.getSize());
PointerInput finger1 = new PointerInput (PointerInput.Kind. TOUCH,"finger1");
Sequence sequence = new Sequence (finger1, 1)
.addAction (finger1.createPointerMove (Duration.ZERO, PointerInput.Origin.viewport(), sourceElementCenter))
.addAction (finger1.createPointerDown(PointerInput.MouseButton. LEFT.asArg()))
.addAction(new Pause(finger1, Duration.ofMillis(588)))
.addAction(finger1.createPointerMove(Duration.ofMillis(588), PointerInput.Origin.viewport(), targetElementCenter))
.addAction (finger1.createPointerUp(PointerInput.MouseButton.LEFT.asArg()));
driver.perform(Collections.singletonList(sequence));
}
    private Point getCenterOfElement(Point location, Dimension size)
    {
    return new Point(location.getX() + size.getWidth()/2,
            location.getY()+ size.getHeight()/2);
    }

Pinch or Zoom Gestures

Point centerOfElement = getCenterOfElement(elem.getLocation(), elem.getSize());
PointerInput finger1 = new PointerInput (PointerInput.Kind. TOUCH, "finger1");
PointerInput finger2 = new PointerInput (PointerInput.Kind. TOUCH, "finger2");
Sequence sequence = new Sequence(finger1,1)
.addAction(finger1.createPointerMove (Duration. ZERO, PointerInput. Origin.viewport(), centerOfElement))
.addAction (finger1.createPointerDown(PointerInput.MouseButton.LEFT.asArg()))
.addAction(new Pause (finger1, Duration.ofMillis(288)))
.addAction(finger1.createPointerMove (Duration.ofMillis(208),
PointerInput.Origin.viewport(),
centerOfElement.getX() + 100,
centerOfElement.getY()- 100))
.addAction (finger1.createPointerUp (PointerInput.MouseButton.LEFT.asArg()));
Sequence sequence2 = new Sequence(finger2,1)
.addAction (finger2.createPointerMove(Duration.ZERO, PointerInput. Origin.viewport(), centerOfElement))
.addAction (finger2.createPointerDown(PointerInput.MouseButton. LEFT.asArg()))
.addAction(new Pause (finger2, Duration.ofMillis(200)))
.addAction (finger2.createPointerMove(Duration.ofMillis(200),
PointerInput.Origin.viewport(),
centerOfElement.getX()-100,
centerOfElement.getY()+ 100))
.addAction (finger2.createPointerUp(PointerInput. MouseButton.LEFT.asArg()));
driver.perform(Arrays.asList(sequence,sequence2));
}
    private Point getCenterOfElement(Point location, Dimension size)
    {
    return new Point(location.getX() + size.getWidth()/2,
            location.getY()+ size.getHeight()/2);
    }

Good to Read- Mobile Test Automation Using Appium Python

Explanation of the Methods:

PointerInput class to create a sequence of actions that move the pointer to the center of the element, press down on the element, and then release the element.

Sequence object, which is a list of actions that will be performed on the device

driver.perform() method to execute the sequence of actions on the device.

  • The Point location and Dimension size variables are used to store the location and size of the element on the screen.
  • The getCenterOfElement() method is used to calculate the center of the element.
  • A new Sequence object is created and populated with the actions that will be performed to tap on the element.
  • The driver.perform() method is called to execute the sequence of actions on the device.

Conclusion

Automating gestures and interactions within mobile apps using W3 Actions Classes with Appium is essential for comprehensive testing and ensuring a smooth user experience. You can build robust, efficient, and reliable automation solutions for your mobile apps.

Share this post

Back to Blog