Selenium/ActionChains.pm

filter: Statementstmt 10.7% 13 / 121 Branchbran 0.0% 0 / 26 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 9.6% 3 / 31 Podpod 100% 15 / 15 totaltotal 16.0% 31 / 193 SCAR 56.8
CC
19
Cov
9%
key_down
20.0
key_up
20.0
send_keys
12.0
CRAP 292.4
« Selenium/Remote/WebElement.pm ↑ Summary Selenium/Firefox/Binary.pm »
1 package Selenium::ActionChains;
2 1 $Selenium::ActionChains::VERSION = '1.49';
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 # ABSTRACT: Action chains for Selenium::Remote::Driver
7 1 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9  
10 1 has 'driver' => ( is => 'ro', );
11  
12 has 'actions' => (
13 is => 'lazy',
14 0 ▶ builder => sub { [] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
15 1 clearer => 1,
16 );
17  
18 sub perform {
19 0 ▶ my $self = shift;
Subroutine perform: not called CC=2 SCAR=17.9
Pod documented
20 0 foreach my $action ( @{ $self->actions } ) {
21 0 $action->();
22 }
23 }
24  
25 sub click {
26 0 ▶ my $self = shift;
Subroutine click: not called CC=2 SCAR=17.9
Pod documented
27 0 my $element = shift;
28 0 ▶ if ($element) {
Branch: 1 branch
BranchTrueFalseTotal
if ($element) 0 0 0
29 0 $self->move_to_element($element);
30 }
31  
32 # left click
33 0 ▶ push @{ $self->actions }, sub { $self->driver->click('LEFT') };
Subroutine __ANON__: not called CC=1 SCAR=6.9
34 0 $self;
35 }
36  
37 sub click_and_hold {
38 0 ▶ my $self = shift;
Subroutine click_and_hold: not called CC=2 SCAR=17.9
Pod documented
39 0 my $element = shift;
40 0 ▶ if ($element) {
Branch: 1 branch
BranchTrueFalseTotal
if ($element) 0 0 0
41 0 $self->move_to_element($element);
42 }
43 0 ▶ push @{ $self->actions }, sub { $self->driver->button_down };
Subroutine __ANON__: not called CC=1 SCAR=6.9
44 0 $self;
45 }
46  
47 sub context_click {
48 0 ▶ my $self = shift;
Subroutine context_click: not called CC=2 SCAR=17.9
Pod documented
49 0 my $element = shift;
50 0 ▶ if ($element) {
Branch: 1 branch
BranchTrueFalseTotal
if ($element) 0 0 0
51 0 $self->move_to_element($element);
52 }
53  
54 # right click
55 0 ▶ push @{ $self->actions }, sub { $self->driver->click('RIGHT') };
Subroutine __ANON__: not called CC=1 SCAR=6.9
56 0 $self;
57 }
58  
59 sub double_click {
60 0 ▶ my $self = shift;
Subroutine double_click: not called CC=2 SCAR=17.9
Pod documented
61 0 my $element = shift;
62 0 ▶ if ($element) {
Branch: 1 branch
BranchTrueFalseTotal
if ($element) 0 0 0
63 0 $self->move_to_element($element);
64 }
65 0 ▶ push @{ $self->actions }, sub { $self->driver->double_click };
Subroutine __ANON__: not called CC=1 SCAR=6.9
66 0 $self;
67 }
68  
69 sub release {
70 0 ▶ my $self = shift;
Subroutine release: not called CC=2 SCAR=17.9
Pod documented
71 0 my $element = shift;
72 0 ▶ if ($element) {
Branch: 1 branch
BranchTrueFalseTotal
if ($element) 0 0 0
73 0 $self->move_to_element($element);
74 }
75 0 ▶ push @{ $self->actions }, sub { $self->driver->button_up };
Subroutine __ANON__: not called CC=1 SCAR=6.9
76 0 $self;
77 }
78  
79 sub drag_and_drop {
80 0 ▶ my $self = shift;
Subroutine drag_and_drop: not called CC=1 SCAR=6.9
Pod documented
81 0 my ( $source, $target ) = @_;
82 0 $self->click_and_hold($source);
83 0 $self->release($target);
84 0 $self;
85 }
86  
87 sub drag_and_drop_by_offset {
88 0 ▶ my $self = shift;
Subroutine drag_and_drop_by_offset: not called CC=1 SCAR=6.9
Pod documented
89 0 my ( $source, $xoffset, $yoffset ) = @_;
90 0 $self->click_and_hold($source);
91 0 $self->move_by_offset( $xoffset, $yoffset );
92 0 $self->release($source);
93 0 $self;
94 }
95  
96 sub move_to_element {
97 0 ▶ my $self = shift;
Subroutine move_to_element: not called CC=1 SCAR=6.9
Pod documented
98 0 my $element = shift;
99 0 push @{ $self->actions },
100 0 ▶ sub { $self->driver->move_to( element => $element ) };
Subroutine __ANON__: not called CC=1 SCAR=6.9
101 0 $self;
102 }
103  
104 sub move_by_offset {
105 0 ▶ my $self = shift;
Subroutine move_by_offset: not called CC=1 SCAR=6.9
Pod documented
106 0 my ( $xoffset, $yoffset ) = @_;
107 0 push @{ $self->actions }, sub {
108 0 ▶ $self->driver->move_to( xoffset => $xoffset, yoffset => $yoffset );
Subroutine __ANON__: not called CC=1 SCAR=6.9
109 0 };
110 0 $self;
111 }
112  
113 sub move_to_element_with_offset {
114 0 ▶ my $self = shift;
Subroutine move_to_element_with_offset: not called CC=1 SCAR=6.9
Pod documented
115 0 my ( $element, $xoffset, $yoffset ) = @_;
116 0 push @{ $self->actions }, sub {
117 0 ▶ $self->driver->move_to(
Subroutine __ANON__: not called CC=1 SCAR=6.9
118 element => $element,
119 xoffset => $xoffset,
120 yoffset => $yoffset
121 );
122 0 };
123 0 $self;
124 }
125  
126 sub key_down {
127 0 ▶ my ( $self, $value, $element ) = @_;
Subroutine key_down: not called CC=4 SCAR=30.0
Pod documented
128  
129 #DWIM
130 0 ▶ $value = [$value] unless ref $value eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $value eq "ARRAY" 0 0 0
131  
132 0 ▶ $self->click($element) if defined $element;
Branch: 1 branch
BranchTrueFalseTotal
if defined $element 0 0 0
133 0 foreach my $v (@$value) {
134 0 push @{ $self->actions },
135 0 ▶ sub { $self->driver->general_action( actions => [ { type => 'key', id => 'key', actions => [ { type => 'keyDown', value => $v } ] } ] ) };
Subroutine __ANON__: not called CC=1 SCAR=6.9
136 }
137 0 return $self;
138 }
139  
140 sub key_up {
141 0 ▶ my ( $self, $value, $element ) = @_;
Subroutine key_up: not called CC=4 SCAR=30.0
Pod documented
142  
143 #DWIM
144 0 ▶ $value = [$value] unless ref $value eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $value eq "ARRAY" 0 0 0
145  
146 0 ▶ $self->click($element) if defined $element;
Branch: 1 branch
BranchTrueFalseTotal
if defined $element 0 0 0
147 0 foreach my $v (@$value) {
148 0 push @{ $self->actions },
149 0 ▶ sub { $self->driver->general_action( actions => [ { type => 'key', id => 'key', actions => [ { type => 'keyUp', value => $v } ] } ] ) };
Subroutine __ANON__: not called CC=1 SCAR=6.9
150 }
151 0 return $self;
152 }
153  
154 sub send_keys {
155 0 ▶ my ($self,$keys) =@_;
Subroutine send_keys: not called CC=3 SCAR=24.8
Pod documented
156  
157 # Do nothing if there are no keys to send
158 0 ▶ return unless $keys;
Branch: 1 branch
BranchTrueFalseTotal
unless $keys 0 0 0
159  
160 # DWIM
161 0 ▶ $keys = [split('',$keys)] unless ref $keys eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $keys eq "ARRAY" 0 0 0
162  
163 0 push @{ $self->actions },
164 sub {
165 0 ▶ foreach my $key (@$keys) {
Subroutine __ANON__: not called CC=2 SCAR=17.9
166 0 $self->key_down($key, $self->driver->get_active_element);
167 0 $self->key_up($key, $self->driver->get_active_element);
168 }
169 0 };
170 0 $self;
171 }
172  
173 sub send_keys_to_element {
174 0 ▶ my ($self, $element, $keys) =@_;
Subroutine send_keys_to_element: not called CC=3 SCAR=24.8
Pod documented
175  
176 # Do nothing if there are no keys to send
177 0 ▶ return unless $keys;
Branch: 1 branch
BranchTrueFalseTotal
unless $keys 0 0 0
178  
179 # DWIM
180 0 ▶ $keys = [split('',$keys)] unless ref $keys eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $keys eq "ARRAY" 0 0 0
181  
182 0 push @{ $self->actions },
183 sub {
184 0 ▶ foreach my $key (@$keys) {
Subroutine __ANON__: not called CC=2 SCAR=17.9
185 0 $self->key_down($key,$element);
186 0 $self->key_up($key,$element);
187 }
188 0 };
189 0 $self;
190 }
191  
192 1 1;
193  
194 __END__
195  
196 =pod
197  
198 =encoding UTF-8
199  
200 =head1 NAME
201  
202 Selenium::ActionChains - Action chains for Selenium::Remote::Driver
203  
204 =head1 VERSION
205  
206 version 1.49
207  
208 =head1 SYNOPSIS
209  
210 use Selenium::Remote::Driver;
211 use Selenium::ActionChains;
212  
213 my $driver = Selenium::Remote::Driver->new;
214 my $action_chains = Selenium::ActionChains->new(driver => $driver);
215  
216 $driver->get("http://www.some.web/site");
217 my $elt_1 = $driver->find_element("//*[\@id='someid']");
218 my $elt_2 = $driver->find_element("//*[\@id='someotherid']");
219 $action_chains->send_keys_to_element($elt_1)->click($elt_2)->perform;
220  
221 =head1 DESCRIPTION
222  
223 This module implements ActionChains for Selenium, which is a way of automating
224 low level interactions like mouse movements, mouse button actions , key presses and
225 context menu interactions.
226 The code was inspired by the L<Python implementation|http://selenium.googlecode.com/svn/trunk/docs/api/py/_modules/selenium/webdriver/common/action_chains.html#ActionChains>.
227  
228 =for Pod::Coverage driver
229  
230 =head1 DRAG AND DROP IS NOT WORKING !
231  
232 The implementation contains a drag_and_drop function, but due to Selenium limitations, it is L<not working|https://code.google.com/p/selenium/issues/detail?id=3604>.
233  
234 Nevertheless, we decided to implement the function, because eventually one day it will work.
235  
236 In the meantime, there are workarounds that can be used to simulate drag and drop, like L<this StackOverflow post|http://stackoverflow.com/questions/29381233/how-to-simulate-html5-drag-and-drop-in-selenium-webdriver-in-python>.
237  
238 =head1 FUNCTIONS
239  
240 =head2 new
241  
242 Creates a new ActionChains object. Requires a Selenium::Remote::Driver as a mandatory parameter:
243  
244 my $driver = Selenium::Remote::Driver->new;
245 my $action_chains = Selenium::ActionChains->new(driver => $driver);
246  
247 =head2 perform
248  
249 Performs all the actions stored in the ActionChains object in the order they were called:
250  
251 Args: None
252  
253 Usage:
254 my $action_chains = Selenium::ActionChains->new(driver => $driver);
255 # assuming that $some_element and $other_element are valid
256 # Selenium::Remote::WebElement objects
257 $action_chains->click($some_element);
258 $action_chains->move_to_element($other_element);
259 $action_chains->click($other_element);
260 # click some_element, move to other_element, then click other_element
261 $action_chains->perform;
262  
263 =head2 click
264  
265 Clicks an element. If none specified, clicks on current mouse position.
266  
267 Args: A Selenium::Remote::WebElement object
268  
269 Usage:
270 my $element = $driver->find_element("//div[\@id='some_id']");
271 $action_chains->click($element);
272  
273 =head2 click_and_hold
274  
275 Holds down the left mouse button on an element. If none specified, clicks on current
276 mouse position.
277  
278 Args: A Selenium::Remote::WebElement object
279  
280 Usage:
281 my $element = $driver->find_element("//div[\@id='some_id']");
282 $action_chains->click_and_hold($element);
283  
284 =head2 context_click
285  
286 Right clicks an element. If none specified, right clicks on current mouse
287 position.
288  
289 Args: A Selenium::Remote::WebElement object
290  
291 Usage:
292 my $element = $driver->find_element("//div[\@id='some_id']");
293 $action_chains->context_click($element);
294  
295 =head2 double_click
296  
297 Double clicks an element. If none specified, double clicks on current mouse
298 position.
299  
300 Args: A Selenium::Remote::WebElement object
301  
302 Usage:
303 my $element = $driver->find_element("//div[\@id='some_id']");
304 $action_chains->double_click($element);
305  
306 =head2 drag_and_drop - NOT WORKING
307  
308 Holds down the left mouse button on the source element, then moves to the target
309 element and releases the mouse button. IT IS NOT WORKING DUE TO CURRENT SELENIUM
310 LIMITATIONS.
311  
312 Args:
313 A source Selenium::Remote::WebElement object
314 A target Selenium::Remote::WebElement object
315  
316 Usage:
317 my $src_element = $driver->find_element("//*[\@class='foo']");
318 my $tgt_element = $driver->find_element("//*[\@class='bar']");
319 $action_chains->drag_and_drop($src_element,$tgt_element);
320  
321 =head2 drag_and_drop_by_offset - NOT WORKING
322  
323 Holds down the left mouse button on the source element, then moves to the offset
324 specified and releases the mouse button. IT IS NOT WORKING DUE TO CURRENT SELENIUM
325 LIMITATIONS.
326  
327 Args:
328 A source Selenium::Remote::WebElement object
329 An integer X offset
330 An integer Y offset
331  
332 Usage:
333 my $src_element = $driver->find_element("//*[\@class='foo']");
334 my $xoffset = 10;
335 my $yoffset = 10;
336 $action_chains->drag_and_drop($src_element,$xoffset,$yoffset);
337  
338 =head2 key_down
339  
340 Sends key presses only, without releasing them.
341 Useful when modifier keys are requried
342  
343 Will DWIM your input and accept either a string or ARRAYREF of keys.
344  
345 Args:
346 An array ref to keys to send. Use the KEY constant from Selenium::Remote::WDKeys
347 The element to send keys to. If none, sends keys to the current focused element
348  
349 Usage:
350 use Selenium::Remote::WDKeys 'KEYS';
351 # DEFINITELY cut and paste this in without looking
352 $action_chains->key_down( [ KEYS->{'alt'}, KEYS->{'F4'} ] );
353  
354 =head2 key_up
355  
356 Releases prior key presses.
357 Useful when modifier keys are requried
358  
359 Will DWIM your input and accept either a string or ARRAYREF of keys.
360  
361 Args:
362 An array ref to keys to send. Use the KEY constant from Selenium::Remote::WDKeys
363 The element to send keys to. If none, sends keys to the current focused element
364  
365 Usage:
366 use Selenium::Remote::WDKeys 'KEYS';
367 # Fullscreen the foo element
368 my $element = $driver->find_element('foo','id');
369 $action_chains->key_down( [ KEYS->{'alt'}, KEYS->{'enter'} ], $element );
370 $action_chains->key_up( [ KEYS->{'alt'}, KEYS->{'enter'} ], $element);
371  
372 =head2 move_by_offset
373  
374 Moves the mouse to an offset from current mouse position.
375  
376 Args:
377 An integer X offset
378 An integer Y offset
379  
380 Usage:
381 $action_chains->move_by_offset(10,100);
382  
383 =head2 move_to_element
384  
385 Moves the mouse to the middle of an element
386  
387 Args:
388 A Selenium::Remote::WebElement to move to
389  
390 Usage:
391 my $element = $driver->find_element('foo','id');
392 $action_chains->move_to_element($element);
393  
394 =head2 move_to_element_with_offset
395  
396 Moves the mouse by an offset of the specified element.
397 Offsets are relative to the top-left corner of the element
398  
399 Args:
400 A Selenium::Remote::WebElement
401 An integer X offset
402 An integer Y offset
403  
404 Usage:
405 my $element = $driver->find_element('foo','id');
406 $action_chains->move_to_element_with_offset($element,10,10);
407  
408 =head2 release
409  
410 Releases a held mouse_button
411  
412 Args:
413 A Selenium::Remote::WebElement, the element to mouse up
414  
415 Usage:
416 my $element = $driver->find_element('foo','id');
417 $action_chains->release($element);
418  
419 =head2 send_keys
420  
421 Sends keys to the currently focused element.
422 Essentially an alias around key_down then key_up.
423  
424 Will DWIM your input and accept either a string or ARRAYREF of keys.
425  
426 Args:
427 The keys to send
428  
429 Usage:
430 $action_chains->send_keys('abcd');
431  
432 =head2 send_keys_to_element
433  
434 Sends keys to an element in much the same fashion as send_keys.
435  
436 Will DWIM your input and accept either a string or ARRAYREF of keys.
437  
438 Args:
439 A Selenium::Remote::WebElement
440 The keys to send
441  
442 Usage:
443 my $element = $driver->find_element('foo','id');
444 $action_chains->send_keys_to_element($element,'abcd');
445  
446 =head1 AUTHORS
447  
448 Current Maintainers:
449  
450 =over 4
451  
452 =item *
453  
454 George S. Baugh <george@troglodyne.net>
455  
456 =back
457  
458 Previous maintainers:
459  
460 =over 4
461  
462 =item *
463  
464 Daniel Gempesaw <gempesaw@gmail.com>
465  
466 =item *
467  
468 Emmanuel Peroumalnaïk <peroumalnaik.emmanuel@gmail.com>
469  
470 =item *
471  
472 Luke Closs <cpan@5thplane.com>
473  
474 =item *
475  
476 Mark Stosberg <mark@stosberg.com>
477  
478 =back
479  
480 Original authors:
481  
482 =over 4
483  
484 =item *
485  
486 Aditya Ivaturi <ivaturi@gmail.com>
487  
488 =back
489  
490 =head1 COPYRIGHT AND LICENSE
491  
492 Copyright (c) 2010-2011 Aditya Ivaturi, Gordon Child
493  
494 Copyright (c) 2014-2017 Daniel Gempesaw
495  
496 Copyright (c) 2018-2021 George S. Baugh
497  
498 Licensed under the Apache License, Version 2.0 (the "License");
499 you may not use this file except in compliance with the License.
500 You may obtain a copy of the License at
501  
502 http://www.apache.org/licenses/LICENSE-2.0
503  
504 Unless required by applicable law or agreed to in writing, software
505 distributed under the License is distributed on an "AS IS" BASIS,
506 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
507 See the License for the specific language governing permissions and
508 limitations under the License.
509  
510 =cut
« Selenium/Remote/WebElement.pm ↑ Summary Selenium/Firefox/Binary.pm »