Driver.pm 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746
  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 => 'ClassName',
  11. class_name => 'ClassName',
  12. css => 'CssSelector',
  13. id => 'Id',
  14. link => 'LinkText',
  15. link_text => 'LinkText',
  16. name => 'Name',
  17. partial_link_text => 'PartialLinkText',
  18. tag_name => 'TagName',
  19. xpath => 'Xpath',
  20. };
  21. our $VERSION = "0.10";
  22. =head1 NAME
  23. Selenium::Remote::Driver - Perl Client for Selenium Remote Driver
  24. =cut
  25. =head1 SYNOPSIS
  26. use Selenium::Remote::Driver;
  27. my $driver = new Selenium::Remote::Driver;
  28. $driver->get('http://www.google.com');
  29. print $driver->get_title();
  30. $driver->quit();
  31. =cut
  32. =head1 DESCRIPTION
  33. Selenium is a test tool that allows you to write
  34. automated web application UI tests in any programming language against
  35. any HTTP website using any mainstream JavaScript-enabled browser. This module is
  36. an implementation of the Perl Bindings (client) for the Remote driver that
  37. Selenium provides. You can find bindings for other languages at this location:
  38. L<http://code.google.com/p/selenium/>
  39. This module sends commands directly to the Server using simple HTTP requests.
  40. Using this module together with the Selenium Server, you can automatically
  41. control any supported browser.
  42. To use this module, you need to have already downloaded and started
  43. the Selenium Server. (The Selenium Server is a Java application.)
  44. =cut
  45. =head1 FUNCTIONS
  46. =cut
  47. =head2 new
  48. Description:
  49. Constructor for Driver. It'll instantiate the object if it can communicate
  50. with the Selenium RC server.
  51. Input Parameter: 1
  52. desired_capabilities - HASH - Following options are accepted:
  53. Optional:
  54. 'remote_server_addr' - <string> - IP or FQDN of the RC server machine
  55. 'browser_name' - <string> - desired browser string:
  56. {iphone|firefox|internet explorer|htmlunit|iphone|chrome}
  57. 'version' - <string> - desired browser version number
  58. 'platform' - <string> - desired platform:
  59. {WINDOWS|XP|VISTA|MAC|LINUX|UNIX|ANY}
  60. 'javascript' - <boolean> - whether javascript should be supported
  61. If no values are provided, then these defaults will be assumed:
  62. 'remote_server_addr' => 'localhost'
  63. 'browser_name' => 'firefox'
  64. 'version' => ''
  65. 'platform' => 'ANY'
  66. 'javascript' => 1
  67. Output:
  68. Remote Driver object
  69. Usage:
  70. my $driver = new Selenium::Remote::Driver;
  71. or
  72. my $driver = new Selenium::Remote::Driver('browser_name' => '10.37.129.2',
  73. 'platform' => 'MAC')
  74. =cut
  75. sub new {
  76. my ( $class, %args ) = @_;
  77. my $ress = new Selenium::Remote::Commands;
  78. # Set the defaults if user doesn't send any
  79. my $self = {
  80. remote_server_addr => delete $args{remote_server_addr} || 'localhost',
  81. browser_name => delete $args{browser_name} || 'firefox',
  82. platform => delete $args{platform} || 'ANY',
  83. port => delete $args{port} || '4444',
  84. version => delete $args{version} || '',
  85. session_id => undef,
  86. remote_conn => undef,
  87. commands => $ress,
  88. auto_close => 1, # by default we will close remote session on DESTROY
  89. };
  90. bless $self, $class or die "Can't bless $class: $!";
  91. if ( defined $args{javascript} ) {
  92. if ( $args{javascript} ) {
  93. $self->{javascript} = JSON::true;
  94. }
  95. else {
  96. $self->{javascript} = JSON::false;
  97. }
  98. }
  99. else {
  100. $self->{javascript} = JSON::true;
  101. }
  102. # Connect to remote server & establish a new session
  103. $self->{remote_conn} =
  104. new Selenium::Remote::RemoteConnection( $self->{remote_server_addr},
  105. $self->{port} );
  106. $self->new_session();
  107. if ( !( defined $self->{session_id} ) ) {
  108. croak "Could not establish a session with the remote server\n";
  109. }
  110. return $self;
  111. }
  112. sub DESTROY {
  113. my ($self) = @_;
  114. $self->quit() if ($self->{auto_close} && defined $self->{session_id});
  115. }
  116. # This is an internal method used the Driver & is not supposed to be used by
  117. # end user. This method is used by Driver to set up all the parameters (url & JSON),
  118. # send commands & receive response from the server.
  119. sub _execute_command {
  120. my ( $self, $res, $params ) = @_;
  121. $res->{'session_id'} = $self->{'session_id'};
  122. my $resource = $self->{commands}->get_params($res);
  123. if ($resource) {
  124. my $resp = $self->{remote_conn}
  125. ->request( $resource->{'method'}, $resource->{'url'}, $params );
  126. return $resp;
  127. }
  128. else {
  129. croak "Couldn't retrieve command settings properly\n";
  130. }
  131. }
  132. # A method that is used by the Driver itself. It'll be called to set the
  133. # desired capabilities on the server.
  134. sub new_session {
  135. my $self = shift;
  136. my $args = {
  137. 'desiredCapabilities' => {
  138. 'browserName' => $self->{browser_name},
  139. 'platform' => $self->{platform},
  140. 'javascriptEnabled' => $self->{javascript},
  141. 'version' => $self->{version},
  142. }
  143. };
  144. my $resp =
  145. $self->{remote_conn}
  146. ->request( $self->{commands}->{'newSession'}->{'method'},
  147. $self->{commands}->{'newSession'}->{'url'}, $args, );
  148. if ( ( defined $resp->{'sessionId'} ) && $resp->{'sessionId'} ne '' ) {
  149. $self->{session_id} = $resp->{'sessionId'};
  150. }
  151. else {
  152. croak "Could not create new session";
  153. }
  154. }
  155. =head2 get_capabilities
  156. Description:
  157. Retrieve the capabilities of the specified session.
  158. Output:
  159. A hash of all the values.
  160. Usage:
  161. my $capab = $driver->get_capabilities();
  162. print Dumper($capab);
  163. =cut
  164. sub get_capabilities {
  165. my $self = shift;
  166. my $res = {'command' => 'getCapabilities'};
  167. return $self->_execute_command($res);
  168. }
  169. sub set_implicit_wait_timeout {
  170. my ($self, $ms) = @_;
  171. my $res = {'command' => 'setImplicitWaitTimeout'};
  172. my $params = {'ms' => $ms};
  173. return $self->_execute_command($res, $params);
  174. }
  175. =head2 quit
  176. Description:
  177. Delete the session & close open browsers.
  178. Usage:
  179. $driver->quit();
  180. =cut
  181. sub quit {
  182. my $self = shift;
  183. my $res = { 'command' => 'quit' };
  184. $self->_execute_command($res);
  185. $self->{session_id} = undef;
  186. }
  187. =head2 get_current_window_handle
  188. Description:
  189. Retrieve the current window handle.
  190. Output:
  191. String - the window handle
  192. Usage:
  193. print $driver->get_current_window_handle();
  194. =cut
  195. sub get_current_window_handle {
  196. my $self = shift;
  197. my $res = { 'command' => 'getCurrentWindowHandle' };
  198. return $self->_execute_command($res);
  199. }
  200. =head2 get_current_window_handles
  201. Description:
  202. Retrieve the list of window handles used in the session.
  203. Output:
  204. Array of string - list of the window handles
  205. Usage:
  206. print Dumper($driver->get_current_window_handles());
  207. =cut
  208. sub get_window_handles {
  209. my $self = shift;
  210. my $res = { 'command' => 'getWindowHandles' };
  211. return $self->_execute_command($res);
  212. }
  213. =head2 get_current_url
  214. Description:
  215. Retrieve the url of the current page
  216. Output:
  217. String - url
  218. Usage:
  219. print $driver->get_current_url();
  220. =cut
  221. sub get_current_url {
  222. my $self = shift;
  223. my $res = { 'command' => 'getCurrentUrl' };
  224. return $self->_execute_command($res);
  225. }
  226. =head2 navigate
  227. Description:
  228. Navigate to a given url. This is same as get() method.
  229. Input:
  230. String - url
  231. Usage:
  232. $driver->navigate('http://www.google.com');
  233. =cut
  234. sub navigate {
  235. my ( $self, $url ) = @_;
  236. $self->get($url);
  237. }
  238. =head2 get
  239. Description:
  240. Navigate to a given url
  241. Input:
  242. String - url
  243. Usage:
  244. $driver->get('http://www.google.com');
  245. =cut
  246. sub get {
  247. my ( $self, $url ) = @_;
  248. my $res = { 'command' => 'get' };
  249. my $params = { 'url' => $url };
  250. return $self->_execute_command( $res, $params );
  251. }
  252. =head2 get_title
  253. Description:
  254. Get the current page title
  255. Output:
  256. String - Page title
  257. Usage:
  258. print $driver->get_title();
  259. =cut
  260. sub get_title {
  261. my $self = shift;
  262. my $res = { 'command' => 'getTitle' };
  263. return $self->_execute_command($res);
  264. }
  265. =head2 go_back
  266. Description:
  267. Equivalent to hitting the back button on the browser.
  268. Usage:
  269. $driver->go_back();
  270. =cut
  271. sub go_back {
  272. my $self = shift;
  273. my $res = { 'command' => 'goBack' };
  274. return $self->_execute_command($res);
  275. }
  276. =head2 go_back
  277. Description:
  278. Equivalent to hitting the forward button on the browser.
  279. Usage:
  280. $driver->go_forward();
  281. =cut
  282. sub go_forward {
  283. my $self = shift;
  284. my $res = { 'command' => 'goForward' };
  285. return $self->_execute_command($res);
  286. }
  287. =head2 refresh
  288. Description:
  289. Reload the current page.
  290. Usage:
  291. $driver->refresh();
  292. =cut
  293. sub refresh {
  294. my $self = shift;
  295. my $res = { 'command' => 'goForward' };
  296. return $self->_execute_command($res);
  297. }
  298. sub execute_script {
  299. my ( $self, $script, @args ) = @_;
  300. if ($self->javascript) {
  301. if ( not defined $script ) {
  302. return 'No script provided';
  303. }
  304. my $res = { 'command' => 'executeScript' };
  305. # Check the args array if the elem obj is provided & replace it with
  306. # JSON representation
  307. for (my $i=0; $i<@args; $i++) {
  308. if (ref $args[$i] eq 'Selenium::Remote::WebElement') {
  309. $args[$i] = {'ELEMENT' => ($args[$i])->{id}};
  310. }
  311. }
  312. my $params = {'args' => @args};
  313. my $ret = $self->_execute_command($res, $params);
  314. # replace any ELEMENTS with WebElement
  315. if (exists $ret->{'cmd_return'}->{'ELEMENT'}) {
  316. $ret->{'cmd_return'} =
  317. new Selenium::Remote::WebElement(
  318. $ret->{'cmd_return'}->{ELEMENT}, $self);
  319. }
  320. return $ret;
  321. }
  322. else {
  323. return 'Javascript is not enabled on remote driver instance.';
  324. }
  325. }
  326. =head2 screenshot
  327. Description:
  328. Get a screenshot of the current page as a base64 encoded image.
  329. Output:
  330. String - base64 encoded image
  331. Usage:
  332. print $driver->go_screenshot();
  333. =cut
  334. sub screenshot {
  335. my ($self) = @_;
  336. my $res = { 'command' => 'screenshot' };
  337. return $self->_execute_command($res);
  338. }
  339. sub switch_to_frame {
  340. my ( $self, $id ) = @_;
  341. my $json_null = JSON::null;
  342. $id = ( defined $id ) ? $id : $json_null;
  343. my $res = { 'command' => 'switchToFrame' };
  344. my $params = { 'id' => $id };
  345. return $self->_execute_command( $res, $params );
  346. }
  347. sub switch_to_window {
  348. my ( $self, $name ) = @_;
  349. if ( not defined $name ) {
  350. return 'Window name not provided';
  351. }
  352. my $res = { 'command' => 'switchToWindow' };
  353. my $params = { 'name' => $name };
  354. return $self->_execute_command( $res, $params );
  355. }
  356. =head2 get_speed
  357. Description:
  358. Get the current user input speed. The actual input speed is still browser
  359. specific and not covered by the Driver.
  360. Output:
  361. String - One of these: SLOW, MEDIUM, FAST
  362. Usage:
  363. print $driver->get_speed();
  364. =cut
  365. sub get_speed {
  366. my ($self) = @_;
  367. my $res = { 'command' => 'getSpeed' };
  368. return $self->_execute_command($res);
  369. }
  370. =head2 set_speed
  371. Description:
  372. Set the user input speed.
  373. Input:
  374. String - One of these: SLOW, MEDIUM, FAST
  375. Usage:
  376. $driver->set_speed('MEDIUM');
  377. =cut
  378. sub set_speed {
  379. my ( $self, $speed ) = @_;
  380. if ( not defined $speed ) {
  381. return 'Speed not provided.';
  382. }
  383. my $res = { 'command' => 'setSpeed' };
  384. my $params = { 'speed' => $speed };
  385. return $self->_execute_command( $res, $params );
  386. }
  387. sub get_all_cookies {
  388. my ($self) = @_;
  389. my $res = { 'command' => 'getAllCookies' };
  390. return $self->_execute_command($res);
  391. }
  392. sub add_cookie {
  393. my ( $self, $name, $value, $path, $domain, $secure ) = @_;
  394. if ( ( not defined $name )
  395. || ( not defined $value )
  396. || ( not defined $path )
  397. || ( not defined $domain ) )
  398. {
  399. return "Missing parameters";
  400. }
  401. my $res = { 'command' => 'addCookie' };
  402. my $json_false = JSON::false;
  403. my $json_true = JSON::true;
  404. $secure = ( defined $secure ) ? $json_true : $json_false;
  405. my $params = {
  406. 'cookie' => {
  407. 'name' => $name,
  408. 'value' => $value,
  409. 'path' => $path,
  410. 'domain' => $domain,
  411. 'secure' => $secure,
  412. }
  413. };
  414. return $self->_execute_command( $res, $params );
  415. }
  416. sub delete_all_cookies {
  417. my ($self) = @_;
  418. my $res = { 'command' => 'deleteAllCookies' };
  419. return $self->_execute_command($res);
  420. }
  421. sub delete_cookie_named {
  422. my ( $self, $cookie_name ) = @_;
  423. if ( not defined $cookie_name ) {
  424. return "Cookie name not provided";
  425. }
  426. my $res = { 'command' => 'deleteAllCookies', 'name' => $cookie_name };
  427. return $self->_execute_command($res);
  428. }
  429. sub get_page_source {
  430. my ($self) = @_;
  431. my $res = { 'command' => 'getPageSource' };
  432. return $self->_execute_command($res);
  433. }
  434. sub find_element {
  435. my ( $self, $query, $method ) = @_;
  436. if ( not defined $query ) {
  437. return 'Search string to find element not provided.';
  438. }
  439. my $using = ( defined $method ) ? $method : 'xpath';
  440. my $ret;
  441. if (exists FINDERS->{$using}) {
  442. my $res = { 'command' => 'findElement' };
  443. my $params = { 'using' => $using, 'value' => $query };
  444. my $ret_data = $self->_execute_command( $res, $params );
  445. if (defined $ret_data->{'cmd_error'}) {
  446. $ret = $ret_data;
  447. }
  448. else {
  449. $ret_data->{'cmd_return'} = new Selenium::Remote::WebElement($ret_data->{'cmd_return'}->{ELEMENT}, $self);
  450. $ret = $ret_data;
  451. }
  452. }
  453. else {
  454. $ret = "Bad method, expected - class, class_name, css, id, link,
  455. link_text, partial_link_text, name, tag_name, xpath";
  456. }
  457. return $ret;
  458. }
  459. sub find_elements {
  460. my ( $self, $query, $method ) = @_;
  461. if ( not defined $query ) {
  462. return 'Search string to find element not provided.';
  463. }
  464. my $using = ( defined $method ) ? $method : 'xpath';
  465. my $ret;
  466. if (exists FINDERS->{$using}) {
  467. my $res = { 'command' => 'findElements' };
  468. my $params = { 'using' => $using, 'value' => $query };
  469. my $ret_data = $self->_execute_command( $res, $params );
  470. if (defined $ret_data->{'cmd_error'}) {
  471. $ret = $ret_data;
  472. }
  473. else {
  474. my $elem_obj_arr;
  475. my $i = 0;
  476. my $elem_arr = $ret_data->{'cmd_return'};
  477. foreach (@$elem_arr) {
  478. $elem_obj_arr->[$i] = new Selenium::Remote::WebElement($_->{ELEMENT}, $self);
  479. $i++;
  480. }
  481. $ret_data->{'cmd_return'} = $elem_obj_arr;
  482. $ret = $ret_data;
  483. }
  484. }
  485. else {
  486. $ret = "Bad method, expected - class, class_name, css, id, link,
  487. link_text, partial_link_text, name, tag_name, xpath";
  488. }
  489. return $ret;
  490. }
  491. sub find_child_element {
  492. my ( $self, $elem, $query, $method ) = @_;
  493. my $ret;
  494. if ( ( not defined $elem ) || ( not defined $query ) ) {
  495. return "Missing parameters";
  496. }
  497. my $using = ( defined $method ) ? $method : 'xpath';
  498. if (exists FINDERS->{$using}) {
  499. my $res = { 'command' => 'findChildElement', 'id' => $elem->{id} };
  500. my $params = { 'using' => $using, 'value' => $query };
  501. my $ret_data = $self->_execute_command( $res, $params );
  502. if (defined $ret_data->{'cmd_error'}) {
  503. $ret = $ret_data;
  504. }
  505. else {
  506. $ret_data->{'cmd_return'} = new Selenium::Remote::WebElement($ret_data->{'cmd_return'}->{ELEMENT}, $self);
  507. $ret = $ret_data;
  508. }
  509. }
  510. else {
  511. $ret = "Bad method, expected - class, class_name, css, id, link,
  512. link_text, partial_link_text, name, tag_name, xpath";
  513. }
  514. return $ret;
  515. }
  516. sub find_child_elements {
  517. my ( $self, $elem, $query, $method ) = @_;
  518. my $ret;
  519. if ( ( not defined $elem ) || ( not defined $query ) ) {
  520. return "Missing parameters";
  521. }
  522. my $using = ( defined $method ) ? $method : 'xpath';
  523. if (exists FINDERS->{$using}) {
  524. my $res = { 'command' => 'findChildElements', 'id' => $elem->{id} };
  525. my $params = { 'using' => $using, 'value' => $query };
  526. my $ret_data = $self->_execute_command( $res, $params );
  527. if (defined $ret_data->{'cmd_error'}) {
  528. $ret = $ret_data;
  529. }
  530. else {
  531. my $elem_obj_arr;
  532. my $i = 0;
  533. my $elem_arr = $ret_data->{'cmd_return'};
  534. foreach (@$elem_arr) {
  535. $elem_obj_arr->[$i] = new Selenium::Remote::WebElement($_->{ELEMENT}, $self);
  536. $i++;
  537. }
  538. $ret_data->{'cmd_return'} = $elem_obj_arr;
  539. $ret = $ret_data;
  540. }
  541. }
  542. else {
  543. $ret = "Bad method, expected - class, class_name, css, id, link,
  544. link_text, partial_link_text, name, tag_name, xpath";
  545. }
  546. return $ret;
  547. }
  548. sub get_active_element {
  549. my ($self) = @_;
  550. my $res = { 'command' => 'getActiveElement' };
  551. return $self->_execute_command($res);
  552. }
  553. sub describe_element {
  554. my ( $self, $element ) = @_;
  555. #if (not defined $element) {
  556. # return "Element not provided";
  557. #}
  558. #my $res = {'command' => 'desribeElement', 'name' => $element};
  559. #return $self->_execute_command($res);
  560. return "Not yet supported";
  561. }
  562. sub compare_elements {
  563. my ($self, $elem1, $elem2) = @_;
  564. my $res = { 'command' => 'elementEquals',
  565. 'id' => $elem1->{id},
  566. 'other' => $elem2->{id}
  567. };
  568. return $self->_execute_command($res);
  569. }
  570. 1;
  571. __END__
  572. =head1 SEE ALSO
  573. For more information about Selenium , visit the website at
  574. L<http://code.google.com/p/selenium/>.
  575. =head1 BUGS
  576. The Selenium issue tracking system is available online at
  577. L<http://code.google.com/p/selenium/issues/list>.
  578. =head1 AUTHOR
  579. Perl Bindings for Remote Driver by Aditya Ivaturi <ivaturi@gmail.com>
  580. =head1 LICENSE
  581. Copyright (c) 2010 Aditya Ivaturi
  582. Licensed under the Apache License, Version 2.0 (the "License");
  583. you may not use this file except in compliance with the License.
  584. You may obtain a copy of the License at
  585. http://www.apache.org/licenses/LICENSE-2.0
  586. Unless required by applicable law or agreed to in writing, software
  587. distributed under the License is distributed on an "AS IS" BASIS,
  588. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  589. See the License for the specific language governing permissions and
  590. limitations under the License.