How to perform swipe using appium in Java for android native app
Asked Answered
F

7

5

I need to swipe my app(Both from left to right and right to left), wherelse I am using Java in appium for android native app automation.

I have tries this link, Swipe method not working in android automation testing

But i can't, is any other link please share or anyone help me out.

Fred answered 28/4, 2014 at 11:20 Comment(1)
You can refer to the following topic with same context.Correspondence
T
10

Here is how we do it-

Swipe left-

appiumDriver.context("NATIVE_APP"); 
Dimension size = appiumDriver.manage().window().getSize(); 
int startx = (int) (size.width * 0.8); 
int endx = (int) (size.width * 0.20); 
int starty = size.height / 2; 
appiumDriver.swipe(startx, starty, endx, starty, 1000);

Swipe right-

appiumDriver.context("NATIVE_APP"); 
Dimension size = appiumDriver.manage().window().getSize(); 
int endx = (int) (size.width * 0.8); 
int startx = (int) (size.width * 0.20); 
int starty = size.height / 2; 
appiumDriver.swipe(startx, starty, endx, starty, 1000);

Here appiumDriver is an instance of io.appium.java_client.AppiumDriver

Toad answered 21/4, 2015 at 12:20 Comment(1)
what if we are in webview context?Thrombokinase
H
3

swipe() method is deprecated.

so we can use below methods.

Swipe left will be perform as:

new TouchAction(driver).longPress(250, 1200).moveTo(900, 1200).release().perform();

Swipe right will be perform as:

new TouchAction(driver).longPress(1000, 450).moveTo(500, 450).release().perform();

Here, driver is your driver like AndroidDriver, RemoteWebDriver, AppiumDriver.

And 250, 1200 and other is Your app view Co-ordinate that you can see in the UIAutomaterView batch file located under the android-sdk platform-tools.

Hoebart answered 8/9, 2017 at 10:16 Comment(1)
That solution wouldn't work, if you are using Selendroid as automationName in AppiumCantor
F
0

Use this below code for Swipe using appium in Junit for android native app,

    public void swipe()
    {  
    JavascriptExecutor js = (JavascriptExecutor) driver;
    HashMap<String, Double> swipeObject = new HashMap<String, Double>();
    swipeObject.put("startX", 0.95);
    swipeObject.put("startY", 0.5);
    swipeObject.put("endX", 0.05);
    swipeObject.put("endY", 0.5);
    swipeObject.put("duration", 1.8);
    js.executeScript("mobile: swipe", swipeObject);
     }

//Call this method where you need to swipe,

    swipe(); //it will call the swipe method

I have tried this out and got swiped successfully in android native app.

For more this might helpful:

https://github.com/appium/appium/blob/master/docs/en/gestures.md

Fred answered 29/4, 2014 at 7:36 Comment(0)
T
0
public void swipeUpElement(AppiumDriver<WebElement> driver, WebElement element, int duration){
    int bottomY = element.getLocation().getY()-200;
    driver.swipe(element.getLocation().getX(), element.getLocation().getY(), element.getLocation().getX(), bottomY, duration);
}
Tranquillize answered 17/11, 2015 at 19:47 Comment(2)
While this code may answer the question, it would be better to explain how it solves the problem without introducing others and why to use it. Code-only answers are not useful in the long run.Liddle
In the above code - I am getting the element to be scrolled and then its start and end location and the duration for which swipe function should be executed.Tranquillize
P
0
public void swipingHorizontally() throws MalformedURLException, InterruptedException{
        DesiredCapabilities();
        Dimension size = driver.manage().window().getSize();
         System.out.println(size);
         int startx = (int) (size.width * 0.70);
         int endx = (int) (size.width * 0.30);
         int starty = size.height / 2;
         driver.swipe(startx, starty, endx, starty, 2000); // it swipes from right to left
         driver.swipe(endx, starty, startx, starty, 2000); // it swiptes from left to right
          }
         Thread.sleep(2000);
    }
Pleasant answered 6/10, 2016 at 13:9 Comment(0)
A
0

@ABDUL SATHAR BEIGH : Absolutely right . Just to add , for me to work on Android I had to typecast it by (AndroidDriver) driver) if the above doesn't work for you . The following worked with this modification. this is swipe right.

((AndroidDriver) driver).context("NATIVE_APP");
   Dimension size = driver.manage().window().getSize();
   int endx = (int) (size.width * 0.8);
   int startx = (int) (size.width * 0.20);
   int starty = size.height / 2;
   ((AndroidDriver) driver).swipe(startx, starty, endx, starty, 1000);
Alary answered 1/11, 2016 at 19:41 Comment(0)
B
0
 public void leftRightSwipe(int timeduration) {
  // duration should be in milliseconds
  size = driver.manage().window().getSize();
  System.out.println(size);
  startx = (int) (size.width * 0.70);
  endx = (int) (size.width * 0.30);
  starty = size.height / 2;
  System.out.println("Start swipe operation");
  driver.swipe(endx, starty, startx, starty, timeduration);

 }

public void rightLeftSwipe(int timeduration) {

  size = driver.manage().window().getSize();
  System.out.println(size);
  startx = (int) (size.width * 0.70);
  endx = (int) (size.width * 0.30);
  starty = size.height / 2;
  System.out.println("Start swipe operation");
  driver.swipe(startx, starty, endx, starty, timeduration);

 }

In case you want to learn in details Refer here - http://www.qaautomated.com/2017/10/swipe-rightleftup-down-using-appium.html

Backdate answered 10/10, 2017 at 18:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.