Selenium PageObject: Bok-Choy
Tim Babych
Functional tests for Web
means
Browser Automation
Browser Automantion
Means
Selenium
● Checks system behavior from user's perspective
● Emulates user's actions with real browsers– Firefox– Chrome
● And not so real– Phantom JS (headless)
browser = webdriver.Firefox()
browser.get('https://github.com/')
assert 'GitHub' in browser.title
from selenium.webdriver.common.keys import Keys
elem = browser.find_element_by_css_selector("input[name='q']")
elem.send_keys('splinter' + Keys.RETURN)
assert "splinter" in browser.title
browser.close()
Repetitive
Automate more
Splinter
browser = Browser()
browser.visit('https://github.com/')
assert 'GitHub' in browser.title
from selenium.webdriver.common.keys import Keys
browser.fill_form({'q': 'splinter'+Keys.RETURN})
assert "splinter" in browser.title
browser.quit()
check() for checkboxes
choose() for selects
click_link_by_href() and even by .._partial_href for links
CookieManager
mouse_over and mouse_out
is_element_present_by... (value, tag, name, id)
Helpers
we need to get high
Bok-Choy:the PageObject
from bok_choy.page_object import PageObject
class GitHubSearchPage(PageObject):
url = 'http://www.github.com/search'
def is_browser_on_page(self):
return 'code search' in self.browser.title.lower()
def search_for_term(self, text):
"""
Fill the text into the input field and submit the form
"""
self.q(
css='#search_form > input[type="text"]'
).fill(text) self.q(css='button.button').click()
def search_for(self, text):
"""
Fill the text into the input field and submit the form
"""
…. fill, click....
GitHubSearchResultsPage(
self.browser
).wait_for_page()
● Hide page implementation details– css rules for finding elements– multi-step interactions to get to certain point
● Inherit page classes from one another– share common methods and details
● Decorate tests– @wait_for_js (modules)– @js_defines (vars)
Element Query Sequence
● map● filter● transform (apply func)● replace (values)
• execute
Browser Query Sequence
● attrs● text● selected● visible● fill
Promise
Other Goodies
● Browsermob-proxy:– HAR files
● Screenshots of the exceptions● N retries for selenium queries
Github: https://github.com/edx/bok-choy
Docs:
http://bok-choy.readthedocs.org
@tymofiy
Thanks!