1
0

Driver.pm 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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 $ress = 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. commands => $ress,
  46. };
  47. bless $self, $class or die "Can't bless $class: $!";
  48. # Connect to remote server & establish a new session
  49. $self->{remote_conn} =
  50. new Selenium::Remote::RemoteConnection($self->{remote_server_addr},
  51. $self->{port});
  52. $self->new_session();
  53. if (!(defined $self->{session_id})) {
  54. croak "Could not establish a session with the remote server\n";
  55. }
  56. return $self;
  57. }
  58. sub _execute_command {
  59. my ($self, $res, $params) = @_;
  60. $res->{'session_id'} = $self->{'session_id'};
  61. my $resource = $self->{commands}->get_params($res);
  62. if ($resource) {
  63. return $self->{remote_conn}->request($resource->{'method'}, $resource->{'url'}, $params);
  64. }
  65. else {
  66. croak "Couldn't retrieve command settings properly\n";
  67. }
  68. }
  69. sub new_session {
  70. my $self = shift;
  71. my $args = { 'desiredCapabilities' => {
  72. 'browserName' => $self->{browser_name},
  73. 'platform' => $self->{platform},
  74. 'javascriptEnabled' => $self->{javascript},
  75. 'version' => $self->{version},
  76. }
  77. };
  78. my $resp =
  79. $self->{remote_conn}->request(
  80. $self->{commands}->{'newSession'}->{'method'},
  81. $self->{commands}->{'newSession'}->{'url'},
  82. $args,
  83. );
  84. if ((defined $resp->{'sessionId'}) && $resp->{'sessionId'} ne '') {
  85. $self->{session_id} = $resp->{'sessionId'};
  86. }
  87. else {
  88. croak "Could not create new session";
  89. }
  90. }
  91. sub get_capabilities {
  92. my $self = shift;
  93. my $res = 'getCapabilities';
  94. return $self->_execute_command($res);
  95. }
  96. sub quit {
  97. my $self = shift;
  98. my $res = {'command' => 'quit'};
  99. return $self->_execute_command($res);
  100. }
  101. sub get_current_window_handle {
  102. my $self = shift;
  103. my $res = {'command' => 'getCurrentWindowHandle'};
  104. return $self->_execute_command($res);
  105. }
  106. sub get_window_handles {
  107. my $self = shift;
  108. my $res = {'command' => 'getWindowHandles'};
  109. return $self->_execute_command($res);
  110. }
  111. sub get_current_url {
  112. my $self = shift;
  113. my $res = {'command' => 'getCurrentUrl'};
  114. return $self->_execute_command($res);
  115. }
  116. sub navigate {
  117. my ($self, $url) = @_;
  118. $self->get($url);
  119. }
  120. sub get {
  121. my ($self, $url) = @_;
  122. my $res = {'command' => 'get'};
  123. my $params = {'url' => $url};
  124. return $self->_execute_command($res, $params);
  125. }
  126. sub get_title {
  127. my $self = shift;
  128. my $res = {'command' => 'getTitle'};
  129. return $self->_execute_command($res);
  130. }
  131. sub go_back {
  132. my $self = shift;
  133. my $res = {'command' => 'goBack'};
  134. return $self->_execute_command($res);
  135. }
  136. sub go_forward {
  137. my $self = shift;
  138. my $res = {'command' => 'goForward'};
  139. return $self->_execute_command($res);
  140. }
  141. sub refresh {
  142. my $self = shift;
  143. my $res = {'command' => 'goForward'};
  144. return $self->_execute_command($res);
  145. }
  146. sub execute_script {
  147. # TODO: this method is not finished
  148. my ($self, $script, @args) = @_;
  149. if (not defined $script) {
  150. return 'No script provided';
  151. }
  152. my $res = {'command' => 'executeScript'};
  153. my $args = { 'session_id' => $self->{'session_id'}, };
  154. my $resource = $self->{commands}->getParams($res, $args);
  155. if ($resource) {
  156. return $self->{remote_conn}->request($resource->{'method'}, $resource->{'url'});
  157. }
  158. else {
  159. croak "Couldn't retrieve command $res settings\n";
  160. }
  161. }
  162. sub screenshot {
  163. my ($self) = @_;
  164. my $res = {'command' => 'screenshot'};
  165. return $self->_execute_command($res);
  166. }
  167. sub switch_to_frame {
  168. my ($self, $id) = @_;
  169. my $json_null = JSON::null;
  170. $id = (defined $id)?$id:$json_null;
  171. my $res = {'command' => 'switchToFrame'};
  172. my $params = {'id' => $id};
  173. return $self->_execute_command($res, $params);
  174. }
  175. sub switch_to_window {
  176. my ($self, $name) = @_;
  177. if (not defined $name) {
  178. return 'Window name not provided';
  179. }
  180. my $res = {'command' => 'switchToWindow'};
  181. my $params = {'name' => $name};
  182. return $self->_execute_command($res, $params);
  183. }
  184. sub get_speed {
  185. my ($self) = @_;
  186. my $res = {'command' => 'getSpeed'};
  187. return $self->_execute_command($res);
  188. }
  189. sub set_speed {
  190. my ($self, $speed) = @_;
  191. if (not defined $speed) {
  192. return 'Speed not provided.';
  193. }
  194. my $res = {'command' => 'switchToWindow'};
  195. my $params = {'speed' => $speed};
  196. return $self->_execute_command($res, $params);
  197. }
  198. sub get_all_cookies {
  199. my ($self) = @_;
  200. my $res = {'command' => 'getAllCookies'};
  201. return $self->_execute_command($res);
  202. }
  203. sub add_cookie {
  204. my($self, $name, $value, $path, $domain, $secure) = @_;
  205. if ((not defined $name) ||(not defined $value) ||(not defined $path) ||
  206. (not defined $domain)) {
  207. return "Missing parameters";
  208. }
  209. my $res = {'command' => 'addCookie'};
  210. my $json_false = JSON::false;
  211. my $json_true = JSON::true;
  212. $secure = (defined $secure)?$json_true:$json_false;
  213. my $params = {
  214. 'name' => $name,
  215. 'value' => $value,
  216. 'path' => $path,
  217. 'domain' => $domain,
  218. 'secure' => $secure,
  219. };
  220. return $self->_execute_command($res, $params);
  221. }
  222. sub delete_all_cookies {
  223. my ($self) = @_;
  224. my $res = {'command' => 'deleteAllCookies'};
  225. return $self->_execute_command($res);
  226. }
  227. 1;
  228. __END__
  229. =head1 SEE ALSO
  230. For more information about Selenium , visit the website at
  231. L<http://code.google.com/p/selenium/>.
  232. =head1 BUGS
  233. The Selenium issue tracking system is available online at
  234. L<http://code.google.com/p/selenium/issues/list>.
  235. =head1 AUTHOR
  236. Perl Bindings for Remote Driver by Aditya Ivaturi <ivaturi@gmail.com>
  237. =head1 LICENSE
  238. Copyright (c) 2010 Juniper Networks, Inc
  239. Licensed under the Apache License, Version 2.0 (the "License");
  240. you may not use this file except in compliance with the License.
  241. You may obtain a copy of the License at
  242. http://www.apache.org/licenses/LICENSE-2.0
  243. Unless required by applicable law or agreed to in writing, software
  244. distributed under the License is distributed on an "AS IS" BASIS,
  245. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  246. See the License for the specific language governing permissions and
  247. limitations under the License.