-
Notifications
You must be signed in to change notification settings - Fork 91
/
WebElement.pm
721 lines (524 loc) · 18.5 KB
/
WebElement.pm
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
package Selenium::Remote::WebElement;
# ABSTRACT: Representation of an HTML Element used by Selenium Remote Driver
use strict;
use warnings;
use Moo;
use Carp qw(carp croak);
=head1 DESCRIPTION
Selenium Webdriver represents all the HTML elements as WebElements.
This module provides a mechanism to represent them as objects &
perform various actions on the related elements. This module should
not be instantiated directly by the end user. Selenium::Remote::Driver
instantiates this module when required. Typically, the find_element
method in Selenium::Remote::Driver returns this object on which
various element related operations can be carried out.
What is probably most useful on this page is the list of methods below
that you can perform on an element once you've found one and S::R::D
has made an instance of this for you.
=head1 CONSTRUCTOR
=head2 new
=over 4
=item B<id>
Required: Pass in a string representing the ID of the object. The
string should be obtained from the response object of making one of
the C<find_element> calls from L<Selenium::Remote::Driver>.
The attribute is also set up to handle spec compliant element response
objects via its `coerce` such that any of the following will work and
are all equivalent:
my $old_elem = Selenium::Remote::WebElement->new(
id => 1,
driver => $driver
);
my $new_remote_elem = Selenium::Remote::WebElement->new(
id => { ELEMENT => 1 },
driver => $driver
);
my $new_spec_elem = Selenium::Remote::WebElement->new(
id => { 'element-6066-11e4-a52e-4f735466cecf' => 1 },
driver => $driver
);
and then after instantiation, all three would give the following for
`id`:
print $elem->id; # prints 1
=item B<driver>
Required: Pass in a Selenium::Remote::Driver instance or one of its
subclasses. The WebElement needs the appropriate Driver session to
execute its commands properly.
=back
For typical usage of S::R::D and this module, none of this
matters and it should Just Work without you having to worry about it
at all. For further reading, the L<W3C
spec|https://www.w3.org/TR/webdriver/#elements> strictly dictates the
exact behavior.
=cut
has 'id' => (
is => 'ro',
required => 1,
coerce => sub {
my ($value) = @_;
if (ref($value) eq 'HASH') {
if (exists $value->{ELEMENT}) {
# The JSONWireProtocol web element object looks like
#
# { "ELEMENT": $INTEGER_ID }
return $value->{ELEMENT};
}
elsif (exists $value->{'element-6066-11e4-a52e-4f735466cecf'}) {
# but the WebDriver spec web element uses a magic
# string. See the spec for more information:
#
# https://www.w3.org/TR/webdriver/#elements
return $value->{'element-6066-11e4-a52e-4f735466cecf'};
}
else {
croak 'When passing in an object to the WebElement id attribute, it must have at least one of the ELEMENT or element-6066-11e4-a52e-4f735466cecf keys.';
}
}
else {
return $value;
}
}
);
has 'driver' => (
is => 'ro',
required => 1,
handles => [qw(_execute_command)],
);
=head1 FUNCTIONS
=head2 click
Description:
Click the element.
Usage:
$elem->click();
=cut
sub click {
my ($self) = @_;
my $res = { 'command' => 'clickElement', 'id' => $self->id };
return $self->_execute_command($res);
}
=head2 submit
Description:
Submit a FORM element. The submit command may also be applied to any element
that is a descendant of a FORM element.
Compatibility:
On webdriver3 enabled servers, this uses a JS shim, which WILL NOT submit correctly unless your element is an <input>.
Try clicking it if possible instead.
Usage:
$elem->submit();
=cut
sub submit {
my ($self) = @_;
if ($self->driver->{is_wd3} && !(grep { $self->driver->browser_name eq $_ } qw{chrome MicrosoftEdge})) {
if ($self->get_tag_name() ne 'form') {
return $self->driver->execute_script("return arguments[0].form.submit();",{'element-6066-11e4-a52e-4f735466cecf'=> $self->{id}} );
} else {
return $self->driver->execute_script("return arguments[0].submit();",{'element-6066-11e4-a52e-4f735466cecf'=> $self->{id}} );
}
}
my $res = { 'command' => 'submitElement', 'id' => $self->id };
return $self->_execute_command($res);
}
=head2 send_keys
Description:
Send a sequence of key strokes to an element. If you want to send specific
Keyboard events, then use the WDKeys module along with theis method. See e.g.
for reference
Input: 1
Required:
{ARRAY | STRING} - Array of strings or a string.
Usage:
$elem->send_keys('abcd', 'efg');
$elem->send_keys('hijk');
or
# include the WDKeys module
use Selenium::Remote::WDKeys;
.
.
$elem->send_keys(KEYS->{'space'}, KEYS->{'enter'});
=cut
sub send_keys {
my ( $self, @strings ) = @_;
croak "no keys to send" unless scalar @strings >= 1;
my $res = { 'command' => 'sendKeysToElement', 'id' => $self->id };
# We need to send an array of single characters to be WebDriver
# spec compatible. That is, for @strings = ('hel', 'lo'), the
# corresponding value must be ('h', 'e', 'l', 'l', 'o' ). This
# format conforms with the Spec AND works with the Selenium
# standalone server.
my $strings = join('', map { $_."" } @strings);
my $params = {
'value' => [ split('', $strings) ],
text => $strings,
};
return $self->_execute_command( $res, $params );
}
=head2 is_selected
Description:
Determine if an OPTION element, or an INPUT element of type checkbox or
radiobutton is currently selected.
Output:
BOOLEAN - whether the element is selected
Usage:
$elem->is_selected();
=cut
sub is_selected {
my ($self) = @_;
return $self->get_property('checked') if $self->driver->{is_wd3} && !(grep { $self->driver->browser_name eq $_ } qw{chrome MicrosoftEdge});
my $res = { 'command' => 'isElementSelected', 'id' => $self->id };
return $self->_execute_command($res);
}
=head2 set_selected
Description:
Select an OPTION element, or an INPUT element of type checkbox or radiobutton.
Forces selected=1 on the element..
Usage:
$elem->set_selected();
=cut
sub set_selected {
my ($self) = @_;
if ($self->driver->{is_wd3}) {
return if $self->is_selected();
return $self->click();
}
my $res = { 'command' => 'setElementSelected', 'id' => $self->id };
return $self->_execute_command($res);
}
=head2 toggle
Description:
Toggle whether an OPTION element, or an INPUT element of type checkbox or
radiobutton is currently selected.
Output:
BOOLEAN - Whether the element is selected after toggling its state.
Usage:
$elem->toggle();
=cut
sub toggle {
my ($self) = @_;
if ($self->driver->{is_wd3}) {
return $self->click() unless $self->is_selected();
return $self->driver->execute_script(qq/ if (arguments[0].checked) { arguments[0].checked = 0 }; return arguments[0].checked; /, {'element-6066-11e4-a52e-4f735466cecf'=> $self->{id}});
}
my $res = { 'command' => 'toggleElement', 'id' => $self->id };
return $self->_execute_command($res);
}
=head2 is_enabled
Description:
Determine if an element is currently enabled.
Output:
BOOLEAN - Whether the element is enabled.
Usage:
$elem->is_enabled();
=cut
sub is_enabled {
my ($self) = @_;
if ($self->driver->{is_wd3} && !(grep { $self->driver->browser_name eq $_ } qw{chrome MicrosoftEdge})) {
return 1 if $self->get_tag_name() ne 'input';
return $self->get_property('disabled') ? 0 : 1;
}
my $res = { 'command' => 'isElementEnabled', 'id' => $self->id };
return $self->_execute_command($res);
}
=head2 get_element_location
Description:
Determine an element's location on the page. The point (0, 0) refers to the
upper-left corner of the page.
Compatibility:
On WebDriver 3 enabled servers, this is an alias for get_element_rect().
Output:
HASH - The X and Y coordinates for the element on the page.
Usage:
$elem->get_element_location();
This method is DEPRECATED on webdriver3 enabled servers.
=cut
sub get_element_location {
my ($self) = @_;
if ($self->driver->{is_wd3} && !(grep { $self->driver->browser_name eq $_ } qw{chrome MicrosoftEdge})) {
my $data = $self->get_element_rect();
delete $data->{height};
delete $data->{width};
return $data;
}
my $res = { 'command' => 'getElementLocation', 'id' => $self->id };
return $self->_execute_command($res);
}
=head2 get_size
Description:
Determine an element's size in pixels. The size will be returned with width
and height properties.
Compatibility:
On WebDriver 3 enabled servers, this is an alias for get_element_rect().
Output:
HASH - The width and height of the element, in pixels.
Usage:
$elem->get_size();
This method is DEPRECATED on webdriver3 enabled servers.
=cut
sub get_size {
my ($self) = @_;
if ($self->driver->{is_wd3} && !(grep { $self->driver->browser_name eq $_ } qw{chrome MicrosoftEdge})) {
my $data = $self->get_element_rect();
delete $data->{x};
delete $data->{y};
return $data;
}
my $res = { 'command' => 'getElementSize', 'id' => $self->id };
return $self->_execute_command($res);
}
=head2 get_element_rect
Get the element's size AND location in a hash.
Example Output:
{ x => 0, y => 0, height => 10, width => 10 }
=cut
sub get_element_rect {
my ($self) = @_;
my $res = { 'command' => 'getElementRect', 'id' => $self->id };
return $self->_execute_command($res);
}
=head2 get_element_location_in_view
Description:
Determine an element's location on the screen once it has been scrolled
into view.
Note: This is considered an internal command and should only be used to
determine an element's location for correctly generating native events.
Compatibility:
On Webdriver3 servers, we have to implement this with a JS shim.
This means in some contexts, you won't get any position returned, as the element isn't considered an element internally.
You may have to go up the element stack to find the element that actually has the bounding box.
Output:
{x:number, y:number} The X and Y coordinates for the element on the page.
Usage:
$elem->get_element_location_in_view();
=cut
sub get_element_location_in_view {
my ($self) = @_;
#XXX chrome is dopey here
return $self->driver->execute_script(qq{
if (typeof(arguments[0]) !== 'undefined' && arguments[0].nodeType === Node.ELEMENT_NODE) {
arguments[0].scrollIntoView();
var pos = arguments[0].getBoundingClientRect();
return {y:pos.top,x:pos.left};
}
return {};
}, {'element-6066-11e4-a52e-4f735466cecf'=> $self->{id}} ) if $self->driver->{is_wd3} && grep { $self->driver->browser_name eq $_ } ('firefox','internet explorer');
my $res = { 'command' => 'getElementLocationInView', 'id' => $self->id };
return $self->_execute_command($res);
}
=head2 get_tag_name
Description:
Query for an element's tag name.
Output:
STRING - The element's tag name, as a lowercase string.
Usage:
$elem->get_tag_name();
=cut
sub get_tag_name {
my ($self) = @_;
my $res = { 'command' => 'getElementTagName', 'id' => $self->id };
return $self->_execute_command($res);
}
=head2 clear
Description:
Clear a TEXTAREA or text INPUT element's value.
Usage:
$elem->clear();
=cut
sub clear {
my ($self) = @_;
my $res = { 'command' => 'clearElement', 'id' => $self->id };
return $self->_execute_command($res);
}
=head2 get_attribute
Description:
Get the value of an element's attribute.
Compatibility:
In older webDriver, this actually got the value of an element's property.
If you want to get the initial condition (e.g. the values in the tag hardcoded in HTML), pass 1 as the second argument.
Or, set $driver->{emulate_jsonwire} = 0 to not have to pass the extra arg.
This can only done on WebDriver 3 enabled servers.
Input: 2
Required:
STRING - name of the attribute of the element
Optional:
BOOLEAN - "I really mean that I want the initial condition, quit being so compatible!!!"
Output:
{STRING | NULL} The value of the attribute, or null if it is not set on the element.
Usage:
$elem->get_attribute('name',1);
=cut
sub get_attribute {
my ( $self, $attr_name, $no_i_really_mean_it ) = @_;
if ( not defined $attr_name ) {
croak 'Attribute name not provided';
}
#Handle global JSONWire emulation flag
$no_i_really_mean_it = 1 unless $self->{driver}->{emulate_jsonwire};
return $self->get_property($attr_name) if $self->driver->{is_wd3} && !(grep { $self->driver->browser_name eq $_ } qw{chrome MicrosoftEdge}) && !$no_i_really_mean_it;
my $res = {
'command' => 'getElementAttribute',
'id' => $self->id,
'name' => $attr_name,
};
return $self->_execute_command($res);
}
=head2 get_property
Gets the C<Current Value> of an element's attribute.
Takes a named property as an argument.
Only available on WebDriver 3 enabled servers.
=cut
sub get_property {
my ($self,$prop) = @_;
return $self->get_attribute($prop) if $self->driver->{is_wd3} && (grep { $self->driver->browser_name eq $_ } qw{chrome MicrosoftEdge});
my $res = { 'command' => 'getElementProperty', id => $self->id, name => $prop };
return $self->_execute_command($res);
}
=head2 get_value
Description:
Query for the value of an element, as determined by its value attribute.
Output:
{STRING | NULL} The element's value, or null if it doesn't have a value attribute.
Usage:
$elem->get_value();
=cut
sub get_value {
my ($self) = @_;
return $self->get_attribute('value');
}
=head2 is_displayed
Description:
Determine if an element is currently displayed.
Note: This does *not* tell you an element's 'visibility' property; as it still takes up space in the DOM and is therefore considered 'displayed'.
Output:
BOOLEAN - Whether the element is displayed.
Usage:
$elem->is_displayed();
=cut
sub is_displayed {
my ($self) = @_;
if ($self->driver->{is_wd3} && !(grep { $self->driver->browser_name eq $_ } qw{chrome MicrosoftEdge})) {
return 0 if $self->get_tag_name() eq 'input' && $self->get_property('type') eq 'hidden'; #hidden type inputs
return int($self->get_css_attribute('display') ne 'none');
}
my $res = { 'command' => 'isElementDisplayed', 'id' => $self->id };
return $self->_execute_command($res);
}
=head2 is_hidden
Description:
Determine if an element is currently hidden.
Output:
BOOLEAN - Whether the element is hidden.
Usage:
$elem->is_hidden();
=cut
sub is_hidden {
my ($self) = @_;
return ! $self->is_displayed();
}
=head2 drag
Alias for Selenium::ActionChains::drag_and_drop().
Provide element you wish to drag to as argument.
my $target = $driver->find_element('receptacle','id');
my $subject = $driver->find_element('thingy','id');
$subject->drag($target);
=cut
sub drag {
my ($self,$target) = @_;
require Selenium::ActionChains;
my $chain = Selenium::ActionChians->new( driver => $self->driver );
return $chain->drag_and_drop($self,$target)->perform();
}
=head2 get_text
Description:
Get the innerText of the element.
Output:
STRING - innerText of an element
Usage:
$elem->get_text();
=cut
sub get_text {
my ($self) = @_;
my $res = { 'command' => 'getElementText', 'id' => $self->id };
return $self->_execute_command($res);
}
=head2 get_css_attribute
Description:
Query the value of an element's computed CSS property. The CSS property to
query should be specified using the CSS property name, not the JavaScript
property name (e.g. background-color instead of backgroundColor).
Input: 1
Required:
STRING - name of the css-attribute
Output:
STRING - Value of the css attribute
Usage:
$elem->get_css_attribute('background-color');
=cut
sub get_css_attribute {
my ( $self, $attr_name ) = @_;
if ( not defined $attr_name ) {
croak 'CSS attribute name not provided';
}
my $res = {
'command' => 'getElementValueOfCssProperty',
'id' => $self->id,
'property_name' => $attr_name,
};
return $self->_execute_command($res);
}
=head2 describe
Description:
Describe the identified element
Usage:
$elem->describe();
Note: DEPRECATED as of 2.42.2 -- use get_text, get_value, is_displayed, or
whatever appropriate WebElement function you need instead
Entirely unsupported on WebDriver 3 enabled servers.
=cut
sub describe {
my ($self) = @_;
my $res = { 'command' => 'describeElement', 'id' => $self->id };
return $self->_execute_command($res);
}
=head2 screenshot
Description:
Get a screenshot of the visible region that is a subset of the element's bounding box as a base64 encoded image.
Compatibility:
Only available on Webdriver3 enabled selenium servers.
Input (optional):
$scroll_into_view - BOOLEAN default true. If false, will not scroll the element into the viewport first.
Failing to do so may result in an image being cropped partially or entirely.
Output:
STRING - base64 encoded image
Usage:
print $element->screenshot();
To conveniently write the screenshot to a file, see L</capture_screenshot>.
=cut
sub screenshot {
my ($self, $scroll) = @_;
$scroll //= 1;
my $res = { 'command' => 'elementScreenshot', id => $self->id };
my $input = {scroll => int($scroll) };
return $self->_execute_command($res, $input);
}
=head2 capture_screenshot
Description:
Capture a screenshot of said element and save as a PNG to provided file name.
Compatibility:
Only available on Webdriver3 enabled selenium servers.
Input (optional):
$scroll_into_view - BOOLEAN default true. If false, will not scroll the element into the viewport first.
Failing to do so may result in an image being cropped partially or entirely.
Output:
TRUE - (Screenshot is written to file)
Usage:
$element->capture_screenshot($filename);
=cut
sub capture_screenshot {
my ( $self, $filename, $scroll ) = @_;
croak '$filename is required' unless $filename;
open( my $fh, '>', $filename );
binmode $fh;
print $fh MIME::Base64::decode_base64( $self->screenshot($scroll) );
CORE::close $fh;
return 1;
}
1;