1
0

Driver.pm 32 KB

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