Left or Right swipe not working in appium for android app

To swipe in the latest version 5.0+ . You can watch this video : https://www.youtube.com/watch?v=9rHe0r6YsVg

I have followed the reference from the above discussion but nothing got at the o/p on the real device, Please help.

Code for swipe from right to left-

Dimension size=driver.manage().window().getSize();
System.out.println(size);
int startx= (int)(size.width0.70);
int endx= (int)(size.width
0.30);
int starty=size.height/2;
long duration=0;
int y=0;
new TouchAction(driver).press(startx, y).waitAction(Duration.ofMillis(duration)).moveTo(endx, y).release().perform();

I’m getting the "true’ status in the Logs, but nothing is implementing on the real device-

logs-

[debug] [MJSONWP] Calling AppiumDriver.performTouch() with args: [[{“action”:“press”,“options”:{“x”:503,“y”:0}},{“action”:“wait”,“options”:{“ms”:0}},{“action”:“moveTo”,“options”:{“x”:216,“y”:0}},{“action”:“release”,“options”:{}}],“0a9677b1-78bf-407c-a744-8039daa28a61”]
[debug] [AndroidBootstrap] Sending command to android: {“cmd”:“action”,“action”:“swipe”,“params”:{“startX”:503,“startY”:0,“endX”:216,“endY”:0,“steps”:22}}
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Got data from client: {“cmd”:“action”,“action”:“swipe”,“params”:{“startX”:503,“startY”:0,“endX”:216,“endY”:0,“steps”:22}}
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Got command of type ACTION
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Got command action: swipe
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Display bounds: [0,0][720,1184]
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Display bounds: [0,0][720,1184]
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Swiping from [x=503.0, y=592.0] to [x=216.0, y=592.0] with steps: 22
[debug] [AndroidBootstrap] Received command result from bootstrap
[debug] [MJSONWP] Responding to client with driver.performTouch() result: true
[HTTP] <-- POST /wd/hub/session/0a9677b1-78bf-407c-a744-8039daa28a61/touch/perform 200 468 ms - 76
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Returning result: {“status”:0,“value”:true}
[BaseDriver] Shutting down because we waited 45 seconds for a command
[debug] [AndroidDriver] Shutting down Android driver
[Appium] Closing session, cause was ‘New Command Timeout of 45 seconds expired. Try customizing the timeout using the ‘newCommandTimeout’ desired capability’
[Appium] Removing session 0a9677b1-78bf-407c-a744-8039daa28a61 from our master session list
[debug] [ADB] Getting connected devices…
[debug] [ADB] 1 device(s) connected
[debug] [ADB] Running ‘C:\Users\SJ\AppData\Local\Android\Sdk\platform-tools\adb.exe’ with args: ["-P",5037,"-s",“X606A1ZH69090204”,“shell”,“am”,“force-stop”,“com.letsdogether.dogether”]
[debug] [ADB] Pressing the HOME button
[debug] [ADB] Getting connected devices…
[debug] [ADB] 1 device(s) connected
[debug] [ADB] Running ‘C:\Users\SJ\AppData\Local\Android\Sdk\platform-tools\adb.exe’ with args: ["-P",5037,"-s",“X606A1ZH69090204”,“shell”,“input”,“keyevent”,3]
[debug] [AndroidBootstrap] Sending command to android: {“cmd”:“shutdown”}
[debug] [AndroidBootstrap] Received command result from bootstrap
[debug] [UiAutomator] Shutting down UiAutomator
[debug] [UiAutomator] Moving to state ‘stopping’
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Got data from client: {“cmd”:“shutdown”}
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Got command of type SHUTDOWN
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Returning result: {“status”:0,“value”:“OK, shutting down”}
[debug] [AndroidBootstrap] [UIAUTO STDOUT] close [socket][/127.0.0.1:4724]
[debug] [UiAutomator] UiAutomator shut down normally
[debug] [UiAutomator] Moving to state ‘stopped’
[debug] [ADB] Attempting to kill all uiautomator processes
[debug] [ADB] Getting all processes with uiautomator
[debug] [ADB] Getting connected devices…
[debug] [ADB] 1 device(s) connected
[debug] [ADB] Running ‘C:\Users\SJ\AppData\Local\Android\Sdk\platform-tools\adb.exe’ with args: ["-P",5037,"-s",“X606A1ZH69090204”,“shell”,“ps”]
[ADB] No uiautomator process found to kill, continuing…
[debug] [UiAutomator] Moving to state ‘stopped’
[debug] [Logcat] Stopping logcat capture
[debug] [ADB] Getting connected devices…
[debug] [ADB] 1 device(s) connected
[debug] [ADB] Running ‘C:\Users\SJ\AppData\Local\Android\Sdk\platform-tools\adb.exe’ with args: ["-P",5037,"-s",“X606A1ZH69090204”,“shell”,“am”,“force-stop”,“io.appium.unlock”]
[debug] [AndroidDriver] Not cleaning generated files. Add clearSystemFiles capability if wanted.

