1
0

Driver.pm 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  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. # TODO: Verify all these cookied methods - some return errors some don't
  199. # No idea whether they're implemented on the server yet
  200. sub get_all_cookies {
  201. my ($self) = @_;
  202. my $res = {'command' => 'getAllCookies'};
  203. return $self->_execute_command($res);
  204. }
  205. sub add_cookie {
  206. my($self, $name, $value, $path, $domain, $secure) = @_;
  207. if ((not defined $name) ||(not defined $value) ||(not defined $path) ||
  208. (not defined $domain)) {
  209. return "Missing parameters";
  210. }
  211. my $res = {'command' => 'addCookie'};
  212. my $json_false = JSON::false;
  213. my $json_true = JSON::true;
  214. $secure = (defined $secure)?$json_true:$json_false;
  215. my $params = {
  216. 'name' => $name,
  217. 'value' => $value,
  218. 'path' => $path,
  219. 'domain' => $domain,
  220. 'secure' => $secure,
  221. };
  222. return $self->_execute_command($res, $params);
  223. }
  224. sub delete_all_cookies {
  225. my ($self) = @_;
  226. my $res = {'command' => 'deleteAllCookies'};
  227. return $self->_execute_command($res);
  228. }
  229. sub delete_cookie_named {
  230. my ($self, $cookie_name) = @_;
  231. if (not defined $cookie_name) {
  232. return "Cookie name not provided";
  233. }
  234. my $res = {'command' => 'deleteAllCookies', 'name' => $cookie_name};
  235. return $self->_execute_command($res);
  236. }
  237. sub get_page_source {
  238. my ($self) = @_;
  239. my $res = {'command' => 'getPageSource'};
  240. return $self->_execute_command($res);
  241. }
  242. sub find_element {
  243. # TODO: Find out what the locator strategies are - I am assuming xpath, css
  244. # dom etc.
  245. my ($self, $query, $method) = @_;
  246. if (not defined $query) {
  247. return 'Search string to find element not provided.';
  248. }
  249. my $using = (defined $method)?$method:'xpath';
  250. my $res = {'command' => 'findElement'};
  251. my $params = {'using' => $using, 'value' => $query};
  252. return $self->_execute_command($res, $params);
  253. }
  254. sub find_elements {
  255. # TODO: Find out what the locator strategies are - I am assuming xpath, css
  256. # dom etc.
  257. my ($self, $query, $method) = @_;
  258. if (not defined $query) {
  259. return 'Search string to find element not provided.';
  260. }
  261. my $using = (defined $method)?$method:'xpath';
  262. my $res = {'command' => 'findElements'};
  263. my $params = {'using' => $using, 'value' => $query};
  264. return $self->_execute_command($res, $params);
  265. }
  266. sub get_active_element {
  267. my ($self) = @_;
  268. my $res = {'command' => 'getActiveElement'};
  269. return $self->_execute_command($res);
  270. }
  271. sub describe_element {
  272. my ($self, $element) = @_;
  273. #if (not defined $element) {
  274. # return "Element not provided";
  275. #}
  276. #my $res = {'command' => 'desribeElement', 'name' => $element};
  277. #return $self->_execute_command($res);
  278. return "Not yet supported";
  279. }
  280. sub find_child_element {
  281. # TODO: same as find_element - no idea what locator strategy string is & no
  282. # idea what the id is.
  283. my ($self, $id, $query, $method) = @_;
  284. if ((not defined $id) || (not defined $query)) {
  285. return "Missing parameters";
  286. }
  287. my $using = (defined $method)?$method:'xpath';
  288. my $res = {'command' => 'findChildElement', 'id' => $id};
  289. my $params = {'using' => $using, 'value' => $query};
  290. return $self->_execute_command($res, $params);
  291. }
  292. sub find_child_elements {
  293. # TODO: same as find_element - no idea what locator strategy string is & no
  294. # idea what the id is.
  295. my ($self, $id, $query, $method) = @_;
  296. if ((not defined $id) || (not defined $query)) {
  297. return "Missing parameters";
  298. }
  299. my $using = (defined $method)?$method:'xpath';
  300. my $res = {'command' => 'findChildElements', 'id' => $id};
  301. my $params = {'using' => $using, 'value' => $query};
  302. return $self->_execute_command($res, $params);
  303. }
  304. sub click {
  305. #TODO: verify - my local tests are failing
  306. my ($self, $id) = @_;
  307. if (not defined $id) {
  308. return "Element id not provided";
  309. }
  310. my $res = {'command' => 'clickElement', 'id' => $id};
  311. return $self->_execute_command($res);
  312. }
  313. 1;
  314. __END__
  315. =head1 SEE ALSO
  316. For more information about Selenium , visit the website at
  317. L<http://code.google.com/p/selenium/>.
  318. =head1 BUGS
  319. The Selenium issue tracking system is available online at
  320. L<http://code.google.com/p/selenium/issues/list>.
  321. =head1 AUTHOR
  322. Perl Bindings for Remote Driver by Aditya Ivaturi <ivaturi@gmail.com>
  323. =head1 LICENSE
  324. Copyright (c) 2010 Juniper Networks, Inc
  325. Licensed under the Apache License, Version 2.0 (the "License");
  326. you may not use this file except in compliance with the License.
  327. You may obtain a copy of the License at
  328. http://www.apache.org/licenses/LICENSE-2.0
  329. Unless required by applicable law or agreed to in writing, software
  330. distributed under the License is distributed on an "AS IS" BASIS,
  331. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  332. See the License for the specific language governing permissions and
  333. limitations under the License.