This repository has been archived by the owner on Nov 3, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
base.py
executable file
·137 lines (107 loc) · 5.33 KB
/
base.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
# 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/.
import time
from marionette.by import By
from marionette.errors import NoSuchElementException
from marionette.errors import StaleElementException
from marionette.wait import Wait
from gaiatest import GaiaApps
from gaiatest import Accessibility
class Base(object):
def __init__(self, marionette):
self.marionette = marionette
self.apps = GaiaApps(self.marionette)
self.accessibility = Accessibility(self.marionette)
self.frame = None
self.manifest_url = hasattr(self, 'manifest_url') and self.manifest_url or None
self.entry_point = hasattr(self, 'entry_point') and self.entry_point or None
def launch(self, launch_timeout=None):
self.app = self.apps.launch(self.name, self.manifest_url, self.entry_point, launch_timeout=launch_timeout)
def wait_for_element_present(self, by, locator, timeout=None):
return Wait(self.marionette, timeout, ignored_exceptions=NoSuchElementException).until(
lambda m: m.find_element(by, locator))
def wait_for_element_not_present(self, by, locator, timeout=None):
self.marionette.set_search_timeout(0)
try:
return Wait(self.marionette, timeout).until(
lambda m: not m.find_element(by, locator))
except NoSuchElementException:
pass
self.marionette.set_search_timeout(self.marionette.timeout or 10000)
def wait_for_element_displayed(self, by, locator, timeout=None):
Wait(self.marionette, timeout, ignored_exceptions=[NoSuchElementException, StaleElementException]).until(
lambda m: m.find_element(by, locator).is_displayed())
def wait_for_element_not_displayed(self, by, locator, timeout=None):
self.marionette.set_search_timeout(0)
try:
Wait(self.marionette, timeout, ignored_exceptions=StaleElementException).until(
lambda m: not m.find_element(by, locator).is_displayed())
except NoSuchElementException:
pass
self.marionette.set_search_timeout(self.marionette.timeout or 10000)
def wait_for_condition(self, method, timeout=None, message=None):
Wait(self.marionette, timeout).until(method, message=message)
def is_element_present(self, by, locator):
self.marionette.set_search_timeout(0)
try:
self.marionette.find_element(by, locator)
return True
except NoSuchElementException:
return False
finally:
self.marionette.set_search_timeout(self.marionette.timeout or 10000)
def is_element_displayed(self, by, locator):
self.marionette.set_search_timeout(0)
try:
return self.marionette.find_element(by, locator).is_displayed()
except NoSuchElementException:
return False
finally:
self.marionette.set_search_timeout(self.marionette.timeout or 10000)
def find_select_item(self, match_string):
_list_item_locator = (
By.XPATH, "//section[contains(@class,'value-selector-container')]/descendant::li[descendant::span[.='%s']]" %
match_string)
# have to go back to top level to get the B2G select box wrapper
self.marionette.switch_to_frame()
# TODO we should find something suitable to wait for, but this goes too
# fast against desktop builds causing intermittent failures
time.sleep(0.2)
li = self.wait_for_element_present(*_list_item_locator)
# TODO Remove scrollintoView upon resolution of bug 877651
self.marionette.execute_script(
'arguments[0].scrollIntoView(false);', [li])
return li
def wait_for_select_closed(self, by, locator):
self.wait_for_element_not_displayed(by, locator)
# TODO we should find something suitable to wait for, but this goes too
# fast against desktop builds causing intermittent failures
time.sleep(0.2)
# now back to app
self.apps.switch_to_displayed_app()
def select(self, match_string):
# cheeky Select wrapper until Marionette has its own
# due to the way B2G wraps the app's select box we match on text
_close_button_locator = (By.CSS_SELECTOR, 'button.value-option-confirm')
li = self.find_select_item(match_string)
li.tap()
# Tap close and wait for it to hide
self.marionette.find_element(*_close_button_locator).tap()
self.wait_for_select_closed(*_close_button_locator)
def a11y_select(self, match_string):
# Accessibility specific select method
_close_button_locator = (By.CSS_SELECTOR, 'button.value-option-confirm')
li = self.find_select_item(match_string)
self.accessibility.click(li)
# A11y click close and wait for it to hide
self.accessibility.click(self.marionette.find_element(*_close_button_locator))
self.wait_for_select_closed(*_close_button_locator)
@property
def keyboard(self):
from gaiatest.apps.keyboard.app import Keyboard
return Keyboard(self.marionette)
class PageRegion(Base):
def __init__(self, marionette, element):
self.root_element = element
Base.__init__(self, marionette)