Skip to content

Instantly share code, notes, and snippets.

@adamgoucher
Created August 15, 2012 14:23
Show Gist options
  • Save adamgoucher/3360560 to your computer and use it in GitHub Desktop.
Save adamgoucher/3360560 to your computer and use it in GitHub Desktop.
Descriptors and @Property
class Select(Element, WebDriverSelect):
"""
This works for getting the current value of a select element, and for setting it
-- but fails for /also/ getting the 'options' that are available. Thinking that I
need to use my_select.selection with @property and @selection.setter and
my_select.options with @property
Now, what I would love is to still use __set__ and __get__ to interact with the
the select, but also be able to get the options...
"""
def __set__(self, obj, val):
s = WebDriverSelect(obj.driver.find_element_by_locator(self.locator))
method = val[:val.find("=")]
value = val[val.find("=") + 1:]
if method == "value":
s.select_by_value(value)
elif method == "index":
s.select_by_index(value)
elif method == "text":
s.select_by_visible_text(value)
else:
raise saunter.exceptions.InvalidLocatorString(val)
def __get__(self, obj, cls=None):
try:
s = WebDriverSelect(obj.driver.find_element_by_locator(self.locator))
e = s.first_selected_option
return str(e.text)
except AttributeError as e:
if str(e) == "'SeleniumWrapper' object has no attribute 'connection'":
pass
else:
raise e
class Text(Element):
"""
Text boxen are only the get/set
"""
def __set__(self, obj, val):
e = obj.driver.find_element_by_locator(self.locator)
e.send_keys(val)
def __get__(self, obj, cls=None):
try:
e = obj.driver.find_element_by_locator(self.locator)
return str(e.text)
except AttributeError as e:
if str(e) == "'SeleniumWrapper' object has no attribute 'connection'":
pass
else:
raise e
except ElementNotFound as e:
msg = "Element %s was not found. It is used in the %s page object in the %s module." % (self.locator, obj.__class__.__name__, self.__module__)
raise ElementNotFound(msg)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment