Skip to content

DatePicker automation sample

Herno edited this page Jun 2, 2015 · 5 revisions

Automating DatePicker control

Here is an example code on how to automate a datepicker by using the rollers.

Having a XAML something like:

 <DatePicker Date="{Binding SelectedDate, Mode=TwoWay}" AutomationProperties.AutomationId="MyDateControl" />

We can write a generic method to input a desired date in the DatePicker:

public void InputDate(DateTime date)
{
   var datepicker = driver.FindElementById("MyDateControl");
   // Find the inner Button in the datePicker
   var input = datepicker.FindElement(By.ClassName("Windows.UI.Xaml.Controls.Button"));
   input.Click();  // Opens the date picker           
   Thread.Sleep(1000);  // Await animation

     // Parse the current datepicker date (this is for spanish format)
     string[] values = input.Text.Replace("\u200E", "").Split('/');
     var touches = new Actions(this.driver);
     var current = new DateTime(Int32.Parse(values[2]), Int32.Parse(values[1]), Int32.Parse(values[0]));

    // Year picker          
        if (current.Year > value.Year)
        {
            DateTap(400, 450, touches); // Activate Roller
            for (int i = current.Year - value.Year; i > 0; i--) DateTap(400, 300, touches);
            DateTap(400, 450, touches); // De-activate Roller               
        }
        else if (current.Year < value.Year)
        {
            DateTap(400, 450, touches); // Activate Roller
            for (int i = value.Year - current.Year; i > 0; i--) DateTap(400, 600, touches);
            DateTap(400, 450, touches); // De-activate Roller
        }

        TapAppBar(driver.FindByXName("AcceptBtn"));

        input = datepicker.FindElement(By.ClassName("Windows.UI.Xaml.Controls.Button"));
        input.Click();  // Opens the date picker        
        Thread.Sleep(1000);

        // Month pick
        if (current.Month > value.Month)
        {
            DateTap(240, 450, touches); // Activate Roller
            for (int i = current.Month - value.Month; i>0; i--) DateTap(240, 300, touches);                
            DateTap(240, 450, touches); // De-activate Roller               
        }
        else if (current.Month < value.Month)
        {
            DateTap(240, 450, touches); // Activate Roller
            for (int i = value.Month - current.Month; i > 0; i--) DateTap(240, 600, touches);
            DateTap(240, 450, touches); // De-activate Roller
        }

        TapAppBar(driver.FindByXName("AcceptBtn"));

        input = datepicker.FindElement(By.ClassName("Windows.UI.Xaml.Controls.Button"));
        input.Click();  // Opens the date picker        
        Thread.Sleep(1000);

        // Day pick
        if (current.Day > value.Day)
        {
            DateTap(80, 450, touches); // Activate Roller
            for (int i = current.Day - value.Day; i > 0; i--) DateTap(80, 300, touches);
            DateTap(80, 450, touches); // De-activate Roller               
        }
        else if (current.Day < value.Day)
        {
            DateTap(80, 450, touches); // Activate Roller
            for (int i = value.Day - current.Day; i > 0; i--) DateTap(80, 600, touches);
            DateTap(80, 450, touches); // De-activate Roller
        }

        TapAppBar(driver.FindByXName("AcceptBtn"));                  
}


    private void TapAppBar(IWebElement appBarButton)
    {
        IJavaScriptExecutor js = driver as IJavaScriptExecutor;
        js.ExecuteScript("automation: InvokePattern.Invoke", appBarButton);
    }

    private void DateTap(int x, int y, Actions touches)
    {
        touches.MoveByOffset(x, y).Click().Perform();
        Thread.Sleep(300);
    }