I am trying to implement swipe method.May I know what is the correct way to achieve my objective?
public void swipeWithCordinateLocation(WebDriver driver,double startX,
                                       double  startY,double endX,double endY,
                                       double Duration)
{
JavascriptExecutor js = (JavascriptExecutor) driver;
HashMap<String, Double> swipeObject = new HashMap<String, Double>();
swipeObject.put("x", startX);
swipeObject.put("y", startY);
swipeObject.put("x", endX);
swipeObject.put("y",endY );
swipeObject.put("duration", Duration);
// HashMap[] param = {swipeObject};
js.executeScript("mobile: swipe", swipeObject);
}
common.swipeWithCordinateLocation(driver, 100.00, 500.00, 500.00, 500.00, 1.00);
But appium is perform swipe but it takes different credentials
[x=360][y=592]
 to [x=360][y=592]
. what to do? Can any one help me please.
The best way to analyze a swipe in automation is to enable "Show touches" and "pointer location" options in your developer option.
The correct code for the swipe parameters is this -
swipeObject.put("startX", 198.00);
swipeObject.put("startY", 685.00);
swipeObject.put("endX", 198.00);
swipeObject.put("endY", 550.00);
swipeObject.put("duration", 1.0);
Hope this helps.
Or even a better way is to use something like using fractions -
swipeObject.put("startX", 0.50);
swipeObject.put("startY", 0.50);
swipeObject.put("endX", 0.50);
swipeObject.put("endY", 0.35);
swipeObject.put("duration", 1.0);
                        The best solution is to use swipe() of AppiumDriver as shown below:
Swipe Left:
 public void swipeLeft()
  {
      AppiumDriver appiumDriver = (AppiumDriver) driver;
      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:
 public void swipeRight()
  {
      AppiumDriver appiumDriver = (AppiumDriver) driver;
      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);
  }
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With