1
0

Driver.pm 32 KB

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