Driver.pm 16 KB

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