Driver.pm 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739
  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.13";
  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 get_sessions
  216. Description:
  217. Returns a list of the currently active sessions. Each session will be
  218. returned as an array of Hashes with the following keys:
  219. 'id' : The session ID
  220. 'capabilities: An object describing session's capabilities
  221. Output:
  222. Array of Hashes
  223. Usage:
  224. print Dumper $driver->get_sessions();
  225. =cut
  226. sub get_sessions{
  227. my ($self) = @_;
  228. my $res = { 'command' => 'getSessions' };
  229. return $self->_execute_command($res);
  230. }
  231. =head2 status
  232. Description:
  233. Query the server's current status. All server implementations
  234. should return two basic objects describing the server's current
  235. platform and when the server was built.
  236. Output:
  237. Hash ref
  238. Usage:
  239. print Dumper $driver->status;
  240. =cut
  241. sub status {
  242. my ($self) = @_;
  243. my $res = { 'command' => 'status' };
  244. return $self->_execute_command($res);
  245. }
  246. =head2 get_alert_text
  247. Description:
  248. Gets the text of the currently displayed JavaScript alert(), confirm()
  249. or prompt() dialog.
  250. Example
  251. my $string = $driver->get_alert_text;
  252. =cut
  253. sub get_alert_text {
  254. my ($self) = @_;
  255. my $res = { 'command' => 'getAlertText' };
  256. return $self->_execute_command($res);
  257. }
  258. =head2 send_keys_to_active_element
  259. Description:
  260. Send a sequence of key strokes to the active element. This command is
  261. similar to the send keys command in every aspect except the implicit
  262. termination: The modifiers are not released at the end of the call.
  263. Rather, the state of the modifier keys is kept between calls, so mouse
  264. interactions can be performed while modifier keys are depressed.
  265. Input: 1
  266. Required:
  267. {ARRAY | STRING} - Array of strings or a string.
  268. Usage:
  269. $driver->send_keys_to_active_element('abcd', 'efg');
  270. $driver->send_keys_to_active_element('hijk');
  271. or
  272. # include the WDKeys module
  273. use Selenium::Remote::WDKeys;
  274. .
  275. .
  276. $driver->send_keys_to_active_element(KEYS->{'space'}, KEYS->{'enter'});
  277. =cut
  278. sub send_keys_to_active_element {
  279. my ($self, @strings) = @_;
  280. my $res = { 'command' => 'sendKeysToActiveElement' };
  281. my $params = {
  282. 'value' => \@strings,
  283. };
  284. return $self->_execute_command($res, $params);
  285. }
  286. =head2 send_keys_to_alert
  287. Synonymous with send_keys_to_prompt
  288. =cut
  289. sub send_keys_to_alert {
  290. return shift->send_keys_to_prompt(@_);
  291. }
  292. =head2 send_keys_to_prompt
  293. Description:
  294. Sends keystrokes to a JavaScript prompt() dialog.
  295. Input:
  296. {string} keys to send
  297. Example:
  298. $driver->send_keys_to_prompt('hello world');
  299. or
  300. ok($driver->get_alert_text eq 'Please Input your name','prompt appears');
  301. $driver->send_keys_to_alert("Larry Wall");
  302. $driver->accept_alert;
  303. =cut
  304. sub send_keys_to_prompt {
  305. my ($self,$keys) = @_;
  306. my $res = { 'command' => 'sendKeysToPrompt' };
  307. my $params = { 'text' => $keys };
  308. return $self->_execute_command($res,$params);
  309. }
  310. =head2 accept_alert
  311. Description:
  312. Accepts the currently displayed alert dialog. Usually, this is
  313. equivalent to clicking the 'OK' button in the dialog.
  314. Example:
  315. $driver->accept_alert;
  316. =cut
  317. sub accept_alert {
  318. my ($self) = @_;
  319. my $res = { 'command' => 'acceptAlert' };
  320. return $self->_execute_command($res);
  321. }
  322. =head2 dismiss_alert
  323. Description:
  324. Dismisses the currently displayed alert dialog. For comfirm()
  325. and prompt() dialogs, this is equivalent to clicking the
  326. 'Cancel' button. For alert() dialogs, this is equivalent to
  327. clicking the 'OK' button.
  328. Example:
  329. $driver->dismiss_alert;
  330. =cut
  331. sub dismiss_alert {
  332. my ($self) = @_;
  333. my $res = { 'command' => 'dismissAlert' };
  334. return $self->_execute_command($res);
  335. }
  336. =head2 mouse_move_to_location
  337. Description:
  338. Move the mouse by an offset of the specificed element. If no
  339. element is specified, the move is relative to the current mouse
  340. cursor. If an element is provided but no offset, the mouse will be
  341. moved to the center of the element. If the element is not visible,
  342. it will be scrolled into view.
  343. Output:
  344. STRING -
  345. Usage:
  346. # element - the element to move to. If not specified or is null, the offset is relative to current position of the mouse.
  347. # 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.
  348. # 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.
  349. print $driver->mouse_move_to_location(element => e, xoffset => x, yoffset => y);
  350. =cut
  351. sub mouse_move_to_location {
  352. my ($self, %params) = @_;
  353. $params{element} = $params{element}{id} if exists $params{element};
  354. my $res = { 'command' => 'mouseMoveToLocation' };
  355. return $self->_execute_command($res, \%params);
  356. }
  357. =head2 move_to
  358. Synonymous with mouse_move_to_location
  359. =cut
  360. sub move_to {
  361. return shift->mouse_move_to_location(@_);
  362. }
  363. =head2 get_capabilities
  364. Description:
  365. Retrieve the capabilities of the specified session.
  366. Output:
  367. HASH of all the capabilities.
  368. Usage:
  369. my $capab = $driver->get_capabilities();
  370. print Dumper($capab);
  371. =cut
  372. sub get_capabilities {
  373. my $self = shift;
  374. my $res = {'command' => 'getCapabilities'};
  375. return $self->_execute_command($res);
  376. }
  377. =head2 set_timeout
  378. Description:
  379. Set the amount of time, in milliseconds, that asynchronous scripts executed
  380. by execute_async_script() are permitted to run before they are
  381. aborted and a |Timeout| error is returned to the client.
  382. Input:
  383. ms - <NUMBER> - The amount of time, in milliseconds, that time-limited
  384. commands are permitted to run.
  385. Usage:
  386. $driver->set_async_script_timeout(1000);
  387. =cut
  388. sub set_async_script_timeout {
  389. my ($self, $ms) = @_;
  390. if (not defined $ms)
  391. {
  392. return "Expecting timeout in ms";
  393. }
  394. my $res = {'command' => 'setAsyncScriptTimeout'};
  395. my $params = {'ms' => $ms};
  396. return $self->_execute_command($res, $params);
  397. }
  398. =head2 set_implicit_wait_timeout
  399. Description:
  400. Set the amount of time the driver should wait when searching for elements.
  401. When searching for a single element, the driver will poll the page until
  402. an element is found or the timeout expires, whichever occurs first.
  403. When searching for multiple elements, the driver should poll the page until
  404. at least one element is found or the timeout expires, at which point it
  405. will return an empty list. If this method is never called, the driver will
  406. default to an implicit wait of 0ms.
  407. Input:
  408. Time in milliseconds.
  409. Output:
  410. Server Response Hash with no data returned back from the server.
  411. Usage:
  412. $driver->set_implicit_wait_timeout(10);
  413. =cut
  414. sub set_implicit_wait_timeout {
  415. my ($self, $ms) = @_;
  416. my $res = {'command' => 'setImplicitWaitTimeout'};
  417. my $params = {'ms' => $ms};
  418. return $self->_execute_command($res, $params);
  419. }
  420. =head2 close
  421. Description:
  422. Close the current window.
  423. Usage:
  424. $driver->close();
  425. or
  426. #close a popup window
  427. my $handles = $driver->get_window_handles;
  428. $driver->switch_to_window($handles->[1]);
  429. $driver->close();
  430. $driver->switch_to_window($handles->[0]);
  431. =cut
  432. sub close {
  433. my $self = shift;
  434. my $res = { 'command' => 'close' };
  435. $self->_execute_command($res);
  436. }
  437. =head2 quit
  438. Description:
  439. Delete the session & close open browsers.
  440. Usage:
  441. $driver->quit();
  442. =cut
  443. sub quit {
  444. my $self = shift;
  445. my $res = { 'command' => 'quit' };
  446. $self->_execute_command($res);
  447. $self->{session_id} = undef;
  448. }
  449. =head2 get_current_window_handle
  450. Description:
  451. Retrieve the current window handle.
  452. Output:
  453. STRING - the window handle
  454. Usage:
  455. print $driver->get_current_window_handle();
  456. =cut
  457. sub get_current_window_handle {
  458. my $self = shift;
  459. my $res = { 'command' => 'getCurrentWindowHandle' };
  460. return $self->_execute_command($res);
  461. }
  462. =head2 get_window_handles
  463. Description:
  464. Retrieve the list of window handles used in the session.
  465. Output:
  466. ARRAY of STRING - list of the window handles
  467. Usage:
  468. print Dumper $driver->get_window_handles;
  469. or
  470. # get popup, close, then back
  471. my $handles = $driver->get_window_handles;
  472. $driver->switch_to_window($handles->[1]);
  473. $driver->close;
  474. $driver->switch_to_window($handles->[0]);
  475. =cut
  476. sub get_window_handles {
  477. my $self = shift;
  478. my $res = { 'command' => 'getWindowHandles' };
  479. return $self->_execute_command($res);
  480. }
  481. =head2 get_window_size
  482. Description:
  483. Retrieve the window size
  484. Input:
  485. STRING - <optional> - window handle (default is 'current' window)
  486. Output:
  487. HASH - containing keys 'height' & 'width'
  488. Usage:
  489. my $window_size = $driver->get_window_size();
  490. print $window_size->{'height'}, $window_size->('width');
  491. =cut
  492. sub get_window_size {
  493. my ( $self, $window ) = @_;
  494. $window = (defined $window)?$window:'current';
  495. my $res = { 'command' => 'getWindowSize', 'window_handle' => $window };
  496. return $self->_execute_command($res);
  497. }
  498. =head2 get_window_position
  499. Description:
  500. Retrieve the window position
  501. Input:
  502. STRING - <optional> - window handle (default is 'current' window)
  503. Output:
  504. HASH - containing keys 'x' & 'y'
  505. Usage:
  506. my $window_size = $driver->get_window_position();
  507. print $window_size->{'x'}, $window_size->('y');
  508. =cut
  509. sub get_window_position {
  510. my ( $self, $window ) = @_;
  511. $window = (defined $window)?$window:'current';
  512. my $res = { 'command' => 'getWindowPosition', 'window_handle' => $window };
  513. return $self->_execute_command($res);
  514. }
  515. =head2 get_current_url
  516. Description:
  517. Retrieve the url of the current page
  518. Output:
  519. STRING - url
  520. Usage:
  521. print $driver->get_current_url();
  522. =cut
  523. sub get_current_url {
  524. my $self = shift;
  525. my $res = { 'command' => 'getCurrentUrl' };
  526. return $self->_execute_command($res);
  527. }
  528. =head2 navigate
  529. Description:
  530. Navigate to a given url. This is same as get() method.
  531. Input:
  532. STRING - url
  533. Usage:
  534. $driver->navigate('http://www.google.com');
  535. =cut
  536. sub navigate {
  537. my ( $self, $url ) = @_;
  538. $self->get($url);
  539. }
  540. =head2 get
  541. Description:
  542. Navigate to a given url
  543. Input:
  544. STRING - url
  545. Usage:
  546. $driver->get('http://www.google.com');
  547. =cut
  548. sub get {
  549. my ( $self, $url ) = @_;
  550. my $res = { 'command' => 'get' };
  551. my $params = { 'url' => $url };
  552. return $self->_execute_command( $res, $params );
  553. }
  554. =head2 get_title
  555. Description:
  556. Get the current page title
  557. Output:
  558. STRING - Page title
  559. Usage:
  560. print $driver->get_title();
  561. =cut
  562. sub get_title {
  563. my $self = shift;
  564. my $res = { 'command' => 'getTitle' };
  565. return $self->_execute_command($res);
  566. }
  567. =head2 go_back
  568. Description:
  569. Equivalent to hitting the back button on the browser.
  570. Usage:
  571. $driver->go_back();
  572. =cut
  573. sub go_back {
  574. my $self = shift;
  575. my $res = { 'command' => 'goBack' };
  576. return $self->_execute_command($res);
  577. }
  578. =head2 go_forward
  579. Description:
  580. Equivalent to hitting the forward button on the browser.
  581. Usage:
  582. $driver->go_forward();
  583. =cut
  584. sub go_forward {
  585. my $self = shift;
  586. my $res = { 'command' => 'goForward' };
  587. return $self->_execute_command($res);
  588. }
  589. =head2 refresh
  590. Description:
  591. Reload the current page.
  592. Usage:
  593. $driver->refresh();
  594. =cut
  595. sub refresh {
  596. my $self = shift;
  597. my $res = { 'command' => 'refresh' };
  598. return $self->_execute_command($res);
  599. }
  600. =head2 javascript
  601. Description:
  602. returns true if javascript is enabled in the driver.
  603. Usage:
  604. if ($driver->javascript) { ...; }
  605. =cut
  606. sub javascript {
  607. my $self = shift;
  608. return $self->{javascript} == JSON::true;
  609. }
  610. =head2 execute_async_script
  611. Description:
  612. Inject a snippet of JavaScript into the page for execution in the context
  613. of the currently selected frame. The executed script is assumed to be
  614. asynchronous and must signal that is done by invoking the provided
  615. callback, which is always provided as the final argument to the function.
  616. The value to this callback will be returned to the client.
  617. Asynchronous script commands may not span page loads. If an unload event
  618. is fired while waiting for a script result, an error should be returned
  619. to the client.
  620. Input: 2 (1 optional)
  621. Required:
  622. STRING - Javascript to execute on the page
  623. Optional:
  624. ARRAY - list of arguments that need to be passed to the script.
  625. Output:
  626. {*} - Varied, depending on the type of result expected back from the script.
  627. Usage:
  628. my $script = q{
  629. var arg1 = arguments[0];
  630. var callback = arguments[arguments.length-1];
  631. var elem = window.document.findElementById(arg1);
  632. callback(elem);
  633. };
  634. my $callback = q{return arguments[0];};
  635. my $elem = $driver->execute_async_script($script,'myid',$callback);
  636. $elem->click;
  637. =cut
  638. sub execute_async_script {
  639. my ( $self, $script, @args ) = @_;
  640. if ($self->javascript) {
  641. if ( not defined $script ) {
  642. return 'No script provided';
  643. }
  644. my $res = { 'command' => 'executeAsyncScript' };
  645. # Check the args array if the elem obj is provided & replace it with
  646. # JSON representation
  647. for (my $i=0; $i<@args; $i++) {
  648. if (ref $args[$i] eq 'Selenium::Remote::WebElement') {
  649. $args[$i] = {'ELEMENT' => ($args[$i])->{id}};
  650. }
  651. }
  652. my $params = {'script' => $script, 'args' => \@args};
  653. my $ret = $self->_execute_command($res, $params);
  654. # replace any ELEMENTS with WebElement
  655. if (ref($ret) and (ref($ret) eq 'HASH') and exists $ret->{'ELEMENT'}) {
  656. $ret =
  657. new Selenium::Remote::WebElement(
  658. $ret->{ELEMENT}, $self);
  659. }
  660. return $ret;
  661. }
  662. else {
  663. croak 'Javascript is not enabled on remote driver instance.';
  664. }
  665. }
  666. =head2 execute_script
  667. Description:
  668. Inject a snippet of JavaScript into the page and return its result.
  669. WebElements that should be passed to the script as an argument should be
  670. specified in the arguments array as WebElement object. Likewise,
  671. any WebElements in the script result will be returned as WebElement object.
  672. Input: 2 (1 optional)
  673. Required:
  674. STRING - Javascript to execute on the page
  675. Optional:
  676. ARRAY - list of arguments that need to be passed to the script.
  677. Output:
  678. {*} - Varied, depending on the type of result expected back from the script.
  679. Usage:
  680. my $script = q{
  681. var arg1 = arguments[0];
  682. var elem = window.document.findElementById(arg1);
  683. return elem;
  684. };
  685. my $elem = $driver->execute_script($script,'myid');
  686. $elem->click;
  687. =cut
  688. sub execute_script {
  689. my ( $self, $script, @args ) = @_;
  690. if ($self->javascript) {
  691. if ( not defined $script ) {
  692. return 'No script provided';
  693. }
  694. my $res = { 'command' => 'executeScript' };
  695. # Check the args array if the elem obj is provided & replace it with
  696. # JSON representation
  697. for (my $i=0; $i<@args; $i++) {
  698. if (ref $args[$i] eq 'Selenium::Remote::WebElement') {
  699. $args[$i] = {'ELEMENT' => ($args[$i])->{id}};
  700. }
  701. }
  702. my $params = {'script' => $script, 'args' => [@args]};
  703. my $ret = $self->_execute_command($res, $params);
  704. # replace any ELEMENTS with WebElement
  705. if (ref($ret) and (ref($ret) eq 'HASH') and exists $ret->{'ELEMENT'}) {
  706. $ret =
  707. new Selenium::Remote::WebElement(
  708. $ret->{ELEMENT}, $self);
  709. }
  710. return $ret;
  711. }
  712. else {
  713. croak 'Javascript is not enabled on remote driver instance.';
  714. }
  715. }
  716. =head2 screenshot
  717. Description:
  718. Get a screenshot of the current page as a base64 encoded image.
  719. Output:
  720. STRING - base64 encoded image
  721. Usage:
  722. print $driver->screenshot();
  723. or
  724. require MIME::Base64;
  725. open(FH,'>','screenshot.png');
  726. binmode FH;
  727. my $png_base64 = $driver->screenshot();
  728. print FH MIME::Base64::decode_base64($png_base64);
  729. close FH;
  730. =cut
  731. sub screenshot {
  732. my ($self) = @_;
  733. my $res = { 'command' => 'screenshot' };
  734. return $self->_execute_command($res);
  735. }
  736. =head2 available_engines
  737. Description:
  738. List all available engines on the machine. To use an engine, it has to be present in this list.
  739. Output:
  740. {Array.<string>} A list of available engines
  741. Usage:
  742. print Dumper $driver->available_engines;
  743. =cut
  744. sub available_engines {
  745. my ($self) = @_;
  746. my $res = { 'command' => 'availableEngines' };
  747. return $self->_execute_command($res);
  748. }
  749. =head2 switch_to_frame
  750. Description:
  751. Change focus to another frame on the page. If the frame ID is null, the
  752. server will switch to the page's default content.
  753. Input: 1
  754. Required:
  755. {STRING | NUMBER | NULL} - ID of the frame which can be one of the three
  756. mentioned.
  757. Usage:
  758. $driver->switch_to_frame('frame_1');
  759. =cut
  760. sub switch_to_frame {
  761. my ( $self, $id ) = @_;
  762. my $json_null = JSON::null;
  763. $id = ( defined $id ) ? $id : $json_null;
  764. my $res = { 'command' => 'switchToFrame' };
  765. my $params = { 'id' => $id };
  766. return $self->_execute_command( $res, $params );
  767. }
  768. =head2 switch_to_window
  769. Description:
  770. Change focus to another window. The window to change focus to may be
  771. specified by its server assigned window handle, or by the value of its name
  772. attribute.
  773. Input: 1
  774. Required:
  775. STRING - Window handle or the Window name
  776. Usage:
  777. $driver->switch_to_window('MY Homepage');
  778. or
  779. # close a popup window and switch back
  780. my $handles = $driver->get_window_handles;
  781. $driver->switch_to_window($handles->[1]);
  782. $driver->close;
  783. $driver->switch_to_window($handles->[0]);
  784. =cut
  785. sub switch_to_window {
  786. my ( $self, $name ) = @_;
  787. if ( not defined $name ) {
  788. return 'Window name not provided';
  789. }
  790. my $res = { 'command' => 'switchToWindow' };
  791. my $params = { 'name' => $name };
  792. return $self->_execute_command( $res, $params );
  793. }
  794. =head2 get_speed
  795. Description:
  796. Get the current user input speed. The actual input speed is still browser
  797. specific and not covered by the Driver.
  798. Output:
  799. STRING - One of these: SLOW, MEDIUM, FAST
  800. Usage:
  801. print $driver->get_speed();
  802. =cut
  803. sub get_speed {
  804. my ($self) = @_;
  805. my $res = { 'command' => 'getSpeed' };
  806. return $self->_execute_command($res);
  807. }
  808. =head2 set_speed
  809. Description:
  810. Set the user input speed.
  811. Input:
  812. STRING - One of these: SLOW, MEDIUM, FAST
  813. Usage:
  814. $driver->set_speed('MEDIUM');
  815. Note: This function is a no-op in WebDriver (?). See
  816. https://groups.google.com/d/topic/selenium-users/oX0ZnYFPuSA/discussion and
  817. http://code.google.com/p/selenium/source/browse/trunk/java/client/src/org/openqa/selenium/WebDriverCommandProcessor.java
  818. =cut
  819. sub set_speed {
  820. my ( $self, $speed ) = @_;
  821. if ( not defined $speed ) {
  822. return 'Speed not provided.';
  823. }
  824. my $res = { 'command' => 'setSpeed' };
  825. my $params = { 'speed' => $speed };
  826. return $self->_execute_command( $res, $params );
  827. }
  828. =head2 set_window_position
  829. Description:
  830. Set the position (on screen) where you want your browser to be displayed.
  831. Input:
  832. INT - x co-ordinate
  833. INT - y co-ordinate
  834. STRING - <optional> - window handle (default is 'current' window)
  835. Output:
  836. BOOLEAN - Success or failure
  837. Usage:
  838. $driver->set_window_position(50, 50);
  839. =cut
  840. sub set_window_position {
  841. my ( $self, $x, $y, $window ) = @_;
  842. $window = (defined $window)?$window:'current';
  843. if (not defined $x and not defined $y){
  844. return "X & Y co-ordinates are required";
  845. }
  846. my $res = { 'command' => 'setWindowPosition', 'window_handle' => $window };
  847. my $params = { 'x' => $x, 'y' => $y };
  848. my $ret = $self->_execute_command($res, $params);
  849. if ($ret =~ m/204/g) {
  850. return 1;
  851. }
  852. else { return 0; }
  853. }
  854. =head2 set_window_size
  855. Description:
  856. Set the size of the browser window
  857. Input:
  858. INT - height of the window
  859. INT - width of the window
  860. STRING - <optional> - window handle (default is 'current' window)
  861. Output:
  862. BOOLEAN - Success or failure
  863. Usage:
  864. $driver->set_window_size(640, 480);
  865. =cut
  866. sub set_window_size {
  867. my ( $self, $height, $width, $window ) = @_;
  868. $window = (defined $window)?$window:'current';
  869. if (not defined $height and not defined $width){
  870. return "height & width of browser are required";
  871. }
  872. my $res = { 'command' => 'setWindowSize', 'window_handle' => $window };
  873. my $params = { 'height' => $height, 'width' => $width };
  874. my $ret = $self->_execute_command($res, $params);
  875. if ($ret =~ m/204/g) {
  876. return 1;
  877. }
  878. else { return 0; }
  879. }
  880. =head2 get_all_cookies
  881. Description:
  882. Retrieve all cookies visible to the current page. Each cookie will be
  883. returned as a HASH reference with the following keys & their value types:
  884. 'name' - STRING
  885. 'value' - STRING
  886. 'path' - STRING
  887. 'domain' - STRING
  888. 'secure' - BOOLEAN
  889. Output:
  890. ARRAY of HASHES - list of all the cookie hashes
  891. Usage:
  892. print Dumper($driver->get_all_cookies());
  893. =cut
  894. sub get_all_cookies {
  895. my ($self) = @_;
  896. my $res = { 'command' => 'getAllCookies' };
  897. return $self->_execute_command($res);
  898. }
  899. =head2 add_cookie
  900. Description:
  901. Set a cookie on the domain.
  902. Input: 5 (1 optional)
  903. Required:
  904. 'name' - STRING
  905. 'value' - STRING
  906. 'path' - STRING
  907. 'domain' - STRING
  908. Optional:
  909. 'secure' - BOOLEAN - default is false.
  910. Usage:
  911. $driver->add_cookie('foo', 'bar', '/', '.google.com', 0)
  912. =cut
  913. sub add_cookie {
  914. my ( $self, $name, $value, $path, $domain, $secure ) = @_;
  915. if ( ( not defined $name )
  916. || ( not defined $value )
  917. || ( not defined $path )
  918. || ( not defined $domain ) )
  919. {
  920. return "Missing parameters";
  921. }
  922. my $res = { 'command' => 'addCookie' };
  923. my $json_false = JSON::false;
  924. my $json_true = JSON::true;
  925. $secure = ( defined $secure ) ? $json_true : $json_false;
  926. my $params = {
  927. 'cookie' => {
  928. 'name' => $name,
  929. 'value' => $value,
  930. 'path' => $path,
  931. 'domain' => $domain,
  932. 'secure' => $secure,
  933. }
  934. };
  935. return $self->_execute_command( $res, $params );
  936. }
  937. =head2 delete_all_cookies
  938. Description:
  939. Delete all cookies visible to the current page.
  940. Usage:
  941. $driver->delete_all_cookies();
  942. =cut
  943. sub delete_all_cookies {
  944. my ($self) = @_;
  945. my $res = { 'command' => 'deleteAllCookies' };
  946. return $self->_execute_command($res);
  947. }
  948. =head2 delete_cookie_named
  949. Description:
  950. Delete the cookie with the given name. This command will be a no-op if there
  951. is no such cookie visible to the current page.
  952. Input: 1
  953. Required:
  954. STRING - name of cookie to delete
  955. Usage:
  956. $driver->delete_cookie_named('foo');
  957. =cut
  958. sub delete_cookie_named {
  959. my ( $self, $cookie_name ) = @_;
  960. if ( not defined $cookie_name ) {
  961. return "Cookie name not provided";
  962. }
  963. my $res = { 'command' => 'deleteCookieNamed', 'name' => $cookie_name };
  964. return $self->_execute_command($res);
  965. }
  966. =head2 get_page_source
  967. Description:
  968. Get the current page source.
  969. Output:
  970. STRING - The page source.
  971. Usage:
  972. print $driver->get_page_source();
  973. =cut
  974. sub get_page_source {
  975. my ($self) = @_;
  976. my $res = { 'command' => 'getPageSource' };
  977. return $self->_execute_command($res);
  978. }
  979. =head2 find_element
  980. Description:
  981. Search for an element on the page, starting from the document root. The
  982. located element will be returned as a WebElement object.
  983. Input: 2 (1 optional)
  984. Required:
  985. STRING - The search target.
  986. Optional:
  987. STRING - Locator scheme to use to search the element, available schemes:
  988. {class, class_name, css, id, link, link_text, partial_link_text,
  989. tag_name, name, xpath}
  990. Defaults to 'xpath'.
  991. Output:
  992. Selenium::Remote::WebElement - WebElement Object
  993. Usage:
  994. $driver->find_element("//input[\@name='q']");
  995. =cut
  996. sub find_element {
  997. my ( $self, $query, $method ) = @_;
  998. if ( not defined $query ) {
  999. return 'Search string to find element not provided.';
  1000. }
  1001. my $using = ( defined $method ) ? FINDERS->{$method} : 'xpath';
  1002. if (defined $using) {
  1003. my $res = { 'command' => 'findElement' };
  1004. my $params = { 'using' => $using, 'value' => $query };
  1005. my $ret_data = eval { $self->_execute_command( $res, $params ); };
  1006. if($@) {
  1007. if($@ =~ /(An element could not be located on the page using the given search parameters)/) {
  1008. # give details on what element wasn't found
  1009. $@ = "$1: $query,$using";
  1010. local @CARP_NOT = ("Selenium::Remote::Driver",@CARP_NOT);
  1011. croak $@;
  1012. } else {
  1013. # re throw if the exception wasn't what we expected
  1014. die $@;
  1015. }
  1016. }
  1017. return new Selenium::Remote::WebElement($ret_data->{ELEMENT}, $self);
  1018. }
  1019. else {
  1020. croak "Bad method, expected - class, class_name, css, id, link,
  1021. link_text, partial_link_text, name, tag_name, xpath";
  1022. }
  1023. }
  1024. =head2 find_elements
  1025. Description:
  1026. Search for multiple elements on the page, starting from the document root.
  1027. The located elements will be returned as an array of WebElement object.
  1028. Input: 2 (1 optional)
  1029. Required:
  1030. STRING - The search target.
  1031. Optional:
  1032. STRING - Locator scheme to use to search the element, available schemes:
  1033. {class, class_name, css, id, link, link_text, partial_link_text,
  1034. tag_name, name, xpath}
  1035. Defaults to 'xpath'.
  1036. Output:
  1037. ARRAY of Selenium::Remote::WebElement - Array of WebElement Objects
  1038. Usage:
  1039. $driver->find_elements("//input");
  1040. =cut
  1041. sub find_elements {
  1042. my ( $self, $query, $method ) = @_;
  1043. if ( not defined $query ) {
  1044. return 'Search string to find element not provided.';
  1045. }
  1046. my $using = ( defined $method ) ? FINDERS->{$method} : 'xpath';
  1047. if (defined $using) {
  1048. my $res = { 'command' => 'findElements' };
  1049. my $params = { 'using' => $using, 'value' => $query };
  1050. my $ret_data = eval {$self->_execute_command( $res, $params );};
  1051. if($@) {
  1052. if($@ =~ /(An element could not be located on the page using the given search parameters)/) {
  1053. # give details on what element wasn't found
  1054. $@ = "$1: $query,$using";
  1055. local @CARP_NOT = ("Selenium::Remote::Driver",@CARP_NOT);
  1056. croak $@;
  1057. } else {
  1058. # re throw if the exception wasn't what we expected
  1059. die $@;
  1060. }
  1061. }
  1062. my $elem_obj_arr;
  1063. my $i = 0;
  1064. foreach (@$ret_data) {
  1065. $elem_obj_arr->[$i] = new Selenium::Remote::WebElement($_->{ELEMENT}, $self);
  1066. $i++;
  1067. }
  1068. return wantarray?@{$elem_obj_arr}:$elem_obj_arr;
  1069. }
  1070. else {
  1071. croak "Bad method, expected - class, class_name, css, id, link,
  1072. link_text, partial_link_text, name, tag_name, xpath";
  1073. }
  1074. }
  1075. =head2 find_child_element
  1076. Description:
  1077. Search for an element on the page, starting from the identified element. The
  1078. located element will be returned as a WebElement object.
  1079. Input: 3 (1 optional)
  1080. Required:
  1081. Selenium::Remote::WebElement - WebElement object from where you want to
  1082. start searching.
  1083. STRING - The search target. (Do not use a double whack('//')
  1084. in an xpath to search for a child element
  1085. ex: '//option[@id="something"]'
  1086. instead use a dot whack ('./')
  1087. ex: './option[@id="something"]')
  1088. Optional:
  1089. STRING - Locator scheme to use to search the element, available schemes:
  1090. {class, class_name, css, id, link, link_text, partial_link_text,
  1091. tag_name, name, xpath}
  1092. Defaults to 'xpath'.
  1093. Output:
  1094. Selenium::Remote::WebElement - WebElement Object
  1095. Usage:
  1096. my $elem1 = $driver->find_element("//select[\@name='ned']");
  1097. # note the usage of ./ when searching for a child element instead of //
  1098. my $child = $driver->find_child_element($elem1, "./option[\@value='es_ar']");
  1099. =cut
  1100. sub find_child_element {
  1101. my ( $self, $elem, $query, $method ) = @_;
  1102. if ( ( not defined $elem ) || ( not defined $query ) ) {
  1103. return "Missing parameters";
  1104. }
  1105. my $using = ( defined $method ) ? $method : 'xpath';
  1106. if (exists FINDERS->{$using}) {
  1107. my $res = { 'command' => 'findChildElement', 'id' => $elem->{id} };
  1108. my $params = { 'using' => FINDERS->{$using}, 'value' => $query };
  1109. my $ret_data = eval {$self->_execute_command( $res, $params );};
  1110. if($@) {
  1111. if($@ =~ /(An element could not be located on the page using the given search parameters)/) {
  1112. # give details on what element wasn't found
  1113. $@ = "$1: $query,$using";
  1114. local @CARP_NOT = ("Selenium::Remote::Driver",@CARP_NOT);
  1115. croak $@;
  1116. } else {
  1117. # re throw if the exception wasn't what we expected
  1118. die $@;
  1119. }
  1120. }
  1121. return new Selenium::Remote::WebElement($ret_data->{ELEMENT}, $self);
  1122. }
  1123. else {
  1124. croak "Bad method, expected - class, class_name, css, id, link,
  1125. link_text, partial_link_text, name, tag_name, xpath";
  1126. }
  1127. }
  1128. =head2 find_child_elements
  1129. Description:
  1130. Search for multiple element on the page, starting from the identified
  1131. element. The located elements will be returned as an array of WebElement
  1132. objects.
  1133. Input: 3 (1 optional)
  1134. Required:
  1135. Selenium::Remote::WebElement - WebElement object from where you want to
  1136. start searching.
  1137. STRING - The search target.
  1138. Optional:
  1139. STRING - Locator scheme to use to search the element, available schemes:
  1140. {class, class_name, css, id, link, link_text, partial_link_text,
  1141. tag_name, name, xpath}
  1142. Defaults to 'xpath'.
  1143. Output:
  1144. ARRAY of Selenium::Remote::WebElement - Array of WebElement Objects.
  1145. Usage:
  1146. my $elem1 = $driver->find_element("//select[\@name='ned']");
  1147. my $child = $driver->find_child_elements($elem1, "//option");
  1148. =cut
  1149. sub find_child_elements {
  1150. my ( $self, $elem, $query, $method ) = @_;
  1151. if ( ( not defined $elem ) || ( not defined $query ) ) {
  1152. return "Missing parameters";
  1153. }
  1154. my $using = ( defined $method ) ? $method : 'xpath';
  1155. if (exists FINDERS->{$using}) {
  1156. my $res = { 'command' => 'findChildElements', 'id' => $elem->{id} };
  1157. my $params = { 'using' => FINDERS->{$using}, 'value' => $query };
  1158. my $ret_data = eval {$self->_execute_command( $res, $params );};
  1159. if($@) {
  1160. if($@ =~ /(An element could not be located on the page using the given search parameters)/) {
  1161. # give details on what element wasn't found
  1162. $@ = "$1: $query,$using";
  1163. local @CARP_NOT = ("Selenium::Remote::Driver",@CARP_NOT);
  1164. croak $@;
  1165. } else {
  1166. # re throw if the exception wasn't what we expected
  1167. die $@;
  1168. }
  1169. }
  1170. my $elem_obj_arr;
  1171. my $i = 0;
  1172. foreach (@$ret_data) {
  1173. $elem_obj_arr->[$i] = new Selenium::Remote::WebElement($_->{ELEMENT}, $self);
  1174. $i++;
  1175. }
  1176. return wantarray?@{$elem_obj_arr}:$elem_obj_arr;
  1177. }
  1178. else {
  1179. croak "Bad method, expected - class, class_name, css, id, link,
  1180. link_text, partial_link_text, name, tag_name, xpath";
  1181. }
  1182. }
  1183. =head2 get_active_element
  1184. Description:
  1185. Get the element on the page that currently has focus.. The located element
  1186. will be returned as a WebElement object.
  1187. Output:
  1188. Selenium::Remote::WebElement - WebElement Object
  1189. Usage:
  1190. $driver->get_active_element();
  1191. =cut
  1192. sub get_active_element {
  1193. my ($self) = @_;
  1194. my $res = { 'command' => 'getActiveElement' };
  1195. my $ret_data = eval { $self->_execute_command($res) };
  1196. if ($@) {
  1197. croak $@;
  1198. } else {
  1199. return new Selenium::Remote::WebElement($ret_data->{ELEMENT}, $self);
  1200. }
  1201. }
  1202. =head2 send_modifier
  1203. Description:
  1204. Send an event to the active element to depress or release a modifier key.
  1205. Input: 2
  1206. Required:
  1207. 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
  1208. isdown - Boolean/String - Whether to generate a key down or key up
  1209. Usage:
  1210. $driver->send_modifier('Alt','down');
  1211. $elem->send_keys('c');
  1212. $driver->send_modifier('Alt','up');
  1213. or
  1214. $driver->send_modifier('Alt',1);
  1215. $elem->send_keys('c');
  1216. $driver->send_modifier('Alt',0);
  1217. =cut
  1218. sub send_modifier {
  1219. my ($self,$modifier,$isdown) = @_;
  1220. if($isdown =~ /(down|up)/) {
  1221. $isdown = $isdown =~ /down/ ? 1:0;
  1222. }
  1223. my $res = {'command' => 'sendModifier'};
  1224. my $params = {value => $modifier,
  1225. isdown => $isdown};
  1226. return $self->_execute_command($res,$params);
  1227. }
  1228. =head2 compare_elements
  1229. Description:
  1230. Test if two element IDs refer to the same DOM element.
  1231. Input: 2
  1232. Required:
  1233. Selenium::Remote::WebElement - WebElement Object
  1234. Selenium::Remote::WebElement - WebElement Object
  1235. Output:
  1236. BOOLEAN
  1237. Usage:
  1238. $driver->compare_elements($elem_obj1, $elem_obj2);
  1239. =cut
  1240. sub compare_elements {
  1241. my ($self, $elem1, $elem2) = @_;
  1242. my $res = { 'command' => 'elementEquals',
  1243. 'id' => $elem1->{id},
  1244. 'other' => $elem2->{id}
  1245. };
  1246. return $self->_execute_command($res);
  1247. }
  1248. =head2 click
  1249. Description:
  1250. Click any mouse button (at the coordinates set by the last moveto command).
  1251. Input:
  1252. button - any one of 'LEFT'/0 'MIDDLE'/1 'RIGHT'/2
  1253. defaults to 'LEFT'
  1254. Usage:
  1255. $driver->click('LEFT');
  1256. $driver->click(1); #MIDDLE
  1257. $driver->click('RIGHT');
  1258. $driver->click; #Defaults to left
  1259. =cut
  1260. sub click {
  1261. my ($self,$button) = @_;
  1262. my $button_enum = {LEFT=>0,MIDDLE=>1,RIGHT=>2};
  1263. if(defined $button && $button =~ /(LEFT|MIDDLE|RIGHT)/i) {
  1264. $button = $button_enum->{uc $1};
  1265. } elsif(defined $button && $button =~ /(0|1|2)/) {
  1266. $button = $1;
  1267. } else {
  1268. $button = 0;
  1269. }
  1270. my $res = { 'command' => 'click' };
  1271. my $params = { 'button' => $button };
  1272. return $self->_execute_command($res,$button);
  1273. }
  1274. =head2 double_click
  1275. Description:
  1276. Double-clicks at the current mouse coordinates (set by moveto).
  1277. Usage:
  1278. $driver->double_click;
  1279. =cut
  1280. sub double_click {
  1281. my ($self) = @_;
  1282. my $res = { 'command' => 'doubleClick' };
  1283. return $self->_execute_command($res);
  1284. }
  1285. =head2 button_down
  1286. Description:
  1287. Click and hold the left mouse button (at the coordinates set by the
  1288. last moveto command). Note that the next mouse-related command that
  1289. should follow is buttondown . Any other mouse command (such as click
  1290. or another call to buttondown) will yield undefined behaviour.
  1291. Usage:
  1292. $self->button_down;
  1293. =cut
  1294. sub button_down {
  1295. my ($self) = @_;
  1296. my $res = { 'command' => 'buttonDown' };
  1297. return $self->_execute_command($res);
  1298. }
  1299. =head2 button_up
  1300. Description:
  1301. Releases the mouse button previously held (where the mouse is
  1302. currently at). Must be called once for every buttondown command
  1303. issued. See the note in click and buttondown about implications of
  1304. out-of-order commands.
  1305. Usage:
  1306. $self->button_up;
  1307. =cut
  1308. sub button_up {
  1309. my ($self) = @_;
  1310. my $res = { 'command' => 'buttonUp' };
  1311. return $self->_execute_command($res);
  1312. }
  1313. 1;
  1314. __END__
  1315. =head1 SEE ALSO
  1316. For more information about Selenium , visit the website at
  1317. L<http://code.google.com/p/selenium/>.
  1318. =head1 BUGS
  1319. The Selenium issue tracking system is available online at
  1320. L<http://github.com/aivaturi/Selenium-Remote-Driver/issues>.
  1321. =head1 AUTHOR
  1322. Perl Bindings for Selenium Remote Driver by Aditya Ivaturi C<< <ivaturi@gmail.com> >>
  1323. =head1 ACKNOWLEDGEMENTS
  1324. The following people have contributed to this module. (Thanks!)
  1325. =over 4
  1326. =item * Gordon Child
  1327. =item * Phil Kania
  1328. =item * Phil Mitchell
  1329. =item * Allen Lew
  1330. =item * Tom Hukins
  1331. =back
  1332. =head1 LICENSE
  1333. Copyright (c) 2010-2011 Aditya Ivaturi, Gordon Child
  1334. Licensed under the Apache License, Version 2.0 (the "License");
  1335. you may not use this file except in compliance with the License.
  1336. You may obtain a copy of the License at
  1337. http://www.apache.org/licenses/LICENSE-2.0
  1338. Unless required by applicable law or agreed to in writing, software
  1339. distributed under the License is distributed on an "AS IS" BASIS,
  1340. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  1341. See the License for the specific language governing permissions and
  1342. limitations under the License.