1
0

Driver.pm 30 KB

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