Monday 29 February 2016

How to do Vertical Scroll in Appium without scrollTo()?



Hi everyone today we will see how to use scroll action in Appium Test case.As we know in latest version of appium swipe() and scrollTo() methods are depricated lets see an alternative way to scroll through as List View.

Video Tutorial - 
 

App Under Test -
Check out the below screenshot of the app which has ListView containing 50 rows . Each row contains Text and Toggle Button. When you click on the row text at the top you can check which row was clicked.
Vertical Scroll Testing with Appium
Vertical Scroll

Test Scenario-
1. Launch Activity.
2. Scroll to List item:25.
3. Click on the row text.
4. Click on the toggle button.
scroll top to bottom in Appium
Vertical Scroll


Test Case -



package com.example.anuja.appiumapplication;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.Dimension;
import org.openqa.selenium.remote.DesiredCapabilities;

import java.net.URL;

import io.appium.java_client.AppiumDriver;
import io.appium.java_client.MobileBy;
import io.appium.java_client.MobileElement;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.android.AndroidElement;
import io.appium.java_client.remote.MobileCapabilityType;

import static junit.framework.Assert.assertNotNull;

/**
 * Created by Anuja on 2/8/2017.
 */



public class HorizontalTabScroll
{
    AppiumDriver driver;
    Dimension size;

    @Before
    public void testCaseSetup()throws  Exception
    {

        DesiredCapabilities cap=new DesiredCapabilities();
        cap.setCapability(MobileCapabilityType.PLATFORM_NAME, "Android");
        cap.setCapability(MobileCapabilityType.DEVICE_NAME, "Android device");
        cap.setCapability(MobileCapabilityType.NEW_COMMAND_TIMEOUT, "4000");
        cap.setCapability(MobileCapabilityType.APP, "c://apks//listviewsample.apk");
        cap.setCapability("noReset", true);
        driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), cap);

    }
    
    @Test
    public void testScroll()throws Exception
    {
        driver.findElementByAccessibilityId("Views").click();
        AndroidElement list = (AndroidElement) driver.findElement(By.id("android:id/mobile_list"));
        MobileElement listGroup = list
                .findElement(MobileBy
                        .AndroidUIAutomator("new UiScrollable(new UiSelector()).scrollIntoView("
                                + "new UiSelector().text(\" List item:25\"));"));
        assertNotNull(listGroup.getLocation());
        listGroup.click();
    }

    @After
    public void testCaseTearDown()
    {
        driver.quit();
    }
}
with this post you can write test code for Vertical Scroll up to specified dimensions , Vertical Scroll till you find the element  and vertical scroll till the end.

I hope you find this tutorial useful. Do share your feedback and questions in comments section below.Please follow me on social media to get latest post updates.

21 comments:

  1. driver.scrollTo("List item:25") is undefined for type AndroidDriver. How do I fix it? Thank advance.

    ReplyDelete
  2. Hi, check this out - http://www.qaautomated.com/2016/10/how-to-perfrom-vertical-scroll-without.html

    ReplyDelete
  3. hi, I am using this method, but eclipse is saying that this method is depreciated, is there any other method round?

    ReplyDelete
  4. hi,i am unable to get these 2 import can u plz guide me how to get these
    import io.appium.java_client.android.AndroidDriver;
    import io.appium.java_client.android.AndroidElement;
    i am useing latest javaclient jar 5.0

    ReplyDelete
  5. Can i get an apk file , please?

    ReplyDelete
  6. I have textview element (Not list view item) which is not visible . It is visible only when i swipe/scroll up. I am unable to click it as it is not visible. How can i scroll up and click on that item

    ReplyDelete
  7. This comment has been removed by the author.

    ReplyDelete
  8. Its really helpful post. Could you please advise- how to achieve this on iOS Native App?

    Thanks

    ReplyDelete
  9. Hi,Can you pls provide an example to do horizontal Scroll in Appium as well without using swipe() and scrollTo()function.

    ReplyDelete
  10. Hi,
    Using your code I am able to scroll to the text but unable to select the text in dropdown. No element found error is being thrown. Below is the code:
    AndroidElement list = (AndroidElement) driver.findElement(By.id("coles.au.simplertools:id/locationName"));
    driver.findElementById("coles.au.simplertools:id/locationName").click();
    MobileElement listGroup = list.findElement(MobileBy.AndroidUIAutomator("new UiScrollable(new UiSelector()).scrollIntoView("+ "new UiSelector().text(\"GROCERY\"));"));
    assertNotNull (listGroup.getLocation());
    listGroup.click();

    ReplyDelete
    Replies
    1. This comment has been removed by the author.

      Delete
    2. Instead of listGroup.click replace listGroup with the element you want to located and perform the action

      Delete
  11. How to swipe list view and recycle view item horizontally

    ReplyDelete
  12. From appium version 1.6.0 and onwanrds 'scrollTo' method is depreciated by appium. You can perform scrollTo action operation using swipe method. You just need to run a 'for' loop number of times you want to scroll for the presence of Element. In each loop check the presence of Element by getting list of required element. If Element found break the loop and click.

    ReplyDelete
  13. This works for scroll down. How would you do for scroll up to an element?

    ReplyDelete
  14. Please do write comment in your code. It would be more usefull to undersatnd.

    ReplyDelete
  15. Sanket Gajbhiv12 May 2018 at 03:33

    Thanks a lot. It works.
    I was facing issue from so many days, now its working perfectly.

    ReplyDelete
  16. Can you plz explain how to scroll up after doing scroll down...??

    ReplyDelete