forked from mozilla/qmo-tests
-
Notifications
You must be signed in to change notification settings - Fork 0
/
page.py
75 lines (54 loc) · 2.39 KB
/
page.py
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
63
64
65
66
67
68
69
70
71
72
73
74
75
#!/usr/bin/env python
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
from unittestzero import Assert
from selenium.webdriver.support.ui import WebDriverWait
from selenium.common.exceptions import NoSuchElementException, ElementNotVisibleException
class Page(object):
def __init__(self, testsetup, **kwargs):
self.testsetup = testsetup
self.base_url = testsetup.base_url
self.selenium = testsetup.selenium
self.timeout = testsetup.timeout
self._selenium_root = hasattr(self, '_root_element') and self._root_element or self.selenium
for key, value in kwargs.items():
setattr(self, key, value)
@property
def is_the_current_page(self):
if self._page_title:
WebDriverWait(self.selenium, self.timeout).until(lambda s: s.title)
Assert.contains(self._page_title, self.selenium.title,
'Expected page title does not match actual page title.')
return True
def is_element_visible(self, *locator):
self.selenium.implicitly_wait(0)
try:
return self._selenium_root.find_element(*locator).is_displayed()
except (NoSuchElementException, ElementNotVisibleException):
return False
finally:
self.selenium.implicitly_wait(self.testsetup.default_implicit_wait)
def get_url_current_page(self):
return self.selenium.current_url
def get_relative_path(self, url):
self.selenium.get(u'%s%s' % (self.base_url, url))
def find_element(self, *locator):
return self._selenium_root.find_element(*locator)
def find_elements(self, *locator):
return self._selenium_root.find_elements(*locator)
def type_in_element(self, locator, text):
"""
Type a string into an element.
This method clears the element first then types the string via send_keys.
Arguments:
locator -- a locator for the element
text -- the string to type via send_keys
"""
text_fld = self._selenium_root.find_element(*locator)
text_fld.clear()
text_fld.send_keys(text)
class PageRegion(Page):
def __init__(self, testsetup, element):
self._root_element = element
Page.__init__(self, testsetup)