Driver.pm 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684
  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.13";
  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_timeout
  334. Description:
  335. Set the amount of time, in milliseconds, that asynchronous scripts executed
  336. by execute_async_script() are permitted to run before they are
  337. aborted and a |Timeout| error is returned to the client.
  338. Input:
  339. ms - <NUMBER> - The amount of time, in milliseconds, that time-limited
  340. commands are permitted to run.
  341. Usage:
  342. $driver->set_async_script_timeout(1000);
  343. =cut
  344. sub set_async_script_timeout {
  345. my ($self, $ms) = @_;
  346. if (not defined $ms)
  347. {
  348. return "Expecting timeout in ms";
  349. }
  350. my $res = {'command' => 'setAsyncScriptTimeout'};
  351. my $params = {'ms' => $ms};
  352. return $self->_execute_command($res, $params);
  353. }
  354. =head2 set_implicit_wait_timeout
  355. Description:
  356. Set the amount of time the driver should wait when searching for elements.
  357. When searching for a single element, the driver will poll the page until
  358. an element is found or the timeout expires, whichever occurs first.
  359. When searching for multiple elements, the driver should poll the page until
  360. at least one element is found or the timeout expires, at which point it
  361. will return an empty list. If this method is never called, the driver will
  362. default to an implicit wait of 0ms.
  363. Input:
  364. Time in milliseconds.
  365. Output:
  366. Server Response Hash with no data returned back from the server.
  367. Usage:
  368. $driver->set_implicit_wait_timeout(10);
  369. =cut
  370. sub set_implicit_wait_timeout {
  371. my ($self, $ms) = @_;
  372. my $res = {'command' => 'setImplicitWaitTimeout'};
  373. my $params = {'ms' => $ms};
  374. return $self->_execute_command($res, $params);
  375. }
  376. =head2 close
  377. Description:
  378. Close the current window.
  379. Usage:
  380. $driver->close();
  381. or
  382. #close a popup window
  383. my $handles = $driver->get_window_handles;
  384. $driver->switch_to_window($handles->[1]);
  385. $driver->close();
  386. $driver->switch_to_window($handles->[0]);
  387. =cut
  388. sub close {
  389. my $self = shift;
  390. my $res = { 'command' => 'close' };
  391. $self->_execute_command($res);
  392. }
  393. =head2 quit
  394. Description:
  395. Delete the session & close open browsers.
  396. Usage:
  397. $driver->quit();
  398. =cut
  399. sub quit {
  400. my $self = shift;
  401. my $res = { 'command' => 'quit' };
  402. $self->_execute_command($res);
  403. $self->{session_id} = undef;
  404. }
  405. =head2 get_current_window_handle
  406. Description:
  407. Retrieve the current window handle.
  408. Output:
  409. STRING - the window handle
  410. Usage:
  411. print $driver->get_current_window_handle();
  412. =cut
  413. sub get_current_window_handle {
  414. my $self = shift;
  415. my $res = { 'command' => 'getCurrentWindowHandle' };
  416. return $self->_execute_command($res);
  417. }
  418. =head2 get_window_handles
  419. Description:
  420. Retrieve the list of window handles used in the session.
  421. Output:
  422. ARRAY of STRING - list of the window handles
  423. Usage:
  424. print Dumper $driver->get_window_handles;
  425. or
  426. # get popup, close, then back
  427. my $handles = $driver->get_window_handles;
  428. $driver->switch_to_window($handles->[1]);
  429. $driver->close;
  430. $driver->switch_to_window($handles->[0]);
  431. =cut
  432. sub get_window_handles {
  433. my $self = shift;
  434. my $res = { 'command' => 'getWindowHandles' };
  435. return $self->_execute_command($res);
  436. }
  437. =head2 get_window_size
  438. Description:
  439. Retrieve the window size
  440. Input:
  441. STRING - <optional> - window handle (default is 'current' window)
  442. Output:
  443. HASH - containing keys 'height' & 'width'
  444. Usage:
  445. my $window_size = $driver->get_window_size();
  446. print $window_size->{'height'}, $window_size->('width');
  447. =cut
  448. sub get_window_size {
  449. my ( $self, $window ) = @_;
  450. $window = (defined $window)?$window:'current';
  451. my $res = { 'command' => 'getWindowSize', 'window_handle' => $window };
  452. return $self->_execute_command($res);
  453. }
  454. =head2 get_window_position
  455. Description:
  456. Retrieve the window position
  457. Input:
  458. STRING - <optional> - window handle (default is 'current' window)
  459. Output:
  460. HASH - containing keys 'x' & 'y'
  461. Usage:
  462. my $window_size = $driver->get_window_position();
  463. print $window_size->{'x'}, $window_size->('y');
  464. =cut
  465. sub get_window_position {
  466. my ( $self, $window ) = @_;
  467. $window = (defined $window)?$window:'current';
  468. my $res = { 'command' => 'getWindowPosition', 'window_handle' => $window };
  469. return $self->_execute_command($res);
  470. }
  471. =head2 get_current_url
  472. Description:
  473. Retrieve the url of the current page
  474. Output:
  475. STRING - url
  476. Usage:
  477. print $driver->get_current_url();
  478. =cut
  479. sub get_current_url {
  480. my $self = shift;
  481. my $res = { 'command' => 'getCurrentUrl' };
  482. return $self->_execute_command($res);
  483. }
  484. =head2 navigate
  485. Description:
  486. Navigate to a given url. This is same as get() method.
  487. Input:
  488. STRING - url
  489. Usage:
  490. $driver->navigate('http://www.google.com');
  491. =cut
  492. sub navigate {
  493. my ( $self, $url ) = @_;
  494. $self->get($url);
  495. }
  496. =head2 get
  497. Description:
  498. Navigate to a given url
  499. Input:
  500. STRING - url
  501. Usage:
  502. $driver->get('http://www.google.com');
  503. =cut
  504. sub get {
  505. my ( $self, $url ) = @_;
  506. my $res = { 'command' => 'get' };
  507. my $params = { 'url' => $url };
  508. return $self->_execute_command( $res, $params );
  509. }
  510. =head2 get_title
  511. Description:
  512. Get the current page title
  513. Output:
  514. STRING - Page title
  515. Usage:
  516. print $driver->get_title();
  517. =cut
  518. sub get_title {
  519. my $self = shift;
  520. my $res = { 'command' => 'getTitle' };
  521. return $self->_execute_command($res);
  522. }
  523. =head2 go_back
  524. Description:
  525. Equivalent to hitting the back button on the browser.
  526. Usage:
  527. $driver->go_back();
  528. =cut
  529. sub go_back {
  530. my $self = shift;
  531. my $res = { 'command' => 'goBack' };
  532. return $self->_execute_command($res);
  533. }
  534. =head2 go_forward
  535. Description:
  536. Equivalent to hitting the forward button on the browser.
  537. Usage:
  538. $driver->go_forward();
  539. =cut
  540. sub go_forward {
  541. my $self = shift;
  542. my $res = { 'command' => 'goForward' };
  543. return $self->_execute_command($res);
  544. }
  545. =head2 refresh
  546. Description:
  547. Reload the current page.
  548. Usage:
  549. $driver->refresh();
  550. =cut
  551. sub refresh {
  552. my $self = shift;
  553. my $res = { 'command' => 'refresh' };
  554. return $self->_execute_command($res);
  555. }
  556. =head2 javascript
  557. Description:
  558. returns true if javascript is enabled in the driver.
  559. Usage:
  560. if ($driver->javascript) { ...; }
  561. =cut
  562. sub javascript {
  563. my $self = shift;
  564. return $self->{javascript} == JSON::true;
  565. }
  566. =head2 execute_async_script
  567. Description:
  568. Inject a snippet of JavaScript into the page for execution in the context
  569. of the currently selected frame. The executed script is assumed to be
  570. asynchronous and must signal that is done by invoking the provided
  571. callback, which is always provided as the final argument to the function.
  572. The value to this callback will be returned to the client.
  573. Asynchronous script commands may not span page loads. If an unload event
  574. is fired while waiting for a script result, an error should be returned
  575. to the client.
  576. Input: 2 (1 optional)
  577. Required:
  578. STRING - Javascript to execute on the page
  579. Optional:
  580. ARRAY - list of arguments that need to be passed to the script.
  581. Output:
  582. {*} - Varied, depending on the type of result expected back from the script.
  583. Usage:
  584. my $script = q{
  585. var arg1 = arguments[0];
  586. var callback = arguments[arguments.length-1];
  587. var elem = window.document.findElementById(arg1);
  588. callback(elem);
  589. };
  590. my $callback = q{return arguments[0];};
  591. my $elem = $driver->execute_async_script($script,'myid',$callback);
  592. $elem->click;
  593. =cut
  594. sub execute_async_script {
  595. my ( $self, $script, @args ) = @_;
  596. if ($self->javascript) {
  597. if ( not defined $script ) {
  598. return 'No script provided';
  599. }
  600. my $res = { 'command' => 'executeAsyncScript' };
  601. # Check the args array if the elem obj is provided & replace it with
  602. # JSON representation
  603. for (my $i=0; $i<@args; $i++) {
  604. if (ref $args[$i] eq 'Selenium::Remote::WebElement') {
  605. $args[$i] = {'ELEMENT' => ($args[$i])->{id}};
  606. }
  607. }
  608. my $params = {'script' => $script, 'args' => \@args};
  609. my $ret = $self->_execute_command($res, $params);
  610. # replace any ELEMENTS with WebElement
  611. if (ref($ret) and (ref($ret) eq 'HASH') and exists $ret->{'ELEMENT'}) {
  612. $ret =
  613. new Selenium::Remote::WebElement(
  614. $ret->{ELEMENT}, $self);
  615. }
  616. return $ret;
  617. }
  618. else {
  619. croak 'Javascript is not enabled on remote driver instance.';
  620. }
  621. }
  622. =head2 execute_script
  623. Description:
  624. Inject a snippet of JavaScript into the page and return its result.
  625. WebElements that should be passed to the script as an argument should be
  626. specified in the arguments array as WebElement object. Likewise,
  627. any WebElements in the script result will be returned as WebElement object.
  628. Input: 2 (1 optional)
  629. Required:
  630. STRING - Javascript to execute on the page
  631. Optional:
  632. ARRAY - list of arguments that need to be passed to the script.
  633. Output:
  634. {*} - Varied, depending on the type of result expected back from the script.
  635. Usage:
  636. my $script = q{
  637. var arg1 = arguments[0];
  638. var elem = window.document.findElementById(arg1);
  639. return elem;
  640. };
  641. my $elem = $driver->execute_script($script,'myid');
  642. $elem->click;
  643. =cut
  644. sub execute_script {
  645. my ( $self, $script, @args ) = @_;
  646. if ($self->javascript) {
  647. if ( not defined $script ) {
  648. return 'No script provided';
  649. }
  650. my $res = { 'command' => 'executeScript' };
  651. # Check the args array if the elem obj is provided & replace it with
  652. # JSON representation
  653. for (my $i=0; $i<@args; $i++) {
  654. if (ref $args[$i] eq 'Selenium::Remote::WebElement') {
  655. $args[$i] = {'ELEMENT' => ($args[$i])->{id}};
  656. }
  657. }
  658. my $params = {'script' => $script, 'args' => [@args]};
  659. my $ret = $self->_execute_command($res, $params);
  660. # replace any ELEMENTS with WebElement
  661. if (ref($ret) and (ref($ret) eq 'HASH') and exists $ret->{'ELEMENT'}) {
  662. $ret =
  663. new Selenium::Remote::WebElement(
  664. $ret->{ELEMENT}, $self);
  665. }
  666. return $ret;
  667. }
  668. else {
  669. croak 'Javascript is not enabled on remote driver instance.';
  670. }
  671. }
  672. =head2 screenshot
  673. Description:
  674. Get a screenshot of the current page as a base64 encoded image.
  675. Output:
  676. STRING - base64 encoded image
  677. Usage:
  678. print $driver->screenshot();
  679. or
  680. require MIME::Base64;
  681. open(FH,'>','screenshot.png');
  682. binmode FH;
  683. my $png_base64 = $driver->screenshot();
  684. print FH MIME::Base64::decode_base64($png_base64);
  685. close FH;
  686. =cut
  687. sub screenshot {
  688. my ($self) = @_;
  689. my $res = { 'command' => 'screenshot' };
  690. return $self->_execute_command($res);
  691. }
  692. =head2 available_engines
  693. Description:
  694. List all available engines on the machine. To use an engine, it has to be present in this list.
  695. Output:
  696. {Array.<string>} A list of available engines
  697. Usage:
  698. print Dumper $driver->available_engines;
  699. =cut
  700. sub available_engines {
  701. my ($self) = @_;
  702. my $res = { 'command' => 'availableEngines' };
  703. return $self->_execute_command($res);
  704. }
  705. =head2 switch_to_frame
  706. Description:
  707. Change focus to another frame on the page. If the frame ID is null, the
  708. server will switch to the page's default content.
  709. Input: 1
  710. Required:
  711. {STRING | NUMBER | NULL} - ID of the frame which can be one of the three
  712. mentioned.
  713. Usage:
  714. $driver->switch_to_frame('frame_1');
  715. =cut
  716. sub switch_to_frame {
  717. my ( $self, $id ) = @_;
  718. my $json_null = JSON::null;
  719. $id = ( defined $id ) ? $id : $json_null;
  720. my $res = { 'command' => 'switchToFrame' };
  721. my $params = { 'id' => $id };
  722. return $self->_execute_command( $res, $params );
  723. }
  724. =head2 switch_to_window
  725. Description:
  726. Change focus to another window. The window to change focus to may be
  727. specified by its server assigned window handle, or by the value of its name
  728. attribute.
  729. Input: 1
  730. Required:
  731. STRING - Window handle or the Window name
  732. Usage:
  733. $driver->switch_to_window('MY Homepage');
  734. or
  735. # close a popup window and switch back
  736. my $handles = $driver->get_window_handles;
  737. $driver->switch_to_window($handles->[1]);
  738. $driver->close;
  739. $driver->switch_to_window($handles->[0]);
  740. =cut
  741. sub switch_to_window {
  742. my ( $self, $name ) = @_;
  743. if ( not defined $name ) {
  744. return 'Window name not provided';
  745. }
  746. my $res = { 'command' => 'switchToWindow' };
  747. my $params = { 'name' => $name };
  748. return $self->_execute_command( $res, $params );
  749. }
  750. =head2 get_speed
  751. Description:
  752. Get the current user input speed. The actual input speed is still browser
  753. specific and not covered by the Driver.
  754. Output:
  755. STRING - One of these: SLOW, MEDIUM, FAST
  756. Usage:
  757. print $driver->get_speed();
  758. =cut
  759. sub get_speed {
  760. my ($self) = @_;
  761. my $res = { 'command' => 'getSpeed' };
  762. return $self->_execute_command($res);
  763. }
  764. =head2 set_speed
  765. Description:
  766. Set the user input speed.
  767. Input:
  768. STRING - One of these: SLOW, MEDIUM, FAST
  769. Usage:
  770. $driver->set_speed('MEDIUM');
  771. Note: This function is a no-op in WebDriver (?). See
  772. https://groups.google.com/d/topic/selenium-users/oX0ZnYFPuSA/discussion and
  773. http://code.google.com/p/selenium/source/browse/trunk/java/client/src/org/openqa/selenium/WebDriverCommandProcessor.java
  774. =cut
  775. sub set_speed {
  776. my ( $self, $speed ) = @_;
  777. if ( not defined $speed ) {
  778. return 'Speed not provided.';
  779. }
  780. my $res = { 'command' => 'setSpeed' };
  781. my $params = { 'speed' => $speed };
  782. return $self->_execute_command( $res, $params );
  783. }
  784. =head2 set_window_position
  785. Description:
  786. Set the position (on screen) where you want your browser to be displayed.
  787. Input:
  788. INT - x co-ordinate
  789. INT - y co-ordinate
  790. STRING - <optional> - window handle (default is 'current' window)
  791. Output:
  792. BOOLEAN - Success or failure
  793. Usage:
  794. $driver->set_window_position(50, 50);
  795. =cut
  796. sub set_window_position {
  797. my ( $self, $x, $y, $window ) = @_;
  798. $window = (defined $window)?$window:'current';
  799. if (not defined $x and not defined $y){
  800. return "X & Y co-ordinates are required";
  801. }
  802. my $res = { 'command' => 'setWindowPosition', 'window_handle' => $window };
  803. my $params = { 'x' => $x, 'y' => $y };
  804. my $ret = $self->_execute_command($res, $params);
  805. if ($ret =~ m/204/g) {
  806. return 1;
  807. }
  808. else { return 0; }
  809. }
  810. =head2 set_window_size
  811. Description:
  812. Set the size of the browser window
  813. Input:
  814. INT - height of the window
  815. INT - width of the window
  816. STRING - <optional> - window handle (default is 'current' window)
  817. Output:
  818. BOOLEAN - Success or failure
  819. Usage:
  820. $driver->set_window_size(640, 480);
  821. =cut
  822. sub set_window_size {
  823. my ( $self, $height, $width, $window ) = @_;
  824. $window = (defined $window)?$window:'current';
  825. if (not defined $height and not defined $width){
  826. return "height & width of browser are required";
  827. }
  828. my $res = { 'command' => 'setWindowSize', 'window_handle' => $window };
  829. my $params = { 'height' => $height, 'width' => $width };
  830. my $ret = $self->_execute_command($res, $params);
  831. if ($ret =~ m/204/g) {
  832. return 1;
  833. }
  834. else { return 0; }
  835. }
  836. =head2 get_all_cookies
  837. Description:
  838. Retrieve all cookies visible to the current page. Each cookie will be
  839. returned as a HASH reference with the following keys & their value types:
  840. 'name' - STRING
  841. 'value' - STRING
  842. 'path' - STRING
  843. 'domain' - STRING
  844. 'secure' - BOOLEAN
  845. Output:
  846. ARRAY of HASHES - list of all the cookie hashes
  847. Usage:
  848. print Dumper($driver->get_all_cookies());
  849. =cut
  850. sub get_all_cookies {
  851. my ($self) = @_;
  852. my $res = { 'command' => 'getAllCookies' };
  853. return $self->_execute_command($res);
  854. }
  855. =head2 add_cookie
  856. Description:
  857. Set a cookie on the domain.
  858. Input: 5 (1 optional)
  859. Required:
  860. 'name' - STRING
  861. 'value' - STRING
  862. 'path' - STRING
  863. 'domain' - STRING
  864. Optional:
  865. 'secure' - BOOLEAN - default is false.
  866. Usage:
  867. $driver->add_cookie('foo', 'bar', '/', '.google.com', 0)
  868. =cut
  869. sub add_cookie {
  870. my ( $self, $name, $value, $path, $domain, $secure ) = @_;
  871. if ( ( not defined $name )
  872. || ( not defined $value )
  873. || ( not defined $path )
  874. || ( not defined $domain ) )
  875. {
  876. return "Missing parameters";
  877. }
  878. my $res = { 'command' => 'addCookie' };
  879. my $json_false = JSON::false;
  880. my $json_true = JSON::true;
  881. $secure = ( defined $secure ) ? $json_true : $json_false;
  882. my $params = {
  883. 'cookie' => {
  884. 'name' => $name,
  885. 'value' => $value,
  886. 'path' => $path,
  887. 'domain' => $domain,
  888. 'secure' => $secure,
  889. }
  890. };
  891. return $self->_execute_command( $res, $params );
  892. }
  893. =head2 delete_all_cookies
  894. Description:
  895. Delete all cookies visible to the current page.
  896. Usage:
  897. $driver->delete_all_cookies();
  898. =cut
  899. sub delete_all_cookies {
  900. my ($self) = @_;
  901. my $res = { 'command' => 'deleteAllCookies' };
  902. return $self->_execute_command($res);
  903. }
  904. =head2 delete_cookie_named
  905. Description:
  906. Delete the cookie with the given name. This command will be a no-op if there
  907. is no such cookie visible to the current page.
  908. Input: 1
  909. Required:
  910. STRING - name of cookie to delete
  911. Usage:
  912. $driver->delete_cookie_named('foo');
  913. =cut
  914. sub delete_cookie_named {
  915. my ( $self, $cookie_name ) = @_;
  916. if ( not defined $cookie_name ) {
  917. return "Cookie name not provided";
  918. }
  919. my $res = { 'command' => 'deleteCookieNamed', 'name' => $cookie_name };
  920. return $self->_execute_command($res);
  921. }
  922. =head2 get_page_source
  923. Description:
  924. Get the current page source.
  925. Output:
  926. STRING - The page source.
  927. Usage:
  928. print $driver->get_page_source();
  929. =cut
  930. sub get_page_source {
  931. my ($self) = @_;
  932. my $res = { 'command' => 'getPageSource' };
  933. return $self->_execute_command($res);
  934. }
  935. =head2 find_element
  936. Description:
  937. Search for an element on the page, starting from the document root. The
  938. located element will be returned as a WebElement object.
  939. Input: 2 (1 optional)
  940. Required:
  941. STRING - The search target.
  942. Optional:
  943. STRING - Locator scheme to use to search the element, available schemes:
  944. {class, class_name, css, id, link, link_text, partial_link_text,
  945. tag_name, name, xpath}
  946. Defaults to 'xpath'.
  947. Output:
  948. Selenium::Remote::WebElement - WebElement Object
  949. Usage:
  950. $driver->find_element("//input[\@name='q']");
  951. =cut
  952. sub find_element {
  953. my ( $self, $query, $method ) = @_;
  954. if ( not defined $query ) {
  955. return 'Search string to find element not provided.';
  956. }
  957. my $using = ( defined $method ) ? FINDERS->{$method} : 'xpath';
  958. if (defined $using) {
  959. my $res = { 'command' => 'findElement' };
  960. my $params = { 'using' => $using, 'value' => $query };
  961. my $ret_data = eval { $self->_execute_command( $res, $params ); };
  962. if($@) {
  963. if($@ =~ /(An element could not be located on the page using the given search parameters)/) {
  964. # give details on what element wasn't found
  965. $@ = "$1: $query,$using";
  966. local @CARP_NOT = ("Selenium::Remote::Driver",@CARP_NOT);
  967. croak $@;
  968. } else {
  969. # re throw if the exception wasn't what we expected
  970. die $@;
  971. }
  972. }
  973. return new Selenium::Remote::WebElement($ret_data->{ELEMENT}, $self);
  974. }
  975. else {
  976. croak "Bad method, expected - class, class_name, css, id, link,
  977. link_text, partial_link_text, name, tag_name, xpath";
  978. }
  979. }
  980. =head2 find_elements
  981. Description:
  982. Search for multiple elements on the page, starting from the document root.
  983. The located elements will be returned as an array of WebElement object.
  984. Input: 2 (1 optional)
  985. Required:
  986. STRING - The search target.
  987. Optional:
  988. STRING - Locator scheme to use to search the element, available schemes:
  989. {class, class_name, css, id, link, link_text, partial_link_text,
  990. tag_name, name, xpath}
  991. Defaults to 'xpath'.
  992. Output:
  993. ARRAY of Selenium::Remote::WebElement - Array of WebElement Objects
  994. Usage:
  995. $driver->find_elements("//input");
  996. =cut
  997. sub find_elements {
  998. my ( $self, $query, $method ) = @_;
  999. if ( not defined $query ) {
  1000. return 'Search string to find element not provided.';
  1001. }
  1002. my $using = ( defined $method ) ? FINDERS->{$method} : 'xpath';
  1003. if (defined $using) {
  1004. my $res = { 'command' => 'findElements' };
  1005. my $params = { 'using' => $using, 'value' => $query };
  1006. my $ret_data = eval {$self->_execute_command( $res, $params );};
  1007. if($@) {
  1008. if($@ =~ /(An element could not be located on the page using the given search parameters)/) {
  1009. # give details on what element wasn't found
  1010. $@ = "$1: $query,$using";
  1011. local @CARP_NOT = ("Selenium::Remote::Driver",@CARP_NOT);
  1012. croak $@;
  1013. } else {
  1014. # re throw if the exception wasn't what we expected
  1015. die $@;
  1016. }
  1017. }
  1018. my $elem_obj_arr;
  1019. my $i = 0;
  1020. foreach (@$ret_data) {
  1021. $elem_obj_arr->[$i] = new Selenium::Remote::WebElement($_->{ELEMENT}, $self);
  1022. $i++;
  1023. }
  1024. return wantarray?@{$elem_obj_arr}:$elem_obj_arr;
  1025. }
  1026. else {
  1027. croak "Bad method, expected - class, class_name, css, id, link,
  1028. link_text, partial_link_text, name, tag_name, xpath";
  1029. }
  1030. }
  1031. =head2 find_child_element
  1032. Description:
  1033. Search for an element on the page, starting from the identified element. The
  1034. located element will be returned as a WebElement object.
  1035. Input: 3 (1 optional)
  1036. Required:
  1037. Selenium::Remote::WebElement - WebElement object from where you want to
  1038. start searching.
  1039. STRING - The search target.
  1040. Optional:
  1041. STRING - Locator scheme to use to search the element, available schemes:
  1042. {class, class_name, css, id, link, link_text, partial_link_text,
  1043. tag_name, name, xpath}
  1044. Defaults to 'xpath'.
  1045. Output:
  1046. Selenium::Remote::WebElement - WebElement Object
  1047. Usage:
  1048. my $elem1 = $driver->find_element("//select[\@name='ned']");
  1049. my $child = $driver->find_child_element($elem1, "//option[\@value='es_ar']");
  1050. =cut
  1051. sub find_child_element {
  1052. my ( $self, $elem, $query, $method ) = @_;
  1053. if ( ( not defined $elem ) || ( not defined $query ) ) {
  1054. return "Missing parameters";
  1055. }
  1056. my $using = ( defined $method ) ? $method : 'xpath';
  1057. if (exists FINDERS->{$using}) {
  1058. my $res = { 'command' => 'findChildElement', 'id' => $elem->{id} };
  1059. my $params = { 'using' => FINDERS->{$using}, 'value' => $query };
  1060. my $ret_data = eval {$self->_execute_command( $res, $params );};
  1061. if($@) {
  1062. if($@ =~ /(An element could not be located on the page using the given search parameters)/) {
  1063. # give details on what element wasn't found
  1064. $@ = "$1: $query,$using";
  1065. local @CARP_NOT = ("Selenium::Remote::Driver",@CARP_NOT);
  1066. croak $@;
  1067. } else {
  1068. # re throw if the exception wasn't what we expected
  1069. die $@;
  1070. }
  1071. }
  1072. return new Selenium::Remote::WebElement($ret_data->{ELEMENT}, $self);
  1073. }
  1074. else {
  1075. croak "Bad method, expected - class, class_name, css, id, link,
  1076. link_text, partial_link_text, name, tag_name, xpath";
  1077. }
  1078. }
  1079. =head2 find_child_elements
  1080. Description:
  1081. Search for multiple element on the page, starting from the identified
  1082. element. The located elements will be returned as an array of WebElement
  1083. objects.
  1084. Input: 3 (1 optional)
  1085. Required:
  1086. Selenium::Remote::WebElement - WebElement object from where you want to
  1087. start searching.
  1088. STRING - The search target.
  1089. Optional:
  1090. STRING - Locator scheme to use to search the element, available schemes:
  1091. {class, class_name, css, id, link, link_text, partial_link_text,
  1092. tag_name, name, xpath}
  1093. Defaults to 'xpath'.
  1094. Output:
  1095. ARRAY of Selenium::Remote::WebElement - Array of WebElement Objects.
  1096. Usage:
  1097. my $elem1 = $driver->find_element("//select[\@name='ned']");
  1098. my $child = $driver->find_child_elements($elem1, "//option");
  1099. =cut
  1100. sub find_child_elements {
  1101. my ( $self, $elem, $query, $method ) = @_;
  1102. if ( ( not defined $elem ) || ( not defined $query ) ) {
  1103. return "Missing parameters";
  1104. }
  1105. my $using = ( defined $method ) ? $method : 'xpath';
  1106. if (exists FINDERS->{$using}) {
  1107. my $res = { 'command' => 'findChildElements', 'id' => $elem->{id} };
  1108. my $params = { 'using' => FINDERS->{$using}, 'value' => $query };
  1109. my $ret_data = eval {$self->_execute_command( $res, $params );};
  1110. if($@) {
  1111. if($@ =~ /(An element could not be located on the page using the given search parameters)/) {
  1112. # give details on what element wasn't found
  1113. $@ = "$1: $query,$using";
  1114. local @CARP_NOT = ("Selenium::Remote::Driver",@CARP_NOT);
  1115. croak $@;
  1116. } else {
  1117. # re throw if the exception wasn't what we expected
  1118. die $@;
  1119. }
  1120. }
  1121. my $elem_obj_arr;
  1122. my $i = 0;
  1123. foreach (@$ret_data) {
  1124. $elem_obj_arr->[$i] = new Selenium::Remote::WebElement($_->{ELEMENT}, $self);
  1125. $i++;
  1126. }
  1127. return wantarray?@{$elem_obj_arr}:$elem_obj_arr;
  1128. }
  1129. else {
  1130. croak "Bad method, expected - class, class_name, css, id, link,
  1131. link_text, partial_link_text, name, tag_name, xpath";
  1132. }
  1133. }
  1134. =head2 get_active_element
  1135. Description:
  1136. Get the element on the page that currently has focus.. The located element
  1137. will be returned as a WebElement object.
  1138. Output:
  1139. Selenium::Remote::WebElement - WebElement Object
  1140. Usage:
  1141. $driver->get_active_element();
  1142. =cut
  1143. sub get_active_element {
  1144. my ($self) = @_;
  1145. my $res = { 'command' => 'getActiveElement' };
  1146. my $ret_data = eval { $self->_execute_command($res) };
  1147. if ($@) {
  1148. croak $@;
  1149. } else {
  1150. return new Selenium::Remote::WebElement($ret_data->{ELEMENT}, $self);
  1151. }
  1152. }
  1153. # Not yet supported on the server
  1154. sub describe_element {
  1155. my ( $self, $element ) = @_;
  1156. #if (not defined $element) {
  1157. # return "Element not provided";
  1158. #}
  1159. #my $res = {'command' => 'desribeElement', 'name' => $element};
  1160. #return $self->_execute_command($res);
  1161. return "Not yet supported";
  1162. }
  1163. =head2 send_modifier
  1164. Description:
  1165. Send an event to the active element to depress or release a modifier key.
  1166. Input: 2
  1167. Required:
  1168. 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
  1169. isdown - Boolean/String - Whether to generate a key down or key up
  1170. Usage:
  1171. $driver->send_modifier('Alt','down');
  1172. $elem->send_keys('c');
  1173. $driver->send_modifier('Alt','up');
  1174. or
  1175. $driver->send_modifier('Alt',1);
  1176. $elem->send_keys('c');
  1177. $driver->send_modifier('Alt',0);
  1178. =cut
  1179. sub send_modifier {
  1180. my ($self,$modifier,$isdown) = @_;
  1181. if($isdown =~ /(down|up)/) {
  1182. $isdown = $isdown =~ /down/ ? 1:0;
  1183. }
  1184. my $res = {'command' => 'sendModifier'};
  1185. my $params = {value => $modifier,
  1186. isdown => $isdown};
  1187. return $self->_execute_command($res,$params);
  1188. }
  1189. =head2 compare_elements
  1190. Description:
  1191. Test if two element IDs refer to the same DOM element.
  1192. Input: 2
  1193. Required:
  1194. Selenium::Remote::WebElement - WebElement Object
  1195. Selenium::Remote::WebElement - WebElement Object
  1196. Output:
  1197. BOOLEAN
  1198. Usage:
  1199. $driver->compare_elements($elem_obj1, $elem_obj2);
  1200. =cut
  1201. sub compare_elements {
  1202. my ($self, $elem1, $elem2) = @_;
  1203. my $res = { 'command' => 'elementEquals',
  1204. 'id' => $elem1->{id},
  1205. 'other' => $elem2->{id}
  1206. };
  1207. return $self->_execute_command($res);
  1208. }
  1209. =head2 click
  1210. Description:
  1211. Click any mouse button (at the coordinates set by the last moveto command).
  1212. Input:
  1213. button - any one of 'LEFT'/0 'MIDDLE'/1 'RIGHT'/2
  1214. defaults to 'LEFT'
  1215. Usage:
  1216. $driver->click('LEFT');
  1217. $driver->click(1); #MIDDLE
  1218. $driver->click('RIGHT');
  1219. $driver->click; #Defaults to left
  1220. =cut
  1221. sub click {
  1222. my ($self,$button) = @_;
  1223. my $button_enum = {LEFT=>0,MIDDLE=>1,RIGHT=>2};
  1224. if(defined $button && $button =~ /(LEFT|MIDDLE|RIGHT)/i) {
  1225. $button = $button_enum->{uc $1};
  1226. } elsif(defined $button && $button =~ /(0|1|2)/) {
  1227. $button = $1;
  1228. } else {
  1229. $button = 0;
  1230. }
  1231. my $res = { 'command' => 'click' };
  1232. my $params = { 'button' => $button };
  1233. return $self->_execute_command($res,$button);
  1234. }
  1235. =head2 double_click
  1236. Description:
  1237. Double-clicks at the current mouse coordinates (set by moveto).
  1238. Usage:
  1239. $driver->double_click;
  1240. =cut
  1241. sub double_click {
  1242. my ($self) = @_;
  1243. my $res = { 'command' => 'doubleClick' };
  1244. return $self->_execute_command($res);
  1245. }
  1246. =head2 button_down
  1247. Description:
  1248. Click and hold the left mouse button (at the coordinates set by the
  1249. last moveto command). Note that the next mouse-related command that
  1250. should follow is buttondown . Any other mouse command (such as click
  1251. or another call to buttondown) will yield undefined behaviour.
  1252. Usage:
  1253. $self->button_down;
  1254. =cut
  1255. sub button_down {
  1256. my ($self) = @_;
  1257. my $res = { 'command' => 'buttonDown' };
  1258. return $self->_execute_command($res);
  1259. }
  1260. =head2 button_up
  1261. Description:
  1262. Releases the mouse button previously held (where the mouse is
  1263. currently at). Must be called once for every buttondown command
  1264. issued. See the note in click and buttondown about implications of
  1265. out-of-order commands.
  1266. Usage:
  1267. $self->button_up;
  1268. =cut
  1269. sub button_up {
  1270. my ($self) = @_;
  1271. my $res = { 'command' => 'buttonUp' };
  1272. return $self->_execute_command($res);
  1273. }
  1274. 1;
  1275. __END__
  1276. =head1 SEE ALSO
  1277. For more information about Selenium , visit the website at
  1278. L<http://code.google.com/p/selenium/>.
  1279. =head1 BUGS
  1280. The Selenium issue tracking system is available online at
  1281. L<http://github.com/aivaturi/Selenium-Remote-Driver/issues>.
  1282. =head1 CURRENT MAINTAINER
  1283. Gordon Child C<< <gchild@gordonchild.com> >>
  1284. =head1 AUTHOR
  1285. Perl Bindings for Selenium Remote Driver by Aditya Ivaturi C<< <ivaturi@gmail.com> >>
  1286. =head1 ACKNOWLEDGEMENTS
  1287. The following people have contributed to this module. (Thanks!)
  1288. =over 4
  1289. =item * Phil Kania
  1290. =item * Phil Mitchell
  1291. =back
  1292. =head1 LICENSE
  1293. Copyright (c) 2010-2011 Aditya Ivaturi, Gordon Child
  1294. Licensed under the Apache License, Version 2.0 (the "License");
  1295. you may not use this file except in compliance with the License.
  1296. You may obtain a copy of the License at
  1297. http://www.apache.org/licenses/LICENSE-2.0
  1298. Unless required by applicable law or agreed to in writing, software
  1299. distributed under the License is distributed on an "AS IS" BASIS,
  1300. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  1301. See the License for the specific language governing permissions and
  1302. limitations under the License.