Created
October 31, 2012 00:51
-
-
Save adamgoucher/3984135 to your computer and use it in GitHub Desktop.
Revisions
-
adamgoucher created this gist
Oct 31, 2012 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,65 @@ from selenium.common.exceptions import UnexpectedTagNameException from selenium.webdriver.support.wait import WebDriverWait from selenium.webdriver.common.action_chains import ActionChains class Selectmenu(object): def __init__(self, webelement): if webelement.tag_name.lower() != "select": raise UnexpectedTagNameException( "Selectmenu only works on <select> elements, not on <%s>" % webelement.tag_name) if webelement.is_displayed(): raise ElementNotVisibleException( "jquery.ui.selectmenu select's are hidden. This is not.") self._id = webelement.get_attribute("id") self.driver = webelement._parent @property def options(self): ul = self.driver.find_element_by_id("%s-menu" % self._id) options = ul.find_elements_by_css_selector('a[role="option"]') visible_text = [] for option in options: visible_text.append(self.driver.execute_script( """ var element = arguments[0]; if (element.innerText) { return element.innerText; } else { return element.textContent; } """, option )) return visible_text def select_by_visible_text(self, text): w = WebDriverWait(self.driver, 5) ul = self.driver.find_element_by_id("%s-menu" % self._id) a = ul.find_element_by_xpath('//a[text()="%s"]' % text) li = ul.find_element_by_xpath('//a[text()="%s"]/..' % text) self.driver.find_element_by_id("%s-button" % self._id).click() w.until(lambda driver : a.is_displayed()) # holy cow this is a hack, but there is some bit of internal state # i don't know how to sync on happening here. this makes it work and # my head hurts so giving up import time time.sleep(1) ac = ActionChains(self.driver) ac.click_and_hold(a) ac.release(li) ac.perform() w.until(lambda driver : not a.is_displayed()) @property def first_selected_option(self): button = self.driver.find_element_by_id("%s-button" % self._id) return button.find_element_by_css_selector('.ui-selectmenu-status').text This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,27 @@ from selenium.webdriver import Firefox from selenium.webdriver.support.wait import WebDriverWait from selectmenu import Selectmenu class TestSelectmenu(object): def setup_method(self, method): self.driver = Firefox() self.driver.get("http://jquery-ui.googlecode.com/svn/branches/labs/selectmenu/index.html") def teardown_method(self, method): self.driver.quit() def test_options(self): select = self.driver.find_element_by_id("speedA") selectmenu = Selectmenu(select) assert(len(selectmenu.options) == 5) def test_select_by_visible_text(self): select = self.driver.find_element_by_id("speedA") selectmenu = Selectmenu(select) selectmenu.select_by_visible_text('Faster') assert(selectmenu.first_selected_option == 'Faster') def test_first_selected_option(self): select = self.driver.find_element_by_id("speedA") selectmenu = Selectmenu(select) assert(selectmenu.first_selected_option == 'Medium')