Appium – Set Date And Time In Android App Test Automation

Android software apps contain date pickers. The date picker can be Inline or it can be in a dialog. Just like other elements of Android app software, we can automate an Android app’s date dialog to set the date and time dialog to set time in an Android appium software automation test. Here I have demonstrated a simple example on how to set the Android app date and time in an Appium test. For your Android app’s date picker, the Date format can be different, but the method to set it will remain the same as described below.

App To Use Android App Date And Time Test

We will use the same API Demos app to demonstrate how to set the date and time in the Android appium software automation test. Download the API Demos Android software app from the Appium test apps page.

Aim To Achieve In the Set Date and Time Test

We want to set date = 25 Aug 2009 in the date dialog and time = 1:25 pm in the time dialog as shown in bellow Images.

Appium Set Date In Dialog

Set date in Appium.
Appium Set date. Image by Author.

Appium Set Time In Dialog

Appium set time.
Appium Set time. Image by Author.

Manually, you can navigate to the above screen from the API Demos app -> Tap on Views -> Tap on Date Widgets -> Tap on 1. Dialog.

Create and Run Set Date Time Android Appium Test

Create a new SetDate.java file under the Android package of your project to test the Android software app and copy and paste the test script given below into it.

SetDate.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 SetDate {
 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 dateSet() {
  // Scroll till element which contains "Views" text.
  driver.scrollTo("Views");
  // Click on Views.
  driver.findElement(By.name("Views")).click();
  // Scroll till element which contains "Date Widgets" text.
  driver.scrollTo("Date Widgets");
  // Click on element which contains "Date Widgets" text.
  driver.findElement(By.name("Date Widgets")).click();
  // Scroll till element which contains "1. Dialog" text.
  driver.scrollTo("1. Dialog");
  // Click on element which contains "1. Dialog" text.
  driver.findElement(By.name("1. Dialog")).click();  
  // Click on button which contains "change the date" text.
  driver.findElement(By.name("change the date")).click();
  //Set Date = 25.
  driver.findElement(By.xpath("//android.widget.NumberPicker[@index='0']")).sendKeys("25");
  //Set Month = Aug.
  driver.findElement(By.xpath("//android.widget.NumberPicker[@index='1']")).sendKeys("Aug");
  //Set Year = 2009.
  driver.findElement(By.xpath("//android.widget.NumberPicker[@index='2']")).sendKeys("2009");
  //Click on Done button.
  driver.findElement(By.id("android:id/button1")).click();
 }

 
 @Test
 public void timeSet() {   
  // Click on button which contains "change the time" text.
  driver.findElement(By.name("change the time")).click();
  //Set Hours = 1.
  driver.findElement(By.xpath("//android.widget.NumberPicker[@index='0']")).sendKeys("1");
  //Set Minutes = 25.
  driver.findElement(By.xpath("//android.widget.NumberPicker[@index='2']")).sendKeys("25");
  //Set pm.
  driver.findElement(By.xpath("//android.widget.NumberPicker[@index='1']")).sendKeys("pm");
  //Click on Done button.
  driver.findElement(By.id("android:id/button1")).click();
 }
 @AfterTest
 public void End() {
  driver.quit();
 }
}

Test Script Description

As you can see in the above test, we have created two @Test methods.

dateSet() Method

dateSet() method will,

  • Navigate to the Dialog Screen.
  • Then it will click on the change the date button. It will open a set date dialog.
  • Next 3 driver.findElement() method will locate the date, month, and year textbox and set the requested date, month, and year in the related field.
  • Then it will click on the Done button to close the date dialog.

timeSet() method

  • It will click on the change the time button. It will open a set time dialog.
  • Next 3 driver.findElement() method will locate the hour, minute, and am or pm textbox and set the requested hour, minute, and am or pm in the related field.
  • Then it will click on the Done button to close the date dialog.

This is the way to set the date and time in the date and time dialog of the Android appium software automation test.

14 thoughts on “Appium – Set Date And Time In Android App Test Automation

  1. Hi Aarvind, I tried this code on Motog, Nexus 5X, S4 and it could open the calendar, but not the calendar view like your blog has.
    Also, when i tried to manually choose date by the displayed calendar I couldn't change the date of device.
    Can you help further with same?

  2. Hi Aravind
    How you took Xpath for change date & Time Elements,
    i tried to find index values for day month and year but it shows same index value for all how did you find index values day=0 month = 1, year =3
    Plese let me know
    Thank you for help

  3. Hi, you can try the below to do the same thing as scroll to

    public void dateSet() {
    // Scroll till element which contains "Views" text.
    // driver.scrollTo("Views");
    // Click on Views.
    //driver.findElement(By.name("Views")).click();
    String str="Views";
    driver.findElementByAndroidUIAutomator("new UiScrollable(new UiSelector().scrollable(true).instance(0)).scrollIntoView(new UiSelector().textContains(""+str+"").instance(0))").click();

    Cheer

    Mak

Leave a Reply

Your email address will not be published. Required fields are marked *