As we can see in the logs x value changes from 503 to 216 but nothing is happening on the real device

Have you tried with a duration bigger than 0ms?

Hi Telmo can you tell me how to perfomed flick(left or right) operation?

Hi @Telmo_Cardoso

In Java Client: 5.x.x, it’s working fine but while I have updated to 6.1.0 it’s not working and showing error, how I can resolve please help me!

public static void swipeHorizontal(AppiumDriver driver, double startPercentage, double finalPercentage, double anchorPercentage, int duration) throws Exception {
        Dimension size = driver.manage().window().getSize();
        int anchor = (int) (size.height * anchorPercentage);
        int startPoint = (int) (size.width * startPercentage);
        int endPoint = (int) (size.width * finalPercentage);
        new TouchAction(driver).press(startPoint, anchor).waitAction(Duration.ofMillis(duration)).moveTo(endPoint, anchor).release().perform();
    }

Those methods were removed.Try to adapt your code to the new methods available.

For instance:

void swipeCoordinates(AppiumDriver driver, int startX, int startY, int endX, int endY, int duration) throws Exception {
    new TouchAction(driver).press(point(startX, startY)).waitAction(waitOptions(Duration.ofMillis(duration)))
            .moveTo(point(endX, endY)).release().perform();
}

This works for me.

public void swipeHorizontal(int duration, String dragMeString) throws Exception {
WebElement dragMe = getElementByProperty(dragMeString);
new TouchAction(driver)
.press(PointOption.point(dragMe.getLocation().getX() + (dragMe.getSize().width - 20),
dragMe.getLocation().getY() + (dragMe.getSize().height / 2)))
.waitAction(WaitOptions.waitOptions(Duration.ofMillis(duration)))
.moveTo(PointOption.point(dragMe.getLocation().getX(),
dragMe.getLocation().getY() + (dragMe.getSize().height / 2)))
.release().perform();
}

2 Likes

This should work :slight_smile:

Dimension size = driver.manage().window().getSize();
System.out.println(size.height+“height”);
System.out.println(size.width+“width”);
System.out.println(size);
int startPoint = (int) (size.width * 0.99);
int endPoint = (int) (size.width * 0.15);
int ScreenPlace =(int) (size.height0.40);
int y=(int)size.height
20;

       TouchAction ts = new TouchAction(driver);
       for(int i=0;i<=3;i++) {
       ts.press(PointOption.point(startPoint,ScreenPlace ))
  	  .waitAction(WaitOptions.waitOptions(Duration.ofMillis(1000)))
  	  .moveTo(PointOption.point(endPoint,ScreenPlace )).release().perform();

I hope this article helps

Hey Mica, Were you able to resolve your issue? I am also facing similar issue, kindly share your approach.

Thanks

No issues on the code but when I run it doesn’t make anything, what could I be missing ?

	public static void main(String[] args) throws MalformedURLException {
	
	File f = new File("src");
	File fs = new File(f, "hcandroid-appium.apk");
	DesiredCapabilities cap= new DesiredCapabilities();
	cap.setCapability(MobileCapabilityType.DEVICE_NAME, "pixel4_9.0");
	cap.setCapability(MobileCapabilityType.AUTOMATION_NAME,"UiAutomator2");
	cap.setCapability(MobileCapabilityType.APP, fs.getAbsolutePath());
	AndroidDriver<AndroidElement> driver = new AndroidDriver<>(new URL("http://127.0.0.1:4723/wd/hub"), cap);
	
	Dimension size = driver.manage().window().getSize();
	System.out.println(size.height+ "height");
	System.out.println(size.width+ "width");
	System.out.println(size);
	int startPoint = (int) (size.width * 0.99);
	int endPoint = (int) (size.width * 0.15);
	int ScreenPlace =(int) (size.height * 0.40);
	int y=(int)size.height * 20;
	
       TouchAction ts = new TouchAction(driver);
       for(int i=0;i<=3;i++) {
       ts.press(PointOption.point(startPoint,ScreenPlace )).waitAction(WaitOptions.waitOptions(Duration.ofMillis(1000)))
  	  .moveTo(PointOption.point(endPoint,ScreenPlace )).release().perform();