Repository navigation
Expand file tree
/
Copy pathaccessibility.py
More file actions
executable file
·105 lines (88 loc) · 4.75 KB
/
Copy pathaccessibility.py
File metadata and controls
executable file
·105 lines (88 loc) · 4.75 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
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
# -*- coding: utf-8 -*-
from common_lib import CommonLib as common
import time
import logging
class Accessibility(object):
FIND_ELEMENT_BY_XPATH_JS_TEMPLATE = "var %s = document.evaluate(\"%s\", document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;"
FOCUS_ELEMENT_JS = "%s.focus();"
def __init__(self, spaces_driver, platform_lib):
logging.info("Initialising Accessibility layer")
self.spaces_driver = spaces_driver
self.sys = platform_lib
def check_tab_order(self, tab_order_elem_list, start_elem=None, click_to_start_elem=False, ignore_missing_elem=False):
"""
Checks TAB order for the opened page/view
:param click_to_start_elem:
:param tab_order_elem_list: ordered list of the elements which represent TAB order
:param start_elem: if specified, this element will be focused at the beginning of the check
:param ignore_missing_elem: when specified, check will not fail if element from TAB order is not rendered on the view
:return:
"""
logging.info("Going to check TAB order for page %s" % self.spaces_driver.driver.current_url)
self.sys.lib.take_fullscreen_screenshot("tab_order_check_start")
if start_elem:
logging.info(" - we need to focus element first: %s" % start_elem)
if click_to_start_elem:
logging.info(" - focusing start element by click.")
elem = self.spaces_driver.driver.find_element_by_xpath(start_elem)
elem.click()
else:
self.focus_element_by_xpath(start_elem)
active_element = self.spaces_driver.driver.switch_to_active_element()
logging.info("Checking %d items in TAB order" % len(tab_order_elem_list))
i = 0
for item in tab_order_elem_list:
i += 1
logging.info(" * %d: Element %s" % (i, item))
try:
elem = self.spaces_driver.driver.find_element_by_xpath(item)
if not elem.is_displayed():
raise Exception("Element 'item' (xpath) is found but not visible")
except Exception, e:
logging.info("Specified element not found on the page: %s (%s)" % (item, e), "warning")
if ignore_missing_elem:
logging.info(" - Skipping element.")
continue
else:
raise Exception("Specified element not found on the page: %s" % item)
if not active_element:
raise Exception("Active element not detected")
logging.info(" - hitting TAB")
active_element.send_keys(self.spaces_driver.keys.TAB)
time.sleep(0.5)
logging.info(" - checking if element is focused")
active_element = self.spaces_driver.driver.switch_to_active_element()
self.sys.lib.take_fullscreen_screenshot("%d-tab_order_check" % i)
if elem == active_element:
logging.info(" - PASS")
else:
logging.info(" - FAIL: Wrong element focused (element with class='%s'). Should be element '%s'" % (active_element.get_attribute("class"), tab_order_elem_list[i - 1]))
self.sys.lib.take_fullscreen_screenshot("tab_order_fail")
raise Exception("TAB order not as desired")
def focus_element_by_xpath(self, elem):
"""
Focuses element specified by xpath using JS
:param elem: xpath of the element
:return:
"""
logging.info("Going to focus element: %s" % elem)
script_js = "%s %s" % (self.FIND_ELEMENT_BY_XPATH_JS_TEMPLATE % ("elemToFocus", elem), self.FOCUS_ELEMENT_JS % "elemToFocus")
self.spaces_driver.driver.execute_script(script_js)
time.sleep(0.5)
logging.info("Done.")
def send_keys_and_get_focused_element(self, elem, *keys):
"""
send key(s) to an element
:param elem: target element
:return: element focused after the key (or key combination) was sent
"""
logging.info("send_keys_and_get_focused_element CSS class = '%s'" % (elem.get_attribute("class")))
elem.send_keys(*keys)
time.sleep(0.5)
try:
logging.info("self.spaces_driver.driver.switch_to.active_element")
return self.spaces_driver.driver.switch_to.active_element
except Exception, e:
logging.error(e)
logging.info("failed on 'return self.spaces_driver.driver.active_element()' fallback to deprecated 'return self.spaces_driver.driver.switch_to_active_element()'")
return self.spaces_driver.driver.switch_to_active_element()