Appium - How To Scroll Horizontal Tabs Using Appium In Android App

In android appium software automation test, You also needs to scroll tabs horizontally(from right to left or left to right) If there are multiple tabs In your android software app and you wants to navigate to the tab which Is displaying when you scroll tabs horizontally. Earlier In my previous post, We learnt how to scroll down In android software app using appium test. We also learnt how to swipe In android software app using driver.swipe() In THIS POST and using TouchAction class In THIS POST. Here we can use driver.swipe() method to swipe tabs from right to left. I have prepared very simple android software automation example to learn how to swipe android app's tabs In appium automation test.

Install API Demos App In Mobile Device
You can view my PREVIOUS POST to know from where to download API Demos App. Install It In your mobile device.

Aim To Achieve In This Appium Test
In this test, We wants to scroll tabs horizontally from right to left side until Tab 11 display as shown in bellow Image.

Manually you can view above given screen In your mobile device from API Demos App -> Views -> Tabs -> 5. Scrollable. You will find that top level tabs are scroll-able.

Before learning tabs scrolling In appium software automation test, I recommend you to read my previous post about vertical scrolling In appium test as we are going to use vertical scrolling in this example too.

Get Y Coordinates Of Tabs Grid Middle
To get Y Coordinates of tabs grid,
  1. Connect your mobile device with PC and open API Demos app In your device.
  2. Navigate manually to 5. Scrollable section as described above.
  3. Open uiautomatorviewer from tools folder of SDK. view THIS POST.
  4. Capture Screenshot by clicking on Capture screenshot button.
  5. Move your mouse at middle of tabs grid.
  6. Note down Y Coordinates as shown In bellow Image.

Create And Run Android Appium Tabs Scrolling Test
Create new file under your project and copy paste bellow given test script In It.

Note : Please set your device's capabilities in bellow given test.
package Android;


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.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class ScrollTabs {
 AndroidDriver driver;
 Dimension size;
 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", "");
  driver = new AndroidDriver(new URL(""),capabilities);
  driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);

 public void ScrollToTab() throws InterruptedException {
  //Scroll till element which contains "Views" text If It Is not visible on screen.
  //Click on Views/.
  System.out.println("Vertical scrolling has been started to find text -> Tabs.");
  //Scroll till element which contains "Tabs" text.
  System.out.println("Tabs text has been found and now clicking on It.");
  //Click on Tabs
  //Click on Scrollable text element.
  driver.findElement("5. Scrollable")).click();
  System.out.println("Horizontal scrolling has been started to find tab -> Tab 11.");
  //Used for loop to scroll tabs until Tab 11 displayed.
  for(int i=0; i<=10; i++){
   //Set implicit wait to 2 seconds for fast horizontal scrolling.
   driver.manage().timeouts().implicitlyWait(2, TimeUnit.SECONDS);   
   if(driver.findElements("Tab 11")).size()!= 0){
    //If Tab 11 Is displayed then click on It.
    System.out.println("Tab 11 has been found and now clicking on It.");
    driver.findElement("Tab 11")).click();
    //If Tab 11 Is not displayed then scroll tabs from right to left direction by calling ScrollTabs() method.
  //Set implicit wait to 15 seconds after horizontal scrolling.
  driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
  //Locate parent element of text area.
  WebElement ele1 = (WebElement) driver.findElements("android:id/tabcontent")).get(0);
  //Locate text area of Tab 11 using It's parent element.
  WebElement ele2 = ele1.findElement(By.className("android.widget.TextView"));
  //Get text from text area of Tab 11 and print It In console.
  System.out.println("Text under selected tab is -> "+ele2.getText());
 //To scroll tabs right to left In horizontal direction.
 public void ScrollTabs() {
  //Get the size of screen.
  size = driver.manage().window().getSize();  
  //Find swipe start and end point from screen's with and height.
  //Find startx point which is at right side of screen.
  int startx = (int) (size.width * 0.70);
  //Find endx point which is at left side of screen.
  int endx = (int) (size.width * 0.30);
  //Set Y Coordinates of screen where tabs display.
  int YCoordinates = 150;  

  //Swipe tabs from Right to Left.
  driver.swipe(startx, YCoordinates, endx, YCoordinates, 3000);  
 public void End() {

Test Script Description
In above test script, 
  • I have used for loop to continue execution until Tab 11 display on screen.
  • If condition Inside for loop will check If Tab 11 Is displayed or not.
  • If tab 11 Is displayed then click on It and break the loop.
  • Else Call ScrollTabs(); method which Is responsible for swiping tab from right to left.
  • Statements written bellow for loop will get text from  text area and print It In console.
Now I hope, You already learnt how to run appium software automation test In eclipse with testng. View my previous appium tutorials for more detail. When you run above test, It will Navigate to 5. Scrollable screen and then scroll tabs In horizontal direction until Tab 11 displayed.

This way you can scroll tabs in horizontal direction In your appium android test.

1 comment:

  1. Could you please create a repository with the application .apk files for the examples? Would be very useful
    Thank you