1
0

Driver.pm 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. package Selenium::Remote::Driver;
  2. use strict;
  3. use warnings;
  4. use Carp qw(croak);
  5. use Selenium::Remote::RemoteConnection;
  6. use Selenium::Remote::Commands;
  7. =head1 NAME
  8. Selenium::Remote::Driver - Perl Client for Selenium Remote Driver
  9. =cut
  10. =head1 SYNOPSIS
  11. use Selenium::Remote::Driver;
  12. my $driver = new Selenium::Remote::Driver;
  13. $driver->get("http://www.google.com");
  14. print $driver->get_title();
  15. $driver->quit();
  16. =cut
  17. =head1 DESCRIPTION
  18. Selenium is a test tool that allows you to write
  19. automated web application UI tests in any programming language against
  20. any HTTP website using any mainstream JavaScript-enabled browser. This module is
  21. an implementation of the Perl Bindings (client) for the Remote driver that
  22. Selenium provides. You can find bindings for other languages at this location:
  23. L<http://code.google.com/p/selenium/>
  24. This module sends commands directly to the Server using simple HTTP requests.
  25. Using this module together with the Selenium Server, you can automatically
  26. control any supported browser.
  27. To use this module, you need to have already downloaded and started
  28. the Selenium Server. (The Selenium Server is a Java application.)
  29. =cut
  30. sub new {
  31. my ($class, %args) = @_;
  32. my $commands = new Selenium::Remote::Commands;
  33. # Set the defaults if user doesn't send any
  34. my $self = {
  35. remote_server_addr => delete $args{remote_server_addr} || 'localhost',
  36. browser_name => delete $args{browser_name} || 'firefox',
  37. platform => delete $args{platform} || 'ANY',
  38. port => delete $args{port} || '4444',
  39. javascript => delete $args{javascript} || JSON::true,
  40. version => delete $args{version} || '',
  41. session_id => undef,
  42. remote_conn => undef,
  43. commands => $commands,
  44. };
  45. bless $self, $class or die "Can't bless $class: $!";
  46. # Connect to remote server & establish a new session
  47. $self->{remote_conn} =
  48. new Selenium::Remote::RemoteConnection($self->{remote_server_addr},
  49. $self->{port});
  50. $self->new_session();
  51. if (!(defined $self->{session_id})) {
  52. croak "Could not establish a session with the remote server\n";
  53. }
  54. return $self;
  55. }
  56. # When a command is processed by the remote server & a result is sent back, it
  57. # also includes other relevant info. We strip those & just return the value we're
  58. # interested in.
  59. sub _get_command_result {
  60. my ($self, @args) = @_;
  61. my $resp = $self->{remote_conn}->request(@args);
  62. if (defined $resp->{'value'}) {
  63. return $resp->{'value'};
  64. }
  65. else {
  66. return;
  67. }
  68. }
  69. sub new_session {
  70. my $self = shift;
  71. my $args = {
  72. 'browserName' => $self->{browser_name},
  73. 'platform' => $self->{platform},
  74. 'javascriptEnabled' => $self->{javascript},
  75. 'version' => $self->{version},
  76. };
  77. my $resp =
  78. $self->{remote_conn}->request(
  79. $self->{commands}->{'newSession'}->{'method'},
  80. $self->{commands}->{'newSession'}->{'url'},
  81. $args,
  82. );
  83. if ((defined $resp->{'sessionId'}) && $resp->{'sessionId'} ne '') {
  84. $self->{session_id} = $resp->{'sessionId'};
  85. }
  86. }
  87. sub quit {
  88. my $self = shift;
  89. my $args = { 'session_id' => $self->{'session_id'}, };
  90. my $data = $self->{commands}->getParams('quit', $args);
  91. if ($data) {
  92. $self->{remote_conn}->request($data->{'method'}, $data->{'url'});
  93. }
  94. else {
  95. croak "Couldn't retrieve command settings properly\n";
  96. }
  97. }
  98. sub navigate {
  99. my ($self, $url) = @_;
  100. $self->get($url);
  101. }
  102. sub get {
  103. my ($self, $url) = @_;
  104. my $command = 'get';
  105. my $args = { 'session_id' => $self->{'session_id'}, };
  106. my $data = $self->{commands}->getParams($command, $args);
  107. if ($data) {
  108. $self->{remote_conn}->request($data->{'method'}, $data->{'url'}, $url);
  109. }
  110. else {
  111. croak "Couldn't retrieve command $command settings\n";
  112. }
  113. }
  114. sub get_title {
  115. my $self = shift;
  116. my $command = 'getTitle';
  117. my $args = { 'session_id' => $self->{'session_id'}, };
  118. my $data = $self->{commands}->getParams($command, $args);
  119. if ($data) {
  120. return $self->_get_command_result($data->{'method'}, $data->{'url'});
  121. }
  122. else {
  123. croak "Couldn't retrieve command $command settings\n";
  124. }
  125. }
  126. sub go_back {
  127. my $self = shift;
  128. my $command = 'goBack';
  129. my $args = { 'session_id' => $self->{'session_id'}, };
  130. my $data = $self->{commands}->getParams($command, $args);
  131. if ($data) {
  132. $self->{remote_conn}->request($data->{'method'}, $data->{'url'});
  133. }
  134. else {
  135. croak "Couldn't retrieve command $command settings\n";
  136. }
  137. }
  138. sub go_forward {
  139. my $self = shift;
  140. my $command = 'goForward';
  141. my $args = { 'session_id' => $self->{'session_id'}, };
  142. my $data = $self->{commands}->getParams($command, $args);
  143. if ($data) {
  144. $self->{remote_conn}->request($data->{'method'}, $data->{'url'});
  145. }
  146. else {
  147. croak "Couldn't retrieve command $command settings\n";
  148. }
  149. }
  150. 1;
  151. __END__
  152. =head1 SEE ALSO
  153. For more information about Selenium , visit the website at
  154. L<http://code.google.com/p/selenium/>.
  155. =head1 BUGS
  156. The Selenium issue tracking system is available online at
  157. L<http://code.google.com/p/selenium/issues/list>.
  158. =head1 AUTHOR
  159. Perl Bindings for Remote Driver by Aditya Ivaturi <ivaturi@gmail.com>
  160. =head1 LICENSE
  161. Copyright (c) 2010 Juniper Networks, Inc
  162. Licensed under the Apache License, Version 2.0 (the "License");
  163. you may not use this file except in compliance with the License.
  164. You may obtain a copy of the License at
  165. http://www.apache.org/licenses/LICENSE-2.0
  166. Unless required by applicable law or agreed to in writing, software
  167. distributed under the License is distributed on an "AS IS" BASIS,
  168. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  169. See the License for the specific language governing permissions and
  170. limitations under the License.