-
Notifications
You must be signed in to change notification settings - Fork 337
/
__init__.py
1250 lines (1216 loc) · 42.8 KB
/
__init__.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from copy import copy
from helium._impl.match_type import PREFIX_IGNORE_CASE
from helium._impl.selenium_wrappers import WebElementWrapper, \
WebDriverWrapper, FrameIterator, FramesChangedWhileIterating
from helium._impl.util.dictionary import inverse
from helium._impl.util.system import is_windows, get_canonical_os_name
from helium._impl.util.xpath import lower, predicate, predicate_or
from inspect import getfullargspec, ismethod, isfunction
from selenium.common.exceptions import UnexpectedAlertPresentException, \
ElementNotVisibleException, MoveTargetOutOfBoundsException, \
WebDriverException, StaleElementReferenceException, \
NoAlertPresentException, NoSuchWindowException
from selenium.webdriver.common.by import By
from selenium.webdriver.firefox.service import Service as ServiceFirefox
from selenium.webdriver.remote.webelement import WebElement
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support.ui import Select
from selenium.webdriver import Chrome, ChromeOptions, Firefox, FirefoxOptions
from time import sleep, time
import atexit
import re
def might_spawn_window(f):
def f_decorated(self, *args, **kwargs):
driver = self.require_driver()
if driver.is_ie() and AlertImpl(driver).exists():
# Accessing .window_handles in IE when an alert is present raises an
# UnexpectedAlertPresentException. When DesiredCapability
# 'unexpectedAlertBehaviour' is not 'ignore' (the default is
# 'dismiss'), this leads to the alert being closed. Since we don't
# want to unintentionally close alert dialogs, we therefore do not
# access .window_handles in IE when an alert is present.
return f(self, *args, **kwargs)
window_handles_before = driver.window_handles[:]
result = f(self, *args, **kwargs)
# As above, don't access .window_handles in IE if an alert is present:
if not (driver.is_ie() and AlertImpl(driver).exists()):
if driver.is_firefox():
# Unlike Chrome, Firefox does not wait for new windows to open.
# Give it a little time to do so:
sleep(.2)
new_window_handles = [
h for h in driver.window_handles
if h not in window_handles_before
]
if new_window_handles:
driver.switch_to.window(new_window_handles[0])
return result
return f_decorated
def handle_unexpected_alert(f):
def f_decorated(*args, **kwargs):
try:
return f(*args, **kwargs)
except UnexpectedAlertPresentException:
raise UnexpectedAlertPresentException(
"This command is not supported when an alert is present. To "
"accept the alert (this usually corresponds to clicking 'OK') "
"use `Alert().accept()`. To dismiss the alert (ie. 'cancel' "
"it), use `Alert().dismiss()`. If the alert contains a text "
"field, you can use write(...) to set its value. "
"Eg.: `write('hi there!')`."
)
return f_decorated
class APIImpl:
DRIVER_REQUIRED_MESSAGE = \
"This operation requires a browser window. Please call one of " \
"the following functions first:\n" \
" * start_chrome()\n" \
" * start_firefox()\n" \
" * set_driver(...)"
def __init__(self):
self.driver = None
def start_firefox_impl(
self, url=None, headless=False, options=None, profile=None
):
firefox_driver = self._start_firefox_driver(headless, options, profile)
return self._start(firefox_driver, url)
def _start_firefox_driver(self, headless, options, profile):
firefox_options = FirefoxOptions() if options is None else options
if headless:
firefox_options.add_argument('--headless')
kwargs = {
'options': firefox_options
}
if profile:
# This is Deprecated in the driver so only do it (and trigger the
# warnings) if the user requests it
kwargs['firefox_profile'] = profile
service_log_path = 'nul' if is_windows() else '/dev/null'
service = ServiceFirefox(log_path=service_log_path)
result = Firefox(service=service, **kwargs)
return result
def start_chrome_impl(
self, url=None, headless=False, maximize=False, options=None
):
chrome_driver = self._start_chrome_driver(headless, maximize, options)
return self._start(chrome_driver, url)
def _start_chrome_driver(self, headless, maximize, options):
chrome_options = self._get_chrome_options(headless, maximize, options)
result = Chrome(options=chrome_options)
atexit.register(self._kill_service, result.service)
return result
def _get_chrome_options(self, headless, maximize, options):
result = ChromeOptions() if options is None else options
# Prevent Chrome's debug logs from appearing in our console window:
result.add_experimental_option('excludeSwitches', ['enable-logging'])
if headless:
result.add_argument('--headless')
elif maximize:
result.add_argument('--start-maximized')
return result
def _kill_service(self, service):
old = service.send_remote_shutdown_command
service.send_remote_shutdown_command = lambda: None
try:
service.stop()
finally:
service.send_remote_shutdown_command = old
def _start(self, browser, url=None):
self.set_driver_impl(browser)
if url is not None:
self.go_to_impl(url)
return self.get_driver_impl()
@might_spawn_window
@handle_unexpected_alert
def go_to_impl(self, url):
if '://' not in url:
url = 'http://' + url
self.require_driver().get(url)
def set_driver_impl(self, driver):
self.driver = WebDriverWrapper(driver)
def get_driver_impl(self):
if self.driver is not None:
return self.driver.unwrap()
@might_spawn_window
@handle_unexpected_alert
def write_impl(self, text, into=None):
if into is not None:
from helium import GUIElement
if isinstance(into, GUIElement):
into = into._impl
self._handle_alerts(
self._write_no_alert, self._write_with_alert, text, into=into
)
def _write_no_alert(self, text, into=None):
if into:
if isinstance(into, str):
into = TextFieldImpl(self.require_driver(), into)
def _write(elt):
if hasattr(elt, 'clear') and callable(elt.clear):
elt.clear()
elt.send_keys(text)
self._manipulate(into, _write)
else:
self.require_driver().switch_to.active_element.send_keys(text)
def _write_with_alert(self, text, into=None):
if into is None:
into = AlertImpl(self.require_driver())
if not isinstance(into, AlertImpl):
raise UnexpectedAlertPresentException(
"into=%r is not allowed when an alert is present." % into
)
into._write(text)
def _handle_alerts(self, no_alert, with_alert, *args, **kwargs):
driver = self.require_driver()
if not AlertImpl(driver).exists():
return no_alert(*args, **kwargs)
return with_alert(*args, **kwargs)
@might_spawn_window
@handle_unexpected_alert
def press_impl(self, key):
self.require_driver().switch_to.active_element.send_keys(key)
def click_impl(self, element):
self._perform_mouse_action(element, self._click)
def doubleclick_impl(self, element):
self._perform_mouse_action(element, self._doubleclick)
def hover_impl(self, element):
self._perform_mouse_action(element, self._hover)
def rightclick_impl(self, element):
self._perform_mouse_action(element, self._rightclick)
def press_mouse_on_impl(self, element):
self._perform_mouse_action(element, self._press_mouse_on)
def release_mouse_over_impl(self, element):
self._perform_mouse_action(element, self._release_mouse_over)
def _click(self, selenium_elt, offset):
self._move_to_element(selenium_elt, offset).click().perform()
def _doubleclick(self, selenium_elt, offset):
self._move_to_element(selenium_elt, offset).double_click().perform()
def _hover(self, selenium_elt, offset):
self._move_to_element(selenium_elt, offset).perform()
def _rightclick(self, selenium_elt, offset):
self._move_to_element(selenium_elt, offset).context_click().perform()
def _press_mouse_on(self, selenium_elt, offset):
self._move_to_element(selenium_elt, offset).click_and_hold().perform()
def _release_mouse_over(self, selenium_elt, offset):
self._move_to_element(selenium_elt, offset).release().perform()
def _move_to_element(self, element, offset):
result = self.require_driver().action()
if offset is not None:
result.move_to_element_with_offset(
element,
offset[0] - element.size['width'] / 2,
offset[1] - element.size['height'] / 2
)
else:
result.move_to_element(element)
return result
def drag_impl(self, element, to):
with DragHelper(self) as drag_helper:
self._perform_mouse_action(element, drag_helper.start_dragging)
self._perform_mouse_action(to, drag_helper.drop_on_target)
@might_spawn_window
@handle_unexpected_alert
def _perform_mouse_action(self, element, action):
element, offset = self._unwrap_clickable_element(element)
self._manipulate(element, lambda wew: action(wew.unwrap(), offset))
def _unwrap_clickable_element(self, elt):
from helium import HTMLElement, Point
offset = None
if isinstance(elt, str):
elt = ClickableText(self.require_driver(), elt)
elif isinstance(elt, HTMLElement):
elt = elt._impl
elif isinstance(elt, Point):
elt, offset = self._point_to_element_and_offset(elt)
return elt, offset
def _point_to_element_and_offset(self, point):
driver = self.require_driver()
element = WebElementWrapper(driver.execute_script(
'return document.elementFromPoint(%r, %r);' % (point.x, point.y)
))
offset = point - (element.location.left, element.location.top)
if offset == (0, 0) and driver.is_firefox():
# In some CSS settings (eg. test_point.html), the (0, 0) point of
# buttons in Firefox is not clickable! The reason for this is that
# Firefox styles buttons to not be perfect squares, but have an
# indent in the corners. This workaround makes `click(btn.top_left)`
# work even when this happens:
offset = (1, 1)
return element, offset
@handle_unexpected_alert
def find_all_impl(self, predicate):
return [
predicate.with_impl(bound_gui_elt_impl)
for bound_gui_elt_impl in predicate._impl.find_all()
]
def scroll_down_impl(self, num_pixels):
self._scroll_by(0, num_pixels)
def scroll_up_impl(self, num_pixels):
self._scroll_by(0, -num_pixels)
def scroll_right_impl(self, num_pixels):
self._scroll_by(num_pixels, 0)
def scroll_left_impl(self, num_pixels):
self._scroll_by(-num_pixels, 0)
@handle_unexpected_alert
def _scroll_by(self, dx_pixels, dy_pixels):
self.require_driver().execute_script(
'window.scrollBy(arguments[0], arguments[1]);', dx_pixels, dy_pixels
)
@might_spawn_window
@handle_unexpected_alert
def select_impl(self, combo_box, value):
from helium import ComboBox
if isinstance(combo_box, str):
combo_box = ComboBoxImpl(self.require_driver(), combo_box)
elif isinstance(combo_box, ComboBox):
combo_box = combo_box._impl
def _select(web_element):
if isinstance(web_element, WebElementWrapper):
web_element = web_element.unwrap()
Select(web_element).select_by_visible_text(value)
self._manipulate(combo_box, _select)
def _manipulate(self, gui_or_web_elt, action):
driver = self.require_driver()
if hasattr(gui_or_web_elt, 'perform') \
and callable(gui_or_web_elt.perform):
driver.last_manipulated_element = gui_or_web_elt.perform(action)
else:
if isinstance(gui_or_web_elt, WebElement):
gui_or_web_elt = WebElementWrapper(gui_or_web_elt)
action(gui_or_web_elt)
driver.last_manipulated_element = gui_or_web_elt
@handle_unexpected_alert
def drag_file_impl(self, file_path, to):
to, _ = self._unwrap_clickable_element(to)
drag_and_drop = DragAndDropFile(self.require_driver(), file_path)
drag_and_drop.begin()
try:
# Some web apps (Gmail in particular) only register for the 'drop'
# event when user has dragged the file over the document. We
# therefore simulate this dragging over the document first:
drag_and_drop.drag_over_document()
self._manipulate(to, lambda elt: drag_and_drop.drop_on(elt))
finally:
drag_and_drop.end()
@might_spawn_window
@handle_unexpected_alert
def attach_file_impl(self, file_path, to=None):
from helium import Point
driver = self.require_driver()
if to is None:
to = FileInput(driver)
elif isinstance(to, str):
to = FileInput(driver, to)
elif isinstance(to, Point):
to, _ = self._point_to_element_and_offset(to)
self._manipulate(to, lambda elt: elt.send_keys(file_path))
def refresh_impl(self):
self._handle_alerts(
self._refresh_no_alert, self._refresh_with_alert
)
def _refresh_no_alert(self):
self.require_driver().refresh()
def _refresh_with_alert(self):
AlertImpl(self.require_driver()).accept()
self._refresh_no_alert()
def wait_until_impl(self, condition_fn, timeout_secs=10, interval_secs=0.5):
if ismethod(condition_fn):
is_bound = condition_fn.__self__ is not None
args_spec = getfullargspec(condition_fn).args
unfilled_args = len(args_spec) - (1 if is_bound else 0)
else:
if not isfunction(condition_fn):
condition_fn = condition_fn.__call__
args_spec = getfullargspec(condition_fn).args
unfilled_args = len(args_spec)
condition = \
condition_fn if unfilled_args else lambda driver: condition_fn()
wait = WebDriverWait(
self.require_driver().unwrap(), timeout_secs,
poll_frequency=interval_secs
)
wait.until(condition)
@handle_unexpected_alert
def switch_to_impl(self, window):
driver = self.require_driver()
from helium import Window
if isinstance(window, str):
window = WindowImpl(driver, window)
elif isinstance(window, Window):
window = window._impl
driver.switch_to.window(window.handle)
def kill_browser_impl(self):
self.require_driver().quit()
self.driver = None
@handle_unexpected_alert
def highlight_impl(self, element):
driver = self.require_driver()
from helium import HTMLElement, Text
if isinstance(element, str):
element = Text(element)
if isinstance(element, HTMLElement):
element = element._impl
try:
element = element.first_occurrence
except AttributeError:
pass
previous_style = element.get_attribute("style")
if isinstance(element, WebElementWrapper):
element = element.unwrap()
driver.execute_script(
"arguments[0].setAttribute("
"'style', 'border: 2px solid red; font-weight: bold;'"
");", element
)
driver.execute_script(
"var target = arguments[0];"
"var previousStyle = arguments[1];"
"setTimeout("
"function() {"
"target.setAttribute('style', previousStyle);"
"}, 2000"
");", element, previous_style
)
def require_driver(self):
if not self.driver:
raise RuntimeError(self.DRIVER_REQUIRED_MESSAGE)
return self.driver
class DragHelper:
def __init__(self, api_impl):
self.api_impl = api_impl
self.is_html_5_drag = None
def __enter__(self):
self._execute_script(
"window.helium = {};"
"window.helium.dragHelper = {"
" createEvent: function(type) {"
" var event = document.createEvent('CustomEvent');"
" event.initCustomEvent(type, true, true, null);"
" event.dataTransfer = {"
" data: {},"
" setData: function(type, val) {"
" this.data[type] = val;"
" },"
" getData: function(type) {"
" return this.data[type];"
" }"
" };"
" return event;"
" }"
"};"
)
return self
def start_dragging(self, element, offset):
if self._attempt_html_5_drag(element):
self.is_html_5_drag = True
else:
self.api_impl._press_mouse_on(element, offset)
def drop_on_target(self, target, offset):
if self.is_html_5_drag:
self._complete_html_5_drag(target)
else:
self.api_impl._release_mouse_over(target, offset)
def _attempt_html_5_drag(self, element_to_drag):
return self._execute_script(
"var source = arguments[0];"
"function getDraggableParent(element) {"
" var previousParent = null;"
" while (element != null && element != previousParent) {"
" previousParent = element;"
" if ('draggable' in element) {"
" var draggable = element.draggable;"
" if (draggable === true)"
" return element;"
" if (typeof draggable == 'string' "
" || draggable instanceof String)"
" if (draggable.toLowerCase() == 'true')"
" return element;"
" }"
" element = element.parentNode;"
" }"
" return null;"
"}"
"var draggableParent = getDraggableParent(source);"
"if (draggableParent == null)"
" return false;"
"window.helium.dragHelper.draggedElement = draggableParent;"
"var dragStart = window.helium.dragHelper.createEvent('dragstart');"
"source.dispatchEvent(dragStart);"
"window.helium.dragHelper.dataTransfer = dragStart.dataTransfer;"
"return true;",
element_to_drag
)
def _complete_html_5_drag(self, on):
self._execute_script(
"var target = arguments[0];"
"var drop = window.helium.dragHelper.createEvent('drop');"
"drop.dataTransfer = window.helium.dragHelper.dataTransfer;"
"target.dispatchEvent(drop);"
"var dragEnd = window.helium.dragHelper.createEvent('dragend');"
"dragEnd.dataTransfer = window.helium.dragHelper.dataTransfer;"
"window.helium.dragHelper.draggedElement.dispatchEvent(dragEnd);",
on
)
def __exit__(self, *_):
self._execute_script("delete window.helium;")
def _execute_script(self, script, *args):
return self.api_impl.require_driver().execute_script(script, *args)
class DragAndDropFile:
def __init__(self, driver, file_path):
self.driver = driver
self.file_path = file_path
self.file_input_element = None
self.dragover_event = None
def begin(self):
self._create_file_input_element()
try:
self.file_input_element.send_keys(self.file_path)
except:
self.end()
raise
def _create_file_input_element(self):
# The input needs to be visible to Selenium to allow sending keys to it
# in Firefox and IE.
# According to http://stackoverflow.com/questions/6101461/
# Selenium criteria whether an element is visible or not are the
# following:
# - visibility != hidden
# - display != none (is also checked against every parent element)
# - opacity != 0
# - height and width are both > 0
# - for an input, the attribute type != hidden
# So let's make sure its all good!
self.file_input_element = self.driver.execute_script(
"var input = document.createElement('input');"
"input.type = 'file';"
"input.style.display = 'block';"
"input.style.opacity = '1';"
"input.style.visibility = 'visible';"
"input.style.height = '1px';"
"input.style.width = '1px';"
"if (document.body.childElementCount > 0) { "
" document.body.insertBefore(input, document.body.childNodes[0]);"
"} else { "
" document.body.appendChild(input);"
"}"
"return input;"
)
def drag_over_document(self):
# According to the HTML5 spec, we need to dispatch the dragenter event
# once, and then the dragover event continuously, every 350+-200ms:
# http://www.w3.org/html/wg/drafts/html/master/editing.html#current-drag
# -operation
# Especially IE implements this spec very tightly, and considers the
# dragging to be over if no dragover event occurs for more than ~1sec.
# We thus need to ensure that we keep dispatching the dragover event.
# This line used to read `_dispatch_event(..., to='document')`. However,
# this doesn't work when adding a photo to a tweet on Twitter.
# Dispatching the event to document.body fixes this, and also works for
# Gmail:
self._dispatch_event('dragenter', to='document.body')
self.dragover_event = self._prepare_continuous_event(
'dragover', 'document', interval_msecs=300
)
self.dragover_event.start()
def _dispatch_event(self, event_name, to):
script, args = self._prepare_dispatch_event(event_name, to)
self.driver.execute_script(script, *args)
def _prepare_continuous_event(self, event_name, to, interval_msecs):
script, args = self._prepare_dispatch_event(event_name, to)
return JavaScriptInterval(self.driver, script, args, interval_msecs)
def _prepare_dispatch_event(self, event_name, to):
script = \
"var files = arguments[0].files;" \
"var items = [];" \
"var types = [];" \
"for (var i = 0; i < files.length; i++) {" \
" items[i] = {kind: 'file', type: files[i].type};" \
" types[i] = 'Files';" \
"}" \
"var event = document.createEvent('CustomEvent');" \
"event.initCustomEvent(arguments[1], true, true, 0);" \
"event.dataTransfer = {" \
" files: files," \
" items: items," \
" types: types" \
"};" \
"arguments[2].dispatchEvent(event);"
if isinstance(to, str):
script = script.replace('arguments[2]', to)
args = self.file_input_element, event_name,
else:
args = self.file_input_element, event_name, to.unwrap()
return script, args
def drop_on(self, target):
self.dragover_event.stop()
self._dispatch_event('drop', to=target)
def end(self):
if self.file_input_element is not None:
self.driver.execute_script(
"arguments[0].parentNode.removeChild(arguments[0]);",
self.file_input_element
)
self.file_input_element = None
class JavaScriptInterval:
def __init__(self, driver, script, args, interval_msecs):
self.driver = driver
self.script = script
self.args = args
self.interval_msecs = interval_msecs
self._interval_id = None
def start(self):
setinterval_script = (
"var originalArguments = arguments;"
"return setInterval(function() {"
" arguments = originalArguments;"
" %s"
"}, %d);"
) % (self.script, self.interval_msecs)
self._interval_id = \
self.driver.execute_script(setinterval_script, *self.args)
def stop(self):
self.driver.execute_script(
"clearInterval(arguments[0]);", self._interval_id
)
self._interval_id = None
class GUIElementImpl:
def __init__(self, driver):
self._bound_occurrence = None
self._driver = driver
def find_all(self):
if self._is_bound():
yield self
else:
for occurrence in self.find_all_occurrences():
yield self.bound_to_occurrence(occurrence)
def _is_bound(self):
return self._bound_occurrence is not None
def find_all_occurrences(self):
raise NotImplementedError()
def bound_to_occurrence(self, occurrence):
result = copy(self)
result._bound_occurrence = occurrence
return result
def exists(self):
try:
next(self.find_all())
except StopIteration:
return False
else:
return True
@property
def first_occurrence(self):
if not self._is_bound():
self._bind_to_first_occurrence()
return self._bound_occurrence
def _bind_to_first_occurrence(self):
self.perform(lambda _: None)
# _perform_no_wait(...) below now sets _bound_occurrence.
def perform(self, action):
from helium import Config
end_time = time() + Config.implicit_wait_secs
# Try to perform `action` at least once:
result = self._perform_no_wait(action)
while result is None and time() < end_time:
result = self._perform_no_wait(action)
if result is not None:
return result
raise LookupError()
def _perform_no_wait(self, action):
for bound_gui_elt_impl in self.find_all():
occurrence = bound_gui_elt_impl.first_occurrence
try:
action(occurrence)
except Exception as e:
if self.should_ignore_exception(e):
continue
else:
raise
else:
self._bound_occurrence = occurrence
return occurrence
def should_ignore_exception(self, exception):
if isinstance(exception, ElementNotVisibleException):
return True
if isinstance(exception, MoveTargetOutOfBoundsException):
return True
if isinstance(exception, StaleElementReferenceException):
return True
if isinstance(exception, WebDriverException):
msg = exception.msg
if 'is not clickable at point' in msg \
and 'Other element would receive the click' in msg:
# This can happen when the element has moved.
return True
return False
class HTMLElementImpl(GUIElementImpl):
def __init__(
self, driver, below=None, to_right_of=None, above=None,
to_left_of=None
):
super(HTMLElementImpl, self).__init__(driver)
self.below = self._unwrap_element(below)
self.to_right_of = self._unwrap_element(to_right_of)
self.above = self._unwrap_element(above)
self.to_left_of = self._unwrap_element(to_left_of)
self.matches = PREFIX_IGNORE_CASE()
def find_anywhere_in_curr_frame(self):
raise NotImplementedError()
@property
def width(self):
return self.first_occurrence.location.width
@property
def height(self):
return self.first_occurrence.location.height
@property
def x(self):
return self.first_occurrence.location.left
@property
def y(self):
return self.first_occurrence.location.top
@property
def top_left(self):
from helium import Point
return Point(self.x, self.y)
@property
def web_element(self):
return self.first_occurrence.unwrap()
def find_all_occurrences(self):
self._handle_closed_window()
self._driver.switch_to.default_content()
already_yielded = set()
for frame_index in FrameIterator(self._driver):
for occurrence in self._find_all_in_curr_frame():
if occurrence.target in already_yielded:
# We have seen this element before, but its frame had a
# different index. This means that the frames have changed.
# Abort:
return
occurrence.frame_index = frame_index
yield occurrence
already_yielded.add(occurrence.target)
def _handle_closed_window(self):
window_handles = self._driver.window_handles
try:
curr_window_handle = self._driver.current_window_handle
except NoSuchWindowException:
window_has_been_closed = True
else:
window_has_been_closed = curr_window_handle not in window_handles
if window_has_been_closed:
self._driver.switch_to.window(window_handles[0])
def _find_all_in_curr_frame(self):
search_regions = self._get_search_regions_in_curr_frame()
for occurrence in self.find_anywhere_in_curr_frame():
if not occurrence.is_displayed():
continue
if self._is_in_any_search_region(occurrence, search_regions):
yield occurrence
def _get_search_regions_in_curr_frame(self):
result = []
if self.below:
result.append([
elt.location.is_above
for elt in self.below._find_all_in_curr_frame()
])
if self.to_right_of:
result.append([
elt.location.is_to_left_of
for elt in self.to_right_of._find_all_in_curr_frame()
])
if self.above:
result.append([
elt.location.is_below
for elt in self.above._find_all_in_curr_frame()
])
if self.to_left_of:
result.append([
elt.location.is_to_right_of
for elt in self.to_left_of._find_all_in_curr_frame()
])
return result
def _is_in_any_search_region(self, element, search_regions):
for direction in search_regions:
found = False
for search_region in direction:
if search_region(element.location):
found = True
break
if not found:
return False
return True
def _is_enabled(self):
"""
Useful for subclasses.
"""
return self.first_occurrence.get_attribute('disabled') is None
def _unwrap_element(self, element):
if isinstance(element, str):
return TextImpl(self._driver, element)
from helium import HTMLElement
if isinstance(element, HTMLElement):
return element._impl
return element
class SImpl(HTMLElementImpl):
def __init__(self, driver, selector, **kwargs):
super(SImpl, self).__init__(driver, **kwargs)
self.selector = selector
def find_anywhere_in_curr_frame(self):
wrap = lambda web_elements: list(map(WebElementWrapper, web_elements))
if self.selector.startswith('@'):
return wrap(self._driver.find_elements(By.NAME, self.selector[1:]))
if self.selector.startswith('//'):
return wrap(self._driver.find_elements(By.XPATH, self.selector))
return wrap(self._driver.find_elements(By.CSS_SELECTOR, self.selector))
class HTMLElementIdentifiedByXPath(HTMLElementImpl):
def find_anywhere_in_curr_frame(self):
x_path = self.get_xpath()
return self._sort_search_result(
list(map(
WebElementWrapper, self._driver.find_elements(By.XPATH, x_path)
))
)
def _sort_search_result(self, search_result):
keys_to_result_items = []
for web_elt in search_result:
try:
key = self.get_sort_index(web_elt)
except StaleElementReferenceException:
pass
else:
keys_to_result_items.append((key, web_elt))
sort_key = lambda tpl: tpl[0]
keys_to_result_items.sort(key=sort_key)
result_item = lambda tpl: tpl[1]
return list(map(result_item, keys_to_result_items))
def get_xpath(self):
raise NotImplementedError()
def get_sort_index(self, web_element):
return self._driver.get_distance_to_last_manipulated(web_element) + 1
class HTMLElementContainingText(HTMLElementIdentifiedByXPath):
def __init__(self, driver, text=None, **kwargs):
super(HTMLElementContainingText, self).__init__(driver, **kwargs)
self.search_text = text
def get_xpath(self):
xpath_base = "//" + self.get_xpath_node_selector() + \
predicate(self.matches.xpath('.', self.search_text))
return '%s[not(self::script)][not(.%s)]' % (xpath_base, xpath_base)
def get_xpath_node_selector(self):
return '*'
class TextImpl(HTMLElementContainingText):
def __init__(self, driver, text=None, include_free_text=True, **kwargs):
super(TextImpl, self).__init__(driver, text, **kwargs)
self.include_free_text = include_free_text
@property
def value(self):
return self.first_occurrence.text
def get_xpath(self):
button_impl = ButtonImpl(self._driver, self.search_text)
link_impl = LinkImpl(self._driver, self.search_text)
components = [
self._get_search_text_xpath(),
button_impl.get_input_button_xpath(),
link_impl.get_xpath()
]
if self.search_text and self.include_free_text:
components.append(
FreeText(self._driver, self.search_text).get_xpath()
)
return ' | '.join(components)
def _get_search_text_xpath(self):
if self.search_text:
result = super(TextImpl, self).get_xpath()
else:
no_descendant_with_same_text = \
"not(.//*[normalize-space(.)=normalize-space(self::*)])"
result = '//*[text() and %s]' % no_descendant_with_same_text
return result + "[not(self::option)]" + \
("" if self.include_free_text else "[count(*) <= 1]")
class FreeText(HTMLElementContainingText):
def get_xpath_node_selector(self):
return 'text()'
def get_xpath(self):
return super(FreeText, self).get_xpath() + '/..'
class LinkImpl(HTMLElementContainingText):
def get_xpath_node_selector(self):
return 'a'
def get_xpath(self):
return super(LinkImpl, self).get_xpath() + ' | ' + \
"//a" + \
predicate(self.matches.xpath('@title', self.search_text)) + \
' | ' + "//*[@role='link']" + \
predicate(self.matches.xpath('.', self.search_text))
@property
def href(self):
return self.web_element.get_attribute('href')
class ListItemImpl(HTMLElementContainingText):
def get_xpath_node_selector(self):
return 'li'
class ButtonImpl(HTMLElementContainingText):
def get_xpath_node_selector(self):
return 'button'
def is_enabled(self):
aria_disabled = self.first_occurrence.get_attribute('aria-disabled')
return self._is_enabled() \
and (not aria_disabled or aria_disabled.lower() == 'false')
def get_xpath(self):
has_aria_label = self.matches.xpath('@aria-label', self.search_text)
has_text = self.matches.xpath('.', self.search_text)
has_text_or_aria_label = predicate_or(has_aria_label, has_text)
return ' | '.join([
super(ButtonImpl, self).get_xpath(), self.get_input_button_xpath(),
"//*[@role='button']" + has_text_or_aria_label,
"//button" + predicate(has_aria_label)
])
def get_input_button_xpath(self):
if self.search_text:
has_value = self.matches.xpath('@value', self.search_text)
has_label = self.matches.xpath('@label', self.search_text)
has_aria_label = self.matches.xpath('@aria-label', self.search_text)
has_title = self.matches.xpath('@title', self.search_text)
has_text = \
predicate_or(has_value, has_label, has_aria_label, has_title)
else:
has_text = ''
return "//input[@type='submit' or @type='button']" + has_text
class ImageImpl(HTMLElementIdentifiedByXPath):
def __init__(self, driver, alt, **kwargs):
super(ImageImpl, self).__init__(driver, **kwargs)
self.alt = alt
def get_xpath(self):
return "//img" + predicate(self.matches.xpath('@alt', self.alt))
class LabelledElement(HTMLElementImpl):
SECONDARY_SEARCH_DIMENSION_PENALTY_FACTOR = 1.5
def __init__(self, driver, label=None, **kwargs):
super(LabelledElement, self).__init__(driver, **kwargs)
self.label = label
def find_anywhere_in_curr_frame(self):
if not self.label:
result = self._find_elts()
else:
labels = TextImpl(
self._driver, self.label, include_free_text=False
).find_anywhere_in_curr_frame()
if labels:
result = list(self._filter_elts_belonging_to_labels(
self._find_elts(), labels
))
else:
result = self._find_elts_by_free_text()
return sorted(result, key=self._driver.get_distance_to_last_manipulated)
def _find_elts(self, xpath=None):
if xpath is None:
xpath = self.get_xpath()
return list(map(
WebElementWrapper, self._driver.find_elements(By.XPATH, xpath)
))
def _find_elts_by_free_text(self):
elt_types = [
xpath.strip().lstrip('/') for xpath in self.get_xpath().split('|')
]
labels = '//text()' + predicate(self.matches.xpath('.', self.label))
xpath = ' | '.join(
[(labels + '/%s::' + elt_type + '[1]')
% ('preceding-sibling'
if 'checkbox' in elt_type or 'radio' in elt_type
else 'following')
for elt_type in elt_types]
)
return self._find_elts(xpath)
def get_xpath(self):
raise NotImplementedError()
def get_primary_search_direction(self):
return 'to_right_of'
def get_secondary_search_direction(self):
return 'below'
def _filter_elts_belonging_to_labels(self, all_elts, labels):
for label, elt in self._get_labels_with_explicit_elts(all_elts, labels):
yield elt
labels.remove(label)
all_elts.remove(elt)
labels_to_elts = self._get_related_elts(all_elts, labels)
labels_to_elts = self._ensure_at_most_one_label_per_elt(labels_to_elts)
self._retain_closest(labels_to_elts)
for elts_for_label in list(labels_to_elts.values()):
assert len(elts_for_label) <= 1
if elts_for_label:
yield next(iter(elts_for_label))
def _get_labels_with_explicit_elts(self, all_elts, labels):
for label in labels:
if label.tag_name == 'label':
label_target = label.get_attribute('for')
if label_target:
for elt in all_elts:
elt_id = elt.get_attribute('id')
if elt_id.lower() == label_target.lower():
yield label, elt
def _get_related_elts(self, all_elts, labels):
result = {}
for label in labels:
for elt in all_elts:
if self._are_related(elt, label):
if label not in result:
result[label] = set()
result[label].add(elt)
return result
def _are_related(self, elt, label):
if elt.location.intersects(label.location):
return True
prim_search_dir = self.get_primary_search_direction()
sec_search_dir = self.get_secondary_search_direction()
return label.location.distance_to(elt.location) <= 150 and (
elt.location.is_in_direction(prim_search_dir, label.location) or
elt.location.is_in_direction(sec_search_dir, label.location)
)
def _ensure_at_most_one_label_per_elt(self, labels_to_elts):
elts_to_labels = inverse(labels_to_elts)
self._retain_closest(elts_to_labels)
return inverse(elts_to_labels)
def _retain_closest(self, pivots_to_elts):
for pivot, elts in list(pivots_to_elts.items()):
if elts:
# Would like to use a set literal {...} here, but this is not
# supported in Python 2.6. Thus we need to use set([...]).
pivots_to_elts[pivot] = set([self._find_closest(pivot, elts)])
def _find_closest(self, to_pivot, among_elts):
remaining_elts = iter(among_elts)
result = next(remaining_elts)
result_distance = self._compute_distance(result, to_pivot)
for element in remaining_elts:
element_distance = self._compute_distance(element, to_pivot)