Created
March 13, 2011 14:57
selenium webdriver
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
class NullElement: | |
def __getattr__(self, name): | |
raise Exception(elementNotFound) | |
def is_displayed(self): | |
return 0 | |
class Browser(object): | |
def __init__(self, browser = 'iexplore'): | |
if browser == 'chrome': | |
from selenium.chrome.webdriver import WebDriver | |
elif browser == 'firefox': | |
from selenium.firefox.webdriver import WebDriver | |
else: | |
from selenium.ie.webdriver import WebDriver | |
self.wd = WebDriver() | |
def __getattr__(self, name): | |
return getattr(self.wd, name) | |
def get_port(self): | |
return variables.get_port() | |
def maximize(self): | |
script = "window.moveTo(0, 0);window.resizeTo(window.screen.availWidth, window.screen.availHeight);" | |
self.execute_script(script) | |
def go(self, url): | |
self.get(url) | |
self.maximize() | |
def get_by_id(self, locator): | |
type, separator, value = locator.partition('=') | |
if type.lower() == 'id': | |
return self.find_element_by_id(value) | |
def get_by_xpath(self, locator): | |
type, separator, value = locator.partition('=') | |
if type.lower() == 'xpath': | |
return self.find_element_by_xpath(value) | |
def find_element(self, locator): | |
element = NullElement() | |
try: | |
element = self.get_by_id(locator) or self.get_by_xpath(locator) or log.Except(wrongLocator) | |
except Exception, e: | |
log.Except(e) | |
return element | |
def toggle(self, locator): | |
''' | |
Just toggle a checkbox by given locator | |
Returns: true or false, that depends on the given checkbox state | |
Actually it should(or might) contain only two string: | |
checkbox = self.find_element(locator) | |
return checkbox.toggle() | |
but unfortunately I can't get such approach worked in a tests suite. | |
I mean that a single test works fine, I never got issues, but if a suite is running | |
I constantly have problems that checkbox is not toggled (have no idea why, very hard to debug in suite) | |
So I use javascript to toggle a checkbox | |
''' | |
script = ''' | |
var elem = $('input[%(locator)s]') | |
if (elem.length === 0) { | |
throw('%(exception)s') | |
} | |
var state = elem.attr('checked'); | |
elem.attr('checked', !state); | |
return elem.attr('checked'); | |
''' % { 'locator': locator, 'exception': elementNotFound } | |
return self.execute_script(script) | |
def click(self, locator): | |
elem = self.find_element(locator) | |
elem.click() | |
def wait_for(self, condition, time_frame): | |
wait_time = int(time_frame) | |
def timeout(wait_time): | |
return not wait_time | |
while not condition(): | |
time.sleep(1) | |
wait_time = wait_time - 1 | |
if timeout(wait_time): | |
raise Exception('Timed out after %s sec' % time_frame) | |
def wait_for_element_present(self, locator, timeout=defaultTimeout): | |
self.wait_for(lambda: self.is_element_present(locator), timeout) | |
def type(self, locator, text): | |
elem = self.find_element(locator) | |
elem.send_keys(text) | |
def wait_for_element_is_displayed(self, locator, timeout=defaultTimeout): | |
self.wait_for(lambda: self.is_element_displayed(locator), timeout) | |
def wait_for_element_is_not_displayed(self, locator, timeout=defaultTimeout): | |
self.wait_for(lambda: not self.is_element_displayed(locator), timeout) | |
def is_element_present(self, locator): | |
element = self.find_element(locator) | |
return not isinstance(element, NullElement) | |
def eval(self, script, *args): | |
return self.execute_script(script, *args) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment