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

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
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();
}
}
5 comments
commentsIt'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.
Replysoftware testing training in chennai
ReplyAfter 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
The article is so informative. This is more helpful for our
Replysoftware testing training institute
Selenium training in chennai Thanks for sharing
This Article is great and fantastic...
ReplyDigital Marketing Course Training Institute in Chennai
Big Data Course Training Institute in Chennai
Data Science Course Training Institute in Chennai
Android Training Institute in Chennai
AngularJS Training Institute in Chennai
Great Article. Thank you for sharing! Really an awesome post for every one.
ReplyProject Centers in Chennai
JavaScript Training in Chennai
Final Year Project Domains for IT
JavaScript Training in Chennai