Appium - Set Date And Time In Android App Test Automation

Android software apps contains date pickers. Date picker can be Inline or It can be In dialog. Just like other elements of android app software, We can automate android app's date dialog to set date and time dialog to set time In android appium software automation test. Here I have demonstrated simple example on how to set android app date and time In appium test. For your android app's date picker, Date format can be different but method to set It will remain same as described bellow.

App To Use Android App Date And Time Test
We will use same API Demos app to demonstrate how to set date and time In android appium software automation test. Download API Demos android software app from THIS PAGE.

Aim To Achieve In Set Date Time Test
We wants to set date = 25 Aug 2009 In date dialog and time = 1:25 pm In time dialog as shown In bellow Images.

Set Date In Dialog

Set Time In Dialog

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

Create and Run Set Date Time Android Appium Test
Create new SetDate.java file under Android package of your project to test android software app and copy paste bellow given test script In 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 above test, We have created two @Test methods. 

dateSet() Method
dateSet() method will,
  • Navigate to Dialog Screen.
  • Then It will click on change the date button. It will open set date dialog.
  • Next 3 driver.findElement() method will locate date, month and year textbox and set requested date, month and year In related field.
  • Then It will click on Done button to close date dialog.
timeSet() method
  • It will click on change the time button. It will open set time dialog.
  • Next 3 driver.findElement() method will locate hour, minute and am or pm textbox and set requested hour, minute and am or pm In related field.
  • Then It will click on Done button to close date dialog.
This Is the way to set date and time in date and time dialog of android appium software automation test.

14 comments:

  1. I tried with above its not working can you please me

    ReplyDelete
  2. Hi, is this going to work on all devcies?
    I mean Nexus, Moto, Samsung and other.

    ReplyDelete
    Replies
    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?

      Delete
    2. If calender view is different then may be you need to change code.

      Delete
    3. 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

      Delete
  3. post some appium interview questions also helpful

    ReplyDelete
  4. What is the method to open the date settings from the android phone?

    ReplyDelete
  5. Well Done Bro!!!
    Eagerly waiting for next series of Tutorials.

    ReplyDelete
  6. scrollTO method is not working

    ReplyDelete
  7. scrollTO method is not working, Can anyone please suggest the workaround

    ReplyDelete
  8. how to scroll the calender.. picking the Single date is working i need to scroll the date automatically.

    ReplyDelete
    Replies
    1. 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

      Delete