Driver.pm 19 KB

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