Appium - Select Item From Drop Down Of Android App

Selecting item/value from drop down is needed in android software app as most of the apps contain drop down. Earlier we learnt how to select value from spinner in android appium software test which is opening in direct list in THIS POST. Now let's see another example of how to select value from drop down list which is opening in popup.

App To Use And Aim Of Test
We will use API Demos software app in this drop down item selection test. Our main aim is to click on drop down to open items list popup and then selecting one item from list as shown in bellow image.

appium select value from drop down list

You can view above screen from API Demos software app's Home -> Views -> Controls -> 2. Dark Theme. -> Tap on Drop Down.

Create And Run Test
I have created very simple appium android drop down item selection software automation test. Create bellow given test in eclipse.

SelectValueDropDown.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 SelectValueDropDown {
 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 select() 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();
  // Scroll till element which contains "Controls" text If It Is not visible on screen.
  driver.scrollTo("Controls");
  // Click on Controls.
  driver.findElement(By.name("Controls")).click();
  // Scroll till element which contains "2. Dark Theme" text If It Is not visible on screen.
  driver.scrollTo("2. Dark Theme");
  // Click on 2. Dark Theme.
  driver.findElement(By.name("2. Dark Theme")).click();
  // Typing in text box using sendKeys command.
  driver.findElement(By.id("io.appium.android.apis:id/edit")).sendKeys("Test");
  //To hide keyboard 
  driver.hideKeyboard();
  //Click on dropdown to open list.
  driver.findElement(By.id("android:id/text1")).click();
  //Select item "Mars" from drop down list.
  driver.findElement(By.name("Mars")).click();   
 }

 @AfterTest
 public void End() {
  driver.quit();
 }
}

Run above test in eclipse using appium and testng and observe test execution. Last syntax will select item "Mars" from list items.

This way you can select value from android software app's drop down list in appium test.

3 comments:

  1. Hello, thanks for all your help by these blogs, but some of the example using "scrollTo" function, but scrollTo is deprecated, can you please update code, so it would be easy for beginners to get help from your blogs.

    ReplyDelete
  2. Hopefully author must be working on updating the scripts as most of the methods are depreciated.
    Thanks in advance.

    ReplyDelete
  3. hello please mention syntax of get drop-down list and click on any of the drop-down list name.
    first thing remember that click on drop-down using coding but next step is not able to get inspect, so how can manage this things.
    Plz give solution on that.
    thanks

    ReplyDelete