Driver.pm 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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. sub new {
  33. my ($class, %args) = @_;
  34. my $commands = new Selenium::Remote::Commands;
  35. # Set the defaults if user doesn't send any
  36. my $self = {
  37. remote_server_addr => delete $args{remote_server_addr} || 'localhost',
  38. browser_name => delete $args{browser_name} || 'firefox',
  39. platform => delete $args{platform} || 'ANY',
  40. port => delete $args{port} || '4444',
  41. javascript => delete $args{javascript} || JSON::true,
  42. version => delete $args{version} || '',
  43. session_id => undef,
  44. remote_conn => undef,
  45. error_handler => undef,
  46. commands => $commands,
  47. };
  48. bless $self, $class or die "Can't bless $class: $!";
  49. $self->{error_handler} = new Selenium::Remote::ErrorHandler;
  50. # Connect to remote server & establish a new session
  51. $self->{remote_conn} =
  52. new Selenium::Remote::RemoteConnection($self->{remote_server_addr},
  53. $self->{port});
  54. $self->new_session();
  55. if (!(defined $self->{session_id})) {
  56. croak "Could not establish a session with the remote server\n";
  57. }
  58. return $self;
  59. }
  60. # When a command is processed by the remote server & a result is sent back, it
  61. # also includes other relevant info. We strip those & just return the value we're
  62. # interested in. And if there is an error, ErrorHandler will handle it.
  63. sub _get_command_result {
  64. my ($self, @args) = @_;
  65. my $resp = $self->{remote_conn}->request(@args);
  66. if (defined $resp->{'status'} && $resp->{'status'} != 0) {
  67. $self->{error_handler}->process_error($resp);
  68. }
  69. elsif (defined $resp->{'value'}) {
  70. return $resp->{'value'};
  71. }
  72. else {
  73. # If there is no value or status assume success
  74. return 1;
  75. }
  76. }
  77. sub new_session {
  78. my $self = shift;
  79. my $args = { 'desiredCapabilities' => {
  80. 'browserName' => $self->{browser_name},
  81. 'platform' => $self->{platform},
  82. 'javascriptEnabled' => $self->{javascript},
  83. 'version' => $self->{version},
  84. }
  85. };
  86. my $resp =
  87. $self->{remote_conn}->request(
  88. $self->{commands}->{'newSession'}->{'method'},
  89. $self->{commands}->{'newSession'}->{'url'},
  90. $args,
  91. );
  92. if ((defined $resp->{'sessionId'}) && $resp->{'sessionId'} ne '') {
  93. $self->{session_id} = $resp->{'sessionId'};
  94. }
  95. else {
  96. croak "Could not create new session";
  97. }
  98. }
  99. sub get_capabilities {
  100. my $self = shift;
  101. my $command = 'getCapabilities';
  102. my $args = { 'session_id' => $self->{'session_id'}, };
  103. my $data = $self->{commands}->getParams($command, $args);
  104. if ($data) {
  105. return $self->_get_command_result($data->{'method'}, $data->{'url'});
  106. }
  107. else {
  108. croak "Couldn't retrieve command $command settings\n";
  109. }
  110. }
  111. sub quit {
  112. my $self = shift;
  113. my $args = { 'session_id' => $self->{'session_id'}, };
  114. my $data = $self->{commands}->getParams('quit', $args);
  115. if ($data) {
  116. $self->{remote_conn}->request($data->{'method'}, $data->{'url'});
  117. }
  118. else {
  119. croak "Couldn't retrieve command settings properly\n";
  120. }
  121. }
  122. sub get_current_window_handle {
  123. my $self = shift;
  124. my $command = 'getCurrentWindowHandle';
  125. my $args = { 'session_id' => $self->{'session_id'}, };
  126. my $data = $self->{commands}->getParams($command, $args);
  127. if ($data) {
  128. return $self->_get_command_result($data->{'method'}, $data->{'url'});
  129. }
  130. else {
  131. croak "Couldn't retrieve command $command settings\n";
  132. }
  133. }
  134. sub get_window_handles {
  135. my $self = shift;
  136. my $command = 'getWindowHandles';
  137. my $args = { 'session_id' => $self->{'session_id'}, };
  138. my $data = $self->{commands}->getParams($command, $args);
  139. if ($data) {
  140. return $self->_get_command_result($data->{'method'}, $data->{'url'});
  141. }
  142. else {
  143. croak "Couldn't retrieve command $command settings\n";
  144. }
  145. }
  146. sub get_current_url {
  147. my $self = shift;
  148. my $command = 'getCurrentUrl';
  149. my $args = { 'session_id' => $self->{'session_id'}, };
  150. my $data = $self->{commands}->getParams($command, $args);
  151. if ($data) {
  152. return $self->_get_command_result($data->{'method'}, $data->{'url'});
  153. }
  154. else {
  155. croak "Couldn't retrieve command $command settings\n";
  156. }
  157. }
  158. sub navigate {
  159. my ($self, $url) = @_;
  160. $self->get($url);
  161. }
  162. sub get {
  163. my ($self, $url) = @_;
  164. my $command = 'get';
  165. my $args = { 'session_id' => $self->{'session_id'}, };
  166. my $data = $self->{commands}->getParams($command, $args);
  167. my $params = {'url' => $url};
  168. if ($data) {
  169. $self->{remote_conn}->request($data->{'method'}, $data->{'url'}, $params);
  170. }
  171. else {
  172. croak "Couldn't retrieve command $command settings\n";
  173. }
  174. }
  175. sub get_title {
  176. my $self = shift;
  177. my $command = 'getTitle';
  178. my $args = { 'session_id' => $self->{'session_id'}, };
  179. my $data = $self->{commands}->getParams($command, $args);
  180. if ($data) {
  181. return $self->_get_command_result($data->{'method'}, $data->{'url'});
  182. }
  183. else {
  184. croak "Couldn't retrieve command $command settings\n";
  185. }
  186. }
  187. sub go_back {
  188. my $self = shift;
  189. my $command = 'goBack';
  190. my $args = { 'session_id' => $self->{'session_id'}, };
  191. my $data = $self->{commands}->getParams($command, $args);
  192. if ($data) {
  193. $self->{remote_conn}->request($data->{'method'}, $data->{'url'});
  194. }
  195. else {
  196. croak "Couldn't retrieve command $command settings\n";
  197. }
  198. }
  199. sub go_forward {
  200. my $self = shift;
  201. my $command = 'goForward';
  202. my $args = { 'session_id' => $self->{'session_id'}, };
  203. my $data = $self->{commands}->getParams($command, $args);
  204. if ($data) {
  205. $self->{remote_conn}->request($data->{'method'}, $data->{'url'});
  206. }
  207. else {
  208. croak "Couldn't retrieve command $command settings\n";
  209. }
  210. }
  211. sub refresh {
  212. my $self = shift;
  213. my $command = 'goForward';
  214. my $args = { 'session_id' => $self->{'session_id'}, };
  215. my $data = $self->{commands}->getParams($command, $args);
  216. if ($data) {
  217. $self->{remote_conn}->request($data->{'method'}, $data->{'url'});
  218. }
  219. else {
  220. croak "Couldn't retrieve command $command settings\n";
  221. }
  222. }
  223. 1;
  224. __END__
  225. =head1 SEE ALSO
  226. For more information about Selenium , visit the website at
  227. L<http://code.google.com/p/selenium/>.
  228. =head1 BUGS
  229. The Selenium issue tracking system is available online at
  230. L<http://code.google.com/p/selenium/issues/list>.
  231. =head1 AUTHOR
  232. Perl Bindings for Remote Driver by Aditya Ivaturi <ivaturi@gmail.com>
  233. =head1 LICENSE
  234. Copyright (c) 2010 Juniper Networks, Inc
  235. Licensed under the Apache License, Version 2.0 (the "License");
  236. you may not use this file except in compliance with the License.
  237. You may obtain a copy of the License at
  238. http://www.apache.org/licenses/LICENSE-2.0
  239. Unless required by applicable law or agreed to in writing, software
  240. distributed under the License is distributed on an "AS IS" BASIS,
  241. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  242. See the License for the specific language governing permissions and
  243. limitations under the License.