1
0

Driver.pm 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651
  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. return $self->{remote_conn}
  107. ->request( $resource->{'method'}, $resource->{'url'}, $params );
  108. }
  109. else {
  110. croak "Couldn't retrieve command settings properly\n";
  111. }
  112. }
  113. # A method that is used by the Driver itself. It'll be called to set the
  114. # desired capabilities on the server.
  115. sub new_session {
  116. my $self = shift;
  117. my $args = {
  118. 'desiredCapabilities' => {
  119. 'browserName' => $self->{browser_name},
  120. 'platform' => $self->{platform},
  121. 'javascriptEnabled' => $self->{javascript},
  122. 'version' => $self->{version},
  123. }
  124. };
  125. my $resp =
  126. $self->{remote_conn}
  127. ->request( $self->{commands}->{'newSession'}->{'method'},
  128. $self->{commands}->{'newSession'}->{'url'}, $args, );
  129. if ( ( defined $resp->{'sessionId'} ) && $resp->{'sessionId'} ne '' ) {
  130. $self->{session_id} = $resp->{'sessionId'};
  131. }
  132. else {
  133. croak "Could not create new session";
  134. }
  135. }
  136. =head2 get_capabilities
  137. Description:
  138. Retrieve the capabilities of the specified session.
  139. Output:
  140. A hash of all the values.
  141. Usage:
  142. my $capab = $driver->get_capabilities();
  143. print Dumper($capab);
  144. =cut
  145. sub get_capabilities {
  146. my $self = shift;
  147. my $res = {'command' => 'getCapabilities'};
  148. return $self->_execute_command($res);
  149. }
  150. =head2 quit
  151. Description:
  152. Delete the session & close open browsers.
  153. Usage:
  154. $driver->quit();
  155. =cut
  156. sub quit {
  157. my $self = shift;
  158. my $res = { 'command' => 'quit' };
  159. return $self->_execute_command($res);
  160. }
  161. =head2 get_current_window_handle
  162. Description:
  163. Retrieve the current window handle.
  164. Output:
  165. String - the window handle
  166. Usage:
  167. print $driver->get_current_window_handle();
  168. =cut
  169. sub get_current_window_handle {
  170. my $self = shift;
  171. my $res = { 'command' => 'getCurrentWindowHandle' };
  172. return $self->_execute_command($res);
  173. }
  174. =head2 get_current_window_handles
  175. Description:
  176. Retrieve the list of window handles used in the session.
  177. Output:
  178. Array of string - list of the window handles
  179. Usage:
  180. print Dumper($driver->get_current_window_handles());
  181. =cut
  182. sub get_window_handles {
  183. my $self = shift;
  184. my $res = { 'command' => 'getWindowHandles' };
  185. return $self->_execute_command($res);
  186. }
  187. =head2 get_current_url
  188. Description:
  189. Retrieve the url of the current page
  190. Output:
  191. String - url
  192. Usage:
  193. print $driver->get_current_url();
  194. =cut
  195. sub get_current_url {
  196. my $self = shift;
  197. my $res = { 'command' => 'getCurrentUrl' };
  198. return $self->_execute_command($res);
  199. }
  200. =head2 navigate
  201. Description:
  202. Navigate to a given url. This is same as get() method.
  203. Input:
  204. String - url
  205. Usage:
  206. $driver->navigate('http://www.google.com');
  207. =cut
  208. sub navigate {
  209. my ( $self, $url ) = @_;
  210. $self->get($url);
  211. }
  212. =head2 get
  213. Description:
  214. Navigate to a given url
  215. Input:
  216. String - url
  217. Usage:
  218. $driver->get('http://www.google.com');
  219. =cut
  220. sub get {
  221. my ( $self, $url ) = @_;
  222. my $res = { 'command' => 'get' };
  223. my $params = { 'url' => $url };
  224. return $self->_execute_command( $res, $params );
  225. }
  226. =head2 get_title
  227. Description:
  228. Get the current page title
  229. Output:
  230. String - Page title
  231. Usage:
  232. print $driver->get_title();
  233. =cut
  234. sub get_title {
  235. my $self = shift;
  236. my $res = { 'command' => 'getTitle' };
  237. return $self->_execute_command($res);
  238. }
  239. =head2 go_back
  240. Description:
  241. Equivalent to hitting the back button on the browser.
  242. Usage:
  243. $driver->go_back();
  244. =cut
  245. sub go_back {
  246. my $self = shift;
  247. my $res = { 'command' => 'goBack' };
  248. return $self->_execute_command($res);
  249. }
  250. =head2 go_back
  251. Description:
  252. Equivalent to hitting the forward button on the browser.
  253. Usage:
  254. $driver->go_forward();
  255. =cut
  256. sub go_forward {
  257. my $self = shift;
  258. my $res = { 'command' => 'goForward' };
  259. return $self->_execute_command($res);
  260. }
  261. =head2 refresh
  262. Description:
  263. Reload the current page.
  264. Usage:
  265. $driver->refresh();
  266. =cut
  267. sub refresh {
  268. my $self = shift;
  269. my $res = { 'command' => 'goForward' };
  270. return $self->_execute_command($res);
  271. }
  272. sub execute_script {
  273. # TODO: this method is not finished
  274. my ( $self, $script, @args ) = @_;
  275. if ( not defined $script ) {
  276. return 'No script provided';
  277. }
  278. my $res = { 'command' => 'executeScript' };
  279. my $args = { 'session_id' => $self->{'session_id'}, };
  280. my $resource = $self->{commands}->getParams( $res, $args );
  281. if ($resource) {
  282. return $self->{remote_conn}
  283. ->request( $resource->{'method'}, $resource->{'url'} );
  284. }
  285. else {
  286. croak "Couldn't retrieve command $res settings\n";
  287. }
  288. }
  289. =head2 screenshot
  290. Description:
  291. Get a screenshot of the current page as a base64 encoded image.
  292. Output:
  293. String - base64 encoded image
  294. Usage:
  295. print $driver->go_screenshot();
  296. =cut
  297. sub screenshot {
  298. my ($self) = @_;
  299. my $res = { 'command' => 'screenshot' };
  300. return $self->_execute_command($res);
  301. }
  302. sub switch_to_frame {
  303. my ( $self, $id ) = @_;
  304. my $json_null = JSON::null;
  305. $id = ( defined $id ) ? $id : $json_null;
  306. my $res = { 'command' => 'switchToFrame' };
  307. my $params = { 'id' => $id };
  308. return $self->_execute_command( $res, $params );
  309. }
  310. sub switch_to_window {
  311. my ( $self, $name ) = @_;
  312. if ( not defined $name ) {
  313. return 'Window name not provided';
  314. }
  315. my $res = { 'command' => 'switchToWindow' };
  316. my $params = { 'name' => $name };
  317. return $self->_execute_command( $res, $params );
  318. }
  319. =head2 get_speed
  320. Description:
  321. Get the current user input speed. The actual input speed is still browser
  322. specific and not covered by the Driver.
  323. Output:
  324. String - One of these: SLOW, MEDIUM, FAST
  325. Usage:
  326. print $driver->get_speed();
  327. =cut
  328. sub get_speed {
  329. my ($self) = @_;
  330. my $res = { 'command' => 'getSpeed' };
  331. return $self->_execute_command($res);
  332. }
  333. =head2 set_speed
  334. Description:
  335. Set the user input speed.
  336. Input:
  337. String - One of these: SLOW, MEDIUM, FAST
  338. Usage:
  339. $driver->set_speed('MEDIUM');
  340. =cut
  341. sub set_speed {
  342. my ( $self, $speed ) = @_;
  343. if ( not defined $speed ) {
  344. return 'Speed not provided.';
  345. }
  346. my $res = { 'command' => 'switchToWindow' };
  347. my $params = { 'speed' => $speed };
  348. return $self->_execute_command( $res, $params );
  349. }
  350. # TODO: Verify all these cookied methods - some return errors some don't
  351. # No idea whether they're implemented on the server yet
  352. sub get_all_cookies {
  353. my ($self) = @_;
  354. my $res = { 'command' => 'getAllCookies' };
  355. return $self->_execute_command($res);
  356. }
  357. sub add_cookie {
  358. my ( $self, $name, $value, $path, $domain, $secure ) = @_;
  359. if ( ( not defined $name )
  360. || ( not defined $value )
  361. || ( not defined $path )
  362. || ( not defined $domain ) )
  363. {
  364. return "Missing parameters";
  365. }
  366. my $res = { 'command' => 'addCookie' };
  367. my $json_false = JSON::false;
  368. my $json_true = JSON::true;
  369. $secure = ( defined $secure ) ? $json_true : $json_false;
  370. my $params = {
  371. 'name' => $name,
  372. 'value' => $value,
  373. 'path' => $path,
  374. 'domain' => $domain,
  375. 'secure' => $secure,
  376. };
  377. return $self->_execute_command( $res, $params );
  378. }
  379. sub delete_all_cookies {
  380. my ($self) = @_;
  381. my $res = { 'command' => 'deleteAllCookies' };
  382. return $self->_execute_command($res);
  383. }
  384. sub delete_cookie_named {
  385. my ( $self, $cookie_name ) = @_;
  386. if ( not defined $cookie_name ) {
  387. return "Cookie name not provided";
  388. }
  389. my $res = { 'command' => 'deleteAllCookies', 'name' => $cookie_name };
  390. return $self->_execute_command($res);
  391. }
  392. sub get_page_source {
  393. my ($self) = @_;
  394. my $res = { 'command' => 'getPageSource' };
  395. return $self->_execute_command($res);
  396. }
  397. sub find_element {
  398. # TODO: Find out what the locator strategies are - I am assuming xpath, css
  399. # dom etc.
  400. my ( $self, $query, $method ) = @_;
  401. if ( not defined $query ) {
  402. return 'Search string to find element not provided.';
  403. }
  404. my $using = ( defined $method ) ? $method : 'xpath';
  405. my $res = { 'command' => 'findElement' };
  406. my $params = { 'using' => $using, 'value' => $query };
  407. return $self->_execute_command( $res, $params );
  408. }
  409. sub find_elements {
  410. # TODO: Find out what the locator strategies are - I am assuming xpath, css
  411. # dom etc.
  412. my ( $self, $query, $method ) = @_;
  413. if ( not defined $query ) {
  414. return 'Search string to find element not provided.';
  415. }
  416. my $using = ( defined $method ) ? $method : 'xpath';
  417. my $res = { 'command' => 'findElements' };
  418. my $params = { 'using' => $using, 'value' => $query };
  419. return $self->_execute_command( $res, $params );
  420. }
  421. sub get_active_element {
  422. my ($self) = @_;
  423. my $res = { 'command' => 'getActiveElement' };
  424. return $self->_execute_command($res);
  425. }
  426. sub describe_element {
  427. my ( $self, $element ) = @_;
  428. #if (not defined $element) {
  429. # return "Element not provided";
  430. #}
  431. #my $res = {'command' => 'desribeElement', 'name' => $element};
  432. #return $self->_execute_command($res);
  433. return "Not yet supported";
  434. }
  435. sub find_child_element {
  436. # TODO: same as find_element - no idea what locator strategy string is & no
  437. # idea what the id is.
  438. my ( $self, $id, $query, $method ) = @_;
  439. if ( ( not defined $id ) || ( not defined $query ) ) {
  440. return "Missing parameters";
  441. }
  442. my $using = ( defined $method ) ? $method : 'xpath';
  443. my $res = { 'command' => 'findChildElement', 'id' => $id };
  444. my $params = { 'using' => $using, 'value' => $query };
  445. return $self->_execute_command( $res, $params );
  446. }
  447. sub find_child_elements {
  448. # TODO: same as find_element - no idea what locator strategy string is & no
  449. # idea what the id is.
  450. my ( $self, $id, $query, $method ) = @_;
  451. if ( ( not defined $id ) || ( not defined $query ) ) {
  452. return "Missing parameters";
  453. }
  454. my $using = ( defined $method ) ? $method : 'xpath';
  455. my $res = { 'command' => 'findChildElements', 'id' => $id };
  456. my $params = { 'using' => $using, 'value' => $query };
  457. return $self->_execute_command( $res, $params );
  458. }
  459. sub click {
  460. #TODO: verify - my local tests are failing
  461. my ( $self, $id ) = @_;
  462. if ( not defined $id ) {
  463. return "Element id not provided";
  464. }
  465. my $res = { 'command' => 'clickElement', 'id' => $id };
  466. return $self->_execute_command($res);
  467. }
  468. 1;
  469. __END__
  470. =head1 SEE ALSO
  471. For more information about Selenium , visit the website at
  472. L<http://code.google.com/p/selenium/>.
  473. =head1 BUGS
  474. The Selenium issue tracking system is available online at
  475. L<http://code.google.com/p/selenium/issues/list>.
  476. =head1 AUTHOR
  477. Perl Bindings for Remote Driver by Aditya Ivaturi <ivaturi@gmail.com>
  478. =head1 LICENSE
  479. Copyright (c) 2010 Aditya Ivaturi
  480. Licensed under the Apache License, Version 2.0 (the "License");
  481. you may not use this file except in compliance with the License.
  482. You may obtain a copy of the License at
  483. http://www.apache.org/licenses/LICENSE-2.0
  484. Unless required by applicable law or agreed to in writing, software
  485. distributed under the License is distributed on an "AS IS" BASIS,
  486. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  487. See the License for the specific language governing permissions and
  488. limitations under the License.