Home Appium Environment Setup Android Emulators iOS Simulators Appium Architecture Android chrome app automation Safari browser automation Appium Capabilities Inspecting android apps Native Android app automation Inspecting iOS app Native iOS app Automation Real Android Phone automation Real iPhone automation Element Identification Pressing button FaceId and TouchId Fingerprint automation Drag and Drop Tap and press Swipe gestures Hybrid app automation Execute script Synchronization in Appium Screenshots Page Object Models Cucumber and Appium Appium integration with azure Appium and Selenium Grid Running tests in browserstack Parallel testing Recording tests Mobile automation framework Errors and Exceptions in Appium Mobile automation test strategy Mobile automation challenges Appium QnA
Swipe gestures using appium
Swiping vertically and horizontally in Andrid emulator using Appium
Some important points
- TouchAction and MultiTouchAction class can be used to do swipe operation
- press, waitAction and moveTo methods should be called in sequence to perform swipe
- We can find the Point of Source and Destination points by calculating screen size
- Finally perform method of MultiTouchAction class can be used to perform swipe
- In same way, we can do this action in iOS as well
package org.softpost.android.simulator;
import io.appium.java_client.MultiTouchAction;
import io.appium.java_client.TouchAction;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.android.AndroidElement;
import io.appium.java_client.touch.WaitOptions;
import io.appium.java_client.touch.offset.PointOption;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.time.Duration;
import java.util.concurrent.TimeUnit;
/**
* Created by Sagar on 10-07-2016.
*/
public class SwipingTest {
WebDriver driver;
private String securityText = "//android.widget.TextView[@text='Security']";
private String touchSensorText = "//android.widget.TextView[@text='Touch the sensor']";
private String doneText = "//android.widget.Button[@text='DONE']";
@Test
public void test1() throws Exception{
DesiredCapabilities caps = new DesiredCapabilities();
caps.setCapability("platformName", "android");
caps.setCapability("deviceName", "Pixel 9");
caps.setCapability("appPackage", "com.android.settings");
caps.setCapability("appActivity","com.android.settings.Settings");
caps.setCapability("automationName","uiautomator2");
try {
driver = new AndroidDriver<AndroidElement>(new URL("http://0.0.0.0:4723/wd/hub"), caps);
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
//swiping from bottom to top...to reverse swipe, just swap the press and moveTo action points
//Note that X point is fixed when swiping vertically
//In below example, we are swiping from (94,height-100) bottom point to (94,400) top point
Thread.sleep(5000);
TouchAction action = new TouchAction(((AndroidDriver<AndroidElement>)driver));
int height = driver.manage().window().getSize().height;
int width = driver.manage().window().getSize().width;
System.out.println("Dimensions of screen -> " + width + " x " + height);
action.press(new PointOption().point(94, height - 1000))
.waitAction(new WaitOptions().withDuration(Duration.ofSeconds(1)))
.moveTo(new PointOption().point(94, 400)).release();
MultiTouchAction multiAction = new MultiTouchAction(((AndroidDriver<AndroidElement>)driver));
multiAction.add(action).perform();
Thread.sleep(5000);
//swiping from left to right...to reverse swipe, just swap the press and moveTo action points
//Note that Y point is fixed at 100 when swiping horizontally
//In below example, we are swiping from (10,100) left point to (width-100,100) right point
Thread.sleep(5000);
action = new TouchAction(((AndroidDriver<AndroidElement>)driver));
action.press(new PointOption().point(10, 100))
.waitAction(new WaitOptions().withDuration(Duration.ofSeconds(1)))
.moveTo(new PointOption().point(width-100, 100)).release();
multiAction = new MultiTouchAction(((AndroidDriver<AndroidElement>)driver));
multiAction.add(action).perform();
Thread.sleep(5000);
}catch (Exception ex) {
ex.printStackTrace();
System.out.println(driver.getPageSource());
}finally {
driver.quit();
}
}
}
Web development and Automation testing
solutions delivered!!