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

Forcing a Rails database column to be not null

How you can force a table column to always have something in it with Rails

Writing Marketing

We've deleted an article's worth of unhelpful words

We've improved several pages across our site by removing words that add no value, and often detract from the article.