Controlling a jQuery datepicker with Watir

Using a jQuery datepicker with the Watir Webdriver - example code

In my last post I talked about some issues using a dropdown with Watir, the context I was using that in was to complete a jQuery datepicker field that contained both a year and month dropdown.

In case anyone needs the code to be able to do this with Watir Webdriver here it is;

@title = div(:class, 'ui-datepicker-title')
text_field(:class, 'datepicker').click
@title.select(:class, 'ui-datepicker-year').select_value('2006')
@title.select(:class, 'ui-datepicker-month').select('Jan')
table(:class, 'ui-datepicker-calendar').a(:class, 'ui-state-default').click

@title is just a container for the div that contains it.

The two lines with .select will change the value of the year and month dropdowns, there is an change event associated with these inside of jQuery so it will update the calendar automatically for you.

The final line is a bit of a hack because each day doesn’t have a unique id or class, in my test I didn’t really care about the day so I just picked the first standard weekday but you could use :index, 5 or something with the a function and play about to pick the appropriate day.


Recent posts View all

Ruby

Iterate over indexed params in Rails

When your API isn't Rails-shaped, sometimes you need to handle input differently

Web Dev Jekyll

Running Jekyll from inside VS Code

How to set up your VS Code to run Jekyll without needing extra extensions