Skip to content

Instantly share code, notes, and snippets.

@yashaka
Last active September 10, 2015 07:52
Show Gist options
  • Save yashaka/2ec7a88bc0b44fa84728 to your computer and use it in GitHub Desktop.
Save yashaka/2ec7a88bc0b44fa84728 to your computer and use it in GitHub Desktop.
Notes from demo "Selenide in less than 15 minutes (in Python)" - version based on Selenium explicit waiters and expected conditions demoed in Minsk on 01.08.2015 ]
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException, StaleElementReferenceException
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.wait import WebDriverWait
browser = webdriver.Firefox()
class WaitingElement(object):
def __init__(self, css_selector):
self._locator = css_selector
def _finder(self):
return browser.find_element_by_css_selector(self._locator)
def __getattr__(self, item):
self.assure(visible)
return getattr(self._finder(), item)
def assure(self, condition, *args):
WebDriverWait(browser, 4).until(condition(self._finder, *args))
return self
def s(css_selector):
return WaitingElement(css_selector)
class text(object):
def __init__(self, finder, text_):
self.finder = finder
self.text = text_
def __call__(self, driver):
try:
element_text = self.finder().text
return self.text in element_text
except StaleElementReferenceException:
return False
class visible(object):
def __init__(self, finder):
self.finder = finder
def __call__(self, driver):
try:
return self.finder().is_displayed()
except StaleElementReferenceException:
return False
browser.get("http://google.com/ncr")
s("[name='q']").send_keys("selenium", Keys.ENTER)
s(".srg .g:nth-child(1)").assure(text, "Selenium automates browsers") # explicitly waiting of Selenide style (via assure instead of shouldBe)
browser.get("http://google.com/ncr")
s("[name='q']").send_keys("selenium", Keys.ENTER)
s(".srg .g:nth-child(1)").find_element_by_css_selector(".r a").click() # implicitly waiting for visibility of element
browser.quit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment