-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathselenium_utils.py
More file actions
62 lines (52 loc) · 2.41 KB
/
selenium_utils.py
File metadata and controls
62 lines (52 loc) · 2.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
from django.contrib.staticfiles.testing import StaticLiveServerTestCase
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.support.wait import WebDriverWait
from selenium import webdriver
# compat
import django
if django.VERSION[:2] < (1, 10):
from django.core.urlresolvers import reverse
else:
from django.urls import reverse
class SeleniumTestCase(StaticLiveServerTestCase):
"""
A base test case for Selenium, providing hepler methods for generating
clients and logging in profiles.
"""
def open(self, url):
self.webdriver.get("%s%s" % (self.live_server_url, url))
def login(self):
self.open(reverse('admin:index'))
# SeleniFIXTURESum knows it has to wait for page loads (except for AJAX requests)
# so we don't need to do anything about that, and can just
# call find_css. Since we can chain methods, we can
# call the built-in send_keys method right away to change the
# value of the field
self.webdriver.find_css('#id_username').send_keys(self.username)
# for the password, we can now just call find_css since we know the page
# has been rendered
self.webdriver.find_css("#id_password").send_keys(self.password)
# You're not limited to CSS selectors only, check
# http://seleniumhq.org/docs/03_webdriver.html for
# a more compreehensive documentation.
self.webdriver.find_element_by_xpath('//input[@type="submit"]').click()
self.webdriver.wait_for_css("body.dashboard")
class CustomWebDriver(webdriver.Firefox):
"""Our own WebDriver with some helpers added"""
def find_css(self, css_selector):
"""Shortcut to find elements by CSS. Returns either a list or singleton"""
elems = self.find_elements_by_css_selector(css_selector)
found = len(elems)
if found == 1:
return elems[0]
elif not elems:
raise NoSuchElementException(css_selector)
return elems
def wait_for_css(self, css_selector, timeout=4):
""" Shortcut for WebDriverWait"""
return WebDriverWait(self, timeout).until(lambda driver: driver.find_css(css_selector))
def wait_for_iframe(self, iframe_selector, timeout=4):
""" Shortcut for WebDriverWait"""
return WebDriverWait(
self, timeout
).until(lambda driver: driver.frame_to_be_available_and_switch_to_it())