Earlier In THIS POST, we learnt how to swipe In horizontal or vertical direction In any android software app using driver.swipe() and swiping element in horizontal direction using action chain In previous post. Most of android applications contain list view and you need to scroll down to select specific element. You can use scrollTo(String text) method of IOSElement class If you wants to scroll down till specific text in appium software automation test. Let's try to Implement It practically In android software appium automation test to know how scrollTo(String text) method works.
PREREQUISITES : All previous 21 steps of appium mobile software app automation tutorials (PART 1 and PART 2) should be completed.
Download And Install API Demos App
We will use API Demos In this example. You can download API Demos android software app from GOOGLE PLAY STORE or THIS PAGE. Install It In your mobile device.
We wants to scroll down till selected element which contains text "Tabs" as shown In bellow Image. And then we wants to tap on It("Tabs" text). Manually you can view bellow given scroll-able listing in API Demos software App from API Demos App -> Views.
So In appium test,
- First we will open API Demos App,
- Tap on "Views".
- On next screen, Scroll down till element which contain text "Tabs".
- And tap on element which contain text "Tabs".
Create And Run Android Appium scrollTo Text Test
Create ScrollingToText.java file under Android package of your project and copy paste bellow given test script In It.
Note : Please set your device's capabilities in bellow given test.
ScrollingToText.java
Note : Please set your device's capabilities in bellow given test.
ScrollingToText.java
package Android;
import io.appium.java_client.android.AndroidDriver;
import java.net.URL;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
public class ScrollingToText {
AndroidDriver driver;
@BeforeTest
public void setUp() throws Exception {
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("deviceName", "ZX1B32FFXF");
capabilities.setCapability("browserName", "Android");
capabilities.setCapability("platformVersion", "4.4.2");
capabilities.setCapability("platformName", "Android");
capabilities.setCapability("appPackage", "io.appium.android.apis");
capabilities.setCapability("appActivity","io.appium.android.apis.ApiDemos");
driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
}
@Test
public void ScrollToText() throws InterruptedException {
//Scroll till element which contains "Views" text If It Is not visible on screen.
driver.scrollTo("Views");
// Click on Views/.
driver.findElement(By.name("Views")).click();
System.out.println("Scrolling has been started to find text -> Tabs.");
// Scroll till element which contains Tabs text.
driver.scrollTo("Tabs");
System.out.println("Tabs text has been found and now clicking on It.");
// Click on Tabs.
driver.findElement(By.name("Tabs")).click();
}
@AfterTest
public void End() {
driver.quit();
}
}
Test Script Description
If you look at above test example, We have used driver.scrollTo(String text) methods for scrolling. It will scroll down steps by step and look If given text Is displayed on screen or not. When text appears, It will stop scrolling.
If you look at above test example, We have used driver.scrollTo(String text) methods for scrolling. It will scroll down steps by step and look If given text Is displayed on screen or not. When text appears, It will stop scrolling.
- First driver.scrollTo("Views"); will check if element containing test "Views" Is display on screen or not. If not display then It will scroll down down and check for text once again.
- Same way, Second driver.scrollTo("Tabs"); will scroll down and check for text "Tabs".
To see scrollToText demo practically, Run above test using testng and appium and observe result on your android mobile screen.
This way you can scroll down vertically to find specific element and click on It.
This ScrollTo and ScrollToExact is not working most of the times.Appium scroll beyond the desired element.
ReplyDeleteIs there any work around with a perfect solution
ScrollTo and ScrollToExact is of no use, doesn't work at all
ReplyDeleteHi,
ReplyDeleteScrollTo method is not working on my Phone and Emulator. I used driver.scrollTo("Tabs"); but nothing happens. Do we need to do some setting on developer options to use it?
Please suggest!!
hi,
ReplyDeleteI want to scroll the page till the end and need to find as scroll reach to the end of page or not , how I can do this. plz reply
thanks in advance
Hi.. I tried all the possible ways but couldn't find scrollTo(). It gives error "The method scrollTo() is undefined for the type WebDriver"
ReplyDeletePlease suggest me something
Hi, I tried all possible ways but couldn't get driver.scrollTo() working. It is giving error "The method scrollTo() is undefined for the type WebDriver".
ReplyDeletePlease suggest me how can I add scroll. My app is a hybrid app.
Define your driver as Appium driver in the beginning of your code. scrollTo is a method defined for appium driver.
Deletethere is not method in the androidDriver and webdriver.
Deletedriver.scrollTo Not working. Showing error message - The method scrollTo(String) is undefined for the type AndroidDriver
ReplyDeleteIs there any alternate solution as in the new appium release the scrollTo() and scrollToExact() methods have been deprecated.?
ReplyDeleteJust want to scroll to a particular element on the android app.
what if scrollTo doesn't get the expected element?? Will it throw an error?
ReplyDeleteIn recent update appium "mobile : scroll" deprecated, following code will work and video will help you to implement.
ReplyDeleteScroll to text :
MobileElement radioGroup = (MobileElement) wd
.findElementByAndroidUIAutomator("new UiScrollable(new UiSelector()"
".resourceId(\"++\")).scrollIntoView("
"new UiSelector().text(\"++\"));");
radioGroup.click();
This link will help you : https://www.youtube.com/watch?v=bT3tqaLNn-Y
Hi, could you tell me what I should pass to ++ in your above code? Even I the video, I did not see he talk about that.
DeleteThis one:
".resourceId(\"++\")).scrollIntoView("
"new UiSelector().text(\"++\"));");
hi, kieu Phan
DeleteI got some idea from internet, the below is a completed script from which you may get what you need:
import io.appium.java_client.MobileDriver;
import io.appium.java_client.TouchAction;
import io.appium.java_client.android.AndroidDriver;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
public class DragAndDropAction {
//Object reference of AndroidDriver.
AndroidDriver driver;
WebDriver driver1;
@BeforeTest
public void setUp() throws MalformedURLException {
File classpathRoot = new File(System.getProperty("user.dir"));
File appDir = new File(classpathRoot, "/App");
System.out.println("1");
File app = new File(appDir, "com.mobeta.android.demodslv-0.5.0-3_APKdot.com.apk");
System.out.println("2");
DesiredCapabilities capabilities = new DesiredCapabilities();
System.out.println("3");
capabilities.setCapability("deviceName", "F3H6R14B07006517");
System.out.println("4");
capabilities.setCapability("browserName", "Android");
System.out.println("5");
capabilities.setCapability("platformVersion", "4.4.2");
System.out.println("6");
capabilities.setCapability("platformName", "Android");
System.out.println("7");
capabilities.setCapability("app", app.getAbsolutePath());
System.out.println("8");
capabilities.setCapability("appPackage", "com.mobeta.android.demodslv");
System.out.println("9");
capabilities.setCapability("appActivity", "com.mobeta.android.demodslv.Launcher");
System.out.println("10");
driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
System.out.println("11");
driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
System.out.println("12");
}
@Test
public void dragDrop() {
//Tap on Basic usage Playground.
System.out.println("13-0");
driver1= driver;
driver1.findElement(By.id("com.mobeta.android.demodslv:id/activity_title")).click();
System.out.println("13");
WebElement ele1 = (WebElement) driver.findElementsById("com.mobeta.android.demodslv:id/drag_handle").get(2);
WebElement ele2 = (WebElement) driver.findElementsById("com.mobeta.android.demodslv:id/drag_handle").get(5);
TouchAction action = new TouchAction((MobileDriver) driver);
System.out.println("It Is dragging element.");
action.longPress(ele1).moveTo(ele2).release().perform();
System.out.println("Element has been droped at destination successfully.");
}
@Test
public void testCal() throws Exception
{
String str="Mark Turner";
driver.findElementByAndroidUIAutomator("new UiScrollable(new UiSelector().scrollable(true).instance(0)).scrollIntoView(new UiSelector().textContains(\""+str+"\").instance(0))");
driver.quit();
}
}
}
Mak
ReplyDeleteThe method setCapability(String, String) is undefined for the type DesiredCapabilities
following error coming under desired capability
Ok guys this is not going to work By.name is deprecated.Try following thing to scroll.
ReplyDeletedriver.findElement(By.name("Views")).click();instead of this line Use___>
driver.findElementByAndroidUIAutomator("new UiScrollable(new UiSelector()).scrollIntoView(text(\"Views\"));");