1
0

ActionChains.pm 9.8 KB

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