Driver.pm 48 KB

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