ActionChains.pm 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. package Selenium::ActionChains;
  2. use strict;
  3. use warnings;
  4. # ABSTRACT: Action chains for Selenium::Remote::Driver
  5. use Moo;
  6. =for Pod::Coverage driver
  7. =cut
  8. has 'driver' => (
  9. is => 'ro',
  10. );
  11. has 'actions' => (
  12. is => 'lazy',
  13. builder => sub { [] },
  14. clearer => 1,
  15. );
  16. sub perform {
  17. my $self = shift;
  18. foreach my $action (@{$self->actions}) {
  19. $action->();
  20. }
  21. }
  22. sub click {
  23. my $self = shift;
  24. my $element = shift;
  25. if ($element) {
  26. $self->move_to_element($element);
  27. }
  28. # left click
  29. push @{$self->actions}, sub { $self->driver->click('LEFT') };
  30. $self;
  31. }
  32. sub click_and_hold {
  33. my $self = shift;
  34. my $element = shift;
  35. if ($element) {
  36. $self->move_to_element($element);
  37. }
  38. push @{$self->actions}, sub { $self->driver->button_down };
  39. $self;
  40. }
  41. sub context_click {
  42. my $self = shift;
  43. my $element = shift;
  44. if ($element) {
  45. $self->move_to_element($element);
  46. }
  47. # right click
  48. push @{$self->actions}, sub { $self->driver->click('RIGHT') };
  49. $self;
  50. }
  51. sub double_click {
  52. my $self = shift;
  53. my $element = shift;
  54. if ($element) {
  55. $self->move_to_element($element);
  56. }
  57. push @{$self->actions}, sub { $self->driver->double_click };
  58. $self;
  59. }
  60. sub release {
  61. my $self = shift;
  62. my $element = shift;
  63. if ($element) {
  64. $self->move_to_element($element);
  65. }
  66. push @{$self->actions}, sub { $self->driver->button_up };
  67. $self;
  68. }
  69. sub drag_and_drop {
  70. my $self = shift;
  71. my ($source,$target) = @_;
  72. $self->click_and_hold($source);
  73. $self->release($target);
  74. $self;
  75. }
  76. sub drag_and_drop_by_offset {
  77. my $self = shift;
  78. my ($source,$xoffset,$yoffset) = @_;
  79. $self->click_and_hold($source);
  80. $self->move_by_offset($xoffset,$yoffset);
  81. $self->release($source);
  82. $self;
  83. }
  84. sub move_to_element {
  85. my $self = shift;
  86. my $element = shift;
  87. push @{ $self->actions },
  88. sub { $self->driver->move_to( element => $element ) };
  89. $self;
  90. }
  91. sub move_by_offset {
  92. my $self = shift;
  93. my ( $xoffset, $yoffset ) = @_;
  94. push @{ $self->actions }, sub {
  95. $self->driver->move_to( xoffset => $xoffset, yoffset => $yoffset );
  96. };
  97. $self;
  98. }
  99. sub move_to_element_with_offset {
  100. my $self = shift;
  101. my ( $element, $xoffset, $yoffset ) = @_;
  102. push @{ $self->actions }, sub {
  103. $self->driver->move_to( element => $element, xoffset => $xoffset,
  104. yoffset => $yoffset );
  105. };
  106. $self;
  107. }
  108. sub key_down {
  109. my $self = shift;
  110. my ($value ,$element) = @_;
  111. if (defined($element)) {
  112. $self->click($element);
  113. }
  114. push @{ $self->actions }, sub { $self->driver->send_keys_to_active_element(@$value) };
  115. $self;
  116. }
  117. sub key_up {
  118. my $self = shift;
  119. my ($value ,$element) = @_;
  120. if (defined($element)) {
  121. $self->click($element);
  122. }
  123. push @{ $self->actions }, sub { $self->driver->send_keys_to_active_element(@$value) };
  124. $self;
  125. }
  126. sub send_keys {
  127. my $self = shift;
  128. my $keys = shift;
  129. push @{ $self->actions} , sub { $self->driver->get_active_element->send_keys($keys) };
  130. $self;
  131. }
  132. sub send_keys_to_element {
  133. my $self = shift;
  134. my ($element,$keys) = @_;
  135. push @{ $self->actions }, sub { $element->send_keys($keys) };
  136. $self;
  137. }
  138. 1;
  139. __END__
  140. =pod
  141. =head1 SYNOPSIS
  142. use Selenium::Remote::Driver;
  143. use Selenium::ActionChains;
  144. my $driver = Selenium::Remote::Driver->new;
  145. my $action_chains = Selenium::ActionChains->new(driver => $driver);
  146. $driver->get("http://www.some.web/site");
  147. my $elt_1 = $driver->find_element("//*[\@id='someid']");
  148. my $elt_2 = $driver->find_element("//*[\@id='someotherid']");
  149. $action_chains->send_keys_to_element($elt_1)->click($elt_2)->perform;
  150. =head1 DESCRIPTION
  151. This module implements ActionChains for Selenium, which is a way of automating
  152. low level interactions like mouse movements, mouse button actions , key presses and
  153. context menu interactions.
  154. 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>.
  155. =head1 DRAG AND DROP IS NOT WORKING !
  156. 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>.
  157. Nevertheless, we decided to implement the function, because eventually one day it will work.
  158. 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>.
  159. =head1 FUNCTIONS
  160. =head2 new
  161. Creates a new ActionChains object. Requires a Selenium::Remote::Driver as a mandatory parameter:
  162. my $driver = Selenium::Remote::Driver->new;
  163. my $action_chains = Selenium::ActionChains->new(driver => $driver);
  164. =head2 perform
  165. Performs all the actions stored in the ActionChains object in the order they were called:
  166. Args: None
  167. Usage:
  168. my $action_chains = Selenium::ActionChains->new(driver => $driver);
  169. # assuming that $some_element and $other_element are valid
  170. # Selenium::Remote::WebElement objects
  171. $action_chains->click($some_element);
  172. $action_chains->move_to_element($other_element);
  173. $action_chains->click($other_element);
  174. # click some_element, move to other_element, then click other_element
  175. $action_chains->perform;
  176. =head2 click
  177. Clicks an element. If none specified, clicks on current mouse position.
  178. Args: A Selenium::Remote::WebElement object
  179. Usage:
  180. my $element = $driver->find_element("//div[\@id='some_id']");
  181. $action_chains->click($element);
  182. =head2 click_and_hold
  183. Holds down the left mouse button on an element. If none specified, clicks on current
  184. mouse position.
  185. Args: A Selenium::Remote::WebElement object
  186. Usage:
  187. my $element = $driver->find_element("//div[\@id='some_id']");
  188. $action_chains->click_and_hold($element);
  189. =head2 context_click
  190. Right clicks an element. If none specified, right clicks on current mouse
  191. position.
  192. Args: A Selenium::Remote::WebElement object
  193. Usage:
  194. my $element = $driver->find_element("//div[\@id='some_id']");
  195. $action_chains->context_click($element);
  196. =head2 double_click
  197. Double clicks an element. If none specified, double clicks on current mouse
  198. position.
  199. Args: A Selenium::Remote::WebElement object
  200. Usage:
  201. my $element = $driver->find_element("//div[\@id='some_id']");
  202. $action_chains->double_click($element);
  203. =head2 drag_and_drop - NOT WORKING
  204. Holds down the left mouse button on the source element, then moves to the target
  205. element and releases the mouse button. IT IS NOT WORKING DUE TO CURRENT SELENIUM
  206. LIMITATIONS.
  207. Args:
  208. A source Selenium::Remote::WebElement object
  209. A target Selenium::Remote::WebElement object
  210. Usage:
  211. my $src_element = $driver->find_element("//*[\@class='foo']");
  212. my $tgt_element = $driver->find_element("//*[\@class='bar']");
  213. $action_chains->drag_and_drop($src_element,$tgt_element);
  214. =head2 drag_and_drop_by_offset - NOT WORKING
  215. Holds down the left mouse button on the source element, then moves to the offset
  216. specified and releases the mouse button. IT IS NOT WORKING DUE TO CURRENT SELENIUM
  217. LIMITATIONS.
  218. Args:
  219. A source Selenium::Remote::WebElement object
  220. An integer X offset
  221. An integer Y offset
  222. Usage:
  223. my $src_element = $driver->find_element("//*[\@class='foo']");
  224. my $xoffset = 10;
  225. my $yoffset = 10;
  226. $action_chains->drag_and_drop($src_element,$xoffset,$yoffset);
  227. =head2 key_down
  228. Sends key presses only, without releasing them.
  229. Should be used only with modifier keys (Control, Alt, Shift)
  230. Args:
  231. An array ref to keys to send. Use the KEY constant from Selenium::Remote::WDKeys
  232. The element to send keys to. If none, sends keys to the current focused element
  233. Usage:
  234. use Selenium::Remote::WDKeys 'KEYS';
  235. $action_chains->key_down( [ KEYS->{'alt'} ] );
  236. =head2 key_up
  237. Releases a mofifier key.
  238. Args:
  239. An array ref to keys to send. Use the KEY constant from Selenium::Remote::WDKeys
  240. The element to send keys to. If none, sends keys to the current focused element
  241. Usage:
  242. use Selenium::Remote::WDKeys 'KEYS';
  243. my $element = $driver->find_element('foo','id');
  244. $action_chains->key_up( [ KEYS->{'alt'} ],$element);
  245. =head2 move_by_offset
  246. Moves the mouse to an offset from current mouse position.
  247. Args:
  248. An integer X offset
  249. An integer Y offset
  250. Usage:
  251. $action_chains->move_by_offset(10,100);
  252. =head2 move_to_element
  253. Moves the mouse to the middle of an element
  254. Args:
  255. A Selenium::Remote::WebElement to move to
  256. Usage:
  257. my $element = $driver->find_element('foo','id');
  258. $action_chains->move_to_element($element);
  259. =head2 move_to_element_with_offset
  260. Moves the mouse by an offset of the specified element.
  261. Offsets are relative to the top-left corner of the element
  262. Args:
  263. A Selenium::Remote::WebElement
  264. An integer X offset
  265. An integer Y offset
  266. Usage:
  267. my $element = $driver->find_element('foo','id');
  268. $action_chains->move_to_element_with_offset($element,10,10);
  269. =head2 release
  270. Releases a held mouse_button
  271. Args:
  272. A Selenium::Remote::WebElement, the element to mouse up
  273. Usage:
  274. my $element = $driver->find_element('foo','id');
  275. $action_chains->release($element);
  276. =head2 send_keys
  277. Sends keys to the currently focused element
  278. Args:
  279. The keys to send
  280. Usage:
  281. $action_chains->send_keys('abcd');
  282. =head2 send_keys_to_element
  283. Sends keys to an element
  284. Args:
  285. A Selenium::Remote::WebElement
  286. The keys to send
  287. Usage:
  288. my $element = $driver->find_element('foo','id');
  289. $action_chains->send_keys_to_element($element,'abcd');
  290. =cut