Driver.pm 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333
  1. package Selenium::Remote::Driver;
  2. use strict;
  3. use warnings;
  4. use Data::Dumper;
  5. use Carp qw(croak);
  6. use vars qw(@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.10";
  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 Server Response Hash
  47. Every method in this module returns either a string or a hash reference. The
  48. string usually means that it has received improper or not received any input.
  49. And that string explains what was wrong. This also means that driver has never
  50. sent any commands to the remote server.
  51. If the methods return a hash reference, that means the driver has sent a command
  52. to the remote server & it has received a response. The driver processes that
  53. response & creates a hash with specific keys & returns it back to the end user.
  54. We shall refer to this hash reference as B<Server Response Hash>. The keys are:
  55. =over
  56. =item cmd_status
  57. The value will either be 'OK' or 'NOTOK'. OK means that server successfully
  58. executed the command & NOTOK means that there was an error encountered on the
  59. server while executing the command. Check cmd_error for further details.
  60. =item cmd_return
  61. This hash key will contain the value returned from the server. It could be of
  62. any data type & each method's POD will list that information.
  63. =item cmd_error
  64. If there was an error on the server while executing the command, this key will
  65. be defined. The value will in turn contain information in a hash with 'stackTrace'
  66. as a key which lists the stack trace of the error encountered on the server &
  67. 'error' which has human readable text of the actual error. It can also contain
  68. a 'screenshot' - a base64 encoded image if the server returns one.
  69. =item session_id
  70. Every successfull command will contain the session id of the current session
  71. against which the command was executed.
  72. =back
  73. So a rule of thumb while invoking methods on the driver is to check whether the
  74. return type is a hash or not. If it is a hash then check for the value of
  75. cmd_status & take necessary actions accordingly. All the method PODs will list
  76. what the cmd_return will contain as that is what an end user is mostly interested
  77. in. Also, for some of the commands, server does not return any thing back. And
  78. Server Response Hash will contain a generic string stating that server didn't
  79. return any thing back. But if cmd_status value is 'OK', then you can safely
  80. assume that command executed successfully on the server.
  81. =head2 WebElement
  82. Selenium Webdriver represents all the HTML elements as WebElement, which is
  83. in turn represented by Selenium::Remote::WebElement module. So any method that
  84. deals with WebElements will return and/or expect WebElement object. The POD for
  85. that module describes all the methods that perform various actions on the
  86. WebElements like click, submit etc.
  87. To interact with any WebElement you have to first "find" it, read the POD for
  88. find_element or find_elements for further info. Once you find the required element
  89. then you can perform various actions. If you don't call find_* method first, all
  90. your further actions will fail for that element. Finally, just remember that you
  91. don't have to instantiate WebElement objects at all - they will be automatically
  92. created when you use the find_* methods.
  93. =cut
  94. =head1 FUNCTIONS
  95. =cut
  96. =head2 new
  97. Description:
  98. Constructor for Driver. It'll instantiate the object if it can communicate
  99. with the Selenium RC server.
  100. Input: 7 (all optional)
  101. desired_capabilities - HASH - Following options are accepted:
  102. Optional:
  103. 'remote_server_addr' - <string> - IP or FQDN of the RC server machine
  104. 'browser_name' - <string> - desired browser string:
  105. {iphone|firefox|internet explorer|htmlunit|iphone|chrome}
  106. 'version' - <string> - desired browser version number
  107. 'platform' - <string> - desired platform:
  108. {WINDOWS|XP|VISTA|MAC|LINUX|UNIX|ANY}
  109. 'javascript' - <boolean> - whether javascript should be supported
  110. 'auto_close' - <boolean> - whether driver should end session on remote
  111. server on close.
  112. 'extra_capabilities' - HASH of extra capabilities
  113. If no values are provided, then these defaults will be assumed:
  114. 'remote_server_addr' => 'localhost'
  115. 'port' => '4444'
  116. 'browser_name' => 'firefox'
  117. 'version' => ''
  118. 'platform' => 'ANY'
  119. 'javascript' => 1
  120. 'auto_close' => 1
  121. Output:
  122. Remote Driver object
  123. Usage:
  124. my $driver = new Selenium::Remote::Driver;
  125. or
  126. my $driver = new Selenium::Remote::Driver('browser_name' => 'firefox',
  127. 'platform' => 'MAC');
  128. or
  129. my $driver = new Selenium::Remote::Driver('remote_server_addr' => '10.10.1.1',
  130. 'port' => '2222',
  131. auto_close => 0
  132. );
  133. or
  134. my $driver = new Selenium::Remote::Driver('browser_name' => 'chrome',
  135. 'platform' => 'VISTA',
  136. 'extra_capabilities' => {'chrome.switches' => ["--user-data-dir=$ENV{LOCALAPPDATA}\\Google\\Chrome\\User Data"],},
  137. );
  138. =cut
  139. sub new {
  140. my ( $class, %args ) = @_;
  141. my $ress = new Selenium::Remote::Commands;
  142. # Set the defaults if user doesn't send any
  143. my $self = {
  144. remote_server_addr => delete $args{remote_server_addr} || 'localhost',
  145. browser_name => delete $args{browser_name} || 'firefox',
  146. platform => delete $args{platform} || 'ANY',
  147. port => delete $args{port} || '4444',
  148. version => delete $args{version} || '',
  149. session_id => undef,
  150. remote_conn => undef,
  151. commands => $ress,
  152. auto_close => 1, # by default we will close remote session on DESTROY
  153. pid => $$,
  154. };
  155. bless $self, $class or die "Can't bless $class: $!";
  156. if ( defined $args{javascript} ) {
  157. if ( $args{javascript} ) {
  158. $self->{javascript} = JSON::true;
  159. }
  160. else {
  161. $self->{javascript} = JSON::false;
  162. }
  163. }
  164. else {
  165. $self->{javascript} = JSON::true;
  166. }
  167. # Connect to remote server & establish a new session
  168. $self->{remote_conn} =
  169. new Selenium::Remote::RemoteConnection( $self->{remote_server_addr},
  170. $self->{port} );
  171. $self->new_session(delete $args{extra_capabilities});
  172. if ( !( defined $self->{session_id} ) ) {
  173. croak "Could not establish a session with the remote server\n";
  174. }
  175. return $self;
  176. }
  177. sub DESTROY {
  178. my ($self) = @_;
  179. return if $$ != $self->{pid};
  180. $self->quit() if ($self->{auto_close} && defined $self->{session_id});
  181. }
  182. # This is an internal method used the Driver & is not supposed to be used by
  183. # end user. This method is used by Driver to set up all the parameters
  184. # (url & JSON), send commands & receive processed response from the server.
  185. sub _execute_command {
  186. my ( $self, $res, $params ) = @_;
  187. $res->{'session_id'} = $self->{'session_id'};
  188. my $resource = $self->{commands}->get_params($res);
  189. if ($resource) {
  190. my $resp = $self->{remote_conn}
  191. ->request( $resource->{'method'}, $resource->{'url'}, $params );
  192. if(ref($resp) eq 'HASH') {
  193. if($resp->{cmd_status} eq 'OK') {
  194. return $resp->{cmd_return};
  195. } else {
  196. my $msg = "Error while executing command";
  197. if($resp->{cmd_error}) {
  198. $msg .= ": $resp->{cmd_error}" if $resp->{cmd_error};
  199. } else {
  200. if(ref($resp->{cmd_return}) eq 'HASH') {
  201. $msg .= ": $resp->{cmd_return}->{error}->{msg}"
  202. if $resp->{cmd_return}->{error}->{msg};
  203. } else {
  204. $msg .= ": $resp->{cmd_return}";
  205. }
  206. }
  207. croak $msg;
  208. }
  209. }
  210. return $resp;
  211. }
  212. else {
  213. croak "Couldn't retrieve command settings properly\n";
  214. }
  215. }
  216. # A method that is used by the Driver itself. It'll be called to set the
  217. # desired capabilities on the server.
  218. sub new_session {
  219. my ($self, $extra_capabilities) = @_;
  220. $extra_capabilities ||= {};
  221. my $args = {
  222. 'desiredCapabilities' => {
  223. 'browserName' => $self->{browser_name},
  224. 'platform' => $self->{platform},
  225. 'javascriptEnabled' => $self->{javascript},
  226. 'version' => $self->{version},
  227. %$extra_capabilities,
  228. },
  229. };
  230. my $resp =
  231. $self->{remote_conn}
  232. ->request( $self->{commands}->{'newSession'}->{'method'},
  233. $self->{commands}->{'newSession'}->{'url'}, $args, );
  234. if ( ( defined $resp->{'sessionId'} ) && $resp->{'sessionId'} ne '' ) {
  235. $self->{session_id} = $resp->{'sessionId'};
  236. }
  237. else {
  238. croak "Could not create new session";
  239. }
  240. }
  241. =head2 status
  242. Description:
  243. Query the server's current status. All server implementations
  244. should return two basic objects describing the server's current
  245. platform and when the server was built.
  246. Output:
  247. Hash ref
  248. Usage:
  249. print Dumper $driver->status;
  250. =cut
  251. sub status {
  252. my ($self) = @_;
  253. my $res = { 'command' => 'status' };
  254. return $self->_execute_command($res);
  255. }
  256. =head2 mouse_move_to_location
  257. Description:
  258. Move the mouse by an offset of the specificed element. If no
  259. element is specified, the move is relative to the current mouse
  260. cursor. If an element is provided but no offset, the mouse will be
  261. moved to the center of the element. If the element is not visible,
  262. it will be scrolled into view.
  263. Output:
  264. STRING -
  265. Usage:
  266. # element - the element to move to. If not specified or is null, the offset is relative to current position of the mouse.
  267. # 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.
  268. # 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.
  269. print $driver->mouse_move_to_location(element => e, xoffset => x, yoffset => y);
  270. =cut
  271. sub mouse_move_to_location {
  272. my ($self, %params) = @_;
  273. $params{element} = $params{element}{id} if exists $params{element};
  274. my $res = { 'command' => 'mouseMoveToLocation' };
  275. return $self->_execute_command($res, \%params);
  276. }
  277. =head2 get_capabilities
  278. Description:
  279. Retrieve the capabilities of the specified session.
  280. Output:
  281. HASH of all the capabilities.
  282. Usage:
  283. my $capab = $driver->get_capabilities();
  284. print Dumper($capab);
  285. =cut
  286. sub get_capabilities {
  287. my $self = shift;
  288. my $res = {'command' => 'getCapabilities'};
  289. return $self->_execute_command($res);
  290. }
  291. =head2 set_implicit_wait_timeout
  292. Description:
  293. Set the amount of time the driver should wait when searching for elements.
  294. When searching for a single element, the driver will poll the page until
  295. an element is found or the timeout expires, whichever occurs first.
  296. When searching for multiple elements, the driver should poll the page until
  297. at least one element is found or the timeout expires, at which point it
  298. will return an empty list. If this method is never called, the driver will
  299. default to an implicit wait of 0ms.
  300. Input:
  301. Time in milliseconds.
  302. Output:
  303. Server Response Hash with no data returned back from the server.
  304. Usage:
  305. $driver->set_implicit_wait_timeout(10);
  306. =cut
  307. sub set_implicit_wait_timeout {
  308. my ($self, $ms) = @_;
  309. my $res = {'command' => 'setImplicitWaitTimeout'};
  310. my $params = {'ms' => $ms};
  311. return $self->_execute_command($res, $params);
  312. }
  313. =head2 quit
  314. Description:
  315. Delete the session & close open browsers.
  316. Usage:
  317. $driver->quit();
  318. =cut
  319. sub quit {
  320. my $self = shift;
  321. my $res = { 'command' => 'quit' };
  322. $self->_execute_command($res);
  323. $self->{session_id} = undef;
  324. }
  325. =head2 get_current_window_handle
  326. Description:
  327. Retrieve the current window handle.
  328. Output:
  329. STRING - the window handle
  330. Usage:
  331. print $driver->get_current_window_handle();
  332. =cut
  333. sub get_current_window_handle {
  334. my $self = shift;
  335. my $res = { 'command' => 'getCurrentWindowHandle' };
  336. return $self->_execute_command($res);
  337. }
  338. =head2 get_current_window_handles
  339. Description:
  340. Retrieve the list of window handles used in the session.
  341. Output:
  342. ARRAY of STRING - list of the window handles
  343. Usage:
  344. print Dumper($driver->get_current_window_handles());
  345. =cut
  346. sub get_window_handles {
  347. my $self = shift;
  348. my $res = { 'command' => 'getWindowHandles' };
  349. return $self->_execute_command($res);
  350. }
  351. =head2 get_current_url
  352. Description:
  353. Retrieve the url of the current page
  354. Output:
  355. STRING - url
  356. Usage:
  357. print $driver->get_current_url();
  358. =cut
  359. sub get_current_url {
  360. my $self = shift;
  361. my $res = { 'command' => 'getCurrentUrl' };
  362. return $self->_execute_command($res);
  363. }
  364. =head2 navigate
  365. Description:
  366. Navigate to a given url. This is same as get() method.
  367. Input:
  368. STRING - url
  369. Usage:
  370. $driver->navigate('http://www.google.com');
  371. =cut
  372. sub navigate {
  373. my ( $self, $url ) = @_;
  374. $self->get($url);
  375. }
  376. =head2 get
  377. Description:
  378. Navigate to a given url
  379. Input:
  380. STRING - url
  381. Usage:
  382. $driver->get('http://www.google.com');
  383. =cut
  384. sub get {
  385. my ( $self, $url ) = @_;
  386. my $res = { 'command' => 'get' };
  387. my $params = { 'url' => $url };
  388. return $self->_execute_command( $res, $params );
  389. }
  390. =head2 get_title
  391. Description:
  392. Get the current page title
  393. Output:
  394. STRING - Page title
  395. Usage:
  396. print $driver->get_title();
  397. =cut
  398. sub get_title {
  399. my $self = shift;
  400. my $res = { 'command' => 'getTitle' };
  401. return $self->_execute_command($res);
  402. }
  403. =head2 go_back
  404. Description:
  405. Equivalent to hitting the back button on the browser.
  406. Usage:
  407. $driver->go_back();
  408. =cut
  409. sub go_back {
  410. my $self = shift;
  411. my $res = { 'command' => 'goBack' };
  412. return $self->_execute_command($res);
  413. }
  414. =head2 go_forward
  415. Description:
  416. Equivalent to hitting the forward button on the browser.
  417. Usage:
  418. $driver->go_forward();
  419. =cut
  420. sub go_forward {
  421. my $self = shift;
  422. my $res = { 'command' => 'goForward' };
  423. return $self->_execute_command($res);
  424. }
  425. =head2 refresh
  426. Description:
  427. Reload the current page.
  428. Usage:
  429. $driver->refresh();
  430. =cut
  431. sub refresh {
  432. my $self = shift;
  433. my $res = { 'command' => 'goForward' };
  434. return $self->_execute_command($res);
  435. }
  436. =head2 javascript
  437. Description:
  438. returns true if javascript is enabled in the driver.
  439. Usage:
  440. if ($driver->javascript) { ...; }
  441. =cut
  442. sub javascript {
  443. my $self = shift;
  444. return $self->{javascript} == JSON::true;
  445. }
  446. =head2 execute_async_script
  447. Description:
  448. Inject a snippet of JavaScript into the page for execution in the context
  449. of the currently selected frame. The executed script is assumed to be
  450. asynchronous and must signal that is done by invoking the provided
  451. callback, which is always provided as the final argument to the function.
  452. The value to this callback will be returned to the client.
  453. Asynchronous script commands may not span page loads. If an unload event
  454. is fired while waiting for a script result, an error should be returned
  455. to the client.
  456. Input: 2 (1 optional)
  457. Required:
  458. STRING - Javascript to execute on the page
  459. Optional:
  460. ARRAY - list of arguments that need to be passed to the script.
  461. Output:
  462. {*} - Varied, depending on the type of result expected back from the script.
  463. Usage:
  464. my $script = q{
  465. var arg1 = arguments[0];
  466. var callback = arguments[arguments.length-1];
  467. var elem = window.document.findElementById(arg1);
  468. callback(elem);
  469. };
  470. my $callback = q{return arguments[0];};
  471. my $elem = $driver->execute_async_script($script,'myid',$callback);
  472. $elem->click;
  473. =cut
  474. sub execute_async_script {
  475. my ( $self, $script, @args ) = @_;
  476. if ($self->javascript) {
  477. if ( not defined $script ) {
  478. return 'No script provided';
  479. }
  480. my $res = { 'command' => 'executeAsyncScript' };
  481. # Check the args array if the elem obj is provided & replace it with
  482. # JSON representation
  483. for (my $i=0; $i<@args; $i++) {
  484. if (ref $args[$i] eq 'Selenium::Remote::WebElement') {
  485. $args[$i] = {'ELEMENT' => ($args[$i])->{id}};
  486. }
  487. }
  488. my $params = {'script' => $script, 'args' => \@args};
  489. my $ret = $self->_execute_command($res, $params);
  490. # replace any ELEMENTS with WebElement
  491. if (ref($ret) and (ref($ret) eq 'HASH') and exists $ret->{'ELEMENT'}) {
  492. $ret =
  493. new Selenium::Remote::WebElement(
  494. $ret->{ELEMENT}, $self);
  495. }
  496. return $ret;
  497. }
  498. else {
  499. croak 'Javascript is not enabled on remote driver instance.';
  500. }
  501. }
  502. =head2 execute_script
  503. Description:
  504. Inject a snippet of JavaScript into the page and return its result.
  505. WebElements that should be passed to the script as an argument should be
  506. specified in the arguments array as WebElement object. Likewise,
  507. any WebElements in the script result will be returned as WebElement object.
  508. Input: 2 (1 optional)
  509. Required:
  510. STRING - Javascript to execute on the page
  511. Optional:
  512. ARRAY - list of arguments that need to be passed to the script.
  513. Output:
  514. {*} - Varied, depending on the type of result expected back from the script.
  515. Usage:
  516. my $script = q{
  517. var arg1 = arguments[0];
  518. var elem = window.document.findElementById(arg1);
  519. return elem;
  520. };
  521. my $elem = $driver->execute_script($script,'myid');
  522. $elem->click;
  523. =cut
  524. sub execute_script {
  525. my ( $self, $script, @args ) = @_;
  526. if ($self->javascript) {
  527. if ( not defined $script ) {
  528. return 'No script provided';
  529. }
  530. my $res = { 'command' => 'executeScript' };
  531. # Check the args array if the elem obj is provided & replace it with
  532. # JSON representation
  533. for (my $i=0; $i<@args; $i++) {
  534. if (ref $args[$i] eq 'Selenium::Remote::WebElement') {
  535. $args[$i] = {'ELEMENT' => ($args[$i])->{id}};
  536. }
  537. }
  538. my $params = {'script' => $script, 'args' => [@args]};
  539. my $ret = $self->_execute_command($res, $params);
  540. # replace any ELEMENTS with WebElement
  541. if (ref($ret) and (ref($ret) eq 'HASH') and exists $ret->{'ELEMENT'}) {
  542. $ret =
  543. new Selenium::Remote::WebElement(
  544. $ret->{ELEMENT}, $self);
  545. }
  546. return $ret;
  547. }
  548. else {
  549. croak 'Javascript is not enabled on remote driver instance.';
  550. }
  551. }
  552. =head2 screenshot
  553. Description:
  554. Get a screenshot of the current page as a base64 encoded image.
  555. Output:
  556. STRING - base64 encoded image
  557. Usage:
  558. print $driver->go_screenshot();
  559. =cut
  560. sub screenshot {
  561. my ($self) = @_;
  562. my $res = { 'command' => 'screenshot' };
  563. return $self->_execute_command($res);
  564. }
  565. =head2 available_engines
  566. Description:
  567. List all available engines on the machine. To use an engine, it has to be present in this list.
  568. Output:
  569. {Array.<string>} A list of available engines
  570. Usage:
  571. print Dumper $driver->available_engines;
  572. =cut
  573. sub available_engines {
  574. my ($self) = @_;
  575. my $res = { 'command' => 'availableEngines' };
  576. return $self->_execute_command($res);
  577. }
  578. =head2 switch_to_frame
  579. Description:
  580. Change focus to another frame on the page. If the frame ID is null, the
  581. server will switch to the page's default content.
  582. Input: 1
  583. Required:
  584. {STRING | NUMBER | NULL} - ID of the frame which can be one of the three
  585. mentioned.
  586. Usage:
  587. $driver->switch_to_frame('frame_1');
  588. =cut
  589. sub switch_to_frame {
  590. my ( $self, $id ) = @_;
  591. my $json_null = JSON::null;
  592. $id = ( defined $id ) ? $id : $json_null;
  593. my $res = { 'command' => 'switchToFrame' };
  594. my $params = { 'id' => $id };
  595. return $self->_execute_command( $res, $params );
  596. }
  597. =head2 switch_to_window
  598. Description:
  599. Change focus to another window. The window to change focus to may be
  600. specified by its server assigned window handle, or by the value of its name
  601. attribute.
  602. Input: 1
  603. Required:
  604. STRING - Window handle or the Window name
  605. Usage:
  606. $driver->switch_to_window('MY Homepage');
  607. =cut
  608. sub switch_to_window {
  609. my ( $self, $name ) = @_;
  610. if ( not defined $name ) {
  611. return 'Window name not provided';
  612. }
  613. my $res = { 'command' => 'switchToWindow' };
  614. my $params = { 'name' => $name };
  615. return $self->_execute_command( $res, $params );
  616. }
  617. =head2 get_speed
  618. Description:
  619. Get the current user input speed. The actual input speed is still browser
  620. specific and not covered by the Driver.
  621. Output:
  622. STRING - One of these: SLOW, MEDIUM, FAST
  623. Usage:
  624. print $driver->get_speed();
  625. =cut
  626. sub get_speed {
  627. my ($self) = @_;
  628. my $res = { 'command' => 'getSpeed' };
  629. return $self->_execute_command($res);
  630. }
  631. =head2 set_speed
  632. Description:
  633. Set the user input speed.
  634. Input:
  635. STRING - One of these: SLOW, MEDIUM, FAST
  636. Usage:
  637. $driver->set_speed('MEDIUM');
  638. Note: This function is a no-op in WebDriver (?). See
  639. https://groups.google.com/d/topic/selenium-users/oX0ZnYFPuSA/discussion and
  640. http://code.google.com/p/selenium/source/browse/trunk/java/client/src/org/openqa/selenium/WebDriverCommandProcessor.java
  641. =cut
  642. sub set_speed {
  643. my ( $self, $speed ) = @_;
  644. if ( not defined $speed ) {
  645. return 'Speed not provided.';
  646. }
  647. my $res = { 'command' => 'setSpeed' };
  648. my $params = { 'speed' => $speed };
  649. return $self->_execute_command( $res, $params );
  650. }
  651. =head2 get_all_cookies
  652. Description:
  653. Retrieve all cookies visible to the current page. Each cookie will be
  654. returned as a HASH reference with the following keys & their value types:
  655. 'name' - STRING
  656. 'value' - STRING
  657. 'path' - STRING
  658. 'domain' - STRING
  659. 'secure' - BOOLEAN
  660. Output:
  661. ARRAY of HASHES - list of all the cookie hashes
  662. Usage:
  663. print Dumper($driver->get_all_cookies());
  664. =cut
  665. sub get_all_cookies {
  666. my ($self) = @_;
  667. my $res = { 'command' => 'getAllCookies' };
  668. return $self->_execute_command($res);
  669. }
  670. =head2 add_cookie
  671. Description:
  672. Set a cookie on the domain.
  673. Input: 5 (1 optional)
  674. Required:
  675. 'name' - STRING
  676. 'value' - STRING
  677. 'path' - STRING
  678. 'domain' - STRING
  679. Optional:
  680. 'secure' - BOOLEAN - default is false.
  681. Usage:
  682. $driver->add_cookie('foo', 'bar', '/', '.google.com', 0)
  683. =cut
  684. sub add_cookie {
  685. my ( $self, $name, $value, $path, $domain, $secure ) = @_;
  686. if ( ( not defined $name )
  687. || ( not defined $value )
  688. || ( not defined $path )
  689. || ( not defined $domain ) )
  690. {
  691. return "Missing parameters";
  692. }
  693. my $res = { 'command' => 'addCookie' };
  694. my $json_false = JSON::false;
  695. my $json_true = JSON::true;
  696. $secure = ( defined $secure ) ? $json_true : $json_false;
  697. my $params = {
  698. 'cookie' => {
  699. 'name' => $name,
  700. 'value' => $value,
  701. 'path' => $path,
  702. 'domain' => $domain,
  703. 'secure' => $secure,
  704. }
  705. };
  706. return $self->_execute_command( $res, $params );
  707. }
  708. =head2 delete_all_cookies
  709. Description:
  710. Delete all cookies visible to the current page.
  711. Usage:
  712. $driver->delete_all_cookies();
  713. =cut
  714. sub delete_all_cookies {
  715. my ($self) = @_;
  716. my $res = { 'command' => 'deleteAllCookies' };
  717. return $self->_execute_command($res);
  718. }
  719. =head2 delete_cookie_named
  720. Description:
  721. Delete the cookie with the given name. This command will be a no-op if there
  722. is no such cookie visible to the current page.
  723. Input: 1
  724. Required:
  725. STRING - name of cookie to delete
  726. Usage:
  727. $driver->delete_cookie_named('foo');
  728. =cut
  729. sub delete_cookie_named {
  730. my ( $self, $cookie_name ) = @_;
  731. if ( not defined $cookie_name ) {
  732. return "Cookie name not provided";
  733. }
  734. my $res = { 'command' => 'deleteAllCookies', 'name' => $cookie_name };
  735. return $self->_execute_command($res);
  736. }
  737. =head2 get_page_source
  738. Description:
  739. Get the current page source.
  740. Output:
  741. STRING - The page source.
  742. Usage:
  743. print $driver->get_page_source();
  744. =cut
  745. sub get_page_source {
  746. my ($self) = @_;
  747. my $res = { 'command' => 'getPageSource' };
  748. return $self->_execute_command($res);
  749. }
  750. =head2 find_element
  751. Description:
  752. Search for an element on the page, starting from the document root. The
  753. located element will be returned as a WebElement object.
  754. Input: 2 (1 optional)
  755. Required:
  756. STRING - The search target.
  757. Optional:
  758. STRING - Locator scheme to use to search the element, available schemes:
  759. {class, class_name, css, id, link, link_text, partial_link_text,
  760. tag_name, name, xpath}
  761. Defaults to 'xpath'.
  762. Output:
  763. Selenium::Remote::WebElement - WebElement Object
  764. Usage:
  765. $driver->find_element("//input[\@name='q']");
  766. =cut
  767. sub find_element {
  768. my ( $self, $query, $method ) = @_;
  769. if ( not defined $query ) {
  770. return 'Search string to find element not provided.';
  771. }
  772. my $using = ( defined $method ) ? FINDERS->{$method} : 'xpath';
  773. if (defined $using) {
  774. my $res = { 'command' => 'findElement' };
  775. my $params = { 'using' => $using, 'value' => $query };
  776. my $ret_data = eval { $self->_execute_command( $res, $params ); };
  777. if($@) {
  778. if($@ =~ /(An element could not be located on the page using the given search parameters)/) {
  779. # give details on what element wasn't found
  780. $@ = "$1: $query,$method";
  781. croak $@;
  782. } else {
  783. # re throw if the exception wasn't what we expected
  784. die $@;
  785. }
  786. }
  787. return new Selenium::Remote::WebElement($ret_data->{ELEMENT}, $self);
  788. }
  789. else {
  790. croak "Bad method, expected - class, class_name, css, id, link,
  791. link_text, partial_link_text, name, tag_name, xpath";
  792. }
  793. }
  794. =head2 find_elements
  795. Description:
  796. Search for multiple elements on the page, starting from the document root.
  797. The located elements will be returned as an array of WebElement object.
  798. Input: 2 (1 optional)
  799. Required:
  800. STRING - The search target.
  801. Optional:
  802. STRING - Locator scheme to use to search the element, available schemes:
  803. {class, class_name, css, id, link, link_text, partial_link_text,
  804. tag_name, name, xpath}
  805. Defaults to 'xpath'.
  806. Output:
  807. ARRAY of Selenium::Remote::WebElement - Array of WebElement Objects
  808. Usage:
  809. $driver->find_elements("//input");
  810. =cut
  811. sub find_elements {
  812. my ( $self, $query, $method ) = @_;
  813. if ( not defined $query ) {
  814. return 'Search string to find element not provided.';
  815. }
  816. my $using = ( defined $method ) ? FINDERS->{$method} : 'xpath';
  817. if (defined $using) {
  818. my $res = { 'command' => 'findElements' };
  819. my $params = { 'using' => $using, 'value' => $query };
  820. my $ret_data = eval {$self->_execute_command( $res, $params );};
  821. if($@) {
  822. if($@ =~ /(An element could not be located on the page using the given search parameters)/) {
  823. # give details on what element wasn't found
  824. $@ = "$1: $query,$method";
  825. croak $@;
  826. } else {
  827. # re throw if the exception wasn't what we expected
  828. die $@;
  829. }
  830. }
  831. my $elem_obj_arr;
  832. my $i = 0;
  833. foreach (@$ret_data) {
  834. $elem_obj_arr->[$i] = new Selenium::Remote::WebElement($_->{ELEMENT}, $self);
  835. $i++;
  836. }
  837. return $elem_obj_arr;
  838. }
  839. else {
  840. croak "Bad method, expected - class, class_name, css, id, link,
  841. link_text, partial_link_text, name, tag_name, xpath";
  842. }
  843. }
  844. =head2 find_child_element
  845. Description:
  846. Search for an element on the page, starting from the identified element. The
  847. located element will be returned as a WebElement object.
  848. Input: 3 (1 optional)
  849. Required:
  850. Selenium::Remote::WebElement - WebElement object from where you want to
  851. start searching.
  852. STRING - The search target.
  853. Optional:
  854. STRING - Locator scheme to use to search the element, available schemes:
  855. {class, class_name, css, id, link, link_text, partial_link_text,
  856. tag_name, name, xpath}
  857. Defaults to 'xpath'.
  858. Output:
  859. Selenium::Remote::WebElement - WebElement Object
  860. Usage:
  861. my $elem1 = $driver->find_element("//select[\@name='ned']");
  862. my $child = $driver->find_child_element($elem1, "//option[\@value='es_ar']");
  863. =cut
  864. sub find_child_element {
  865. my ( $self, $elem, $query, $method ) = @_;
  866. if ( ( not defined $elem ) || ( not defined $query ) ) {
  867. return "Missing parameters";
  868. }
  869. my $using = ( defined $method ) ? $method : 'xpath';
  870. if (exists FINDERS->{$using}) {
  871. my $res = { 'command' => 'findChildElement', 'id' => $elem->{id} };
  872. my $params = { 'using' => $using, 'value' => $query };
  873. my $ret_data = eval {$self->_execute_command( $res, $params );};
  874. if($@) {
  875. if($@ =~ /(An element could not be located on the page using the given search parameters)/) {
  876. # give details on what element wasn't found
  877. $@ = "$1: $query,$method";
  878. croak $@;
  879. } else {
  880. # re throw if the exception wasn't what we expected
  881. die $@;
  882. }
  883. }
  884. return new Selenium::Remote::WebElement($ret_data->{ELEMENT}, $self);
  885. }
  886. else {
  887. croak "Bad method, expected - class, class_name, css, id, link,
  888. link_text, partial_link_text, name, tag_name, xpath";
  889. }
  890. }
  891. =head2 find_child_elements
  892. Description:
  893. Search for multiple element on the page, starting from the identified
  894. element. The located elements will be returned as an array of WebElement
  895. objects.
  896. Input: 3 (1 optional)
  897. Required:
  898. Selenium::Remote::WebElement - WebElement object from where you want to
  899. start searching.
  900. STRING - The search target.
  901. Optional:
  902. STRING - Locator scheme to use to search the element, available schemes:
  903. {class, class_name, css, id, link, link_text, partial_link_text,
  904. tag_name, name, xpath}
  905. Defaults to 'xpath'.
  906. Output:
  907. ARRAY of Selenium::Remote::WebElement - Array of WebElement Objects.
  908. Usage:
  909. my $elem1 = $driver->find_element("//select[\@name='ned']");
  910. my $child = $driver->find_child_elements($elem1, "//option");
  911. =cut
  912. sub find_child_elements {
  913. my ( $self, $elem, $query, $method ) = @_;
  914. if ( ( not defined $elem ) || ( not defined $query ) ) {
  915. return "Missing parameters";
  916. }
  917. my $using = ( defined $method ) ? $method : 'xpath';
  918. if (exists FINDERS->{$using}) {
  919. my $res = { 'command' => 'findChildElements', 'id' => $elem->{id} };
  920. my $params = { 'using' => $using, 'value' => $query };
  921. my $ret_data = eval {$self->_execute_command( $res, $params );};
  922. if($@) {
  923. if($@ =~ /(An element could not be located on the page using the given search parameters)/) {
  924. # give details on what element wasn't found
  925. $@ = "$1: $query,$method";
  926. croak $@;
  927. } else {
  928. # re throw if the exception wasn't what we expected
  929. die $@;
  930. }
  931. }
  932. my $elem_obj_arr;
  933. my $i = 0;
  934. foreach (@$ret_data) {
  935. $elem_obj_arr->[$i] = new Selenium::Remote::WebElement($_->{ELEMENT}, $self);
  936. $i++;
  937. }
  938. return $elem_obj_arr;
  939. }
  940. else {
  941. croak "Bad method, expected - class, class_name, css, id, link,
  942. link_text, partial_link_text, name, tag_name, xpath";
  943. }
  944. }
  945. =head2 get_active_element
  946. Description:
  947. Get the element on the page that currently has focus.. The located element
  948. will be returned as a WebElement object.
  949. Output:
  950. Selenium::Remote::WebElement - WebElement Object
  951. Usage:
  952. $driver->get_active_element();
  953. =cut
  954. sub get_active_element {
  955. my ($self) = @_;
  956. my $res = { 'command' => 'getActiveElement' };
  957. return $self->_execute_command($res);
  958. }
  959. # Not yet supported on the server
  960. sub describe_element {
  961. my ( $self, $element ) = @_;
  962. #if (not defined $element) {
  963. # return "Element not provided";
  964. #}
  965. #my $res = {'command' => 'desribeElement', 'name' => $element};
  966. #return $self->_execute_command($res);
  967. return "Not yet supported";
  968. }
  969. =head2 send_modifier
  970. Description:
  971. Send an event to the active element to depress or release a modifier key.
  972. Input: 2
  973. Required:
  974. 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
  975. isdown - Boolean/String - Whether to generate a key down or key up
  976. Usage:
  977. $driver->send_modifier('Alt','down');
  978. $elem->send_keys('c');
  979. $driver->send_modifier('Alt','up');
  980. or
  981. $driver->send_modifier('Alt',1);
  982. $elem->send_keys('c');
  983. $driver->send_modifier('Alt',0);
  984. =cut
  985. sub send_modifier {
  986. my ($self,$modifier,$isdown) = @_;
  987. if($isdown =~ /(down|up)/) {
  988. $isdown = $isdown =~ /down/ ? 1:0;
  989. }
  990. my $res = {'command' => 'sendModifier'};
  991. my $params = {value => $modifier,
  992. isdown => $isdown};
  993. return $self->_execute_command($res,$params);
  994. }
  995. =head2 compare_elements
  996. Description:
  997. Test if two element IDs refer to the same DOM element.
  998. Input: 2
  999. Required:
  1000. Selenium::Remote::WebElement - WebElement Object
  1001. Selenium::Remote::WebElement - WebElement Object
  1002. Output:
  1003. BOOLEAN
  1004. Usage:
  1005. $driver->compare_elements($elem_obj1, $elem_obj2);
  1006. =cut
  1007. sub compare_elements {
  1008. my ($self, $elem1, $elem2) = @_;
  1009. my $res = { 'command' => 'elementEquals',
  1010. 'id' => $elem1->{id},
  1011. 'other' => $elem2->{id}
  1012. };
  1013. return $self->_execute_command($res);
  1014. }
  1015. 1;
  1016. __END__
  1017. =head1 SEE ALSO
  1018. For more information about Selenium , visit the website at
  1019. L<http://code.google.com/p/selenium/>.
  1020. =head1 BUGS
  1021. The Selenium issue tracking system is available online at
  1022. L<http://github.com/aivaturi/Selenium-Remote-Driver/issues>.
  1023. =head1 AUTHOR
  1024. Perl Bindings for Selenium Remote Driver by Aditya Ivaturi <ivaturi@gmail.com>
  1025. =head1 LICENSE
  1026. Copyright (c) 2010 Aditya Ivaturi
  1027. Licensed under the Apache License, Version 2.0 (the "License");
  1028. you may not use this file except in compliance with the License.
  1029. You may obtain a copy of the License at
  1030. http://www.apache.org/licenses/LICENSE-2.0
  1031. Unless required by applicable law or agreed to in writing, software
  1032. distributed under the License is distributed on an "AS IS" BASIS,
  1033. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  1034. See the License for the specific language governing permissions and
  1035. limitations under the License.