Driver.pm 19 KB

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