forked from ManageIQ/integration_tests
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwidgetastic_manageiq.py
1311 lines (1054 loc) · 41.4 KB
/
widgetastic_manageiq.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
# -*- coding: utf-8 -*-
import re
from datetime import date
from jsmin import jsmin
from selenium.common.exceptions import WebDriverException
from math import ceil
from wait_for import wait_for
from widgetastic.exceptions import NoSuchElementException
from widgetastic.log import logged
from widgetastic.utils import VersionPick, Version
from widgetastic.widget import (
Table as VanillaTable,
TableColumn as VanillaTableColumn,
TableRow as VanillaTableRow,
Widget,
View,
Select,
Text,
TextInput,
Checkbox,
ParametrizedView,
WidgetDescriptor,
do_not_read_this_widget)
from widgetastic.utils import ParametrizedLocator, Parameter, attributize_string
from widgetastic.xpath import quote
from widgetastic_patternfly import (
Accordion as PFAccordion, CandidateNotFound, BootstrapTreeview, Button, Input, BootstrapSelect)
from cached_property import cached_property
class DynaTree(Widget):
""" A class directed at CFME Tree elements
"""
XPATH = """\
function xpath(root, xpath) {
if(root == null)
root = document;
var nt = XPathResult.ANY_UNORDERED_NODE_TYPE;
return document.evaluate(xpath, root, null, nt, null).singleNodeValue;
}
"""
# This function retrieves the root of the tree. Can wait for the tree to get initialized
TREE_GET_ROOT = """\
function get_root(loc) {
var start_time = new Date();
var root = null;
while(root === null && ((new Date()) - start_time) < 10000)
{
try {
root = $(loc).dynatree("getRoot");
} catch(err) {
// Nothing ...
}
}
return root;
}
"""
# This function is used to DRY the decision on which text to match
GET_LEVEL_NAME = XPATH + """\
function get_level_name(level, by_id) {
if(by_id){
return level.li.getAttribute("id");
} else {
var e = xpath(level.li, "./span/a");
if(e === null)
return null;
else
return e.textContent;
}
}
"""
# needs xpath to work, provided by dependencies of the other functions
EXPANDABLE = """\
function expandable(el) {
return xpath(el.li, "./span/span[contains(@class, 'dynatree-expander')]") !== null;
}
"""
# This function reads whole tree. If it faces an ajax load, it returns false.
# If it does not return false, the result is complete.
READ_TREE = jsmin(TREE_GET_ROOT + GET_LEVEL_NAME + EXPANDABLE + """\
function read_tree(root, read_id, _root_tree) {
if(read_id === undefined)
read_id = false;
if(_root_tree === undefined)
_root_tree = true;
if(_root_tree) {
root = get_root(root);
if(root === null)
return null;
if(expandable(root) && (!root.bExpanded)) {
root.expand();
if(root.childList === null && root.data.isLazy){
return false;
}
}
var result = new Array();
var need_wait = false;
var children = (root.childList === null) ? [] : root.childList;
for(var i = 0; i < children.length; i++) {
var child = children[i];
var sub = read_tree(child, read_id, false);
if(sub === false)
need_wait = true;
else
result.push(sub);
}
if(need_wait)
return false;
else if(children.length == 0)
return null;
else
return result;
} else {
if(expandable(root) && (!root.bExpanded)) {
root.expand();
if(root.childList === null && root.data.isLazy){
return false;
}
}
var name = get_level_name(root, read_id);
var result = new Array();
var need_wait = false;
var children = (root.childList === null) ? [] : root.childList;
for(var i = 0; i < children.length; i++) {
var child = children[i];
var sub = read_tree(child, read_id, false);
if(sub === false)
need_wait = true;
else
result.push(sub);
}
if(need_wait)
return false;
else if(children.length == 0)
return name;
else
return [name, result]
}
}
""")
def __init__(self, parent, tree_id=None, logger=None):
Widget.__init__(self, parent, logger=logger)
self._tree_id = tree_id
@property
def tree_id(self):
if self._tree_id is not None:
return self._tree_id
else:
try:
return self.parent.tree_id
except AttributeError:
raise NameError(
'You have to specify tree_id to BootstrapTreeview if the parent object does '
'not implement .tree_id!')
def __locator__(self):
return '#{}'.format(self.tree_id)
def read(self):
return self.currently_selected
def fill(self, value):
if self.currently_selected == value:
return False
self.click_path(*value)
return True
@property
def currently_selected(self):
items = self.browser.elements(
'.//li[.//span[contains(@class, "dynatree-active")]]/span/a',
parent=self,
check_visibility=True)
return map(self.browser.text, items)
def root_el(self):
return self.browser.element(self)
def _get_tag(self):
if getattr(self, 'tag', None) is None:
self.tag = self.browser.tag(self)
return self.tag
def read_contents(self, by_id=False):
result = False
while result is False:
self.browser.plugin.ensure_page_safe()
result = self.browser.execute_script(
"{} return read_tree(arguments[0], arguments[1]);".format(self.READ_TREE),
self.__locator__(),
by_id)
return result
@staticmethod
def _construct_xpath(path, by_id=False):
items = []
for item in path:
if by_id:
items.append('ul/li[@id={}]'.format(quote(item)))
else:
items.append('ul/li[./span/a[normalize-space(.)={}]]'.format(quote(item)))
return './' + '/'.join(items)
def _item_expanded(self, id):
span = self.browser.element('.//li[@id={}]/span'.format(quote(id)), parent=self)
return 'dynatree-expanded' in self.browser.get_attribute('class', span)
def _item_expandable(self, id):
return bool(
self.browser.elements(
'.//li[@id={}]/span/span[contains(@class, "dynatree-expander")]'.format(quote(id)),
parent=self))
def _click_expander(self, id):
expander = self.browser.element(
'.//li[@id={}]/span/span[contains(@class, "dynatree-expander")]'.format(quote(id)),
parent=self)
return self.browser.click(expander)
def expand_id(self, id):
self.browser.plugin.ensure_page_safe()
if not self._item_expanded(id) and self._item_expandable(id):
self.logger.debug('expanding node %r', id)
self._click_expander(id)
wait_for(lambda: self._item_expanded(id), num_sec=15, delay=0.5)
def child_items(self, id, ids=False):
self.expand_id(id)
items = self.browser.elements('.//li[@id={}]/ul/li'.format(quote(id)), parent=self)
result = []
for item in items:
if ids:
result.append(self.browser.get_attribute('id', item))
else:
text_item = self.browser.element('./span/a', parent=item)
result.append(self.browser.text(text_item))
return result
def expand_path(self, *path, **kwargs):
""" Exposes a path.
Args:
*path: The path as multiple positional string arguments denoting the course to take.
Keywords:
by_id: Whether to match ids instead of text.
Returns: The leaf web element.
"""
by_id = kwargs.pop("by_id", False)
current_path = []
last_id = None
node = None
for item in path:
if last_id is None:
last_id = self.browser.get_attribute(
'id', self.browser.element('./ul/li', parent=self))
self.expand_id(last_id)
if isinstance(item, re._pattern_type):
self.logger.debug('Looking for regexp %r in path %r', item.pattern, current_path)
for child_item in self.child_items(last_id, ids=by_id):
if item.match(child_item) is not None:
# found
item = child_item
break
else:
raise CandidateNotFound(
{'message': "r{!r}: could not be found in the tree.".format(item.pattern),
'path': current_path,
'cause': None})
current_path.append(item)
xpath = self._construct_xpath(current_path, by_id=by_id)
try:
node = self.browser.element(xpath, parent=self)
except NoSuchElementException:
raise CandidateNotFound(
{'message': "{}: could not be found in the tree.".format(item),
'path': current_path,
'cause': None})
last_id = self.browser.get_attribute('id', node)
if node is not None:
self.expand_id(last_id)
return self.browser.element('./span/a', parent=node)
def click_path(self, *path, **kwargs):
""" Exposes a path and then clicks it.
Args:
*path: The path as multiple positional string arguments denoting the course to take.
Keywords:
by_id: Whether to match ids instead of text.
Returns: The leaf web element.
"""
leaf = self.expand_path(*path, **kwargs)
self.logger.info("Path %r yielded menuitem %r", path, self.browser.text(leaf))
if leaf is not None:
self.browser.plugin.ensure_page_safe()
self.browser.click(leaf)
return leaf
def ManageIQTree(tree_id=None): # noqa
return VersionPick({
Version.lowest(): DynaTree(tree_id),
'5.7.0.1': BootstrapTreeview(tree_id),
})
class SummaryFormItem(Widget):
"""The UI item that shows the values for objects that are NOT VMs, Providers and such ones."""
LOCATOR = (
'.//h3[normalize-space(.)={}]/following-sibling::div[1]/div'
'/label[normalize-space(.)={}]/following-sibling::div')
def __init__(self, parent, group_title, item_name, text_filter=None, logger=None):
Widget.__init__(self, parent, logger=logger)
self.group_title = group_title
self.item_name = item_name
if text_filter is not None and not callable(text_filter):
raise TypeError('text_filter= must be a callable')
self.text_filter = text_filter
def __locator__(self):
return self.LOCATOR.format(quote(self.group_title), quote(self.item_name))
@property
def text(self):
if not self.is_displayed:
return None
ui_text = self.browser.text(self)
if self.text_filter is not None:
# Process it
ui_text = self.text_filter(ui_text)
return ui_text
def read(self):
text = self.text
if text is None:
do_not_read_this_widget()
return text
class MultiBoxSelect(View):
ROOT = ParametrizedLocator("(.//table[@id={@id|quote}]){@number}")
available_options = Select(id=Parameter("@available_items"))
chosen_options = Select(id=Parameter("@chosen_items"))
def __init__(self, parent, id, number="", move_into=None, move_from=None,
available_items="choices_chosen", chosen_items="members_chosen", logger=None):
View.__init__(self, parent, logger=logger)
self.available_items = available_items
self.chosen_items = chosen_items
self.id = id
if number:
self.number = "[{}]".format(number)
else:
self.number = number
if isinstance(move_into, WidgetDescriptor):
self._move_into = move_into.klass(self, **move_into.kwargs)
else:
self._move_into = move_into
if isinstance(move_from, WidgetDescriptor):
self._move_from = move_from.klass(self, **move_from.kwargs)
else:
self._move_from = move_from
def _values_to_remove(self, values):
return list(self.all_options - set(values))
def _values_to_add(self, values):
return list(set(values) - self.all_options)
@property
def move_into_button(self):
if isinstance(self._move_into, Button):
button = self._move_into
elif isinstance(self._move_into, basestring):
button = self.browser.element(self._move_into, self)
return button
@property
def move_from_button(self):
if isinstance(self._move_from, Button):
button = self._move_from
elif isinstance(self._move_from, basestring):
button = self.browser.element(self._move_from, self)
return button
def fill(self, values):
if set(values) == self.all_options:
return False
else:
values_to_remove = self._values_to_remove(values)
values_to_add = self._values_to_add(values)
if values_to_remove:
self.chosen_options.fill(values_to_remove)
self.move_from_button.click()
self.browser.plugin.ensure_page_safe()
if values_to_add:
self.available_options.fill(values_to_add)
self.move_into_button.click()
self.browser.plugin.ensure_page_safe()
return True
@property
def all_options(self):
return {option.text for option in self.chosen_options.all_options}
def read(self):
return list(self.all_options)
class CheckboxSelect(Widget):
ROOT = ParametrizedLocator(".//div[@id={@search_root|quote}]")
def __init__(self, parent, search_root, text_access_func=None, logger=None):
Widget.__init__(self, parent, logger=logger)
self.search_root = search_root
self._access_func = text_access_func
@property
def checkboxes(self):
"""All checkboxes."""
return {Checkbox(self, id=el.get_attribute("id")) for el in self.browser.elements(
".//input[@type='checkbox']")}
@property
def selected_checkboxes(self):
"""Only selected checkboxes."""
return {cb for cb in self.checkboxes if cb.selected}
@cached_property
def selected_text(self):
"""Only selected checkboxes' text descriptions."""
return {self.browser.element("./..", parent=cb).text for cb in self.selected_checkboxes}
@property
def selected_values(self):
"""Only selected checkboxes' values."""
return {cb.get_attribute("value") for cb in self.selected_checkboxes}
@property
def unselected_checkboxes(self):
"""Only unselected checkboxes."""
return {cb for cb in self.checkboxes if not cb.selected}
@property
def unselected_values(self):
"""Only unselected checkboxes' values."""
return {cb.get_attribute("value") for cb in self.unselected_checkboxes}
def checkbox_by_id(self, id):
"""Find checkbox's WebElement by id."""
return Checkbox(self, id=id)
def _values_to_remove(self, values):
return list(self.selected_text - set(values))
def _values_to_add(self, values):
return list(set(values) - self.selected_text)
def select_all(self):
"""Selects all checkboxes."""
for cb in self.unselected_checkboxes:
cb.fill(True)
def unselect_all(self):
"""Unselects all checkboxes."""
for cb in self.selected_checkboxes:
cb.fill(False)
def checkbox_by_text(self, text):
"""Returns checkbox's WebElement searched by its text."""
if self._access_func is not None:
for cb in self.checkboxes:
txt = self._access_func(cb)
if txt == text:
return cb
else:
raise NameError("Checkbox with text {} not found!".format(text))
else:
# Has to be only single
return Checkbox(
self,
locator=".//*[normalize-space(.)={}]/input[@type='checkbox']".format(quote(text))
)
def fill(self, values):
if set(values) == self.selected_text:
return False
else:
for value in self._values_to_remove(values):
checkbox = self.checkbox_by_text(value)
checkbox.fill(False)
for value in self._values_to_add(values):
checkbox = self.checkbox_by_text(value)
checkbox.fill(True)
return True
def read(self):
"""Only selected checkboxes."""
return [cb for cb in self.checkboxes if cb.selected]
# ManageIQ table objects definition
class TableColumn(VanillaTableColumn):
@property
def checkbox(self):
try:
return self.browser.element('./input[@type="checkbox"]', parent=self)
except NoSuchElementException:
return None
@property
def checked(self):
checkbox = self.checkbox
if checkbox is None:
return None
return self.browser.is_selected(checkbox)
def check(self):
if not self.checked:
self.browser.click(self.checkbox)
def uncheck(self):
if self.checked:
self.browser.click(self.checkbox)
class TableRow(VanillaTableRow):
Column = TableColumn
class Table(VanillaTable):
CHECKBOX_ALL = '|'.join([
'./thead/tr/th[1]/input[contains(@class, "checkall")]',
'./tr/th[1]/input[contains(@class, "checkall")]'])
SORTED_BY_LOC = (
'./thead/tr/th[contains(@class, "sorting_asc") or contains(@class, "sorting_desc")]')
SORT_LINK = './thead/tr/th[{}]/a'
Row = TableRow
@property
def checkbox_all(self):
try:
return self.browser.element(self.CHECKBOX_ALL, parent=self)
except NoSuchElementException:
return None
@property
def all_checked(self):
checkbox = self.checkbox_all
if checkbox is None:
return None
return self.browser.is_selected(checkbox)
def check_all(self):
if not self.all_checked:
self.browser.click(self.checkbox_all)
def uncheck_all(self):
self.check_all()
self.browser.click(self.checkbox_all)
@property
def sorted_by(self):
"""Returns the name of column that the table is sorted by. Attributized!"""
return attributize_string(self.browser.text(self.SORTED_BY_LOC, parent=self))
@property
def sort_order(self):
"""Returns the sorting order of the table for current column.
Returns:
``asc`` or ``desc``
"""
klass = self.browser.get_attribute('class', self.SORTED_BY_LOC, parent=self)
return re.search(r'sorting_(asc|desc)', klass).groups()[0]
def click_sort(self, column):
"""Clicks the sorting link in the given column. The column gets attributized."""
self.logger.info('click_sort(%r)', column)
column = attributize_string(column)
column_position = self.header_index_mapping[self.attributized_headers[column]]
self.browser.click(self.SORT_LINK.format(column_position + 1), parent=self)
def sort_by(self, column, order='asc'):
"""Sort table by column and in given direction.
Args:
column: Name of the column, can be normal or attributized.
order: Sorting order. ``asc`` or ``desc``.
"""
self.logger.info('sort_by(%r, %r)', column, order)
column = attributize_string(column)
# Sort column
if self.sorted_by != column:
self.click_sort(column)
else:
self.logger.debug('sort_by(%r, %r): column already selected', column, order)
# Sort order
if self.sort_order != order:
self.logger.info('sort_by(%r, %r): changing the sort order', column, order)
self.click_sort(column)
self.logger.debug('sort_by(%r, %r): order already selected', column, order)
class Accordion(PFAccordion):
@property
def is_dimmed(self):
return bool(
self.browser.elements('.//div[contains(@id, "tree") and contains(@class, "dimmed")]'))
class Calendar(TextInput):
"""A CFME calendar form field
Calendar fields are readonly, and managed by the dxhtmlCalendar widget. A Calendar field
will accept any object that can be coerced into a string, but the value may not match the format
expected by dhtmlxCalendar or CFME. For best results, either a ``datetime.date`` or
``datetime.datetime`` object should be used to create a valid date field.
Args:
name: "name" property of the readonly calendar field.
"""
def fill(self, value):
if isinstance(value, date):
date_str = value.strftime('%m/%d/%Y')
else:
date_str = str(value)
self.move_to()
# need to write to a readonly field: resort to evil
if self.browser.get_attribute("ng-model", self) is not None:
# self.set_angularjs_value(self, date_str)
raise NotImplementedError
else:
self.browser.set_attribute("value", date_str, self)
# Now when we set the value, we need to simulate a change event.
if self.browser.get_attribute("data-date-autoclose", self):
# New one
script = "$(arguments[0]).trigger('changeDate');"
else:
# Old one
script = "$(arguments[0]).change();"
try:
self.browser.execute_script(script, self.browser.element(self))
except WebDriverException as e:
self.logger.warning(
"An exception was raised during handling of the Cal #{}'s change event:\n{}"
.format(self.name, str(e)))
self.browser.plugin.ensure_page_safe()
return True
class SNMPHostsField(View):
_input = Input("host")
def __init__(self, parent, logger=None):
View.__init__(self, parent, logger=logger)
def fill(self, values):
fields = self.host_fields
if isinstance(values, basestring):
values = [values]
if len(values) > len(fields):
raise ValueError("You cannot specify more hosts than the form allows!")
return any(fields[i].fill(value) for i, value in enumerate(values))
@property
def host_fields(self):
"""Returns list of locators to all host fields"""
if self._input.is_displayed:
return [self._input]
else:
return [Input(self, "host_{}".format(i)) for i in range(1, 4)]
class SNMPTrapsField(Widget):
def __init__(self, parent, logger=None):
Widget.__init__(self, parent, logger=logger)
def fill_oid_field(self, i, oid):
oid_field = Input(self, "oid__{}".format(i))
return oid_field.fill(oid)
def fill_type_field(self, i, type_):
type_field = BootstrapSelect(self, "var_type__{}".format(i))
return type_field.fill(type_)
def fill_value_field(self, i, value):
value_field = Input(self, "value__{}".format(i))
return value_field.fill(value)
def fill(self, traps):
result = []
for i, trap in enumerate(traps, 1):
assert 2 <= len(trap) <= 3, "The tuple must be at least 2 items and max 3 items!"
if len(trap) == 2:
trap += (None,)
oid, type_, value = trap
result.append(any((
self.fill_oid_field(i, oid),
self.fill_type_field(i, type_),
self.fill_value_field(i, value)
)))
return any(result)
def read(self):
do_not_read_this_widget()
class SNMPForm(View):
hosts = SNMPHostsField()
version = BootstrapSelect("snmp_version")
id = Input("trap_id")
traps = SNMPTrapsField()
class ScriptBox(Widget):
"""Represents a script box as is present on the customization templates pages.
This box has to be activated before keys can be sent. Since this can't be done
until the box element is visible, and some dropdowns change the element, it must
be activated "inline".
Args:
"""
def __init__(self, parent, locator=None, item_name=None, logger=None):
Widget.__init__(self, parent, logger=logger)
self.locator = locator
self.item_name = item_name
def __locator__(self):
if not self.locator:
self.locator = "//textarea[contains(@id, 'method_data')]"
return self.locator
@property
def name(self):
if not self.item_name:
self.item_name = 'ManageIQ.editor'
return self.item_name
@property
def script(self):
return self.browser.execute_script('{}.getValue();'.format(self.name))
def fill(self, value):
if self.script == value:
return False
self.browser.execute_script('{}.setValue(arguments[0]);'.format(self.name), value)
self.browser.execute_script('{}.save();'.format(self.name))
return True
def read(self):
return self.script
def get_value(self):
script = self.browser.execute_script('return {}.getValue();'.format(self.name))
script = script.replace('\\"', '"').replace("\\n", "\n")
return script
def workaround_save_issue(self):
# We need to fire off the handlers manually in some cases ...
self.browser.execute_script(
"{}._handlers.change.map(function(handler) {{ handler() }});".format(self.item_name))
class Paginator(Widget):
""" Represents Paginator control that includes First/Last/Next/Prev buttons
and a control displaying amount of items on current page vs overall amount.
It is mainly used in Paginator Pane.
"""
PAGINATOR_CTL = './/ul[@class="pagination"]'
CUR_PAGE_CTL = './li/span/input[@name="limitstart"]/..'
PAGE_BUTTON_CTL = './li[contains(@class, {})]/span'
def __locator__(self):
return self._paginator
@property
def _paginator(self):
return self.browser.element(self.PAGINATOR_CTL, parent=self.parent_view)
def _is_enabled(self, element):
return 'disabled' not in self.browser.classes(element.find_element_by_xpath('..'))
def _click_button(self, cmd):
cur_page_btn = self.browser.element(self.PAGE_BUTTON_CTL.format(quote(cmd)),
parent=self._paginator)
if self._is_enabled(cur_page_btn):
self.browser.click(cur_page_btn)
else:
raise NoSuchElementException('such button {} is absent/grayed out'.format(cmd))
def next_page(self):
self._click_button('next')
def prev_page(self):
self._click_button('prev')
def last_page(self):
self._click_button('last')
def first_page(self):
self._click_button('first')
def page_info(self):
cur_page = self.browser.element(self.CUR_PAGE_CTL, parent=self._paginator)
text = cur_page.text
return re.search('(\d+)\s+of\s+(\d+)', text).groups()
class PaginationPane(View):
""" Represents Paginator Pane with the following controls.
The intention of this view is to use it as nested view on f.e. Infrastructure Providers page.
"""
ROOT = '//div[@id="paging_div"]'
check_all_items = Checkbox(id='masterToggle')
sort_by = BootstrapSelect(id='sort_choice')
items_on_page = BootstrapSelect(id='ppsetting')
paginator = Paginator()
@property
def exists(self):
return self.is_displayed
def check_all(self):
self.check_all_items.fill(True)
def uncheck_all(self):
self.check_all()
self.check_all_items.fill(False)
def sort(self, value):
self.sort_by.select_by_visible_text(value)
@property
def sorted_by(self):
raise NotImplementedError('to implement it when needed')
@property
def items_per_page(self):
selected = self.items_on_page.selected_option
return int(re.sub(r'\s+items', '', selected))
def set_items_per_page(self, value):
self.items_on_page.select_by_visible_text(str(value))
def _parse_pages(self):
max_item, item_amt = self.paginator.page_info()
item_amt = int(item_amt)
max_item = int(max_item)
items_per_page = self.items_per_page
# obtaining amount of existing pages, there is 1 page by default
if item_amt == 0:
page_amt = 1
else:
# round up after dividing total item count by per-page
page_amt = int(ceil(float(item_amt) / float(items_per_page)))
# calculating current_page_number
if max_item <= items_per_page:
cur_page = 1
else:
# round up after dividing highest displayed item number by per-page
cur_page = int(ceil(float(max_item) / float(items_per_page)))
return cur_page, page_amt
@property
def cur_page(self):
return self._parse_pages()[0]
@property
def pages_amount(self):
return self._parse_pages()[1]
def next_page(self):
self.paginator.next_page()
def prev_page(self):
self.paginator.prev_page()
def first_page(self):
self.paginator.first_page()
def last_page(self):
self.paginator.last_page()
def pages(self):
"""Generator to iterate over pages, yielding after moving to the next page"""
if self.exists:
# start iterating at the first page
if self.cur_page != 1:
self.logger.debug('Resetting paginator to first page')
self.first_page()
# Adding 1 to pages_amount to include the last page in loop
for page in range(1, self.pages_amount + 1):
yield self.cur_page
if self.cur_page == self.pages_amount:
# last or only page, stop looping
break
else:
self.logger.debug('Paginator advancing to next page')
self.next_page()
else:
return
@property
def items_amount(self):
return self.paginator.page_info()[1]
class Stepper(View):
""" A CFME Stepper Control
.. code-block:: python
stepper = Stepper(locator='//div[contains(@class, "timeline-stepper")]')
stepper.increase()
"""
ROOT = ParametrizedLocator('{@locator}')
minus_button = Button('-')
plus_button = Button('+')
value_field = Input(locator='.//input[contains(@class, "bootstrap-touchspin")]')
def __init__(self, parent, locator, logger=None):
View.__init__(self, parent=parent, logger=logger)
self.locator = locator
def read(self):
return int(self.value_field.read())
def decrease(self):
self.minus_button.click()
def increase(self):
self.plus_button.click()
def set_value(self, value):
value = int(value)
if value < 1:
raise ValueError('The value cannot be less than 1')
steps = value - self.read()
if steps == 0:
return False
elif steps > 0:
operation = self.increase
else:
operation = self.decrease
steps = abs(steps)
for step in range(steps):
operation()
return True
def fill(self, value):
return self.set_value(value)