forked from cheezy/page-object
/
page_object.rb
757 lines (661 loc) · 24.1 KB
/
page_object.rb
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
require 'watir-webdriver/extensions/alerts'
require 'page-object/elements'
require 'page-object/core_ext/string'
module PageObject
module Platforms
module WatirWebDriver
#
# Watir implementation of the page object platform driver. You should not use the
# class directly. Instead you should include the PageObject module in your page object
# and use the methods dynamically added from the PageObject::Accessors module.
#
class PageObject
attr_reader :browser
def initialize(browser)
@browser = browser
end
#
# platform method to navigate to a provided url
# See PageObject#navigate_to
#
def navigate_to(url)
@browser.goto url
end
#
# platform method to get the current url
# See PageObject#current_url
#
def current_url
@browser.url
end
#
# platform method to retrieve the text from the current page
# See PageObject#text
#
def text
@browser.text
end
#
# platform method to retrieve the html for the current page
# See PageObject#html
#
def html
@browser.html
end
#
# platform method to retrieve the title for the current page
# See PageObject#title
#
def title
@browser.title
end
#
# platform method to wait for a block to return true
# See PageObject#wait_until
def wait_until(timeout, message = nil, &block)
@browser.wait_until(timeout, message, &block)
end
#
# platform method to handle an alert popup
# See PageObject#alert
#
def alert(frame=nil, &block)
switch_to_frame(frame)
@browser.wd.execute_script "window.alert = function(msg) { window.__lastWatirAlert = msg; }"
yield
value = @browser.wd.execute_script "return window.__lastWatirAlert"
switch_to_default_content(frame)
value
end
#
# platform method to handle a confirm popup
# See PageObject#confirm
#
def confirm(response, frame=nil, &block)
switch_to_frame(frame)
@browser.wd.execute_script "window.confirm = function(msg) { window.__lastWatirConfirm = msg; return #{!!response} }"
yield
value = @browser.wd.execute_script "return window.__lastWatirConfirm"
switch_to_default_content(frame)
value
end
#
# platform method to handle a prompt popup
# See PageObject#prompt
#
def prompt(answer, frame=nil, &block)
switch_to_frame(frame)
@browser.wd.execute_script "window.prompt = function(text, value) { window.__lastWatirPrompt = { message: text, default_value: value }; return #{answer}; }"
yield
result = @browser.wd.execute_script "return window.__lastWatirPrompt"
switch_to_default_content(frame)
result && result.dup.each_key { |k| result[k.to_sym] = result.delete(k) }
result
end
#
# platform method to handle attaching to a running window
# See PageObject#attach_to_window
#
def attach_to_window(identifier, &block)
win_id = {identifier.keys.first => /#{Regexp.escape(identifier.values.first)}/}
@browser.window(win_id).use &block
end
#
# platform method to switch to a frame and execute a block
# See PageObject#in_frame
#
def in_frame(identifier, frame=nil, &block)
frame = [] if frame.nil?
frame << identifier
block.call(frame)
end
#
# platform method to refresh the page
# See PageObject#refresh
#
def refresh
@browser.refresh
end
#
# platform method to go back to the previous page
# See PageObject#back
#
def back
@browser.back
end
#
# platform method to go forward to the next page
# See PageObject#forward
#
def forward
@browser.forward
end
#
# platform method to clear the cookies from the browser
# See PageObject#clear_cookies
#
def clear_cookies
@browser.clear_cookies
end
#
# platform method to save the current screenshot to a file
# See PageObject#save_screenshot
#
def save_screenshot(file_name)
@browser.wd.save_screenshot(file_name)
end
#
# platform method to get the value stored in a text field
# See PageObject::Accessors#text_field
#
def text_field_value_for(identifier)
process_watir_call("text_field(identifier).value", Elements::TextField, identifier)
end
#
# platform method to set the value for a text field
# See PageObject::Accessors#text_field
#
def text_field_value_set(identifier, value)
process_watir_call("text_field(identifier).set(value)", Elements::TextField, identifier, value)
end
#
# platform method to retrieve a text field element
# See PageObject::Accessors#text_field
#
def text_field_for(identifier)
find_watir_element("text_field(identifier)", Elements::TextField, identifier)
end
#
# platform method to retrieve an array of text field elements
#
def text_fields_for(identifier)
find_watir_elements("text_fields(identifier)", Elements::TextField, identifier)
end
#
# platform method to get the value stored in a hidden field
# See PageObject::Accessors#hidden_field
#
def hidden_field_value_for(identifier)
process_watir_call("hidden(identifier).value", Elements::HiddenField, identifier)
end
#
# platform method to retrieve a hidden field element
# See PageObject::Accessors#hidden_field
#
def hidden_field_for(identifier)
find_watir_element("hidden(identifier)", Elements::HiddenField, identifier)
end
#
# platform method to retrieve an array of hidden field elements
#
def hidden_fields_for(identifier)
find_watir_elements("hiddens(identifier)", Elements::HiddenField, identifier)
end
#
# platform method to set text in a textarea
# See PageObject::Accessors#text_area
#
def text_area_value_set(identifier, value)
process_watir_call("textarea(identifier).send_keys(value)", Elements::TextArea,
identifier, value)
end
#
# platform method to get the text from a textarea
# See PageObject::Accessors#text_area
#
def text_area_value_for(identifier)
process_watir_call("textarea(identifier).value", Elements::TextArea, identifier)
end
#
# platform method to get the text area element
# See PageObject::Accessors#text_area
#
def text_area_for(identifier)
find_watir_element("textarea(identifier)", Elements::TextArea, identifier)
end
#
# platform method to retrieve an array of textarea elements
#
def text_areas_for(identifier)
find_watir_elements("textareas(identifier)", Elements::TextArea, identifier)
end
#
# platform method to get the currently selected value from a select list
# See PageObject::Accessors#select_list
#
def select_list_value_for(identifier)
process_watir_call("select_list(identifier).value", Elements::SelectList,
identifier)
end
#
# platform method to select a value from a select list
# See PageObject::Accessors#select_list
#
def select_list_value_set(identifier, value)
process_watir_call("select_list(identifier).select(value)", Elements::SelectList,
identifier, value)
end
#
# platform method to return the select list element
# See PageObject::Accessors#select_list
#
def select_list_for(identifier)
find_watir_element("select_list(identifier)", Elements::SelectList, identifier)
end
#
# platform method to retrieve an array of select_list elements
#
def select_lists_for(identifier)
find_watir_elements("select_lists(identifier)", Elements::SelectList, identifier)
end
#
# platform method to click a link
# See PageObject::Accessors#link
#
def click_link_for(identifier)
process_watir_call("link(identifier).click if identifier", Elements::Link,
identifier)
end
#
# platform method to return a PageObject::Elements::Link object
# see PageObject::Accessors#link
#
def link_for(identifier)
find_watir_element("link(identifier)", Elements::Link, identifier)
end
#
# platform method to retrieve an array of link elements
#
def links_for(identifier)
find_watir_elements("links(identifier)", Elements::Link, identifier)
end
#
# platform method to check a checkbox
# See PageObject::Accessors#checkbox
#
def check_checkbox(identifier)
process_watir_call("checkbox(identifier).set", Elements::CheckBox, identifier)
end
#
# platform method to uncheck a checkbox
# See PageObject::Accessors#checkbox
#
def uncheck_checkbox(identifier)
process_watir_call("checkbox(identifier).clear", Elements::CheckBox, identifier)
end
#
# platform method to determine if a checkbox is checked
# See PageObject::Accessors#checkbox
#
def checkbox_checked?(identifier)
process_watir_call("checkbox(identifier).set?", Elements::CheckBox, identifier)
end
#
# platform method to return a PageObject::Elements::CheckBox element
# See PageObject::Accessors#checkbox
#
def checkbox_for(identifier)
find_watir_element("checkbox(identifier)", Elements::CheckBox, identifier)
end
#
# platform method to retrieve an array of checkbox elements
#
def checkboxes_for(identifier)
find_watir_elements("checkboxes(identifier)", Elements::CheckBox, identifier)
end
#
# platform method to select a radio button
# See PageObject::Accessors#radio_button
#
def select_radio(identifier)
process_watir_call("radio(identifier).set", Elements::RadioButton, identifier)
end
#
# platform method to clear a radio button
# See PageObject::Accessors#radio_button
#
def clear_radio(identifier)
process_watir_call("radio(identifier).clear", Elements::RadioButton, identifier)
end
#
# platform method to determine if a radio button is selected
# See PageObject::Accessors#radio_button
#
def radio_selected?(identifier)
process_watir_call("radio(identifier).set?", Elements::RadioButton, identifier)
end
#
# platform method to return a PageObject::Eements::RadioButton element
# See PageObject::Accessors#radio_button
#
def radio_button_for(identifier)
find_watir_element("radio(identifier)", Elements::RadioButton, identifier)
end
#
# platform method to retrieve an array of radio button elements
#
def radio_buttons_for(identifier)
find_watir_elements("radios(identifier)", Elements::RadioButton, identifier)
end
#
# platform method to return the text for a div
# See PageObject::Accessors#div
#
def div_text_for(identifier)
process_watir_call("div(identifier).text", Elements::Div, identifier, nil, 'div')
end
#
# platform method to return a PageObject::Elements::Div element
# See PageObject::Accessors#div
#
def div_for(identifier)
find_watir_element("div(identifier)", Elements::Div, identifier, 'div')
end
#
# platform method to retrieve an array of div elements
#
def divs_for(identifier)
find_watir_elements("divs(identifier)", Elements::Div, identifier, 'div')
end
#
# platform method to return the text for a span
# See PageObject::Accessors#span
#
def span_text_for(identifier)
process_watir_call("span(identifier).text", Elements::Span, identifier, nil, 'span')
end
#
# platform method to return a PageObject::Elements::Span element
# See PageObject::Accessors#span
#
def span_for(identifier)
find_watir_element("span(identifier)", Elements::Span, identifier, 'span')
end
#
# platform method to retrieve an array of span elements
#
def spans_for(identifier)
find_watir_elements("spans(identifier)", Elements::Span, identifier, 'span')
end
#
# platform method to click a button
# See PageObject::Accessors#button
#
def click_button_for(identifier)
process_watir_call("button(identifier).click", Elements::Button, identifier)
end
#
# platform method to retrieve a button element
# See PageObject::Accessors#button
#
def button_for(identifier)
find_watir_element("button(identifier)", Elements::Button, identifier)
end
#
# platform method to retrieve an array of button elements
#
def buttons_for(identifier)
find_watir_elements("buttons(identifier)", Elements::Button, identifier)
end
#
# platform method to retrieve a table element
# See PageObject::Accessors#table
#
def table_for(identifier)
find_watir_element("table(identifier)", Elements::Table, identifier, 'table')
end
#
# platform method to retrieve an array of table elements
#
def tables_for(identifier)
find_watir_elements("tables(identifier)", Elements::Table, identifier, 'table')
end
#
# platform method to retrieve the text from a table cell
# See PageObject::Accessors#cell
#
def cell_text_for(identifier)
process_watir_call("td(identifier).text", Elements::TableCell, identifier,
nil, 'td')
end
#
# platform method to retrieve a table cell element
# See PageObject::Accessors#cell
#
def cell_for(identifier)
find_watir_element("td(identifier)", Elements::TableCell, identifier, 'td')
end
#
# platform method to retrieve an array of table cell elements
#
def cells_for(identifier)
find_watir_elements("tds(identifier)", Elements::TableCell, identifier, 'td')
end
#
# platform method to retrieve an image element
# See PageObject::Accessors#image
#
def image_for(identifier)
find_watir_element("image(identifier)", Elements::Image, identifier)
end
#
# platform method to retrieve an array of image elements
#
def images_for(identifier)
find_watir_elements("images(identifier)", Elements::Image, identifier)
end
#
# platform method to retrieve a form element
# See PageObject::Accessors#form
#
def form_for(identifier)
find_watir_element("form(identifier)", Elements::Form, identifier)
end
#
# platform method to retrieve an array of forms
#
def forms_for(identifier)
find_watir_elements("forms(identifier)", Elements::Form, identifier)
end
#
# platform method to retrieve the text from a list item
# See PageObject::Accessors#list_item
#
def list_item_text_for(identifier)
process_watir_call("li(identifier).text", Elements::ListItem, identifier, nil, 'li')
end
#
# platform method to retrieve a list item element
# See PageObject::Accessors#list_item
#
def list_item_for(identifier)
find_watir_element("li(identifier)", Elements::ListItem, identifier, 'li')
end
#
# platform method to retrieve an array of list items
#
def list_items_for(identifier)
find_watir_elements("lis(identifier)", Elements::ListItem, identifier, 'li')
end
#
# platform method to retrieve an unordered list element
# See PageObject::Accessors#unordered_list
#
def unordered_list_for(identifier)
find_watir_element("ul(identifier)", Elements::UnorderedList, identifier, 'ul')
end
#
# platform method to retrieve an ordered list element
# See PageObject::Accessors#ordered_list
#
def ordered_list_for(identifier)
find_watir_element("ol(identifier)", Elements::OrderedList, identifier, 'ol')
end
#
# platform method to retrieve the text for a h1
# See PageObject::Accessors#h1
#
def h1_text_for(identifier)
process_watir_call("h1(identifier).text", Elements::Heading, identifier, nil, 'h1')
end
#
# platform method to retrieve the h1 element
# See PageObject::Accessors#h1
#
def h1_for(identifier)
find_watir_element("h1(identifier)", Elements::Heading, identifier, 'h1')
end
#
# platform method to retrieve the text for a h2
# See PageObject::Accessors#h2
#
def h2_text_for(identifier)
process_watir_call("h2(identifier).text", Elements::Heading, identifier, nil, 'h2')
end
#
# platform method to retrieve the h2 element
# See PageObject::Accessors#h2
#
def h2_for(identifier)
find_watir_element("h2(identifier)", Elements::Heading, identifier, 'h2')
end
#
# platform method to retrieve the text for a h3
# See PageObject::Accessors#h3
#
def h3_text_for(identifier)
process_watir_call("h3(identifier).text", Elements::Heading, identifier, nil, 'h3')
end
#
# platform method to retrieve the h3 element
# See PageObject::Accessors#h3
#
def h3_for(identifier)
find_watir_element("h3(identifier)", Elements::Heading, identifier, 'h3')
end
#
# platform method to retrieve the text for a h4
# See PageObject::Accessors#h4
#
def h4_text_for(identifier)
process_watir_call("h4(identifier).text", Elements::Heading, identifier, nil, 'h4')
end
#
# platform method to retrieve the h4 element
# See PageObject::Accessors#h4
#
def h4_for(identifier)
find_watir_element("h4(identifier)", Elements::Heading, identifier, 'h4')
end
#
# platform method to retrieve the text for a h5
# See PageObject::Accessors#h5
#
def h5_text_for(identifier)
process_watir_call("h5(identifier).text", Elements::Heading, identifier, nil, 'h5')
end
#
# platform method to retrieve the h5 element
# See PageObject::Accessors#h5
#
def h5_for(identifier)
find_watir_element("h5(identifier)", Elements::Heading, identifier, 'h5')
end
#
# platform method to retrieve the text for a h6
# See PageObject::Accessors#h6
#
def h6_text_for(identifier)
process_watir_call("h6(identifier).text", Elements::Heading, identifier, nil, 'h6')
end
#
# platform method to retrieve the h6 element
# See PageObject::Accessors#h6
#
def h6_for(identifier)
find_watir_element("h6(identifier)", Elements::Heading, identifier, 'h6')
end
#
# platform method to retrieve the text for a paragraph
# See PageObject::Accessors#paragraph
#
def paragraph_text_for(identifier)
process_watir_call("p(identifier).text", Elements::Paragraph, identifier, nil, 'p')
end
#
# platform method to retrieve the paragraph element
# See PageObject::Accessors#paragraph
#
def paragraph_for(identifier)
find_watir_element("p(identifier)", Elements::Paragraph, identifier, 'p')
end
#
# platform method to set the file on a file_field element
# See PageObject::Accessors#file_field
#
def file_field_value_set(identifier, value)
process_watir_call("file_field(identifier).set(value)", Elements::FileField,
identifier, value)
end
#
# platform method to retrieve a file_field element
# See PageObject::Accessors#file_field
#
def file_field_for(identifier)
find_watir_element("file_field(identifier)", Elements::FileField, identifier)
end
private
def find_watir_elements(the_call, type, identifier, tag_name=nil)
identifier, frame_identifiers = parse_identifiers(identifier, type, tag_name)
elements = @browser.instance_eval "#{nested_frames(frame_identifiers)}#{the_call}"
switch_to_default_content(frame_identifiers)
elements.map { |element| type.new(element, :platform => :watir_webdriver) }
end
def find_watir_element(the_call, type, identifier, tag_name=nil)
identifier, frame_identifiers = parse_identifiers(identifier, type, tag_name)
element = @browser.instance_eval "#{nested_frames(frame_identifiers)}#{the_call}"
switch_to_default_content(frame_identifiers)
type.new(element, :platform => :watir_webdriver)
end
def process_watir_call(the_call, type, identifier, value=nil, tag_name=nil)
identifier, frame_identifiers = parse_identifiers(identifier, type, tag_name)
value = @browser.instance_eval "#{nested_frames(frame_identifiers)}#{the_call}"
switch_to_default_content(frame_identifiers)
value
end
def parse_identifiers(identifier, element, tag_name=nil)
frame_identifiers = identifier.delete(:frame)
identifier = add_tagname_if_needed identifier, tag_name if tag_name
identifier = element.watir_identifier_for identifier
return identifier, frame_identifiers
end
def add_tagname_if_needed identifier, tag
return identifier if identifier.length < 2 and not identifier[:name]
identifier[:tag_name] = tag if identifier[:name]
identifier
end
def nested_frames(frame_identifiers)
return if frame_identifiers.nil?
frame_str = ''
frame_identifiers.each do |id|
value = id.values.first
frame_str += "frame(:#{id.keys.first} => #{value})." if value.to_s.is_integer
frame_str += "frame(:#{id.keys.first} => '#{value}')." unless value.to_s.is_integer
end
frame_str
end
def switch_to_default_content(frame_identifiers)
@browser.wd.switch_to.default_content unless frame_identifiers.nil?
end
def switch_to_frame(frame_identifiers)
unless frame_identifiers.nil?
frame_identifiers.each do |frame_id|
value = frame_id.values.first
@browser.wd.switch_to.frame(value)
end
end
end
end
end
end
end