user3540555
user3540555

Reputation: 69

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

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.

Upvotes: 5

Views: 30792

Answers (7)

anuja jain
anuja jain

Reputation: 1387

 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

Upvotes: 0

Pinak Gauswami
Pinak Gauswami

Reputation: 817

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.

Upvotes: 3

Rohan
Rohan

Reputation: 859

@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);

Upvotes: 0

arunkumar sambu
arunkumar sambu

Reputation: 663

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);
    }

Upvotes: 0

Suman
Suman

Reputation: 436

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);
}

Upvotes: 0

ABDUL SATHAR BEIGH
ABDUL SATHAR BEIGH

Reputation: 111

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

Upvotes: 10

user3540555
user3540555

Reputation: 69

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

Upvotes: 0

Related Questions