How to do scroll action till the particular element is present in appium


How to do scroll action till the particular element is present in appium ?


How to do scroll action till the particular element is present or not
I am currently working on appium for automating WhatsApp mobile application. I needed one method which would perform the ‘scroll’ action to a particular name or number and choose to click on a particular name or number. So I googled for that method and I found out that there are two methods, scrollTo(), scrollToExact(), which was supposed to solve my problem. But when  I started writing the code and again I faced another problem.  I am using appium java client 5.0.0-BETA9 (updated version )and on this version, there are no such methods like scrollTo() or scrollToExact().  Previously I had worked on the seetest tool and it is a mobile automation tool, there was a method called ‘swipewhileNotFound’ which would scroll the app view until the desired object was visible.  SeeTest is a licensed tool. Hence there are several readymade methods available for automating mobile application.  So I started writing this method for appium because I needed this method for appium to work.


I have used TestNG to write the test, but you can use any testing framework or simple Java class with the method mentioned above. I have posted my code here so that you can have a look at it. Also, you can download the complete java file from the below link


How to do scroll action till the particular element is present or not

 package appium;  
 import java.net.MalformedURLException;  
 import java.net.URL;  
 import java.util.concurrent.TimeUnit;  
 import org.openqa.selenium.By;  
 import org.openqa.selenium.Dimension;  
 import org.openqa.selenium.WebElement;  
 import org.openqa.selenium.remote.DesiredCapabilities;  
 import org.testng.annotations.*;  
 import io.appium.java_client.android.AndroidDriver;  
 public class WhatsAppAutomate {  
 // WebDriver driver;  
 protected AndroidDriver driver;  
 @BeforeClass  
  public void setUp() throws MalformedURLException {  
  DesiredCapabilities capabilities = new DesiredCapabilities();  
  capabilities.setCapability("deviceName", "8fe5de4a");  
  capabilities.setCapability("platformName", "Android");  
  capabilities.setCapability("appPackage", "com.whatsapp");  
  // This package name of your app (you can get it from apk info app)  
  capabilities.setCapability("appActivity", "com.whatsapp.Main");   
  driver = new AndroidDriver(new URL("http://192.168.0.102:5036/wd/hub"), capabilities);  
 }  
  @Test  
  public void testCal() throws Exception {  
  swipeWhileNotFound("down","//*[@text='Anjan Mondal']",1000,3000,3000,17,true);  
  }  
  public void swipeWhileNotFound(String direction, String xpath,int offset,int waitTime,int duration,int round, boolean click) throws InterruptedException {  
   Dimension size = driver.manage().window().getSize();  
  int startx = size.width/2;  
  int starty=0;  
  int endy=0;  
  try {  
  driver.manage().timeouts().implicitlyWait(1, TimeUnit.SECONDS);  
  boolean loop = true;  
  int count = 0;  
  while (loop) {  
  if (count == round) {  
  System.out.println("round over");  
  break;  
  }  
  if (driver.findElements(By.xpath(xpath)).size() > 0) {  
  loop = false;  
  System.out.println("element found");  
  WebElement elm = driver.findElement(By.xpath(xpath));  
  if (click)  
  elm.click();  
  } else {  
  if(direction.toUpperCase().equals("UP")){   
  starty=size.height-offset;  
  endy = (int) (size.height * 0.60);  
  driver.swipe(startx, starty, startx, endy, duration);  
  Thread.sleep(waitTime);  
  }else if(direction.toUpperCase().equals("DOWN")){  
  starty = (int) (size.height * 0.80);   
  endy= offset;  
  driver.swipe(startx, endy, startx, starty, duration);  
  Thread.sleep(waitTime);  
  }   
  count++;  
  }  
  }  
  } catch (Exception e) {  
  System.out.print(e);  
  } finally {  
  driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);  
  }  
  }  
  @AfterClass  
  public void teardown() {  
  // close the app  
  driver.quit();  
  }  
 }  

Share this

Related Posts

Previous
Next Post »

5 comments

comments
April 17, 2018 at 11:44 PM delete

It's interesting that many of the bloggers to helped clarify a few things for me as well as giving.Most of ideas can be nice content.The people to give them a good shake to get your point and across the command.

software testing training in chennai

Reply
avatar
February 25, 2019 at 3:12 AM delete


After reading this web site I am very satisfied simply because this site is providing comprehensive knowledge for you to audience.
Thank you to the perform as well as discuss anything incredibly important in my opinion. We loose time waiting for your next article writing in addition to I beg one to get back to pay a visit to our website in



Selenium training in Chennai

Reply
avatar
June 27, 2019 at 11:54 PM delete

The article is so informative. This is more helpful for our
software testing training institute
Selenium training in chennai Thanks for sharing

Reply
avatar