Driver.pm 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553
  1. package Selenium::Remote::Driver;
  2. use strict;
  3. use warnings;
  4. use Carp;
  5. our @CARP_NOT;
  6. use Selenium::Remote::RemoteConnection;
  7. use Selenium::Remote::Commands;
  8. use Selenium::Remote::WebElement;
  9. use constant FINDERS => {
  10. class => 'class name',
  11. class_name => 'class name',
  12. css => 'css selector',
  13. id => 'id',
  14. link => 'link text',
  15. link_text => 'link text',
  16. name => 'name',
  17. partial_link_text => 'partial link text',
  18. tag_name => 'tag name',
  19. xpath => 'xpath',
  20. };
  21. our $VERSION = "0.12";
  22. =head1 NAME
  23. Selenium::Remote::Driver - Perl Client for Selenium Remote Driver
  24. =cut
  25. =head1 SYNOPSIS
  26. use Selenium::Remote::Driver;
  27. my $driver = new Selenium::Remote::Driver;
  28. $driver->get('http://www.google.com');
  29. print $driver->get_title();
  30. $driver->quit();
  31. =cut
  32. =head1 DESCRIPTION
  33. Selenium is a test tool that allows you to write
  34. automated web application UI tests in any programming language against
  35. any HTTP website using any mainstream JavaScript-enabled browser. This module is
  36. an implementation of the client for the Remote driver that Selenium provides.
  37. You can find bindings for other languages at this location:
  38. L<http://code.google.com/p/selenium/>
  39. This module sends commands directly to the Server using HTTP. Using this module
  40. together with the Selenium Server, you can automatically control any supported
  41. browser. To use this module, you need to have already downloaded and started
  42. the Selenium Server (Selenium Server is a Java application).
  43. =cut
  44. =head1 USAGE (read this first)
  45. =head2 Remote Driver Response
  46. Selenium::Remote::Driver uses the L<JsonWireProtocol|http://code.google.com/p/selenium/wiki/JsonWireProtocol> to communicate with the
  47. Selenium Server. If an error occurs while executing the command then the server
  48. sends back an HTTP error code with a JSON encoded reponse that indicates the
  49. precise L<Response Error Code|http://code.google.com/p/selenium/wiki/JsonWireProtocol#Response_Status_Codes>. The module will then croak with the error message
  50. associated with this code. If no error occurred, then the subroutine called will
  51. return the value sent back from the server (if a return value was sent).
  52. So a rule of thumb while invoking methods on the driver is if the method did not
  53. croak when called, then you can safely assume the command was successful even if
  54. nothing was returned by the method.
  55. =head2 WebElement
  56. Selenium Webdriver represents all the HTML elements as WebElement, which is
  57. in turn represented by Selenium::Remote::WebElement module. So any method that
  58. deals with WebElements will return and/or expect WebElement object. The POD for
  59. that module describes all the methods that perform various actions on the
  60. WebElements like click, submit etc.
  61. To interact with any WebElement you have to first "find" it, read the POD for
  62. find_element or find_elements for further info. Once you find the required element
  63. then you can perform various actions. If you don't call find_* method first, all
  64. your further actions will fail for that element. Finally, just remember that you
  65. don't have to instantiate WebElement objects at all - they will be automatically
  66. created when you use the find_* methods.
  67. =cut
  68. =head1 FUNCTIONS
  69. =cut
  70. =head2 new
  71. Description:
  72. Constructor for Driver. It'll instantiate the object if it can communicate
  73. with the Selenium RC server.
  74. Input: 7 (all optional)
  75. desired_capabilities - HASH - Following options are accepted:
  76. Optional:
  77. 'remote_server_addr' - <string> - IP or FQDN of the RC server machine
  78. 'browser_name' - <string> - desired browser string:
  79. {iphone|firefox|internet explorer|htmlunit|iphone|chrome}
  80. 'version' - <string> - desired browser version number
  81. 'platform' - <string> - desired platform:
  82. {WINDOWS|XP|VISTA|MAC|LINUX|UNIX|ANY}
  83. 'javascript' - <boolean> - whether javascript should be supported
  84. 'auto_close' - <boolean> - whether driver should end session on remote
  85. server on close.
  86. 'extra_capabilities' - HASH of extra capabilities
  87. If no values are provided, then these defaults will be assumed:
  88. 'remote_server_addr' => 'localhost'
  89. 'port' => '4444'
  90. 'browser_name' => 'firefox'
  91. 'version' => ''
  92. 'platform' => 'ANY'
  93. 'javascript' => 1
  94. 'auto_close' => 1
  95. Output:
  96. Remote Driver object
  97. Usage:
  98. my $driver = new Selenium::Remote::Driver;
  99. or
  100. my $driver = new Selenium::Remote::Driver('browser_name' => 'firefox',
  101. 'platform' => 'MAC');
  102. or
  103. my $driver = new Selenium::Remote::Driver('remote_server_addr' => '10.10.1.1',
  104. 'port' => '2222',
  105. auto_close => 0
  106. );
  107. or
  108. my $driver = new Selenium::Remote::Driver('browser_name' => 'chrome',
  109. 'platform' => 'VISTA',
  110. 'extra_capabilities' => {'chrome.switches' => ["--user-data-dir=$ENV{LOCALAPPDATA}\\Google\\Chrome\\User Data"],},
  111. );
  112. =cut
  113. sub new {
  114. my ( $class, %args ) = @_;
  115. my $ress = new Selenium::Remote::Commands;
  116. # Set the defaults if user doesn't send any
  117. my $self = {
  118. remote_server_addr => delete $args{remote_server_addr} || 'localhost',
  119. browser_name => delete $args{browser_name} || 'firefox',
  120. platform => delete $args{platform} || 'ANY',
  121. port => delete $args{port} || '4444',
  122. version => delete $args{version} || '',
  123. session_id => undef,
  124. remote_conn => undef,
  125. commands => $ress,
  126. auto_close => 1, # by default we will close remote session on DESTROY
  127. pid => $$,
  128. };
  129. bless $self, $class or die "Can't bless $class: $!";
  130. if ( defined $args{javascript} ) {
  131. if ( $args{javascript} ) {
  132. $self->{javascript} = JSON::true;
  133. }
  134. else {
  135. $self->{javascript} = JSON::false;
  136. }
  137. }
  138. else {
  139. $self->{javascript} = JSON::true;
  140. }
  141. # Connect to remote server & establish a new session
  142. $self->{remote_conn} =
  143. new Selenium::Remote::RemoteConnection( $self->{remote_server_addr},
  144. $self->{port} );
  145. $self->new_session(delete $args{extra_capabilities});
  146. if ( !( defined $self->{session_id} ) ) {
  147. croak "Could not establish a session with the remote server\n";
  148. }
  149. return $self;
  150. }
  151. sub DESTROY {
  152. my ($self) = @_;
  153. return if $$ != $self->{pid};
  154. $self->quit() if ($self->{auto_close} && defined $self->{session_id});
  155. }
  156. # This is an internal method used the Driver & is not supposed to be used by
  157. # end user. This method is used by Driver to set up all the parameters
  158. # (url & JSON), send commands & receive processed response from the server.
  159. sub _execute_command {
  160. my ( $self, $res, $params ) = @_;
  161. $res->{'session_id'} = $self->{'session_id'};
  162. my $resource = $self->{commands}->get_params($res);
  163. if ($resource) {
  164. my $resp = $self->{remote_conn}
  165. ->request( $resource->{'method'}, $resource->{'url'}, $params );
  166. if(ref($resp) eq 'HASH') {
  167. if($resp->{cmd_status} eq 'OK') {
  168. return $resp->{cmd_return};
  169. } else {
  170. my $msg = "Error while executing command";
  171. if($resp->{cmd_error}) {
  172. $msg .= ": $resp->{cmd_error}" if $resp->{cmd_error};
  173. } else {
  174. if(ref($resp->{cmd_return}) eq 'HASH') {
  175. $msg .= ": $resp->{cmd_return}->{error}->{msg}"
  176. if $resp->{cmd_return}->{error}->{msg};
  177. } else {
  178. $msg .= ": $resp->{cmd_return}";
  179. }
  180. }
  181. croak $msg;
  182. }
  183. }
  184. return $resp;
  185. }
  186. else {
  187. croak "Couldn't retrieve command settings properly\n";
  188. }
  189. }
  190. # A method that is used by the Driver itself. It'll be called to set the
  191. # desired capabilities on the server.
  192. sub new_session {
  193. my ($self, $extra_capabilities) = @_;
  194. $extra_capabilities ||= {};
  195. my $args = {
  196. 'desiredCapabilities' => {
  197. 'browserName' => $self->{browser_name},
  198. 'platform' => $self->{platform},
  199. 'javascriptEnabled' => $self->{javascript},
  200. 'version' => $self->{version},
  201. %$extra_capabilities,
  202. },
  203. };
  204. my $resp =
  205. $self->{remote_conn}
  206. ->request( $self->{commands}->{'newSession'}->{'method'},
  207. $self->{commands}->{'newSession'}->{'url'}, $args, );
  208. if ( ( defined $resp->{'sessionId'} ) && $resp->{'sessionId'} ne '' ) {
  209. $self->{session_id} = $resp->{'sessionId'};
  210. }
  211. else {
  212. croak "Could not create new session";
  213. }
  214. }
  215. =head2 status
  216. Description:
  217. Query the server's current status. All server implementations
  218. should return two basic objects describing the server's current
  219. platform and when the server was built.
  220. Output:
  221. Hash ref
  222. Usage:
  223. print Dumper $driver->status;
  224. =cut
  225. sub status {
  226. my ($self) = @_;
  227. my $res = { 'command' => 'status' };
  228. return $self->_execute_command($res);
  229. }
  230. =head2 get_alert_text
  231. Description:
  232. Gets the text of the currently displayed JavaScript alert(), confirm()
  233. or prompt() dialog.
  234. Example
  235. my $string = $driver->get_alert_text;
  236. =cut
  237. sub get_alert_text {
  238. my ($self) = @_;
  239. my $res = { 'command' => 'getAlertText' };
  240. return $self->_execute_command($res);
  241. }
  242. =head2 send_keys_to_alert
  243. Synonymous with send_keys_to_prompt
  244. =cut
  245. sub send_keys_to_alert {
  246. return shift->send_keys_to_prompt(@_);
  247. }
  248. =head2 send_keys_to_prompt
  249. Description:
  250. Sends keystrokes to a JavaScript prompt() dialog.
  251. Input:
  252. {string} keys to send
  253. Example:
  254. $driver->send_keys_to_prompt('hello world');
  255. or
  256. ok($driver->get_alert_text eq 'Please Input your name','prompt appears');
  257. $driver->send_keys_to_alert("Larry Wall");
  258. $driver->accept_alert;
  259. =cut
  260. sub send_keys_to_prompt {
  261. my ($self,$keys) = @_;
  262. my $res = { 'command' => 'sendKeysToPrompt' };
  263. my $params = { 'text' => $keys };
  264. return $self->_execute_command($res,$params);
  265. }
  266. =head2 accept_alert
  267. Description:
  268. Accepts the currently displayed alert dialog. Usually, this is
  269. equivalent to clicking the 'OK' button in the dialog.
  270. Example:
  271. $driver->accept_alert;
  272. =cut
  273. sub accept_alert {
  274. my ($self) = @_;
  275. my $res = { 'command' => 'acceptAlert' };
  276. return $self->_execute_command($res);
  277. }
  278. =head2 dismiss_alert
  279. Description:
  280. Dismisses the currently displayed alert dialog. For comfirm()
  281. and prompt() dialogs, this is equivalent to clicking the
  282. 'Cancel' button. For alert() dialogs, this is equivalent to
  283. clicking the 'OK' button.
  284. Example:
  285. $driver->dismiss_alert;
  286. =cut
  287. sub dismiss_alert {
  288. my ($self) = @_;
  289. my $res = { 'command' => 'dismissAlert' };
  290. return $self->_execute_command($res);
  291. }
  292. =head2 mouse_move_to_location
  293. Description:
  294. Move the mouse by an offset of the specificed element. If no
  295. element is specified, the move is relative to the current mouse
  296. cursor. If an element is provided but no offset, the mouse will be
  297. moved to the center of the element. If the element is not visible,
  298. it will be scrolled into view.
  299. Output:
  300. STRING -
  301. Usage:
  302. # element - the element to move to. If not specified or is null, the offset is relative to current position of the mouse.
  303. # xoffset - X offset to move to, relative to the top-left corner of the element. If not specified, the mouse will move to the middle of the element.
  304. # yoffset - Y offset to move to, relative to the top-left corner of the element. If not specified, the mouse will move to the middle of the element.
  305. print $driver->mouse_move_to_location(element => e, xoffset => x, yoffset => y);
  306. =cut
  307. sub mouse_move_to_location {
  308. my ($self, %params) = @_;
  309. $params{element} = $params{element}{id} if exists $params{element};
  310. my $res = { 'command' => 'mouseMoveToLocation' };
  311. return $self->_execute_command($res, \%params);
  312. }
  313. =head2 move_to
  314. Synonymous with mouse_move_to_location
  315. =cut
  316. sub move_to {
  317. return shift->mouse_move_to_location(@_);
  318. }
  319. =head2 get_capabilities
  320. Description:
  321. Retrieve the capabilities of the specified session.
  322. Output:
  323. HASH of all the capabilities.
  324. Usage:
  325. my $capab = $driver->get_capabilities();
  326. print Dumper($capab);
  327. =cut
  328. sub get_capabilities {
  329. my $self = shift;
  330. my $res = {'command' => 'getCapabilities'};
  331. return $self->_execute_command($res);
  332. }
  333. =head2 set_implicit_wait_timeout
  334. Description:
  335. Set the amount of time the driver should wait when searching for elements.
  336. When searching for a single element, the driver will poll the page until
  337. an element is found or the timeout expires, whichever occurs first.
  338. When searching for multiple elements, the driver should poll the page until
  339. at least one element is found or the timeout expires, at which point it
  340. will return an empty list. If this method is never called, the driver will
  341. default to an implicit wait of 0ms.
  342. Input:
  343. Time in milliseconds.
  344. Output:
  345. Server Response Hash with no data returned back from the server.
  346. Usage:
  347. $driver->set_implicit_wait_timeout(10);
  348. =cut
  349. sub set_implicit_wait_timeout {
  350. my ($self, $ms) = @_;
  351. my $res = {'command' => 'setImplicitWaitTimeout'};
  352. my $params = {'ms' => $ms};
  353. return $self->_execute_command($res, $params);
  354. }
  355. =head2 close
  356. Description:
  357. Close the current window.
  358. Usage:
  359. $driver->close();
  360. or
  361. #close a popup window
  362. my $handles = $driver->get_window_handles;
  363. $driver->switch_to_window($handles->[1]);
  364. $driver->close();
  365. $driver->switch_to_window($handles->[0]);
  366. =cut
  367. sub close {
  368. my $self = shift;
  369. my $res = { 'command' => 'close' };
  370. $self->_execute_command($res);
  371. }
  372. =head2 quit
  373. Description:
  374. Delete the session & close open browsers.
  375. Usage:
  376. $driver->quit();
  377. =cut
  378. sub quit {
  379. my $self = shift;
  380. my $res = { 'command' => 'quit' };
  381. $self->_execute_command($res);
  382. $self->{session_id} = undef;
  383. }
  384. =head2 get_current_window_handle
  385. Description:
  386. Retrieve the current window handle.
  387. Output:
  388. STRING - the window handle
  389. Usage:
  390. print $driver->get_current_window_handle();
  391. =cut
  392. sub get_current_window_handle {
  393. my $self = shift;
  394. my $res = { 'command' => 'getCurrentWindowHandle' };
  395. return $self->_execute_command($res);
  396. }
  397. =head2 get_window_handles
  398. Description:
  399. Retrieve the list of window handles used in the session.
  400. Output:
  401. ARRAY of STRING - list of the window handles
  402. Usage:
  403. print Dumper $driver->get_window_handles;
  404. or
  405. # get popup, close, then back
  406. my $handles = $driver->get_window_handles;
  407. $driver->switch_to_window($handles->[1]);
  408. $driver->close;
  409. $driver->switch_to_window($handles->[0]);
  410. =cut
  411. sub get_window_handles {
  412. my $self = shift;
  413. my $res = { 'command' => 'getWindowHandles' };
  414. return $self->_execute_command($res);
  415. }
  416. =head2 get_current_url
  417. Description:
  418. Retrieve the url of the current page
  419. Output:
  420. STRING - url
  421. Usage:
  422. print $driver->get_current_url();
  423. =cut
  424. sub get_current_url {
  425. my $self = shift;
  426. my $res = { 'command' => 'getCurrentUrl' };
  427. return $self->_execute_command($res);
  428. }
  429. =head2 navigate
  430. Description:
  431. Navigate to a given url. This is same as get() method.
  432. Input:
  433. STRING - url
  434. Usage:
  435. $driver->navigate('http://www.google.com');
  436. =cut
  437. sub navigate {
  438. my ( $self, $url ) = @_;
  439. $self->get($url);
  440. }
  441. =head2 get
  442. Description:
  443. Navigate to a given url
  444. Input:
  445. STRING - url
  446. Usage:
  447. $driver->get('http://www.google.com');
  448. =cut
  449. sub get {
  450. my ( $self, $url ) = @_;
  451. my $res = { 'command' => 'get' };
  452. my $params = { 'url' => $url };
  453. return $self->_execute_command( $res, $params );
  454. }
  455. =head2 get_title
  456. Description:
  457. Get the current page title
  458. Output:
  459. STRING - Page title
  460. Usage:
  461. print $driver->get_title();
  462. =cut
  463. sub get_title {
  464. my $self = shift;
  465. my $res = { 'command' => 'getTitle' };
  466. return $self->_execute_command($res);
  467. }
  468. =head2 go_back
  469. Description:
  470. Equivalent to hitting the back button on the browser.
  471. Usage:
  472. $driver->go_back();
  473. =cut
  474. sub go_back {
  475. my $self = shift;
  476. my $res = { 'command' => 'goBack' };
  477. return $self->_execute_command($res);
  478. }
  479. =head2 go_forward
  480. Description:
  481. Equivalent to hitting the forward button on the browser.
  482. Usage:
  483. $driver->go_forward();
  484. =cut
  485. sub go_forward {
  486. my $self = shift;
  487. my $res = { 'command' => 'goForward' };
  488. return $self->_execute_command($res);
  489. }
  490. =head2 refresh
  491. Description:
  492. Reload the current page.
  493. Usage:
  494. $driver->refresh();
  495. =cut
  496. sub refresh {
  497. my $self = shift;
  498. my $res = { 'command' => 'refresh' };
  499. return $self->_execute_command($res);
  500. }
  501. =head2 javascript
  502. Description:
  503. returns true if javascript is enabled in the driver.
  504. Usage:
  505. if ($driver->javascript) { ...; }
  506. =cut
  507. sub javascript {
  508. my $self = shift;
  509. return $self->{javascript} == JSON::true;
  510. }
  511. =head2 execute_async_script
  512. Description:
  513. Inject a snippet of JavaScript into the page for execution in the context
  514. of the currently selected frame. The executed script is assumed to be
  515. asynchronous and must signal that is done by invoking the provided
  516. callback, which is always provided as the final argument to the function.
  517. The value to this callback will be returned to the client.
  518. Asynchronous script commands may not span page loads. If an unload event
  519. is fired while waiting for a script result, an error should be returned
  520. to the client.
  521. Input: 2 (1 optional)
  522. Required:
  523. STRING - Javascript to execute on the page
  524. Optional:
  525. ARRAY - list of arguments that need to be passed to the script.
  526. Output:
  527. {*} - Varied, depending on the type of result expected back from the script.
  528. Usage:
  529. my $script = q{
  530. var arg1 = arguments[0];
  531. var callback = arguments[arguments.length-1];
  532. var elem = window.document.findElementById(arg1);
  533. callback(elem);
  534. };
  535. my $callback = q{return arguments[0];};
  536. my $elem = $driver->execute_async_script($script,'myid',$callback);
  537. $elem->click;
  538. =cut
  539. sub execute_async_script {
  540. my ( $self, $script, @args ) = @_;
  541. if ($self->javascript) {
  542. if ( not defined $script ) {
  543. return 'No script provided';
  544. }
  545. my $res = { 'command' => 'executeAsyncScript' };
  546. # Check the args array if the elem obj is provided & replace it with
  547. # JSON representation
  548. for (my $i=0; $i<@args; $i++) {
  549. if (ref $args[$i] eq 'Selenium::Remote::WebElement') {
  550. $args[$i] = {'ELEMENT' => ($args[$i])->{id}};
  551. }
  552. }
  553. my $params = {'script' => $script, 'args' => \@args};
  554. my $ret = $self->_execute_command($res, $params);
  555. # replace any ELEMENTS with WebElement
  556. if (ref($ret) and (ref($ret) eq 'HASH') and exists $ret->{'ELEMENT'}) {
  557. $ret =
  558. new Selenium::Remote::WebElement(
  559. $ret->{ELEMENT}, $self);
  560. }
  561. return $ret;
  562. }
  563. else {
  564. croak 'Javascript is not enabled on remote driver instance.';
  565. }
  566. }
  567. =head2 execute_script
  568. Description:
  569. Inject a snippet of JavaScript into the page and return its result.
  570. WebElements that should be passed to the script as an argument should be
  571. specified in the arguments array as WebElement object. Likewise,
  572. any WebElements in the script result will be returned as WebElement object.
  573. Input: 2 (1 optional)
  574. Required:
  575. STRING - Javascript to execute on the page
  576. Optional:
  577. ARRAY - list of arguments that need to be passed to the script.
  578. Output:
  579. {*} - Varied, depending on the type of result expected back from the script.
  580. Usage:
  581. my $script = q{
  582. var arg1 = arguments[0];
  583. var elem = window.document.findElementById(arg1);
  584. return elem;
  585. };
  586. my $elem = $driver->execute_script($script,'myid');
  587. $elem->click;
  588. =cut
  589. sub execute_script {
  590. my ( $self, $script, @args ) = @_;
  591. if ($self->javascript) {
  592. if ( not defined $script ) {
  593. return 'No script provided';
  594. }
  595. my $res = { 'command' => 'executeScript' };
  596. # Check the args array if the elem obj is provided & replace it with
  597. # JSON representation
  598. for (my $i=0; $i<@args; $i++) {
  599. if (ref $args[$i] eq 'Selenium::Remote::WebElement') {
  600. $args[$i] = {'ELEMENT' => ($args[$i])->{id}};
  601. }
  602. }
  603. my $params = {'script' => $script, 'args' => [@args]};
  604. my $ret = $self->_execute_command($res, $params);
  605. # replace any ELEMENTS with WebElement
  606. if (ref($ret) and (ref($ret) eq 'HASH') and exists $ret->{'ELEMENT'}) {
  607. $ret =
  608. new Selenium::Remote::WebElement(
  609. $ret->{ELEMENT}, $self);
  610. }
  611. return $ret;
  612. }
  613. else {
  614. croak 'Javascript is not enabled on remote driver instance.';
  615. }
  616. }
  617. =head2 screenshot
  618. Description:
  619. Get a screenshot of the current page as a base64 encoded image.
  620. Output:
  621. STRING - base64 encoded image
  622. Usage:
  623. print $driver->screenshot();
  624. or
  625. require MIME::Base64;
  626. open(FH,'>','screenshot.png');
  627. binmode FH;
  628. my $png_base64 = $driver->screenshot();
  629. print FH MIME::Base64::decode_base64($png_base64);
  630. close FH;
  631. =cut
  632. sub screenshot {
  633. my ($self) = @_;
  634. my $res = { 'command' => 'screenshot' };
  635. return $self->_execute_command($res);
  636. }
  637. =head2 available_engines
  638. Description:
  639. List all available engines on the machine. To use an engine, it has to be present in this list.
  640. Output:
  641. {Array.<string>} A list of available engines
  642. Usage:
  643. print Dumper $driver->available_engines;
  644. =cut
  645. sub available_engines {
  646. my ($self) = @_;
  647. my $res = { 'command' => 'availableEngines' };
  648. return $self->_execute_command($res);
  649. }
  650. =head2 switch_to_frame
  651. Description:
  652. Change focus to another frame on the page. If the frame ID is null, the
  653. server will switch to the page's default content.
  654. Input: 1
  655. Required:
  656. {STRING | NUMBER | NULL} - ID of the frame which can be one of the three
  657. mentioned.
  658. Usage:
  659. $driver->switch_to_frame('frame_1');
  660. =cut
  661. sub switch_to_frame {
  662. my ( $self, $id ) = @_;
  663. my $json_null = JSON::null;
  664. $id = ( defined $id ) ? $id : $json_null;
  665. my $res = { 'command' => 'switchToFrame' };
  666. my $params = { 'id' => $id };
  667. return $self->_execute_command( $res, $params );
  668. }
  669. =head2 switch_to_window
  670. Description:
  671. Change focus to another window. The window to change focus to may be
  672. specified by its server assigned window handle, or by the value of its name
  673. attribute.
  674. Input: 1
  675. Required:
  676. STRING - Window handle or the Window name
  677. Usage:
  678. $driver->switch_to_window('MY Homepage');
  679. or
  680. # close a popup window and switch back
  681. my $handles = $driver->get_window_handles;
  682. $driver->switch_to_window($handles->[1]);
  683. $driver->close;
  684. $driver->switch_to_window($handles->[0]);
  685. =cut
  686. sub switch_to_window {
  687. my ( $self, $name ) = @_;
  688. if ( not defined $name ) {
  689. return 'Window name not provided';
  690. }
  691. my $res = { 'command' => 'switchToWindow' };
  692. my $params = { 'name' => $name };
  693. return $self->_execute_command( $res, $params );
  694. }
  695. =head2 get_speed
  696. Description:
  697. Get the current user input speed. The actual input speed is still browser
  698. specific and not covered by the Driver.
  699. Output:
  700. STRING - One of these: SLOW, MEDIUM, FAST
  701. Usage:
  702. print $driver->get_speed();
  703. =cut
  704. sub get_speed {
  705. my ($self) = @_;
  706. my $res = { 'command' => 'getSpeed' };
  707. return $self->_execute_command($res);
  708. }
  709. =head2 set_speed
  710. Description:
  711. Set the user input speed.
  712. Input:
  713. STRING - One of these: SLOW, MEDIUM, FAST
  714. Usage:
  715. $driver->set_speed('MEDIUM');
  716. Note: This function is a no-op in WebDriver (?). See
  717. https://groups.google.com/d/topic/selenium-users/oX0ZnYFPuSA/discussion and
  718. http://code.google.com/p/selenium/source/browse/trunk/java/client/src/org/openqa/selenium/WebDriverCommandProcessor.java
  719. =cut
  720. sub set_speed {
  721. my ( $self, $speed ) = @_;
  722. if ( not defined $speed ) {
  723. return 'Speed not provided.';
  724. }
  725. my $res = { 'command' => 'setSpeed' };
  726. my $params = { 'speed' => $speed };
  727. return $self->_execute_command( $res, $params );
  728. }
  729. =head2 get_all_cookies
  730. Description:
  731. Retrieve all cookies visible to the current page. Each cookie will be
  732. returned as a HASH reference with the following keys & their value types:
  733. 'name' - STRING
  734. 'value' - STRING
  735. 'path' - STRING
  736. 'domain' - STRING
  737. 'secure' - BOOLEAN
  738. Output:
  739. ARRAY of HASHES - list of all the cookie hashes
  740. Usage:
  741. print Dumper($driver->get_all_cookies());
  742. =cut
  743. sub get_all_cookies {
  744. my ($self) = @_;
  745. my $res = { 'command' => 'getAllCookies' };
  746. return $self->_execute_command($res);
  747. }
  748. =head2 add_cookie
  749. Description:
  750. Set a cookie on the domain.
  751. Input: 5 (1 optional)
  752. Required:
  753. 'name' - STRING
  754. 'value' - STRING
  755. 'path' - STRING
  756. 'domain' - STRING
  757. Optional:
  758. 'secure' - BOOLEAN - default is false.
  759. Usage:
  760. $driver->add_cookie('foo', 'bar', '/', '.google.com', 0)
  761. =cut
  762. sub add_cookie {
  763. my ( $self, $name, $value, $path, $domain, $secure ) = @_;
  764. if ( ( not defined $name )
  765. || ( not defined $value )
  766. || ( not defined $path )
  767. || ( not defined $domain ) )
  768. {
  769. return "Missing parameters";
  770. }
  771. my $res = { 'command' => 'addCookie' };
  772. my $json_false = JSON::false;
  773. my $json_true = JSON::true;
  774. $secure = ( defined $secure ) ? $json_true : $json_false;
  775. my $params = {
  776. 'cookie' => {
  777. 'name' => $name,
  778. 'value' => $value,
  779. 'path' => $path,
  780. 'domain' => $domain,
  781. 'secure' => $secure,
  782. }
  783. };
  784. return $self->_execute_command( $res, $params );
  785. }
  786. =head2 delete_all_cookies
  787. Description:
  788. Delete all cookies visible to the current page.
  789. Usage:
  790. $driver->delete_all_cookies();
  791. =cut
  792. sub delete_all_cookies {
  793. my ($self) = @_;
  794. my $res = { 'command' => 'deleteAllCookies' };
  795. return $self->_execute_command($res);
  796. }
  797. =head2 delete_cookie_named
  798. Description:
  799. Delete the cookie with the given name. This command will be a no-op if there
  800. is no such cookie visible to the current page.
  801. Input: 1
  802. Required:
  803. STRING - name of cookie to delete
  804. Usage:
  805. $driver->delete_cookie_named('foo');
  806. =cut
  807. sub delete_cookie_named {
  808. my ( $self, $cookie_name ) = @_;
  809. if ( not defined $cookie_name ) {
  810. return "Cookie name not provided";
  811. }
  812. my $res = { 'command' => 'deleteCookieNamed', 'name' => $cookie_name };
  813. return $self->_execute_command($res);
  814. }
  815. =head2 get_page_source
  816. Description:
  817. Get the current page source.
  818. Output:
  819. STRING - The page source.
  820. Usage:
  821. print $driver->get_page_source();
  822. =cut
  823. sub get_page_source {
  824. my ($self) = @_;
  825. my $res = { 'command' => 'getPageSource' };
  826. return $self->_execute_command($res);
  827. }
  828. =head2 find_element
  829. Description:
  830. Search for an element on the page, starting from the document root. The
  831. located element will be returned as a WebElement object.
  832. Input: 2 (1 optional)
  833. Required:
  834. STRING - The search target.
  835. Optional:
  836. STRING - Locator scheme to use to search the element, available schemes:
  837. {class, class_name, css, id, link, link_text, partial_link_text,
  838. tag_name, name, xpath}
  839. Defaults to 'xpath'.
  840. Output:
  841. Selenium::Remote::WebElement - WebElement Object
  842. Usage:
  843. $driver->find_element("//input[\@name='q']");
  844. =cut
  845. sub find_element {
  846. my ( $self, $query, $method ) = @_;
  847. if ( not defined $query ) {
  848. return 'Search string to find element not provided.';
  849. }
  850. my $using = ( defined $method ) ? FINDERS->{$method} : 'xpath';
  851. if (defined $using) {
  852. my $res = { 'command' => 'findElement' };
  853. my $params = { 'using' => $using, 'value' => $query };
  854. my $ret_data = eval { $self->_execute_command( $res, $params ); };
  855. if($@) {
  856. if($@ =~ /(An element could not be located on the page using the given search parameters)/) {
  857. # give details on what element wasn't found
  858. $@ = "$1: $query,$using";
  859. local @CARP_NOT = ("Selenium::Remote::Driver",@CARP_NOT);
  860. croak $@;
  861. } else {
  862. # re throw if the exception wasn't what we expected
  863. die $@;
  864. }
  865. }
  866. return new Selenium::Remote::WebElement($ret_data->{ELEMENT}, $self);
  867. }
  868. else {
  869. croak "Bad method, expected - class, class_name, css, id, link,
  870. link_text, partial_link_text, name, tag_name, xpath";
  871. }
  872. }
  873. =head2 find_elements
  874. Description:
  875. Search for multiple elements on the page, starting from the document root.
  876. The located elements will be returned as an array of WebElement object.
  877. Input: 2 (1 optional)
  878. Required:
  879. STRING - The search target.
  880. Optional:
  881. STRING - Locator scheme to use to search the element, available schemes:
  882. {class, class_name, css, id, link, link_text, partial_link_text,
  883. tag_name, name, xpath}
  884. Defaults to 'xpath'.
  885. Output:
  886. ARRAY of Selenium::Remote::WebElement - Array of WebElement Objects
  887. Usage:
  888. $driver->find_elements("//input");
  889. =cut
  890. sub find_elements {
  891. my ( $self, $query, $method ) = @_;
  892. if ( not defined $query ) {
  893. return 'Search string to find element not provided.';
  894. }
  895. my $using = ( defined $method ) ? FINDERS->{$method} : 'xpath';
  896. if (defined $using) {
  897. my $res = { 'command' => 'findElements' };
  898. my $params = { 'using' => $using, 'value' => $query };
  899. my $ret_data = eval {$self->_execute_command( $res, $params );};
  900. if($@) {
  901. if($@ =~ /(An element could not be located on the page using the given search parameters)/) {
  902. # give details on what element wasn't found
  903. $@ = "$1: $query,$using";
  904. local @CARP_NOT = ("Selenium::Remote::Driver",@CARP_NOT);
  905. croak $@;
  906. } else {
  907. # re throw if the exception wasn't what we expected
  908. die $@;
  909. }
  910. }
  911. my $elem_obj_arr;
  912. my $i = 0;
  913. foreach (@$ret_data) {
  914. $elem_obj_arr->[$i] = new Selenium::Remote::WebElement($_->{ELEMENT}, $self);
  915. $i++;
  916. }
  917. return wantarray?@{$elem_obj_arr}:$elem_obj_arr;
  918. }
  919. else {
  920. croak "Bad method, expected - class, class_name, css, id, link,
  921. link_text, partial_link_text, name, tag_name, xpath";
  922. }
  923. }
  924. =head2 find_child_element
  925. Description:
  926. Search for an element on the page, starting from the identified element. The
  927. located element will be returned as a WebElement object.
  928. Input: 3 (1 optional)
  929. Required:
  930. Selenium::Remote::WebElement - WebElement object from where you want to
  931. start searching.
  932. STRING - The search target. (Do not use a double whack('//')
  933. in an xpath to search for a child element
  934. ex: '//option[@id="something"]'
  935. instead use a dot whack ('./')
  936. ex: './option[@id="something"]')
  937. Optional:
  938. STRING - Locator scheme to use to search the element, available schemes:
  939. {class, class_name, css, id, link, link_text, partial_link_text,
  940. tag_name, name, xpath}
  941. Defaults to 'xpath'.
  942. Output:
  943. Selenium::Remote::WebElement - WebElement Object
  944. Usage:
  945. my $elem1 = $driver->find_element("//select[\@name='ned']");
  946. # note the usage of ./ when searching for a child element instead of //
  947. my $child = $driver->find_child_element($elem1, "./option[\@value='es_ar']");
  948. =cut
  949. sub find_child_element {
  950. my ( $self, $elem, $query, $method ) = @_;
  951. if ( ( not defined $elem ) || ( not defined $query ) ) {
  952. return "Missing parameters";
  953. }
  954. my $using = ( defined $method ) ? $method : 'xpath';
  955. if (exists FINDERS->{$using}) {
  956. my $res = { 'command' => 'findChildElement', 'id' => $elem->{id} };
  957. my $params = { 'using' => FINDERS->{$using}, 'value' => $query };
  958. my $ret_data = eval {$self->_execute_command( $res, $params );};
  959. if($@) {
  960. if($@ =~ /(An element could not be located on the page using the given search parameters)/) {
  961. # give details on what element wasn't found
  962. $@ = "$1: $query,$using";
  963. local @CARP_NOT = ("Selenium::Remote::Driver",@CARP_NOT);
  964. croak $@;
  965. } else {
  966. # re throw if the exception wasn't what we expected
  967. die $@;
  968. }
  969. }
  970. return new Selenium::Remote::WebElement($ret_data->{ELEMENT}, $self);
  971. }
  972. else {
  973. croak "Bad method, expected - class, class_name, css, id, link,
  974. link_text, partial_link_text, name, tag_name, xpath";
  975. }
  976. }
  977. =head2 find_child_elements
  978. Description:
  979. Search for multiple element on the page, starting from the identified
  980. element. The located elements will be returned as an array of WebElement
  981. objects.
  982. Input: 3 (1 optional)
  983. Required:
  984. Selenium::Remote::WebElement - WebElement object from where you want to
  985. start searching.
  986. STRING - The search target. (Do not use a double whack('//')
  987. in an xpath to search for child elements
  988. ex: '//option[@class="something"]'
  989. instead use a dot whack ('./')
  990. ex: './option[@class="something"]')
  991. Optional:
  992. STRING - Locator scheme to use to search the element, available schemes:
  993. {class, class_name, css, id, link, link_text, partial_link_text,
  994. tag_name, name, xpath}
  995. Defaults to 'xpath'.
  996. Output:
  997. ARRAY of Selenium::Remote::WebElement - Array of WebElement Objects.
  998. Usage:
  999. my $elem1 = $driver->find_element("//select[\@name='ned']");
  1000. # note the usage of ./ to search for child elements instead of //
  1001. my $child = $driver->find_child_elements($elem1, "./option");
  1002. =cut
  1003. sub find_child_elements {
  1004. my ( $self, $elem, $query, $method ) = @_;
  1005. if ( ( not defined $elem ) || ( not defined $query ) ) {
  1006. return "Missing parameters";
  1007. }
  1008. my $using = ( defined $method ) ? $method : 'xpath';
  1009. if (exists FINDERS->{$using}) {
  1010. my $res = { 'command' => 'findChildElements', 'id' => $elem->{id} };
  1011. my $params = { 'using' => FINDERS->{$using}, 'value' => $query };
  1012. my $ret_data = eval {$self->_execute_command( $res, $params );};
  1013. if($@) {
  1014. if($@ =~ /(An element could not be located on the page using the given search parameters)/) {
  1015. # give details on what element wasn't found
  1016. $@ = "$1: $query,$using";
  1017. local @CARP_NOT = ("Selenium::Remote::Driver",@CARP_NOT);
  1018. croak $@;
  1019. } else {
  1020. # re throw if the exception wasn't what we expected
  1021. die $@;
  1022. }
  1023. }
  1024. my $elem_obj_arr;
  1025. my $i = 0;
  1026. foreach (@$ret_data) {
  1027. $elem_obj_arr->[$i] = new Selenium::Remote::WebElement($_->{ELEMENT}, $self);
  1028. $i++;
  1029. }
  1030. return wantarray?@{$elem_obj_arr}:$elem_obj_arr;
  1031. }
  1032. else {
  1033. croak "Bad method, expected - class, class_name, css, id, link,
  1034. link_text, partial_link_text, name, tag_name, xpath";
  1035. }
  1036. }
  1037. =head2 get_active_element
  1038. Description:
  1039. Get the element on the page that currently has focus.. The located element
  1040. will be returned as a WebElement object.
  1041. Output:
  1042. Selenium::Remote::WebElement - WebElement Object
  1043. Usage:
  1044. $driver->get_active_element();
  1045. =cut
  1046. sub get_active_element {
  1047. my ($self) = @_;
  1048. my $res = { 'command' => 'getActiveElement' };
  1049. my $ret_data = eval { $self->_execute_command( $res ); };
  1050. if($@) {
  1051. carp $@;
  1052. }
  1053. return new Selenium::Remote::WebElement($ret_data->{ELEMENT}, $self);
  1054. }
  1055. # Not yet supported on the server
  1056. sub describe_element {
  1057. my ( $self, $element ) = @_;
  1058. #if (not defined $element) {
  1059. # return "Element not provided";
  1060. #}
  1061. #my $res = {'command' => 'desribeElement', 'name' => $element};
  1062. #return $self->_execute_command($res);
  1063. return "Not yet supported";
  1064. }
  1065. =head2 send_modifier
  1066. Description:
  1067. Send an event to the active element to depress or release a modifier key.
  1068. Input: 2
  1069. Required:
  1070. value - String - The modifier key event to be sent. This key must be one 'Ctrl','Shift','Alt',' or 'Command'/'Meta' as defined by the send keys command
  1071. isdown - Boolean/String - Whether to generate a key down or key up
  1072. Usage:
  1073. $driver->send_modifier('Alt','down');
  1074. $elem->send_keys('c');
  1075. $driver->send_modifier('Alt','up');
  1076. or
  1077. $driver->send_modifier('Alt',1);
  1078. $elem->send_keys('c');
  1079. $driver->send_modifier('Alt',0);
  1080. =cut
  1081. sub send_modifier {
  1082. my ($self,$modifier,$isdown) = @_;
  1083. if($isdown =~ /(down|up)/) {
  1084. $isdown = $isdown =~ /down/ ? 1:0;
  1085. }
  1086. my $res = {'command' => 'sendModifier'};
  1087. my $params = {value => $modifier,
  1088. isdown => $isdown};
  1089. return $self->_execute_command($res,$params);
  1090. }
  1091. =head2 compare_elements
  1092. Description:
  1093. Test if two element IDs refer to the same DOM element.
  1094. Input: 2
  1095. Required:
  1096. Selenium::Remote::WebElement - WebElement Object
  1097. Selenium::Remote::WebElement - WebElement Object
  1098. Output:
  1099. BOOLEAN
  1100. Usage:
  1101. $driver->compare_elements($elem_obj1, $elem_obj2);
  1102. =cut
  1103. sub compare_elements {
  1104. my ($self, $elem1, $elem2) = @_;
  1105. my $res = { 'command' => 'elementEquals',
  1106. 'id' => $elem1->{id},
  1107. 'other' => $elem2->{id}
  1108. };
  1109. return $self->_execute_command($res);
  1110. }
  1111. =head2 click
  1112. Description:
  1113. Click any mouse button (at the coordinates set by the last moveto command).
  1114. Input:
  1115. button - any one of 'LEFT'/0 'MIDDLE'/1 'RIGHT'/2
  1116. defaults to 'LEFT'
  1117. Usage:
  1118. $driver->click('LEFT');
  1119. $driver->click(1); #MIDDLE
  1120. $driver->click('RIGHT');
  1121. $driver->click; #Defaults to left
  1122. =cut
  1123. sub click {
  1124. my ($self,$button) = @_;
  1125. my $button_enum = {LEFT=>0,MIDDLE=>1,RIGHT=>2};
  1126. if(defined $button && $button =~ /(LEFT|MIDDLE|RIGHT)/i) {
  1127. $button = $button_enum->{uc $1};
  1128. } elsif(defined $button && $button =~ /(0|1|2)/) {
  1129. $button = $1;
  1130. } else {
  1131. $button = 0;
  1132. }
  1133. my $res = { 'command' => 'click' };
  1134. my $params = { 'button' => $button };
  1135. return $self->_execute_command($res,$params);
  1136. }
  1137. =head2 double_click
  1138. Description:
  1139. Double-clicks at the current mouse coordinates (set by moveto).
  1140. Usage:
  1141. $driver->double_click;
  1142. =cut
  1143. sub double_click {
  1144. my ($self) = @_;
  1145. my $res = { 'command' => 'doubleClick' };
  1146. return $self->_execute_command($res);
  1147. }
  1148. =head2 button_down
  1149. Description:
  1150. Click and hold the left mouse button (at the coordinates set by the
  1151. last moveto command). Note that the next mouse-related command that
  1152. should follow is buttondown . Any other mouse command (such as click
  1153. or another call to buttondown) will yield undefined behaviour.
  1154. Usage:
  1155. $self->button_down;
  1156. =cut
  1157. sub button_down {
  1158. my ($self) = @_;
  1159. my $res = { 'command' => 'buttonDown' };
  1160. return $self->_execute_command($res);
  1161. }
  1162. =head2 button_up
  1163. Description:
  1164. Releases the mouse button previously held (where the mouse is
  1165. currently at). Must be called once for every buttondown command
  1166. issued. See the note in click and buttondown about implications of
  1167. out-of-order commands.
  1168. Usage:
  1169. $self->button_up;
  1170. =cut
  1171. sub button_up {
  1172. my ($self) = @_;
  1173. my $res = { 'command' => 'buttonUp' };
  1174. return $self->_execute_command($res);
  1175. }
  1176. 1;
  1177. __END__
  1178. =head1 SEE ALSO
  1179. For more information about Selenium , visit the website at
  1180. L<http://code.google.com/p/selenium/>.
  1181. =head1 BUGS
  1182. The Selenium issue tracking system is available online at
  1183. L<http://github.com/aivaturi/Selenium-Remote-Driver/issues>.
  1184. =head1 CURRENT MAINTAINER
  1185. Gordon Child C<< <gchild@gordonchild.com> >>
  1186. =head1 AUTHOR
  1187. Perl Bindings for Selenium Remote Driver by Aditya Ivaturi C<< <ivaturi@gmail.com> >>
  1188. =head1 ACKNOWLEDGEMENTS
  1189. The following people have contributed to this module. (Thanks!)
  1190. =over 4
  1191. =item * Phil Kania
  1192. =item * Phil Mitchell
  1193. =back
  1194. =head1 LICENSE
  1195. Copyright (c) 2010-2011 Aditya Ivaturi, Gordon Child
  1196. Licensed under the Apache License, Version 2.0 (the "License");
  1197. you may not use this file except in compliance with the License.
  1198. You may obtain a copy of the License at
  1199. http://www.apache.org/licenses/LICENSE-2.0
  1200. Unless required by applicable law or agreed to in writing, software
  1201. distributed under the License is distributed on an "AS IS" BASIS,
  1202. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  1203. See the License for the specific language governing permissions and
  1204. limitations under the License.