Driver.pm 32 KB

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