Pages

Appium

How to handle Datepicker in Appium ?


Hello Friends,

In this tutorial you will learn how to handle datepickers in your android app using Appium.

It is little bit tricky to handle but after this tutorial you will definitely able to do so. Datepicker view can be different as per android version and devices. Here I have below kind of datepicker and opened in UIAutomatorviewer to see the elements and we have to go through below steps to select a date.

For example I have to select a date i.e 15 August 2010. So I have to go using these steps

    Step 1: Click on the year present on calendar to select required year (2010)


    Step 2 :  perform scroll and match weather your required year is there.



    Step 3 :  If required year is there then perform click on it. this is how we will be able to select the year


    Step 4 : Now get all the dates present in the view and compare one by one weather your required date (15 August 2010) is there if now then scroll up until January month if still not found the perform scroll down until date matches



    Step 5 : Once date is there and select it and click OK of the calendar.


I have implemented above mentioned steps in below code. hope you understood well. I'm using Page Factory design pattern you can modify the code as per your need.

Here I have created 2 classes named DatePickerHelper and ScrollAndSwipeHelper.

DatePickerHelper have one common method named selectDate(String fromOrToDate)  it take one string type parameter as date you have to call this method from your code where you require date selection and pass date in this method as 10 January 2018 format.

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
DatePickerHelper.java

       
package com.myapp.helper;
import java.util.List;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
import org.openqa.selenium.support.pagefactory.AjaxElementLocatorFactory;
import io.appium.java_client.MobileElement;
import io.appium.java_client.android.AndroidDriver;

public class DatePickerHelper
{
 
    AndroidDriver driver;
 
    @FindBy(id="android:id/date_picker_year")    
    public WebElement datepickerYearView;
 
    @FindBy(id="android:id/month_text_view")    
    public List datepickerYear;
 
    @FindBy(xpath="//*[@class='android.widget.ListView']/*[@class='android.view.View']/*[@class='android.view.View']")    
    public List dateSelector;
    boolean Flag = true;
 
    public DatePickerHelper(AndroidDriver driver)
    { 
        PageFactory.initElements(driver, this);
        this.driver=driver;
    }
 
    public void selectDate(String fromOrToDate) throws InterruptedException
    {  
       String[]  data = fromOrToDate.split(" ");

       datepickerYearView.click();
       Boolean yearflag = true;
   
       while(yearflag)
       {
           for(WebElement year : datepickerYear)
           {
               if(year.getText().equals(data[2]) && year.isDisplayed())
               {
                   year.click();
                   yearflag=false;
                   break;
               }
           }
          if(yearflag)
          {
              ScrollAndSwipeHelper.calenderScrollDown(driver);
          }
      }

      Boolean dateflag = true; 
      while(dateflag)
      {  
          for(WebElement dateAndMonth : dateSelector)
          {
              if(dateAndMonth.getAttribute("name").contains(fromOrToDate.trim()))
              {
                  dateAndMonth.click();
                  dateflag =false;
                  break;
              }
          }
         if(dateflag)
         {
             if(!(dateSelector.get(10).getAttribute("name").contains("January"))&&(Flag==true))
             {
                 ScrollAndSwipeHelper.calenderScrollDown(driver);
                 Flag =  false;
             }
             else
             {
                 ScrollAndSwipeHelper.calenderScrollUp(driver);
             }
         }
      }
   }
}

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
This class have 2 method to swiping  calendar view up and down regarding required date selection.

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------


ScrollAndSwipeHelper.java

package com.myapp.helper;
import org.openqa.selenium.Dimension;
import org.openqa.selenium.WebElement;
import io.appium.java_client.MobileElement;
import io.appium.java_client.TouchAction;
import io.appium.java_client.android.AndroidDriver;

public class ScrollAndSwipeHelper {

    public static void calenderScrollDown(AndroidDriver driver) 
    {
        int x = 700;
        int starty = 1110;
        int  endy = 1900;
        new TouchAction(driver).press(x, starty).waitAction().moveTo(x, endy).release().perform();
     }
 
     public static void calenderScrollUp(AndroidDriver driver) 
     {
        int x = 700;
        int starty = 1900;
        int  endy = 1110;
        new TouchAction(driver).press(x, starty).waitAction().moveTo(x, endy).release().perform();
     }
} 

No comments:

Post a Comment