-
-
Save dariodiaz/3374676 to your computer and use it in GitHub Desktop.
python: dhx_combo_box
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
import pytest | |
from selenium.webdriver import Firefox | |
from selenium.webdriver.common.by import By | |
from selenium.webdriver.support.wait import WebDriverWait | |
from selenium.webdriver.support.select import Select | |
from selenium.common.exceptions import UnexpectedTagNameException | |
class ComboBox(object): | |
def __init__(self, webelement): | |
if webelement.tag_name.lower() != "div": | |
raise UnexpectedTagNameException( | |
"Combo only works on <div> elements, not on <%s>" % | |
webelement.tag_name) | |
if "dhx_combo_box" not in webelement.get_attribute("class"): | |
raise UnexpectedTagNameException( | |
"Combo only works on elements, with 'dhx_combo_box' classes") | |
self._el = webelement | |
self.name = self._el.find_element_by_css_selector('input[type="hidden"]:not([name$="_new_value"])').get_attribute("name") | |
@property | |
def options(self): | |
return self._el._parent.execute_script( | |
""" | |
var name = arguments[0]; | |
var combo = null; | |
var options = new Array(); | |
for (var i = 0; i < window.dhx_glbSelectAr.length; i++) { | |
if (window.dhx_glbSelectAr[i].name == name) { | |
var combo = window.dhx_glbSelectAr[i]; | |
break; | |
} | |
} | |
if (combo) { | |
for (var o = 0; o < combo.optionsArr.length; o++) { | |
options.push(combo.optionsArr[o].text); | |
} | |
} else { | |
throw new Exception("dhtmlxCombo not found"); | |
} | |
return options; | |
""", | |
self.name | |
) | |
@property | |
def selected(self): | |
return self._el._parent.execute_script( | |
""" | |
var name = arguments[0]; | |
var combo = null; | |
var options = new Array(); | |
for (var i = 0; i < window.dhx_glbSelectAr.length; i++) { | |
if (window.dhx_glbSelectAr[i].name == name) { | |
var combo = window.dhx_glbSelectAr[i]; | |
break; | |
} | |
} | |
if (combo) { | |
return combo.getSelectedText(); | |
} else { | |
throw new Exception("dhtmlxCombo not found"); | |
} | |
""", | |
self.name | |
) | |
def select(self, option): | |
return self._el._parent.execute_script( | |
""" | |
var name = arguments[0]; | |
var option = arguments[1]; | |
var index = null; | |
var combo = null; | |
var options = new Array(); | |
for (var i = 0; i < window.dhx_glbSelectAr.length; i++) { | |
if (window.dhx_glbSelectAr[i].name == name) { | |
var combo = window.dhx_glbSelectAr[i]; | |
break; | |
} | |
} | |
if (combo) { | |
ops: | |
for (var o = 0; o < combo.optionsArr.length; o++) { | |
if (combo.optionsArr[o].text == option) { | |
index = o; | |
break ops; | |
} | |
} | |
combo.selectOption(o); | |
} else { | |
throw new Exception("dhtmlxCombo not found"); | |
} | |
""", | |
self.name, | |
option | |
) | |
class TestComboBox(object): | |
def setup_method(self, method): | |
self.driver = Firefox() | |
self.driver.get("http://dhtmlx.com/docs/products/dhtmlxCombo/samples/02_actions/01_combo_actions.html") | |
WebDriverWait(self.driver, 5).until(lambda d: d.execute_script("return window.dhx_glbSelectAr[0].optionsArr.length > 0")) | |
def teardown_method(self, method): | |
self.driver.quit() | |
def test_options(self): | |
container = self.driver.find_element_by_css_selector("#combo_zone2 .dhx_combo_box") | |
combo_box = ComboBox(container) | |
assert(len(combo_box.options) == 10) | |
def test_select(self): | |
container = self.driver.find_element_by_css_selector("#combo_zone2 .dhx_combo_box") | |
combo_box = ComboBox(container) | |
combo_box.select("three") | |
selected = combo_box.selected | |
assert(selected == "three") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment