-
Notifications
You must be signed in to change notification settings - Fork 3
/
sunbro.py
170 lines (124 loc) · 4.84 KB
/
sunbro.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
"""Handy module to create page objects.
Sunbro is a module that allows the creation of page objects in a declarative
way, so when wiriting tests you only have to worry about wiriting the actual
test, instead of tricky selectors.
Usage:
Page object classes must inherit from sunbro.Page class, and selectors
should be sublasses of Find.
class MyAwesomePage(sunbro.Page):
title = sunbro.FindByTag('h1')
tricky_css = sunbro.FindByCSS('div.tricky p')
link = sunbro.FindByID('linky')
page = MyAwesomePage.link.click()
"""
from selenium.webdriver.common.by import By
from selenium.webdriver.remote.webelement import WebElement
from selenium.webdriver.remote.command import Command
from selenium.common.exceptions import WebDriverException
def click(self):
try:
self._execute(Command.CLICK_ELEMENT)
except WebDriverException as e:
self.parent.execute_script("window.scrollTo({0}, {1})".format(self.location['x'], self.location['y']))
self.parent.execute_script("arguments[0].click()", self)
WebElement.click = click
class Find(object):
def __init__(self, selector, within=None):
self._selector = selector
self._within = within
# TODO find metods should only receive the root element and work it's way
# through the hierarchy
class FindElements(Find):
"""Abstraction for selenium find_elements"""
def __init__(self, by, selector):
self._by = by
super(FindElements, self).__init__(selector)
def find(self, element):
"""Performs the actual search.
`element` is a driver or WebElement"""
return element.find_elements(self._by, self._selector)
class FindElement(Find):
"""Abstraction for selenium find_element"""
def __init__(self, by, selector):
self._by = by
super(FindElement, self).__init__(selector)
def find(self, element):
"""Performs the actual search.
`element` is a driver or WebElement"""
return element.find_element(self._by, self._selector)
class MetaFind(type):
"""Selenium selector metaclass"""
def __new__(cls, classname, bases, attrs):
def find(self, root):
return root.find_element(self._by, self._selector)
attrs['find'] = find
return type.__new__(cls, classname, (Find,) + bases, attrs)
class MetaFindAll(type):
"""Selenium selector metaclass"""
def __new__(cls, classname, bases, attrs):
def find(self, root_element):
return root_element.find_elements(self._by, self._selector)
attrs['find'] = find
return type.__new__(cls, classname, (Find,) + bases, attrs)
By.IOS_UIAUTOMATION = '-ios uiautomation'
By.ANDROID_UIAUTOMATOR = '-android uiautomator'
By.ACCESSIBILITY_ID = 'accessibility id'
selectors = {
'Class': By.CLASS_NAME,
'CSS': By.CSS_SELECTOR,
'ID': By.ID,
'LinkText': By.LINK_TEXT,
'Name': By.NAME,
'PartialLinkText': By.PARTIAL_LINK_TEXT,
'Tag': By.TAG_NAME,
'XPath': By.XPATH,
'IosUiAutomator' : By.IOS_UIAUTOMATION,
'AndroidUiAutomator' : By.ANDROID_UIAUTOMATOR,
'AccessibilityId' : By.ACCESSIBILITY_ID,
}
for name, by in selectors.items():
classname = 'FindBy' + name
selclass = MetaFind(classname, (), {'_by': by})
vars()[classname] = selclass
classname = 'FindAllBy' + name
selclass = MetaFindAll(classname, (), {'_by': by})
vars()[classname] = selclass
def decorated_find(finder):
def getter(self):
driver = self._driver
if finder._within:
root = getattr(self, finder._within)
else:
root = driver
return finder.find(root)
return getter
class BasePage(object):
"""Base page for page objects, you should not extend from this,
use Page instead"""
def __init__(self, driver):
self._driver = driver
def fill_fields(self, **kwargs):
"""Fills the fields referenced by kwargs keys and fill them with
the value"""
for name, value in kwargs.items():
field = getattr(self, name)
field.send_keys(value)
def selector(self, fieldname):
"""Gets a selector for the given page element as a tuple
(by, selector)"""
finder = self._finders[fieldname]
return (finder._by, finder._selector)
class PageMetaclass(type):
"""Metaclass that search for selenium selector objects on the
class and makes them usable on the Page object
"""
def __new__(cls, classname, bases, attrs):
finders = {}
for k, v in [i for i in attrs.items() if isinstance(i[1], Find)]:
if isinstance(v, Find):
attrs[k] = property(decorated_find(v))
finders[k] = v
attrs['_finders'] = finders
return type.__new__(cls, classname, bases, attrs)
Page = PageMetaclass('Page', (BasePage, ), {})
Page.__doc__ = "Base class for page objects"