1
0

Driver.pm 46 KB

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