Driver.pm 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640
  1. package Selenium::Remote::Driver;
  2. use strict;
  3. use warnings;
  4. use Carp;
  5. our @CARP_NOT;
  6. use Selenium::Remote::RemoteConnection;
  7. use Selenium::Remote::Commands;
  8. use Selenium::Remote::WebElement;
  9. use constant FINDERS => {
  10. class => 'class name',
  11. class_name => 'class name',
  12. css => 'css selector',
  13. id => 'id',
  14. link => 'link text',
  15. link_text => 'link text',
  16. name => 'name',
  17. partial_link_text => 'partial link text',
  18. tag_name => 'tag name',
  19. xpath => 'xpath',
  20. };
  21. our $VERSION = "0.12";
  22. =head1 NAME
  23. Selenium::Remote::Driver - Perl Client for Selenium Remote Driver
  24. =cut
  25. =head1 SYNOPSIS
  26. use Selenium::Remote::Driver;
  27. my $driver = new Selenium::Remote::Driver;
  28. $driver->get('http://www.google.com');
  29. print $driver->get_title();
  30. $driver->quit();
  31. =cut
  32. =head1 DESCRIPTION
  33. Selenium is a test tool that allows you to write
  34. automated web application UI tests in any programming language against
  35. any HTTP website using any mainstream JavaScript-enabled browser. This module is
  36. an implementation of the client for the Remote driver that Selenium provides.
  37. You can find bindings for other languages at this location:
  38. L<http://code.google.com/p/selenium/>
  39. This module sends commands directly to the Server using HTTP. Using this module
  40. together with the Selenium Server, you can automatically control any supported
  41. browser. To use this module, you need to have already downloaded and started
  42. the Selenium Server (Selenium Server is a Java application).
  43. =cut
  44. =head1 USAGE (read this first)
  45. =head2 Remote Driver Response
  46. Selenium::Remote::Driver uses the L<JsonWireProtocol|http://code.google.com/p/selenium/wiki/JsonWireProtocol> to communicate with the
  47. Selenium Server. If an error occurs while executing the command then the server
  48. sends back an HTTP error code with a JSON encoded reponse that indicates the
  49. precise L<Response Error Code|http://code.google.com/p/selenium/wiki/JsonWireProtocol#Response_Status_Codes>. The module will then croak with the error message
  50. associated with this code. If no error occurred, then the subroutine called will
  51. return the value sent back from the server (if a return value was sent).
  52. So a rule of thumb while invoking methods on the driver is if the method did not
  53. croak when called, then you can safely assume the command was successful even if
  54. nothing was returned by the method.
  55. =head2 WebElement
  56. Selenium Webdriver represents all the HTML elements as WebElement, which is
  57. in turn represented by Selenium::Remote::WebElement module. So any method that
  58. deals with WebElements will return and/or expect WebElement object. The POD for
  59. that module describes all the methods that perform various actions on the
  60. WebElements like click, submit etc.
  61. To interact with any WebElement you have to first "find" it, read the POD for
  62. find_element or find_elements for further info. Once you find the required element
  63. then you can perform various actions. If you don't call find_* method first, all
  64. your further actions will fail for that element. Finally, just remember that you
  65. don't have to instantiate WebElement objects at all - they will be automatically
  66. created when you use the find_* methods.
  67. =cut
  68. =head1 FUNCTIONS
  69. =cut
  70. =head2 new
  71. Description:
  72. Constructor for Driver. It'll instantiate the object if it can communicate
  73. with the Selenium RC server.
  74. Input: 7 (all optional)
  75. desired_capabilities - HASH - Following options are accepted:
  76. Optional:
  77. 'remote_server_addr' - <string> - IP or FQDN of the RC server machine
  78. 'browser_name' - <string> - desired browser string:
  79. {iphone|firefox|internet explorer|htmlunit|iphone|chrome}
  80. 'version' - <string> - desired browser version number
  81. 'platform' - <string> - desired platform:
  82. {WINDOWS|XP|VISTA|MAC|LINUX|UNIX|ANY}
  83. 'javascript' - <boolean> - whether javascript should be supported
  84. 'auto_close' - <boolean> - whether driver should end session on remote
  85. server on close.
  86. 'extra_capabilities' - HASH of extra capabilities
  87. If no values are provided, then these defaults will be assumed:
  88. 'remote_server_addr' => 'localhost'
  89. 'port' => '4444'
  90. 'browser_name' => 'firefox'
  91. 'version' => ''
  92. 'platform' => 'ANY'
  93. 'javascript' => 1
  94. 'auto_close' => 1
  95. Output:
  96. Remote Driver object
  97. Usage:
  98. my $driver = new Selenium::Remote::Driver;
  99. or
  100. my $driver = new Selenium::Remote::Driver('browser_name' => 'firefox',
  101. 'platform' => 'MAC');
  102. or
  103. my $driver = new Selenium::Remote::Driver('remote_server_addr' => '10.10.1.1',
  104. 'port' => '2222',
  105. auto_close => 0
  106. );
  107. or
  108. my $driver = new Selenium::Remote::Driver('browser_name' => 'chrome',
  109. 'platform' => 'VISTA',
  110. 'extra_capabilities' => {'chrome.switches' => ["--user-data-dir=$ENV{LOCALAPPDATA}\\Google\\Chrome\\User Data"],},
  111. );
  112. =cut
  113. sub new {
  114. my ( $class, %args ) = @_;
  115. my $ress = new Selenium::Remote::Commands;
  116. # Set the defaults if user doesn't send any
  117. my $self = {
  118. remote_server_addr => delete $args{remote_server_addr} || 'localhost',
  119. browser_name => delete $args{browser_name} || 'firefox',
  120. platform => delete $args{platform} || 'ANY',
  121. port => delete $args{port} || '4444',
  122. version => delete $args{version} || '',
  123. session_id => undef,
  124. remote_conn => undef,
  125. commands => $ress,
  126. auto_close => 1, # by default we will close remote session on DESTROY
  127. pid => $$,
  128. };
  129. bless $self, $class or die "Can't bless $class: $!";
  130. if ( defined $args{javascript} ) {
  131. if ( $args{javascript} ) {
  132. $self->{javascript} = JSON::true;
  133. }
  134. else {
  135. $self->{javascript} = JSON::false;
  136. }
  137. }
  138. else {
  139. $self->{javascript} = JSON::true;
  140. }
  141. # Connect to remote server & establish a new session
  142. $self->{remote_conn} =
  143. new Selenium::Remote::RemoteConnection( $self->{remote_server_addr},
  144. $self->{port} );
  145. $self->new_session(delete $args{extra_capabilities});
  146. if ( !( defined $self->{session_id} ) ) {
  147. croak "Could not establish a session with the remote server\n";
  148. }
  149. return $self;
  150. }
  151. sub DESTROY {
  152. my ($self) = @_;
  153. return if $$ != $self->{pid};
  154. $self->quit() if ($self->{auto_close} && defined $self->{session_id});
  155. }
  156. # This is an internal method used the Driver & is not supposed to be used by
  157. # end user. This method is used by Driver to set up all the parameters
  158. # (url & JSON), send commands & receive processed response from the server.
  159. sub _execute_command {
  160. my ( $self, $res, $params ) = @_;
  161. $res->{'session_id'} = $self->{'session_id'};
  162. my $resource = $self->{commands}->get_params($res);
  163. if ($resource) {
  164. my $resp = $self->{remote_conn}
  165. ->request( $resource->{'method'}, $resource->{'url'}, $params );
  166. if(ref($resp) eq 'HASH') {
  167. if($resp->{cmd_status} eq 'OK') {
  168. return $resp->{cmd_return};
  169. } else {
  170. my $msg = "Error while executing command";
  171. if($resp->{cmd_error}) {
  172. $msg .= ": $resp->{cmd_error}" if $resp->{cmd_error};
  173. } else {
  174. if(ref($resp->{cmd_return}) eq 'HASH') {
  175. $msg .= ": $resp->{cmd_return}->{error}->{msg}"
  176. if $resp->{cmd_return}->{error}->{msg};
  177. } else {
  178. $msg .= ": $resp->{cmd_return}";
  179. }
  180. }
  181. croak $msg;
  182. }
  183. }
  184. return $resp;
  185. }
  186. else {
  187. croak "Couldn't retrieve command settings properly\n";
  188. }
  189. }
  190. # A method that is used by the Driver itself. It'll be called to set the
  191. # desired capabilities on the server.
  192. sub new_session {
  193. my ($self, $extra_capabilities) = @_;
  194. $extra_capabilities ||= {};
  195. my $args = {
  196. 'desiredCapabilities' => {
  197. 'browserName' => $self->{browser_name},
  198. 'platform' => $self->{platform},
  199. 'javascriptEnabled' => $self->{javascript},
  200. 'version' => $self->{version},
  201. %$extra_capabilities,
  202. },
  203. };
  204. my $resp =
  205. $self->{remote_conn}
  206. ->request( $self->{commands}->{'newSession'}->{'method'},
  207. $self->{commands}->{'newSession'}->{'url'}, $args, );
  208. if ( ( defined $resp->{'sessionId'} ) && $resp->{'sessionId'} ne '' ) {
  209. $self->{session_id} = $resp->{'sessionId'};
  210. }
  211. else {
  212. croak "Could not create new session";
  213. }
  214. }
  215. =head2 status
  216. Description:
  217. Query the server's current status. All server implementations
  218. should return two basic objects describing the server's current
  219. platform and when the server was built.
  220. Output:
  221. Hash ref
  222. Usage:
  223. print Dumper $driver->status;
  224. =cut
  225. sub status {
  226. my ($self) = @_;
  227. my $res = { 'command' => 'status' };
  228. return $self->_execute_command($res);
  229. }
  230. =head2 get_alert_text
  231. Description:
  232. Gets the text of the currently displayed JavaScript alert(), confirm()
  233. or prompt() dialog.
  234. Example
  235. my $string = $driver->get_alert_text;
  236. =cut
  237. sub get_alert_text {
  238. my ($self) = @_;
  239. my $res = { 'command' => 'getAlertText' };
  240. return $self->_execute_command($res);
  241. }
  242. =head2 send_keys_to_alert
  243. Synonymous with send_keys_to_prompt
  244. =cut
  245. sub send_keys_to_alert {
  246. return shift->send_keys_to_prompt(@_);
  247. }
  248. =head2 send_keys_to_prompt
  249. Description:
  250. Sends keystrokes to a JavaScript prompt() dialog.
  251. Input:
  252. {string} keys to send
  253. Example:
  254. $driver->send_keys_to_prompt('hello world');
  255. or
  256. ok($driver->get_alert_text eq 'Please Input your name','prompt appears');
  257. $driver->send_keys_to_alert("Larry Wall");
  258. $driver->accept_alert;
  259. =cut
  260. sub send_keys_to_prompt {
  261. my ($self,$keys) = @_;
  262. my $res = { 'command' => 'sendKeysToPrompt' };
  263. my $params = { 'text' => $keys };
  264. return $self->_execute_command($res,$params);
  265. }
  266. =head2 accept_alert
  267. Description:
  268. Accepts the currently displayed alert dialog. Usually, this is
  269. equivalent to clicking the 'OK' button in the dialog.
  270. Example:
  271. $driver->accept_alert;
  272. =cut
  273. sub accept_alert {
  274. my ($self) = @_;
  275. my $res = { 'command' => 'acceptAlert' };
  276. return $self->_execute_command($res);
  277. }
  278. =head2 dismiss_alert
  279. Description:
  280. Dismisses the currently displayed alert dialog. For comfirm()
  281. and prompt() dialogs, this is equivalent to clicking the
  282. 'Cancel' button. For alert() dialogs, this is equivalent to
  283. clicking the 'OK' button.
  284. Example:
  285. $driver->dismiss_alert;
  286. =cut
  287. sub dismiss_alert {
  288. my ($self) = @_;
  289. my $res = { 'command' => 'dismissAlert' };
  290. return $self->_execute_command($res);
  291. }
  292. =head2 mouse_move_to_location
  293. Description:
  294. Move the mouse by an offset of the specificed element. If no
  295. element is specified, the move is relative to the current mouse
  296. cursor. If an element is provided but no offset, the mouse will be
  297. moved to the center of the element. If the element is not visible,
  298. it will be scrolled into view.
  299. Output:
  300. STRING -
  301. Usage:
  302. # element - the element to move to. If not specified or is null, the offset is relative to current position of the mouse.
  303. # xoffset - X offset to move to, relative to the top-left corner of the element. If not specified, the mouse will move to the middle of the element.
  304. # yoffset - Y offset to move to, relative to the top-left corner of the element. If not specified, the mouse will move to the middle of the element.
  305. print $driver->mouse_move_to_location(element => e, xoffset => x, yoffset => y);
  306. =cut
  307. sub mouse_move_to_location {
  308. my ($self, %params) = @_;
  309. $params{element} = $params{element}{id} if exists $params{element};
  310. my $res = { 'command' => 'mouseMoveToLocation' };
  311. return $self->_execute_command($res, \%params);
  312. }
  313. =head2 move_to
  314. Synonymous with mouse_move_to_location
  315. =cut
  316. sub move_to {
  317. return shift->mouse_move_to_location(@_);
  318. }
  319. =head2 get_capabilities
  320. Description:
  321. Retrieve the capabilities of the specified session.
  322. Output:
  323. HASH of all the capabilities.
  324. Usage:
  325. my $capab = $driver->get_capabilities();
  326. print Dumper($capab);
  327. =cut
  328. sub get_capabilities {
  329. my $self = shift;
  330. my $res = {'command' => 'getCapabilities'};
  331. return $self->_execute_command($res);
  332. }
  333. =head2 set_implicit_wait_timeout
  334. Description:
  335. Set the amount of time the driver should wait when searching for elements.
  336. When searching for a single element, the driver will poll the page until
  337. an element is found or the timeout expires, whichever occurs first.
  338. When searching for multiple elements, the driver should poll the page until
  339. at least one element is found or the timeout expires, at which point it
  340. will return an empty list. If this method is never called, the driver will
  341. default to an implicit wait of 0ms.
  342. Input:
  343. Time in milliseconds.
  344. Output:
  345. Server Response Hash with no data returned back from the server.
  346. Usage:
  347. $driver->set_implicit_wait_timeout(10);
  348. =cut
  349. sub set_implicit_wait_timeout {
  350. my ($self, $ms) = @_;
  351. my $res = {'command' => 'setImplicitWaitTimeout'};
  352. my $params = {'ms' => $ms};
  353. return $self->_execute_command($res, $params);
  354. }
  355. =head2 close
  356. Description:
  357. Close the current window.
  358. Usage:
  359. $driver->close();
  360. or
  361. #close a popup window
  362. my $handles = $driver->get_window_handles;
  363. $driver->switch_to_window($handles->[1]);
  364. $driver->close();
  365. $driver->switch_to_window($handles->[0]);
  366. =cut
  367. sub close {
  368. my $self = shift;
  369. my $res = { 'command' => 'close' };
  370. $self->_execute_command($res);
  371. }
  372. =head2 quit
  373. Description:
  374. Delete the session & close open browsers.
  375. Usage:
  376. $driver->quit();
  377. =cut
  378. sub quit {
  379. my $self = shift;
  380. my $res = { 'command' => 'quit' };
  381. $self->_execute_command($res);
  382. $self->{session_id} = undef;
  383. }
  384. =head2 get_current_window_handle
  385. Description:
  386. Retrieve the current window handle.
  387. Output:
  388. STRING - the window handle
  389. Usage:
  390. print $driver->get_current_window_handle();
  391. =cut
  392. sub get_current_window_handle {
  393. my $self = shift;
  394. my $res = { 'command' => 'getCurrentWindowHandle' };
  395. return $self->_execute_command($res);
  396. }
  397. =head2 get_window_handles
  398. Description:
  399. Retrieve the list of window handles used in the session.
  400. Output:
  401. ARRAY of STRING - list of the window handles
  402. Usage:
  403. print Dumper $driver->get_window_handles;
  404. or
  405. # get popup, close, then back
  406. my $handles = $driver->get_window_handles;
  407. $driver->switch_to_window($handles->[1]);
  408. $driver->close;
  409. $driver->switch_to_window($handles->[0]);
  410. =cut
  411. sub get_window_handles {
  412. my $self = shift;
  413. my $res = { 'command' => 'getWindowHandles' };
  414. return $self->_execute_command($res);
  415. }
  416. =head2 get_window_size
  417. Description:
  418. Retrieve the window size
  419. Input:
  420. STRING - <optional> - window handle (default is 'current' window)
  421. Output:
  422. HASH - containing keys 'height' & 'width'
  423. Usage:
  424. my $window_size = $driver->get_window_size();
  425. print $window_size->{'height'}, $window_size->('width');
  426. =cut
  427. sub get_window_size {
  428. my ( $self, $window ) = @_;
  429. $window = (defined $window)?$window:'current';
  430. my $res = { 'command' => 'getWindowSize', 'window_handle' => $window };
  431. return $self->_execute_command($res);
  432. }
  433. =head2 get_window_position
  434. Description:
  435. Retrieve the window position
  436. Input:
  437. STRING - <optional> - window handle (default is 'current' window)
  438. Output:
  439. HASH - containing keys 'x' & 'y'
  440. Usage:
  441. my $window_size = $driver->get_window_position();
  442. print $window_size->{'x'}, $window_size->('y');
  443. =cut
  444. sub get_window_position {
  445. my ( $self, $window ) = @_;
  446. $window = (defined $window)?$window:'current';
  447. my $res = { 'command' => 'getWindowPosition', 'window_handle' => $window };
  448. return $self->_execute_command($res);
  449. }
  450. =head2 get_current_url
  451. Description:
  452. Retrieve the url of the current page
  453. Output:
  454. STRING - url
  455. Usage:
  456. print $driver->get_current_url();
  457. =cut
  458. sub get_current_url {
  459. my $self = shift;
  460. my $res = { 'command' => 'getCurrentUrl' };
  461. return $self->_execute_command($res);
  462. }
  463. =head2 navigate
  464. Description:
  465. Navigate to a given url. This is same as get() method.
  466. Input:
  467. STRING - url
  468. Usage:
  469. $driver->navigate('http://www.google.com');
  470. =cut
  471. sub navigate {
  472. my ( $self, $url ) = @_;
  473. $self->get($url);
  474. }
  475. =head2 get
  476. Description:
  477. Navigate to a given url
  478. Input:
  479. STRING - url
  480. Usage:
  481. $driver->get('http://www.google.com');
  482. =cut
  483. sub get {
  484. my ( $self, $url ) = @_;
  485. my $res = { 'command' => 'get' };
  486. my $params = { 'url' => $url };
  487. return $self->_execute_command( $res, $params );
  488. }
  489. =head2 get_title
  490. Description:
  491. Get the current page title
  492. Output:
  493. STRING - Page title
  494. Usage:
  495. print $driver->get_title();
  496. =cut
  497. sub get_title {
  498. my $self = shift;
  499. my $res = { 'command' => 'getTitle' };
  500. return $self->_execute_command($res);
  501. }
  502. =head2 go_back
  503. Description:
  504. Equivalent to hitting the back button on the browser.
  505. Usage:
  506. $driver->go_back();
  507. =cut
  508. sub go_back {
  509. my $self = shift;
  510. my $res = { 'command' => 'goBack' };
  511. return $self->_execute_command($res);
  512. }
  513. =head2 go_forward
  514. Description:
  515. Equivalent to hitting the forward button on the browser.
  516. Usage:
  517. $driver->go_forward();
  518. =cut
  519. sub go_forward {
  520. my $self = shift;
  521. my $res = { 'command' => 'goForward' };
  522. return $self->_execute_command($res);
  523. }
  524. =head2 refresh
  525. Description:
  526. Reload the current page.
  527. Usage:
  528. $driver->refresh();
  529. =cut
  530. sub refresh {
  531. my $self = shift;
  532. my $res = { 'command' => 'refresh' };
  533. return $self->_execute_command($res);
  534. }
  535. =head2 javascript
  536. Description:
  537. returns true if javascript is enabled in the driver.
  538. Usage:
  539. if ($driver->javascript) { ...; }
  540. =cut
  541. sub javascript {
  542. my $self = shift;
  543. return $self->{javascript} == JSON::true;
  544. }
  545. =head2 execute_async_script
  546. Description:
  547. Inject a snippet of JavaScript into the page for execution in the context
  548. of the currently selected frame. The executed script is assumed to be
  549. asynchronous and must signal that is done by invoking the provided
  550. callback, which is always provided as the final argument to the function.
  551. The value to this callback will be returned to the client.
  552. Asynchronous script commands may not span page loads. If an unload event
  553. is fired while waiting for a script result, an error should be returned
  554. to the client.
  555. Input: 2 (1 optional)
  556. Required:
  557. STRING - Javascript to execute on the page
  558. Optional:
  559. ARRAY - list of arguments that need to be passed to the script.
  560. Output:
  561. {*} - Varied, depending on the type of result expected back from the script.
  562. Usage:
  563. my $script = q{
  564. var arg1 = arguments[0];
  565. var callback = arguments[arguments.length-1];
  566. var elem = window.document.findElementById(arg1);
  567. callback(elem);
  568. };
  569. my $callback = q{return arguments[0];};
  570. my $elem = $driver->execute_async_script($script,'myid',$callback);
  571. $elem->click;
  572. =cut
  573. sub execute_async_script {
  574. my ( $self, $script, @args ) = @_;
  575. if ($self->javascript) {
  576. if ( not defined $script ) {
  577. return 'No script provided';
  578. }
  579. my $res = { 'command' => 'executeAsyncScript' };
  580. # Check the args array if the elem obj is provided & replace it with
  581. # JSON representation
  582. for (my $i=0; $i<@args; $i++) {
  583. if (ref $args[$i] eq 'Selenium::Remote::WebElement') {
  584. $args[$i] = {'ELEMENT' => ($args[$i])->{id}};
  585. }
  586. }
  587. my $params = {'script' => $script, 'args' => \@args};
  588. my $ret = $self->_execute_command($res, $params);
  589. # replace any ELEMENTS with WebElement
  590. if (ref($ret) and (ref($ret) eq 'HASH') and exists $ret->{'ELEMENT'}) {
  591. $ret =
  592. new Selenium::Remote::WebElement(
  593. $ret->{ELEMENT}, $self);
  594. }
  595. return $ret;
  596. }
  597. else {
  598. croak 'Javascript is not enabled on remote driver instance.';
  599. }
  600. }
  601. =head2 execute_script
  602. Description:
  603. Inject a snippet of JavaScript into the page and return its result.
  604. WebElements that should be passed to the script as an argument should be
  605. specified in the arguments array as WebElement object. Likewise,
  606. any WebElements in the script result will be returned as WebElement object.
  607. Input: 2 (1 optional)
  608. Required:
  609. STRING - Javascript to execute on the page
  610. Optional:
  611. ARRAY - list of arguments that need to be passed to the script.
  612. Output:
  613. {*} - Varied, depending on the type of result expected back from the script.
  614. Usage:
  615. my $script = q{
  616. var arg1 = arguments[0];
  617. var elem = window.document.findElementById(arg1);
  618. return elem;
  619. };
  620. my $elem = $driver->execute_script($script,'myid');
  621. $elem->click;
  622. =cut
  623. sub execute_script {
  624. my ( $self, $script, @args ) = @_;
  625. if ($self->javascript) {
  626. if ( not defined $script ) {
  627. return 'No script provided';
  628. }
  629. my $res = { 'command' => 'executeScript' };
  630. # Check the args array if the elem obj is provided & replace it with
  631. # JSON representation
  632. for (my $i=0; $i<@args; $i++) {
  633. if (ref $args[$i] eq 'Selenium::Remote::WebElement') {
  634. $args[$i] = {'ELEMENT' => ($args[$i])->{id}};
  635. }
  636. }
  637. my $params = {'script' => $script, 'args' => [@args]};
  638. my $ret = $self->_execute_command($res, $params);
  639. # replace any ELEMENTS with WebElement
  640. if (ref($ret) and (ref($ret) eq 'HASH') and exists $ret->{'ELEMENT'}) {
  641. $ret =
  642. new Selenium::Remote::WebElement(
  643. $ret->{ELEMENT}, $self);
  644. }
  645. return $ret;
  646. }
  647. else {
  648. croak 'Javascript is not enabled on remote driver instance.';
  649. }
  650. }
  651. =head2 screenshot
  652. Description:
  653. Get a screenshot of the current page as a base64 encoded image.
  654. Output:
  655. STRING - base64 encoded image
  656. Usage:
  657. print $driver->screenshot();
  658. or
  659. require MIME::Base64;
  660. open(FH,'>','screenshot.png');
  661. binmode FH;
  662. my $png_base64 = $driver->screenshot();
  663. print FH MIME::Base64::decode_base64($png_base64);
  664. close FH;
  665. =cut
  666. sub screenshot {
  667. my ($self) = @_;
  668. my $res = { 'command' => 'screenshot' };
  669. return $self->_execute_command($res);
  670. }
  671. =head2 available_engines
  672. Description:
  673. List all available engines on the machine. To use an engine, it has to be present in this list.
  674. Output:
  675. {Array.<string>} A list of available engines
  676. Usage:
  677. print Dumper $driver->available_engines;
  678. =cut
  679. sub available_engines {
  680. my ($self) = @_;
  681. my $res = { 'command' => 'availableEngines' };
  682. return $self->_execute_command($res);
  683. }
  684. =head2 switch_to_frame
  685. Description:
  686. Change focus to another frame on the page. If the frame ID is null, the
  687. server will switch to the page's default content.
  688. Input: 1
  689. Required:
  690. {STRING | NUMBER | NULL} - ID of the frame which can be one of the three
  691. mentioned.
  692. Usage:
  693. $driver->switch_to_frame('frame_1');
  694. =cut
  695. sub switch_to_frame {
  696. my ( $self, $id ) = @_;
  697. my $json_null = JSON::null;
  698. $id = ( defined $id ) ? $id : $json_null;
  699. my $res = { 'command' => 'switchToFrame' };
  700. my $params = { 'id' => $id };
  701. return $self->_execute_command( $res, $params );
  702. }
  703. =head2 switch_to_window
  704. Description:
  705. Change focus to another window. The window to change focus to may be
  706. specified by its server assigned window handle, or by the value of its name
  707. attribute.
  708. Input: 1
  709. Required:
  710. STRING - Window handle or the Window name
  711. Usage:
  712. $driver->switch_to_window('MY Homepage');
  713. or
  714. # close a popup window and switch back
  715. my $handles = $driver->get_window_handles;
  716. $driver->switch_to_window($handles->[1]);
  717. $driver->close;
  718. $driver->switch_to_window($handles->[0]);
  719. =cut
  720. sub switch_to_window {
  721. my ( $self, $name ) = @_;
  722. if ( not defined $name ) {
  723. return 'Window name not provided';
  724. }
  725. my $res = { 'command' => 'switchToWindow' };
  726. my $params = { 'name' => $name };
  727. return $self->_execute_command( $res, $params );
  728. }
  729. =head2 get_speed
  730. Description:
  731. Get the current user input speed. The actual input speed is still browser
  732. specific and not covered by the Driver.
  733. Output:
  734. STRING - One of these: SLOW, MEDIUM, FAST
  735. Usage:
  736. print $driver->get_speed();
  737. =cut
  738. sub get_speed {
  739. my ($self) = @_;
  740. my $res = { 'command' => 'getSpeed' };
  741. return $self->_execute_command($res);
  742. }
  743. =head2 set_speed
  744. Description:
  745. Set the user input speed.
  746. Input:
  747. STRING - One of these: SLOW, MEDIUM, FAST
  748. Usage:
  749. $driver->set_speed('MEDIUM');
  750. Note: This function is a no-op in WebDriver (?). See
  751. https://groups.google.com/d/topic/selenium-users/oX0ZnYFPuSA/discussion and
  752. http://code.google.com/p/selenium/source/browse/trunk/java/client/src/org/openqa/selenium/WebDriverCommandProcessor.java
  753. =cut
  754. sub set_speed {
  755. my ( $self, $speed ) = @_;
  756. if ( not defined $speed ) {
  757. return 'Speed not provided.';
  758. }
  759. my $res = { 'command' => 'setSpeed' };
  760. my $params = { 'speed' => $speed };
  761. return $self->_execute_command( $res, $params );
  762. }
  763. =head2 set_window_position
  764. Description:
  765. Set the position (on screen) where you want your browser to be displayed.
  766. Input:
  767. INT - x co-ordinate
  768. INT - y co-ordinate
  769. STRING - <optional> - window handle (default is 'current' window)
  770. Usage:
  771. $driver->set_window_position(50, 50);
  772. =cut
  773. sub set_window_position {
  774. my ( $self, $x, $y, $window ) = @_;
  775. $window = (defined $window)?$window:'current';
  776. if (not defined $x and not defined $y)
  777. {
  778. return "X & Y co-ordinates are required";
  779. }
  780. my $res = { 'command' => 'setWindowPosition', 'window_handle' => $window };
  781. my $params = { 'x' => $x, 'y' => $y };
  782. return $self->_execute_command($res, $params);
  783. }
  784. =head2 set_window_size
  785. Description:
  786. Set the size of the browser window
  787. Input:
  788. INT - height of the window
  789. INT - width of the window
  790. STRING - <optional> - window handle (default is 'current' window)
  791. Usage:
  792. $driver->set_window_size(640, 480);
  793. =cut
  794. sub set_window_size {
  795. my ( $self, $height, $width, $window ) = @_;
  796. $window = (defined $window)?$window:'current';
  797. if (not defined $height and not defined $width)
  798. {
  799. return "height & width of browser are required";
  800. }
  801. my $res = { 'command' => 'setWindowSize', 'window_handle' => $window };
  802. my $params = { 'height' => $height, 'width' => $width };
  803. return $self->_execute_command($res, $params);
  804. }
  805. =head2 get_all_cookies
  806. Description:
  807. Retrieve all cookies visible to the current page. Each cookie will be
  808. returned as a HASH reference with the following keys & their value types:
  809. 'name' - STRING
  810. 'value' - STRING
  811. 'path' - STRING
  812. 'domain' - STRING
  813. 'secure' - BOOLEAN
  814. Output:
  815. ARRAY of HASHES - list of all the cookie hashes
  816. Usage:
  817. print Dumper($driver->get_all_cookies());
  818. =cut
  819. sub get_all_cookies {
  820. my ($self) = @_;
  821. my $res = { 'command' => 'getAllCookies' };
  822. return $self->_execute_command($res);
  823. }
  824. =head2 add_cookie
  825. Description:
  826. Set a cookie on the domain.
  827. Input: 5 (1 optional)
  828. Required:
  829. 'name' - STRING
  830. 'value' - STRING
  831. 'path' - STRING
  832. 'domain' - STRING
  833. Optional:
  834. 'secure' - BOOLEAN - default is false.
  835. Usage:
  836. $driver->add_cookie('foo', 'bar', '/', '.google.com', 0)
  837. =cut
  838. sub add_cookie {
  839. my ( $self, $name, $value, $path, $domain, $secure ) = @_;
  840. if ( ( not defined $name )
  841. || ( not defined $value )
  842. || ( not defined $path )
  843. || ( not defined $domain ) )
  844. {
  845. return "Missing parameters";
  846. }
  847. my $res = { 'command' => 'addCookie' };
  848. my $json_false = JSON::false;
  849. my $json_true = JSON::true;
  850. $secure = ( defined $secure ) ? $json_true : $json_false;
  851. my $params = {
  852. 'cookie' => {
  853. 'name' => $name,
  854. 'value' => $value,
  855. 'path' => $path,
  856. 'domain' => $domain,
  857. 'secure' => $secure,
  858. }
  859. };
  860. return $self->_execute_command( $res, $params );
  861. }
  862. =head2 delete_all_cookies
  863. Description:
  864. Delete all cookies visible to the current page.
  865. Usage:
  866. $driver->delete_all_cookies();
  867. =cut
  868. sub delete_all_cookies {
  869. my ($self) = @_;
  870. my $res = { 'command' => 'deleteAllCookies' };
  871. return $self->_execute_command($res);
  872. }
  873. =head2 delete_cookie_named
  874. Description:
  875. Delete the cookie with the given name. This command will be a no-op if there
  876. is no such cookie visible to the current page.
  877. Input: 1
  878. Required:
  879. STRING - name of cookie to delete
  880. Usage:
  881. $driver->delete_cookie_named('foo');
  882. =cut
  883. sub delete_cookie_named {
  884. my ( $self, $cookie_name ) = @_;
  885. if ( not defined $cookie_name ) {
  886. return "Cookie name not provided";
  887. }
  888. my $res = { 'command' => 'deleteCookieNamed', 'name' => $cookie_name };
  889. return $self->_execute_command($res);
  890. }
  891. =head2 get_page_source
  892. Description:
  893. Get the current page source.
  894. Output:
  895. STRING - The page source.
  896. Usage:
  897. print $driver->get_page_source();
  898. =cut
  899. sub get_page_source {
  900. my ($self) = @_;
  901. my $res = { 'command' => 'getPageSource' };
  902. return $self->_execute_command($res);
  903. }
  904. =head2 find_element
  905. Description:
  906. Search for an element on the page, starting from the document root. The
  907. located element will be returned as a WebElement object.
  908. Input: 2 (1 optional)
  909. Required:
  910. STRING - The search target.
  911. Optional:
  912. STRING - Locator scheme to use to search the element, available schemes:
  913. {class, class_name, css, id, link, link_text, partial_link_text,
  914. tag_name, name, xpath}
  915. Defaults to 'xpath'.
  916. Output:
  917. Selenium::Remote::WebElement - WebElement Object
  918. Usage:
  919. $driver->find_element("//input[\@name='q']");
  920. =cut
  921. sub find_element {
  922. my ( $self, $query, $method ) = @_;
  923. if ( not defined $query ) {
  924. return 'Search string to find element not provided.';
  925. }
  926. my $using = ( defined $method ) ? FINDERS->{$method} : 'xpath';
  927. if (defined $using) {
  928. my $res = { 'command' => 'findElement' };
  929. my $params = { 'using' => $using, 'value' => $query };
  930. my $ret_data = eval { $self->_execute_command( $res, $params ); };
  931. if($@) {
  932. if($@ =~ /(An element could not be located on the page using the given search parameters)/) {
  933. # give details on what element wasn't found
  934. $@ = "$1: $query,$using";
  935. local @CARP_NOT = ("Selenium::Remote::Driver",@CARP_NOT);
  936. croak $@;
  937. } else {
  938. # re throw if the exception wasn't what we expected
  939. die $@;
  940. }
  941. }
  942. return new Selenium::Remote::WebElement($ret_data->{ELEMENT}, $self);
  943. }
  944. else {
  945. croak "Bad method, expected - class, class_name, css, id, link,
  946. link_text, partial_link_text, name, tag_name, xpath";
  947. }
  948. }
  949. =head2 find_elements
  950. Description:
  951. Search for multiple elements on the page, starting from the document root.
  952. The located elements will be returned as an array of WebElement object.
  953. Input: 2 (1 optional)
  954. Required:
  955. STRING - The search target.
  956. Optional:
  957. STRING - Locator scheme to use to search the element, available schemes:
  958. {class, class_name, css, id, link, link_text, partial_link_text,
  959. tag_name, name, xpath}
  960. Defaults to 'xpath'.
  961. Output:
  962. ARRAY of Selenium::Remote::WebElement - Array of WebElement Objects
  963. Usage:
  964. $driver->find_elements("//input");
  965. =cut
  966. sub find_elements {
  967. my ( $self, $query, $method ) = @_;
  968. if ( not defined $query ) {
  969. return 'Search string to find element not provided.';
  970. }
  971. my $using = ( defined $method ) ? FINDERS->{$method} : 'xpath';
  972. if (defined $using) {
  973. my $res = { 'command' => 'findElements' };
  974. my $params = { 'using' => $using, 'value' => $query };
  975. my $ret_data = eval {$self->_execute_command( $res, $params );};
  976. if($@) {
  977. if($@ =~ /(An element could not be located on the page using the given search parameters)/) {
  978. # give details on what element wasn't found
  979. $@ = "$1: $query,$using";
  980. local @CARP_NOT = ("Selenium::Remote::Driver",@CARP_NOT);
  981. croak $@;
  982. } else {
  983. # re throw if the exception wasn't what we expected
  984. die $@;
  985. }
  986. }
  987. my $elem_obj_arr;
  988. my $i = 0;
  989. foreach (@$ret_data) {
  990. $elem_obj_arr->[$i] = new Selenium::Remote::WebElement($_->{ELEMENT}, $self);
  991. $i++;
  992. }
  993. return wantarray?@{$elem_obj_arr}:$elem_obj_arr;
  994. }
  995. else {
  996. croak "Bad method, expected - class, class_name, css, id, link,
  997. link_text, partial_link_text, name, tag_name, xpath";
  998. }
  999. }
  1000. =head2 find_child_element
  1001. Description:
  1002. Search for an element on the page, starting from the identified element. The
  1003. located element will be returned as a WebElement object.
  1004. Input: 3 (1 optional)
  1005. Required:
  1006. Selenium::Remote::WebElement - WebElement object from where you want to
  1007. start searching.
  1008. STRING - The search target.
  1009. Optional:
  1010. STRING - Locator scheme to use to search the element, available schemes:
  1011. {class, class_name, css, id, link, link_text, partial_link_text,
  1012. tag_name, name, xpath}
  1013. Defaults to 'xpath'.
  1014. Output:
  1015. Selenium::Remote::WebElement - WebElement Object
  1016. Usage:
  1017. my $elem1 = $driver->find_element("//select[\@name='ned']");
  1018. my $child = $driver->find_child_element($elem1, "//option[\@value='es_ar']");
  1019. =cut
  1020. sub find_child_element {
  1021. my ( $self, $elem, $query, $method ) = @_;
  1022. if ( ( not defined $elem ) || ( not defined $query ) ) {
  1023. return "Missing parameters";
  1024. }
  1025. my $using = ( defined $method ) ? $method : 'xpath';
  1026. if (exists FINDERS->{$using}) {
  1027. my $res = { 'command' => 'findChildElement', 'id' => $elem->{id} };
  1028. my $params = { 'using' => FINDERS->{$using}, 'value' => $query };
  1029. my $ret_data = eval {$self->_execute_command( $res, $params );};
  1030. if($@) {
  1031. if($@ =~ /(An element could not be located on the page using the given search parameters)/) {
  1032. # give details on what element wasn't found
  1033. $@ = "$1: $query,$using";
  1034. local @CARP_NOT = ("Selenium::Remote::Driver",@CARP_NOT);
  1035. croak $@;
  1036. } else {
  1037. # re throw if the exception wasn't what we expected
  1038. die $@;
  1039. }
  1040. }
  1041. return new Selenium::Remote::WebElement($ret_data->{ELEMENT}, $self);
  1042. }
  1043. else {
  1044. croak "Bad method, expected - class, class_name, css, id, link,
  1045. link_text, partial_link_text, name, tag_name, xpath";
  1046. }
  1047. }
  1048. =head2 find_child_elements
  1049. Description:
  1050. Search for multiple element on the page, starting from the identified
  1051. element. The located elements will be returned as an array of WebElement
  1052. objects.
  1053. Input: 3 (1 optional)
  1054. Required:
  1055. Selenium::Remote::WebElement - WebElement object from where you want to
  1056. start searching.
  1057. STRING - The search target.
  1058. Optional:
  1059. STRING - Locator scheme to use to search the element, available schemes:
  1060. {class, class_name, css, id, link, link_text, partial_link_text,
  1061. tag_name, name, xpath}
  1062. Defaults to 'xpath'.
  1063. Output:
  1064. ARRAY of Selenium::Remote::WebElement - Array of WebElement Objects.
  1065. Usage:
  1066. my $elem1 = $driver->find_element("//select[\@name='ned']");
  1067. my $child = $driver->find_child_elements($elem1, "//option");
  1068. =cut
  1069. sub find_child_elements {
  1070. my ( $self, $elem, $query, $method ) = @_;
  1071. if ( ( not defined $elem ) || ( not defined $query ) ) {
  1072. return "Missing parameters";
  1073. }
  1074. my $using = ( defined $method ) ? $method : 'xpath';
  1075. if (exists FINDERS->{$using}) {
  1076. my $res = { 'command' => 'findChildElements', 'id' => $elem->{id} };
  1077. my $params = { 'using' => FINDERS->{$using}, 'value' => $query };
  1078. my $ret_data = eval {$self->_execute_command( $res, $params );};
  1079. if($@) {
  1080. if($@ =~ /(An element could not be located on the page using the given search parameters)/) {
  1081. # give details on what element wasn't found
  1082. $@ = "$1: $query,$using";
  1083. local @CARP_NOT = ("Selenium::Remote::Driver",@CARP_NOT);
  1084. croak $@;
  1085. } else {
  1086. # re throw if the exception wasn't what we expected
  1087. die $@;
  1088. }
  1089. }
  1090. my $elem_obj_arr;
  1091. my $i = 0;
  1092. foreach (@$ret_data) {
  1093. $elem_obj_arr->[$i] = new Selenium::Remote::WebElement($_->{ELEMENT}, $self);
  1094. $i++;
  1095. }
  1096. return wantarray?@{$elem_obj_arr}:$elem_obj_arr;
  1097. }
  1098. else {
  1099. croak "Bad method, expected - class, class_name, css, id, link,
  1100. link_text, partial_link_text, name, tag_name, xpath";
  1101. }
  1102. }
  1103. =head2 get_active_element
  1104. Description:
  1105. Get the element on the page that currently has focus.. The located element
  1106. will be returned as a WebElement object.
  1107. Output:
  1108. Selenium::Remote::WebElement - WebElement Object
  1109. Usage:
  1110. $driver->get_active_element();
  1111. =cut
  1112. sub get_active_element {
  1113. my ($self) = @_;
  1114. my $res = { 'command' => 'getActiveElement' };
  1115. return $self->_execute_command($res);
  1116. }
  1117. # Not yet supported on the server
  1118. sub describe_element {
  1119. my ( $self, $element ) = @_;
  1120. #if (not defined $element) {
  1121. # return "Element not provided";
  1122. #}
  1123. #my $res = {'command' => 'desribeElement', 'name' => $element};
  1124. #return $self->_execute_command($res);
  1125. return "Not yet supported";
  1126. }
  1127. =head2 send_modifier
  1128. Description:
  1129. Send an event to the active element to depress or release a modifier key.
  1130. Input: 2
  1131. Required:
  1132. value - String - The modifier key event to be sent. This key must be one 'Ctrl','Shift','Alt',' or 'Command'/'Meta' as defined by the send keys command
  1133. isdown - Boolean/String - Whether to generate a key down or key up
  1134. Usage:
  1135. $driver->send_modifier('Alt','down');
  1136. $elem->send_keys('c');
  1137. $driver->send_modifier('Alt','up');
  1138. or
  1139. $driver->send_modifier('Alt',1);
  1140. $elem->send_keys('c');
  1141. $driver->send_modifier('Alt',0);
  1142. =cut
  1143. sub send_modifier {
  1144. my ($self,$modifier,$isdown) = @_;
  1145. if($isdown =~ /(down|up)/) {
  1146. $isdown = $isdown =~ /down/ ? 1:0;
  1147. }
  1148. my $res = {'command' => 'sendModifier'};
  1149. my $params = {value => $modifier,
  1150. isdown => $isdown};
  1151. return $self->_execute_command($res,$params);
  1152. }
  1153. =head2 compare_elements
  1154. Description:
  1155. Test if two element IDs refer to the same DOM element.
  1156. Input: 2
  1157. Required:
  1158. Selenium::Remote::WebElement - WebElement Object
  1159. Selenium::Remote::WebElement - WebElement Object
  1160. Output:
  1161. BOOLEAN
  1162. Usage:
  1163. $driver->compare_elements($elem_obj1, $elem_obj2);
  1164. =cut
  1165. sub compare_elements {
  1166. my ($self, $elem1, $elem2) = @_;
  1167. my $res = { 'command' => 'elementEquals',
  1168. 'id' => $elem1->{id},
  1169. 'other' => $elem2->{id}
  1170. };
  1171. return $self->_execute_command($res);
  1172. }
  1173. =head2 click
  1174. Description:
  1175. Click any mouse button (at the coordinates set by the last moveto command).
  1176. Input:
  1177. button - any one of 'LEFT'/0 'MIDDLE'/1 'RIGHT'/2
  1178. defaults to 'LEFT'
  1179. Usage:
  1180. $driver->click('LEFT');
  1181. $driver->click(1); #MIDDLE
  1182. $driver->click('RIGHT');
  1183. $driver->click; #Defaults to left
  1184. =cut
  1185. sub click {
  1186. my ($self,$button) = @_;
  1187. my $button_enum = {LEFT=>0,MIDDLE=>1,RIGHT=>2};
  1188. if(defined $button && $button =~ /(LEFT|MIDDLE|RIGHT)/i) {
  1189. $button = $button_enum->{uc $1};
  1190. } elsif(defined $button && $button =~ /(0|1|2)/) {
  1191. $button = $1;
  1192. } else {
  1193. $button = 0;
  1194. }
  1195. my $res = { 'command' => 'click' };
  1196. my $params = { 'button' => $button };
  1197. return $self->_execute_command($res,$button);
  1198. }
  1199. =head2 double_click
  1200. Description:
  1201. Double-clicks at the current mouse coordinates (set by moveto).
  1202. Usage:
  1203. $driver->double_click;
  1204. =cut
  1205. sub double_click {
  1206. my ($self) = @_;
  1207. my $res = { 'command' => 'doubleClick' };
  1208. return $self->_execute_command($res);
  1209. }
  1210. =head2 button_down
  1211. Description:
  1212. Click and hold the left mouse button (at the coordinates set by the
  1213. last moveto command). Note that the next mouse-related command that
  1214. should follow is buttondown . Any other mouse command (such as click
  1215. or another call to buttondown) will yield undefined behaviour.
  1216. Usage:
  1217. $self->button_down;
  1218. =cut
  1219. sub button_down {
  1220. my ($self) = @_;
  1221. my $res = { 'command' => 'buttonDown' };
  1222. return $self->_execute_command($res);
  1223. }
  1224. =head2 button_up
  1225. Description:
  1226. Releases the mouse button previously held (where the mouse is
  1227. currently at). Must be called once for every buttondown command
  1228. issued. See the note in click and buttondown about implications of
  1229. out-of-order commands.
  1230. Usage:
  1231. $self->button_up;
  1232. =cut
  1233. sub button_up {
  1234. my ($self) = @_;
  1235. my $res = { 'command' => 'buttonUp' };
  1236. return $self->_execute_command($res);
  1237. }
  1238. 1;
  1239. __END__
  1240. =head1 SEE ALSO
  1241. For more information about Selenium , visit the website at
  1242. L<http://code.google.com/p/selenium/>.
  1243. =head1 BUGS
  1244. The Selenium issue tracking system is available online at
  1245. L<http://github.com/aivaturi/Selenium-Remote-Driver/issues>.
  1246. =head1 CURRENT MAINTAINER
  1247. Gordon Child C<< <gchild@gordonchild.com> >>
  1248. =head1 AUTHOR
  1249. Perl Bindings for Selenium Remote Driver by Aditya Ivaturi C<< <ivaturi@gmail.com> >>
  1250. =head1 ACKNOWLEDGEMENTS
  1251. The following people have contributed to this module. (Thanks!)
  1252. =over 4
  1253. =item * Phil Kania
  1254. =item * Phil Mitchell
  1255. =back
  1256. =head1 LICENSE
  1257. Copyright (c) 2010-2011 Aditya Ivaturi, Gordon Child
  1258. Licensed under the Apache License, Version 2.0 (the "License");
  1259. you may not use this file except in compliance with the License.
  1260. You may obtain a copy of the License at
  1261. http://www.apache.org/licenses/LICENSE-2.0
  1262. Unless required by applicable law or agreed to in writing, software
  1263. distributed under the License is distributed on an "AS IS" BASIS,
  1264. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  1265. See the License for the specific language governing permissions and
  1266. limitations under the License.