Monday 29 February 2016

How to do Horizontal Scroll in Appium?



In the previous post we have seen that how to perform vertical scroll in Appium and now it is turn to learn horizontal scroll in Appium.Now many apps has horizontal tab scroll and testing that seems challenging first but with this post you will be able to write appium test case very efficiently. We will use ToucAction class and driver.swipe() method to achieve desired result.

App Under Test-
Consider that you have a app with 15 Horizontal Tabs named as tab1,tab2,tab3 and so on till tab15

Test Case Scenario-
1. Launch the app
2. perform right to left tab till we can see tab8.
3. click on tab8.

Finding Y-Coordinate -
We need y co-ordinates of the screen  and to find that there are two ways -
1. Using Pointer Location-
    * Go to phones Settings
    * Click on Developer Options
    * Enable Pointer Location 
    * open your app and click on the mid of the horizontal tab and then you will get x and y
       coordinates but note down y- coordinates.

2. Using UI automator - 
   *Open UIAutomator Inspector.
   * Take screen shot.
   * Click on middle of the horizontal tab and note down y-coordinates displayed at top right corner.

Test Case -
1. We will implement horizontalScroll() method. In this method we will get the screen size with that we can find our start point on right side and end point on left side as we are going for right to left swipe. We also have y co-ordinates so we can use driver.swipe() to perform horizontal scroll as shown below
2. We are using for loop to scroll till we find the expected tab.

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//seekbarsample.apk");
        cap.setCapability("noReset", true);
        driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), cap);

    }
  
    @Test
    public void testHorizontalScroll()throws Exception
    {
        for(int i=0;i<4;i++)
        {
            Thread.sleep(2000);
            if (driver.findElement(By.name("tab8")).isDisplayed())
            {
                driver.findElement(By.name("tab8")).click();
                break;
            }
            else
            {
                horizontalScroll();
            }

        }
    }
    public void horizontalScroll()
    {
        size=driver.manage().window().getSize();
        int x_start=(int)(size.width*0.60);
        int x_end=(int)(size.width*0.30);
        int y=130;
        driver.swipe(x_start,y,x_end,y,4000);
    }
    @After
    public void testCaseTearDown()
    {
        driver.quit();
    }
}

Note - The same test case can be used for Vertical Scroll using start_y and end_y and x value will be constant (Try this out).

With help of this tutorial you can write appium test code for right to left horzontal scroll/swipe , left to right horzontal scroll/swipe , horizontal scroll until you find matching element.

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.

8 comments:

  1. ITs not working for ios device.

    ReplyDelete
  2. ITs not working for ios device.

    ReplyDelete
  3. Can you Add Video of this

    ReplyDelete
  4. Swipe is deprecated I got it working with following code.
    el = self.driver.find_element_by_xpath("//XCUIElementTypeTable[1]/XCUIElementTypeCell[1]")
    #delete first row of the table by long pressing it moving it to left release and perform.then tap and perform.
    el_size = el.size
    print el_size['width']
    print el_size['height']
    touch = TouchAction(self.driver)
    touch.press(el,375,65).wait(15).move_to(el,225,65).release()
    touch.perform()
    # to delete element we need to tap on same location
    touch.tap(el,375,65,1).perform()
    https://appium.readthedocs.io/en/stable/en/writing-running-appium/touch-actions/

    ReplyDelete
  5. BUT WONT this code is specific for a particular device only, If I use this code for a 4 inch phone and 6 inch phone, would it work?

    ReplyDelete
  6. this code is not working at all for swiping. And swipe method is deprecated

    ReplyDelete
    Replies
    1. Swipe method is deprecated even latest appium java client 7.3.0

      Delete
  7. With help of this tutorial you can write appium test code for right to left horzontal scroll/swipe , left to right horzontal scroll/swipe , horizontal scroll until you find matching element. antique gold earrings designs with price , flapper head pieces , cardboard jewelry boxes , nishat linen scarf

    ReplyDelete