Driver.pm 9.3 KB

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