-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathautocomplete_light.py
141 lines (102 loc) · 3.94 KB
/
autocomplete_light.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
"""
Test script helpers in Python.
Not necessary to run autocomplete-light.js
"""
from selenium.webdriver.common.by import By
def retry(cb, *args, expected=None, **kwargs):
i = 15
while True:
try:
result = cb(*args, **kwargs)
except:
if not i:
raise
else:
if expected is not None and result == expected:
return result
elif expected is None and result:
return result
i -= 1
return wrapper
class AutocompleteLight:
def __init__(self, browser, autocomplete_light_id):
self.id = autocomplete_light_id
self.browser = browser
def component(self):
return self.browser.find_by_id(self.id)
def attr(self, name):
return self.browser.evaluate_script(
f'document.getElementById("{self.id}").' + name
)
def input(self):
return self.attr('input')
def box(self):
return self.attr('box')
def hilight(self):
return self.browser.evaluate_script(
f'document.getElementById("{self.id}").box.querySelector("[data-value].hilight")'
)
def choices(self):
return self.browser.evaluate_script(
f'document.getElementById("{self.id}").box.querySelectorAll("[data-value]")'
)
def focus(self):
return self.input().click()
def defocus(self):
return self.browser.find_by_css('body').click()
def type(self, val):
return self.browser.find_by_css(f'#{self.id} input')[0].type(val)
class AutocompleteSelect(AutocompleteLight):
def alight(self):
return self.attr('input')
def input(self):
return self.attr('input.input')
def box(self):
return self.attr('input.box')
def selected(self):
return self.browser.evaluate_script(
f'document.getElementById("{self.id}").deck.querySelectorAll("[data-value]")'
)
def selectedOptions(self):
return self.attr('select.selectedOptions')
def select(self):
return self.attr('select')
def value(self):
return self.select().get_property('value')
def choices(self):
return self.browser.evaluate_script(
f'document.getElementById("{self.id}").input.box.querySelectorAll("[data-value]")'
)
def unselect(self, index):
self.selected()[index].find_element(By.TAG_NAME, 'span').click()
def assert_selected(self, label, value):
# get selected choices from deck
selected = retry(self.selected)
# there should only be one selected choice
assert len(selected) == 1
# value of the select should be that of the selected choice
assert selected[0].get_attribute('data-value') == str(value)
assert self.value() == str(value)
# text of the selected choice in the deck should be right
assert selected[0].text.split('\n')[0] == label
# maxChoices reached: autocomplete should be hidden
assert self.input().get_property('hidden')
class AutocompleteSelectMultiple(AutocompleteSelect):
def assert_selected(self, *label_values):
# get selected choices from deck
selected = retry(self.selected)
# number of selected choices should match
assert len(selected) == len(label_values)
# number of selectedOptions should match
assert len(self.selectedOptions()) == len(label_values)
# value of the select should be that of the selected choice
for label_value, choice in zip(label_values, selected):
label, value = label_value
assert choice.get_attribute('data-value') == str(value)
assert choice.text.split('\n')[0] == label
# maxChoices of 0: autocomplete should not hide
assert not self.alight().get_property('hidden')
def get_input(browser, id):
return retry(browser.evaluate_script)(
f'document.getElementById("{id}").input',
)