Q5 of 26 · Mobile QA

How do you automate a swipe gesture in Appium 2?

Mobile QAJuniormobileappiumgesturesswipew3c-actions

Short answer

Short answer: Use the W3C Actions API — define a pointer input sequence with a pointerDown at the start coordinates, a pause to simulate press duration, a pointerMove to the end coordinates, then a pointerUp. The deprecated TouchAction API is removed in Appium 2.

Detail

The W3C Actions API models touch gestures as sequences on named input sources. For a swipe you need one pointer input (representing a single finger) with four actions: press down, hold briefly, move to the destination, lift up.

Calculate coordinates as percentages of screen size rather than hardcoded pixels so the test works across different device resolutions.

// EXAMPLE

swipe.ts

const { width, height } = await driver.getWindowSize();

await driver.performActions([{
  type: 'pointer',
  id: 'finger1',
  parameters: { pointerType: 'touch' },
  actions: [
    { type: 'pointerMove', duration: 0, x: width * 0.5, y: height * 0.8 },
    { type: 'pointerDown', button: 0 },
    { type: 'pause', duration: 100 },
    { type: 'pointerMove', duration: 600, x: width * 0.5, y: height * 0.2 },
    { type: 'pointerUp', button: 0 },
  ],
}]);

// WHAT INTERVIEWERS LOOK FOR

Knows the W3C Actions API and that TouchAction is deprecated. Uses relative coordinates for portability.

// COMMON PITFALL

Still using the deprecated TouchAction or MobileElement.swipe() APIs. In Appium 2 these are gone — tests that relied on them will fail on first run.