1
0

Driver.pm 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106
  1. package Selenium::Remote::Driver;
  2. use strict;
  3. use warnings;
  4. use Data::Dumper;
  5. use Carp qw(croak);
  6. use Selenium::Remote::RemoteConnection;
  7. use Selenium::Remote::Commands;
  8. use Selenium::Remote::WebElement;
  9. use constant FINDERS => {
  10. class => 'ClassName',
  11. class_name => 'ClassName',
  12. css => 'CssSelector',
  13. id => 'Id',
  14. link => 'LinkText',
  15. link_text => 'LinkText',
  16. name => 'Name',
  17. partial_link_text => 'PartialLinkText',
  18. tag_name => 'TagName',
  19. xpath => 'Xpath',
  20. };
  21. our $VERSION = "0.10";
  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 Server Response Hash
  46. Every method in this module returns either a string or a hash reference. The
  47. string usually means that it has received improper or not received any input.
  48. And that string explains what was wrong. This also means that driver has never
  49. sent any commands to the remote server.
  50. If the methods return a hash reference, that means the driver has sent a command
  51. to the remote server & it has received a response. The driver processes that
  52. response & creates a hash with specific keys & returns it back to the end user.
  53. We shall refer to this hash reference as B<Server Response Hash>. The keys are:
  54. =over
  55. =item cmd_status
  56. The value will either be 'OK' or 'NOTOK'. OK means that server successfully
  57. executed the command & NOTOK means that there was an error encountered on the
  58. server while executing the command. Check cmd_error for further details.
  59. =item cmd_return
  60. This hash key will contain the value returned from the server. It could be of
  61. any data type & each method's POD will list that information.
  62. =item cmd_error
  63. If there was an error on the server while executing the command, this key will
  64. be defined. The value will in turn contain information in a hash with 'stackTrace'
  65. as a key which lists the stack trace of the error encountered on the server &
  66. 'error' which has human readable text of the actual error. It can also contain
  67. a 'screenshot' - a base64 encoded image if the server returns one.
  68. =item session_id
  69. Every successfull command will contain the session id of the current session
  70. against which the command was executed.
  71. =back
  72. So a rule of thumb while invoking methods on the driver is to check whether the
  73. return type is a hash or not. If it is a hash then check for the value of
  74. cmd_status & take necessary actions accordingly. All the method PODs will list
  75. what the cmd_return will contain as that is what an end user is mostly interested
  76. in. Also, for some of the commands, server does not return any thing back. And
  77. Server Response Hash will contain a generic string stating that server didn't
  78. return any thing back. But if cmd_status value is 'OK', then you can safely
  79. assume that command executed successfully on the server.
  80. =head2 WebElement
  81. Selenium Webdriver represents all the HTML elements as WebElement, which is
  82. in turn represented by Selenium::Remote::WebElement module. So any method that
  83. deals with WebElements will return and/or expect WebElement object. The POD for
  84. that module describes all the methods that perform various actions on the
  85. WebElements like click, submit etc.
  86. To interact with any WebElement you have to first "find" it, read the POD for
  87. find_element or find_elements for further info. Once you find the required element
  88. then you can perform various actions. If you don't call find_* method first, all
  89. your further actions will fail for that element. Finally, just remember that you
  90. don't have to instantiate WebElement objects at all - they will be automatically
  91. created when you use the find_* methods.
  92. =cut
  93. =head1 FUNCTIONS
  94. =cut
  95. =head2 new
  96. Description:
  97. Constructor for Driver. It'll instantiate the object if it can communicate
  98. with the Selenium RC server.
  99. Input: 7 (all optional)
  100. desired_capabilities - HASH - Following options are accepted:
  101. Optional:
  102. 'remote_server_addr' - <string> - IP or FQDN of the RC server machine
  103. 'browser_name' - <string> - desired browser string:
  104. {iphone|firefox|internet explorer|htmlunit|iphone|chrome}
  105. 'version' - <string> - desired browser version number
  106. 'platform' - <string> - desired platform:
  107. {WINDOWS|XP|VISTA|MAC|LINUX|UNIX|ANY}
  108. 'javascript' - <boolean> - whether javascript should be supported
  109. 'auto_close' - <boolean> - whether driver should end session on remote
  110. server on close.
  111. If no values are provided, then these defaults will be assumed:
  112. 'remote_server_addr' => 'localhost'
  113. 'port' => '4444'
  114. 'browser_name' => 'firefox'
  115. 'version' => ''
  116. 'platform' => 'ANY'
  117. 'javascript' => 1
  118. 'auto_close' => 1
  119. Output:
  120. Remote Driver object
  121. Usage:
  122. my $driver = new Selenium::Remote::Driver;
  123. or
  124. my $driver = new Selenium::Remote::Driver('browser_name' => 'firefox',
  125. 'platform' => 'MAC')
  126. or
  127. my $driver = new Selenium::Remote::Driver('remote_server_addr' => '10.10.1.1',
  128. 'port' => '2222',
  129. auto_close => 0
  130. )
  131. =cut
  132. sub new {
  133. my ( $class, %args ) = @_;
  134. my $ress = new Selenium::Remote::Commands;
  135. # Set the defaults if user doesn't send any
  136. my $self = {
  137. remote_server_addr => delete $args{remote_server_addr} || 'localhost',
  138. browser_name => delete $args{browser_name} || 'firefox',
  139. platform => delete $args{platform} || 'ANY',
  140. port => delete $args{port} || '4444',
  141. version => delete $args{version} || '',
  142. session_id => undef,
  143. remote_conn => undef,
  144. commands => $ress,
  145. auto_close => 1, # by default we will close remote session on DESTROY
  146. };
  147. bless $self, $class or die "Can't bless $class: $!";
  148. if ( defined $args{javascript} ) {
  149. if ( $args{javascript} ) {
  150. $self->{javascript} = JSON::true;
  151. }
  152. else {
  153. $self->{javascript} = JSON::false;
  154. }
  155. }
  156. else {
  157. $self->{javascript} = JSON::true;
  158. }
  159. # Connect to remote server & establish a new session
  160. $self->{remote_conn} =
  161. new Selenium::Remote::RemoteConnection( $self->{remote_server_addr},
  162. $self->{port} );
  163. $self->new_session();
  164. if ( !( defined $self->{session_id} ) ) {
  165. croak "Could not establish a session with the remote server\n";
  166. }
  167. return $self;
  168. }
  169. sub DESTROY {
  170. my ($self) = @_;
  171. $self->quit() if ($self->{auto_close} && defined $self->{session_id});
  172. }
  173. # This is an internal method used the Driver & is not supposed to be used by
  174. # end user. This method is used by Driver to set up all the parameters
  175. # (url & JSON), send commands & receive processed response from the server.
  176. sub _execute_command {
  177. my ( $self, $res, $params ) = @_;
  178. $res->{'session_id'} = $self->{'session_id'};
  179. my $resource = $self->{commands}->get_params($res);
  180. if ($resource) {
  181. my $resp = $self->{remote_conn}
  182. ->request( $resource->{'method'}, $resource->{'url'}, $params );
  183. return $resp;
  184. }
  185. else {
  186. croak "Couldn't retrieve command settings properly\n";
  187. }
  188. }
  189. # A method that is used by the Driver itself. It'll be called to set the
  190. # desired capabilities on the server.
  191. sub new_session {
  192. my $self = shift;
  193. my $args = {
  194. 'desiredCapabilities' => {
  195. 'browserName' => $self->{browser_name},
  196. 'platform' => $self->{platform},
  197. 'javascriptEnabled' => $self->{javascript},
  198. 'version' => $self->{version},
  199. }
  200. };
  201. my $resp =
  202. $self->{remote_conn}
  203. ->request( $self->{commands}->{'newSession'}->{'method'},
  204. $self->{commands}->{'newSession'}->{'url'}, $args, );
  205. if ( ( defined $resp->{'sessionId'} ) && $resp->{'sessionId'} ne '' ) {
  206. $self->{session_id} = $resp->{'sessionId'};
  207. }
  208. else {
  209. croak "Could not create new session";
  210. }
  211. }
  212. =head2 get_capabilities
  213. Description:
  214. Retrieve the capabilities of the specified session.
  215. Output:
  216. HASH of all the capabilities.
  217. Usage:
  218. my $capab = $driver->get_capabilities();
  219. print Dumper($capab);
  220. =cut
  221. sub get_capabilities {
  222. my $self = shift;
  223. my $res = {'command' => 'getCapabilities'};
  224. return $self->_execute_command($res);
  225. }
  226. =head2 set_implicit_wait_timeout
  227. Description:
  228. Set the amount of time the driver should wait when searching for elements.
  229. When searching for a single element, the driver will poll the page until
  230. an element is found or the timeout expires, whichever occurs first.
  231. When searching for multiple elements, the driver should poll the page until
  232. at least one element is found or the timeout expires, at which point it
  233. will return an empty list. If this method is never called, the driver will
  234. default to an implicit wait of 0ms.
  235. Input:
  236. Time in milliseconds.
  237. Output:
  238. Server Response Hash with no data returned back from the server.
  239. Usage:
  240. $driver->set_implicit_wait_timeout(10);
  241. =cut
  242. sub set_implicit_wait_timeout {
  243. my ($self, $ms) = @_;
  244. my $res = {'command' => 'setImplicitWaitTimeout'};
  245. my $params = {'ms' => $ms};
  246. return $self->_execute_command($res, $params);
  247. }
  248. =head2 quit
  249. Description:
  250. Delete the session & close open browsers.
  251. Usage:
  252. $driver->quit();
  253. =cut
  254. sub quit {
  255. my $self = shift;
  256. my $res = { 'command' => 'quit' };
  257. $self->_execute_command($res);
  258. $self->{session_id} = undef;
  259. }
  260. =head2 get_current_window_handle
  261. Description:
  262. Retrieve the current window handle.
  263. Output:
  264. STRING - the window handle
  265. Usage:
  266. print $driver->get_current_window_handle();
  267. =cut
  268. sub get_current_window_handle {
  269. my $self = shift;
  270. my $res = { 'command' => 'getCurrentWindowHandle' };
  271. return $self->_execute_command($res);
  272. }
  273. =head2 get_current_window_handles
  274. Description:
  275. Retrieve the list of window handles used in the session.
  276. Output:
  277. ARRAY of STRING - list of the window handles
  278. Usage:
  279. print Dumper($driver->get_current_window_handles());
  280. =cut
  281. sub get_window_handles {
  282. my $self = shift;
  283. my $res = { 'command' => 'getWindowHandles' };
  284. return $self->_execute_command($res);
  285. }
  286. =head2 get_current_url
  287. Description:
  288. Retrieve the url of the current page
  289. Output:
  290. STRING - url
  291. Usage:
  292. print $driver->get_current_url();
  293. =cut
  294. sub get_current_url {
  295. my $self = shift;
  296. my $res = { 'command' => 'getCurrentUrl' };
  297. return $self->_execute_command($res);
  298. }
  299. =head2 navigate
  300. Description:
  301. Navigate to a given url. This is same as get() method.
  302. Input:
  303. STRING - url
  304. Usage:
  305. $driver->navigate('http://www.google.com');
  306. =cut
  307. sub navigate {
  308. my ( $self, $url ) = @_;
  309. $self->get($url);
  310. }
  311. =head2 get
  312. Description:
  313. Navigate to a given url
  314. Input:
  315. STRING - url
  316. Usage:
  317. $driver->get('http://www.google.com');
  318. =cut
  319. sub get {
  320. my ( $self, $url ) = @_;
  321. my $res = { 'command' => 'get' };
  322. my $params = { 'url' => $url };
  323. return $self->_execute_command( $res, $params );
  324. }
  325. =head2 get_title
  326. Description:
  327. Get the current page title
  328. Output:
  329. STRING - Page title
  330. Usage:
  331. print $driver->get_title();
  332. =cut
  333. sub get_title {
  334. my $self = shift;
  335. my $res = { 'command' => 'getTitle' };
  336. return $self->_execute_command($res);
  337. }
  338. =head2 go_back
  339. Description:
  340. Equivalent to hitting the back button on the browser.
  341. Usage:
  342. $driver->go_back();
  343. =cut
  344. sub go_back {
  345. my $self = shift;
  346. my $res = { 'command' => 'goBack' };
  347. return $self->_execute_command($res);
  348. }
  349. =head2 go_forward
  350. Description:
  351. Equivalent to hitting the forward button on the browser.
  352. Usage:
  353. $driver->go_forward();
  354. =cut
  355. sub go_forward {
  356. my $self = shift;
  357. my $res = { 'command' => 'goForward' };
  358. return $self->_execute_command($res);
  359. }
  360. =head2 refresh
  361. Description:
  362. Reload the current page.
  363. Usage:
  364. $driver->refresh();
  365. =cut
  366. sub refresh {
  367. my $self = shift;
  368. my $res = { 'command' => 'goForward' };
  369. return $self->_execute_command($res);
  370. }
  371. =head2 execute_script
  372. Description:
  373. Inject a snippet of JavaScript into the page and return its result.
  374. WebElements that should be passed to the script as an argument should be
  375. specified in the arguments array as WebElement object. Likewise,
  376. any WebElements in the script result will be returned as WebElement object.
  377. Input: 2 (1 optional)
  378. Required:
  379. STRING - Javascript to execute on the page
  380. Optional:
  381. ARRAY - list of arguments that need to be passed to the script.
  382. Output:
  383. {*} - Varied, depending on the type of result expected back from the script.
  384. Usage:
  385. $driver->switch_to_frame('frame_1');
  386. =cut
  387. sub execute_script {
  388. my ( $self, $script, @args ) = @_;
  389. if ($self->javascript) {
  390. if ( not defined $script ) {
  391. return 'No script provided';
  392. }
  393. my $res = { 'command' => 'executeScript' };
  394. # Check the args array if the elem obj is provided & replace it with
  395. # JSON representation
  396. for (my $i=0; $i<@args; $i++) {
  397. if (ref $args[$i] eq 'Selenium::Remote::WebElement') {
  398. $args[$i] = {'ELEMENT' => ($args[$i])->{id}};
  399. }
  400. }
  401. my $params = {'args' => @args};
  402. my $ret = $self->_execute_command($res, $params);
  403. # replace any ELEMENTS with WebElement
  404. if (exists $ret->{'cmd_return'}->{'ELEMENT'}) {
  405. $ret->{'cmd_return'} =
  406. new Selenium::Remote::WebElement(
  407. $ret->{'cmd_return'}->{ELEMENT}, $self);
  408. }
  409. return $ret;
  410. }
  411. else {
  412. return 'Javascript is not enabled on remote driver instance.';
  413. }
  414. }
  415. =head2 screenshot
  416. Description:
  417. Get a screenshot of the current page as a base64 encoded image.
  418. Output:
  419. STRING - base64 encoded image
  420. Usage:
  421. print $driver->go_screenshot();
  422. =cut
  423. sub screenshot {
  424. my ($self) = @_;
  425. my $res = { 'command' => 'screenshot' };
  426. return $self->_execute_command($res);
  427. }
  428. =head2 switch_to_frame
  429. Description:
  430. Change focus to another frame on the page. If the frame ID is null, the
  431. server will switch to the page's default content.
  432. Input: 1
  433. Required:
  434. {STRING | NUMBER | NULL} - ID of the frame which can be one of the three
  435. mentioned.
  436. Usage:
  437. $driver->switch_to_frame('frame_1');
  438. =cut
  439. sub switch_to_frame {
  440. my ( $self, $id ) = @_;
  441. my $json_null = JSON::null;
  442. $id = ( defined $id ) ? $id : $json_null;
  443. my $res = { 'command' => 'switchToFrame' };
  444. my $params = { 'id' => $id };
  445. return $self->_execute_command( $res, $params );
  446. }
  447. =head2 switch_to_window
  448. Description:
  449. Change focus to another window. The window to change focus to may be
  450. specified by its server assigned window handle, or by the value of its name
  451. attribute.
  452. Input: 1
  453. Required:
  454. STRING - Window handle or the Window name
  455. Usage:
  456. $driver->switch_to_window('MY Homepage');
  457. =cut
  458. sub switch_to_window {
  459. my ( $self, $name ) = @_;
  460. if ( not defined $name ) {
  461. return 'Window name not provided';
  462. }
  463. my $res = { 'command' => 'switchToWindow' };
  464. my $params = { 'name' => $name };
  465. return $self->_execute_command( $res, $params );
  466. }
  467. =head2 get_speed
  468. Description:
  469. Get the current user input speed. The actual input speed is still browser
  470. specific and not covered by the Driver.
  471. Output:
  472. STRING - One of these: SLOW, MEDIUM, FAST
  473. Usage:
  474. print $driver->get_speed();
  475. =cut
  476. sub get_speed {
  477. my ($self) = @_;
  478. my $res = { 'command' => 'getSpeed' };
  479. return $self->_execute_command($res);
  480. }
  481. =head2 set_speed
  482. Description:
  483. Set the user input speed.
  484. Input:
  485. STRING - One of these: SLOW, MEDIUM, FAST
  486. Usage:
  487. $driver->set_speed('MEDIUM');
  488. =cut
  489. sub set_speed {
  490. my ( $self, $speed ) = @_;
  491. if ( not defined $speed ) {
  492. return 'Speed not provided.';
  493. }
  494. my $res = { 'command' => 'setSpeed' };
  495. my $params = { 'speed' => $speed };
  496. return $self->_execute_command( $res, $params );
  497. }
  498. =head2 get_all_cookies
  499. Description:
  500. Retrieve all cookies visible to the current page. Each cookie will be
  501. returned as a HASH reference with the following keys & their value types:
  502. 'name' - STRING
  503. 'value' - STRING
  504. 'path' - STRING
  505. 'domain' - STRING
  506. 'secure' - BOOLEAN
  507. Output:
  508. ARRAY of HASHES - list of all the cookie hashes
  509. Usage:
  510. print Dumper($driver->get_all_cookies());
  511. =cut
  512. sub get_all_cookies {
  513. my ($self) = @_;
  514. my $res = { 'command' => 'getAllCookies' };
  515. return $self->_execute_command($res);
  516. }
  517. =head2 add_cookie
  518. Description:
  519. Set a cookie on the domain.
  520. Input: 5 (1 optional)
  521. Required:
  522. 'name' - STRING
  523. 'value' - STRING
  524. 'path' - STRING
  525. 'domain' - STRING
  526. Optional:
  527. 'secure' - BOOLEAN - default is false.
  528. Usage:
  529. $driver->add_cookie('foo', 'bar', '/', '.google.com', 0)
  530. =cut
  531. sub add_cookie {
  532. my ( $self, $name, $value, $path, $domain, $secure ) = @_;
  533. if ( ( not defined $name )
  534. || ( not defined $value )
  535. || ( not defined $path )
  536. || ( not defined $domain ) )
  537. {
  538. return "Missing parameters";
  539. }
  540. my $res = { 'command' => 'addCookie' };
  541. my $json_false = JSON::false;
  542. my $json_true = JSON::true;
  543. $secure = ( defined $secure ) ? $json_true : $json_false;
  544. my $params = {
  545. 'cookie' => {
  546. 'name' => $name,
  547. 'value' => $value,
  548. 'path' => $path,
  549. 'domain' => $domain,
  550. 'secure' => $secure,
  551. }
  552. };
  553. return $self->_execute_command( $res, $params );
  554. }
  555. =head2 delete_all_cookies
  556. Description:
  557. Delete all cookies visible to the current page.
  558. Usage:
  559. $driver->delete_all_cookies();
  560. =cut
  561. sub delete_all_cookies {
  562. my ($self) = @_;
  563. my $res = { 'command' => 'deleteAllCookies' };
  564. return $self->_execute_command($res);
  565. }
  566. =head2 delete_cookie_named
  567. Description:
  568. Delete the cookie with the given name. This command will be a no-op if there
  569. is no such cookie visible to the current page.
  570. Input: 1
  571. Required:
  572. STRING - name of cookie to delete
  573. Usage:
  574. $driver->delete_cookie_named('foo');
  575. =cut
  576. sub delete_cookie_named {
  577. my ( $self, $cookie_name ) = @_;
  578. if ( not defined $cookie_name ) {
  579. return "Cookie name not provided";
  580. }
  581. my $res = { 'command' => 'deleteAllCookies', 'name' => $cookie_name };
  582. return $self->_execute_command($res);
  583. }
  584. =head2 get_page_source
  585. Description:
  586. Get the current page source.
  587. Output:
  588. STRING - The page source.
  589. Usage:
  590. print $driver->get_page_source();
  591. =cut
  592. sub get_page_source {
  593. my ($self) = @_;
  594. my $res = { 'command' => 'getPageSource' };
  595. return $self->_execute_command($res);
  596. }
  597. =head2 find_element
  598. Description:
  599. Search for an element on the page, starting from the document root. The
  600. located element will be returned as a WebElement object.
  601. Input: 2 (1 optional)
  602. Required:
  603. STRING - The search target.
  604. Optional:
  605. STRING - Locator scheme to use to search the element, available schemes:
  606. {class, class_name, css, id, link, link_text, partial_link_text,
  607. tag_name, name, xpath}
  608. Defaults to 'xpath'.
  609. Output:
  610. Selenium::Remote::WebElement - WebElement Object
  611. Usage:
  612. $driver->find_element("//input[\@name='q']");
  613. =cut
  614. sub find_element {
  615. my ( $self, $query, $method ) = @_;
  616. if ( not defined $query ) {
  617. return 'Search string to find element not provided.';
  618. }
  619. my $using = ( defined $method ) ? $method : 'xpath';
  620. my $ret;
  621. if (exists FINDERS->{$using}) {
  622. my $res = { 'command' => 'findElement' };
  623. my $params = { 'using' => $using, 'value' => $query };
  624. my $ret_data = $self->_execute_command( $res, $params );
  625. if (defined $ret_data->{'cmd_error'}) {
  626. $ret = $ret_data;
  627. }
  628. else {
  629. $ret_data->{'cmd_return'} = new Selenium::Remote::WebElement($ret_data->{'cmd_return'}->{ELEMENT}, $self);
  630. $ret = $ret_data;
  631. }
  632. }
  633. else {
  634. $ret = "Bad method, expected - class, class_name, css, id, link,
  635. link_text, partial_link_text, name, tag_name, xpath";
  636. }
  637. return $ret;
  638. }
  639. =head2 find_elements
  640. Description:
  641. Search for multiple elements on the page, starting from the document root.
  642. The located elements will be returned as an array of WebElement object.
  643. Input: 2 (1 optional)
  644. Required:
  645. STRING - The search target.
  646. Optional:
  647. STRING - Locator scheme to use to search the element, available schemes:
  648. {class, class_name, css, id, link, link_text, partial_link_text,
  649. tag_name, name, xpath}
  650. Defaults to 'xpath'.
  651. Output:
  652. ARRAY of Selenium::Remote::WebElement - Array of WebElement Objects
  653. Usage:
  654. $driver->find_elements("//input");
  655. =cut
  656. sub find_elements {
  657. my ( $self, $query, $method ) = @_;
  658. if ( not defined $query ) {
  659. return 'Search string to find element not provided.';
  660. }
  661. my $using = ( defined $method ) ? $method : 'xpath';
  662. my $ret;
  663. if (exists FINDERS->{$using}) {
  664. my $res = { 'command' => 'findElements' };
  665. my $params = { 'using' => $using, 'value' => $query };
  666. my $ret_data = $self->_execute_command( $res, $params );
  667. if (defined $ret_data->{'cmd_error'}) {
  668. $ret = $ret_data;
  669. }
  670. else {
  671. my $elem_obj_arr;
  672. my $i = 0;
  673. my $elem_arr = $ret_data->{'cmd_return'};
  674. foreach (@$elem_arr) {
  675. $elem_obj_arr->[$i] = new Selenium::Remote::WebElement($_->{ELEMENT}, $self);
  676. $i++;
  677. }
  678. $ret_data->{'cmd_return'} = $elem_obj_arr;
  679. $ret = $ret_data;
  680. }
  681. }
  682. else {
  683. $ret = "Bad method, expected - class, class_name, css, id, link,
  684. link_text, partial_link_text, name, tag_name, xpath";
  685. }
  686. return $ret;
  687. }
  688. =head2 find_child_element
  689. Description:
  690. Search for an element on the page, starting from the identified element. The
  691. located element will be returned as a WebElement object.
  692. Input: 3 (1 optional)
  693. Required:
  694. Selenium::Remote::WebElement - WebElement object from where you want to
  695. start searching.
  696. STRING - The search target.
  697. Optional:
  698. STRING - Locator scheme to use to search the element, available schemes:
  699. {class, class_name, css, id, link, link_text, partial_link_text,
  700. tag_name, name, xpath}
  701. Defaults to 'xpath'.
  702. Output:
  703. Selenium::Remote::WebElement - WebElement Object
  704. Usage:
  705. my $elem1 = $driver->find_element("//select[\@name='ned']");
  706. my $child = $driver->find_child_element($elem1, "//option[\@value='es_ar']");
  707. =cut
  708. sub find_child_element {
  709. my ( $self, $elem, $query, $method ) = @_;
  710. my $ret;
  711. if ( ( not defined $elem ) || ( not defined $query ) ) {
  712. return "Missing parameters";
  713. }
  714. my $using = ( defined $method ) ? $method : 'xpath';
  715. if (exists FINDERS->{$using}) {
  716. my $res = { 'command' => 'findChildElement', 'id' => $elem->{id} };
  717. my $params = { 'using' => $using, 'value' => $query };
  718. my $ret_data = $self->_execute_command( $res, $params );
  719. if (defined $ret_data->{'cmd_error'}) {
  720. $ret = $ret_data;
  721. }
  722. else {
  723. $ret_data->{'cmd_return'} = new Selenium::Remote::WebElement($ret_data->{'cmd_return'}->{ELEMENT}, $self);
  724. $ret = $ret_data;
  725. }
  726. }
  727. else {
  728. $ret = "Bad method, expected - class, class_name, css, id, link,
  729. link_text, partial_link_text, name, tag_name, xpath";
  730. }
  731. return $ret;
  732. }
  733. =head2 find_child_elements
  734. Description:
  735. Search for multiple element on the page, starting from the identified
  736. element. The located elements will be returned as an array of WebElement
  737. objects.
  738. Input: 3 (1 optional)
  739. Required:
  740. Selenium::Remote::WebElement - WebElement object from where you want to
  741. start searching.
  742. STRING - The search target.
  743. Optional:
  744. STRING - Locator scheme to use to search the element, available schemes:
  745. {class, class_name, css, id, link, link_text, partial_link_text,
  746. tag_name, name, xpath}
  747. Defaults to 'xpath'.
  748. Output:
  749. ARRAY of Selenium::Remote::WebElement - Array of WebElement Objects.
  750. Usage:
  751. my $elem1 = $driver->find_element("//select[\@name='ned']");
  752. my $child = $driver->find_child_elements($elem1, "//option");
  753. =cut
  754. sub find_child_elements {
  755. my ( $self, $elem, $query, $method ) = @_;
  756. my $ret;
  757. if ( ( not defined $elem ) || ( not defined $query ) ) {
  758. return "Missing parameters";
  759. }
  760. my $using = ( defined $method ) ? $method : 'xpath';
  761. if (exists FINDERS->{$using}) {
  762. my $res = { 'command' => 'findChildElements', 'id' => $elem->{id} };
  763. my $params = { 'using' => $using, 'value' => $query };
  764. my $ret_data = $self->_execute_command( $res, $params );
  765. if (defined $ret_data->{'cmd_error'}) {
  766. $ret = $ret_data;
  767. }
  768. else {
  769. my $elem_obj_arr;
  770. my $i = 0;
  771. my $elem_arr = $ret_data->{'cmd_return'};
  772. foreach (@$elem_arr) {
  773. $elem_obj_arr->[$i] = new Selenium::Remote::WebElement($_->{ELEMENT}, $self);
  774. $i++;
  775. }
  776. $ret_data->{'cmd_return'} = $elem_obj_arr;
  777. $ret = $ret_data;
  778. }
  779. }
  780. else {
  781. $ret = "Bad method, expected - class, class_name, css, id, link,
  782. link_text, partial_link_text, name, tag_name, xpath";
  783. }
  784. return $ret;
  785. }
  786. =head2 get_active_element
  787. Description:
  788. Get the element on the page that currently has focus.. The located element
  789. will be returned as a WebElement object.
  790. Output:
  791. Selenium::Remote::WebElement - WebElement Object
  792. Usage:
  793. $driver->get_active_element();
  794. =cut
  795. sub get_active_element {
  796. my ($self) = @_;
  797. my $res = { 'command' => 'getActiveElement' };
  798. return $self->_execute_command($res);
  799. }
  800. # Not yet supported on the server
  801. sub describe_element {
  802. my ( $self, $element ) = @_;
  803. #if (not defined $element) {
  804. # return "Element not provided";
  805. #}
  806. #my $res = {'command' => 'desribeElement', 'name' => $element};
  807. #return $self->_execute_command($res);
  808. return "Not yet supported";
  809. }
  810. =head2 compare_elements
  811. Description:
  812. Test if two element IDs refer to the same DOM element.
  813. Input: 2
  814. Required:
  815. Selenium::Remote::WebElement - WebElement Object
  816. Selenium::Remote::WebElement - WebElement Object
  817. Output:
  818. BOOLEAN
  819. Usage:
  820. $driver->compare_elements($elem_obj1, $elem_obj2);
  821. =cut
  822. sub compare_elements {
  823. my ($self, $elem1, $elem2) = @_;
  824. my $res = { 'command' => 'elementEquals',
  825. 'id' => $elem1->{id},
  826. 'other' => $elem2->{id}
  827. };
  828. return $self->_execute_command($res);
  829. }
  830. 1;
  831. __END__
  832. =head1 SEE ALSO
  833. For more information about Selenium , visit the website at
  834. L<http://code.google.com/p/selenium/>.
  835. =head1 BUGS
  836. The Selenium issue tracking system is available online at
  837. L<http://github.com/aivaturi/Selenium-Remote-Driver/issues>.
  838. =head1 AUTHOR
  839. Perl Bindings for Selenium Remote Driver by Aditya Ivaturi <ivaturi@gmail.com>
  840. =head1 LICENSE
  841. Copyright (c) 2010 Aditya Ivaturi
  842. Licensed under the Apache License, Version 2.0 (the "License");
  843. you may not use this file except in compliance with the License.
  844. You may obtain a copy of the License at
  845. http://www.apache.org/licenses/LICENSE-2.0
  846. Unless required by applicable law or agreed to in writing, software
  847. distributed under the License is distributed on an "AS IS" BASIS,
  848. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  849. See the License for the specific language governing permissions and
  850. limitations under the License.