"Fossies" - the Fresh Open Source Software Archive

Member "otrs-5.0.40/Kernel/cpan-lib/Selenium/ActionChains.pm" (12 Dec 2019, 11859 Bytes) of package /linux/www/otrs-5.0.40.tar.bz2:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Perl source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "ActionChains.pm" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 5.0.39_vs_5.0.40.

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