Created
May 8, 2015 23:54
-
-
Save adamgoucher/aade0b8719149cbfcf22 to your computer and use it in GitHub Desktop.
JQueryDatePicker helper class for webedriver
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from selenium import webdriver | |
class NoSuchDayException(Exception): | |
""" Custom exception for trying to select an invalid day of the month | |
Rather than try and be clever around which days are valid in a month, we trust that | |
the widget is populating things correctly and just blow up if you request the 4th day | |
of the month for instance. | |
""" | |
pass | |
class JQueryDatePicker(object): | |
""" Helper class for dealing with JQueryUI Datepicker controls/widgets/whatever """ | |
def __init__(self, browser, locator): | |
self.driver = browser | |
self.locator = locator | |
def pick(self, day="", month="", year=""): | |
""" Navigate through the datepicker control and select the date | |
Using named arguments rather than try and dictate a specific date format. If | |
someone wants to format things a specific way they can wrap this class and parse | |
their input before passing it along here | |
""" | |
_how_many_clicks = 0 | |
# this is the input field that the datepicker control is attached to (in this | |
# standalone example it doesn't make sense, but in the larger framework it does...) | |
click_to_open = self.driver.find_element_by_css_selector(self.locator) | |
click_to_open.click() | |
# everything is relative to this though | |
datepicker = self.driver.find_element_by_id('ui-datepicker-div') | |
# is current year? | |
year_element = datepicker.find_element_by_css_selector('.ui-datepicker-year') | |
current_year = year_element.text | |
year_difference = int(current_year) - year | |
_how_many_clicks += year_difference * 12 | |
# is current month? | |
month_element = datepicker.find_element_by_css_selector('.ui-datepicker-month') | |
current_month = month_element.text | |
months = ['January', 'February', 'March', 'April', 'May', 'June', | |
'July', 'August', 'September', 'October', 'November', 'December'] | |
current_month_number = months.index(current_month) + 1 | |
month_difference = current_month_number - month | |
_how_many_clicks += month_difference | |
# navigate to correct month | |
if _how_many_clicks != 0: | |
if _how_many_clicks < 0: | |
_how_many_clicks = abs(_how_many_clicks) | |
month_mover_locator = '.ui-datepicker-next' | |
else: | |
month_mover_locator = '.ui-datepicker-prev' | |
for x in range(0, _how_many_clicks): | |
month_mover = datepicker.find_element_by_css_selector(month_mover_locator) | |
month_mover.click() | |
has_date = datepicker.find_elements_by_xpath('//table//a[text()="%s"]' % day) | |
if len(has_date) == 1: | |
has_date[0].click() | |
else: | |
raise NoSuchDayException() | |
browser = webdriver.Firefox() | |
browser.get('https://jqueryui.com/datepicker/') | |
f = browser.find_element_by_css_selector('iframe.demo-frame') | |
browser.switch_to.frame(f) | |
picker = JQueryDatePicker(browser, '#datepicker') | |
picker.pick(day=15, month=4, year=2015) | |
browser.quit() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment