1
0

Driver.pm 101 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530
  1. package Selenium::Remote::Driver;
  2. use strict;
  3. use warnings;
  4. # ABSTRACT: Perl Client for Selenium Remote Driver
  5. use Moo;
  6. use Try::Tiny;
  7. use 5.006;
  8. use v5.10.0; # Before 5.006, v5.10.0 would not be understood.
  9. # See http://perldoc.perl.org/5.10.0/functions/use.html#use-VERSION
  10. # and http://www.dagolden.com/index.php/369/version-numbers-should-be-boring/
  11. # for details.
  12. use Carp;
  13. our @CARP_NOT;
  14. use IO::String;
  15. use Archive::Zip qw( :ERROR_CODES );
  16. use Scalar::Util;
  17. use Selenium::Remote::RemoteConnection;
  18. use Selenium::Remote::Commands;
  19. use Selenium::Remote::Spec;
  20. use Selenium::Remote::WebElement;
  21. use Selenium::Remote::WDKeys;
  22. use File::Spec::Functions ();
  23. use File::Basename qw(basename);
  24. use Sub::Install ();
  25. use MIME::Base64 ();
  26. use Time::HiRes qw(usleep);
  27. use Clone qw{clone};
  28. use List::Util qw{any};
  29. use constant FINDERS => {
  30. class => 'class name',
  31. class_name => 'class name',
  32. css => 'css selector',
  33. id => 'id',
  34. link => 'link text',
  35. link_text => 'link text',
  36. name => 'name',
  37. partial_link_text => 'partial link text',
  38. tag_name => 'tag name',
  39. xpath => 'xpath',
  40. };
  41. our $FORCE_WD2 = 0;
  42. our $FORCE_WD3 = 0;
  43. our %CURRENT_ACTION_CHAIN = ( actions => [] );
  44. =for Pod::Coverage BUILD
  45. =for Pod::Coverage DEMOLISH
  46. =head1 SYNOPSIS
  47. use Selenium::Remote::Driver;
  48. my $driver = Selenium::Remote::Driver->new;
  49. $driver->get('http://www.google.com');
  50. print $driver->get_title();
  51. $driver->quit();
  52. =cut
  53. =head1 DESCRIPTION
  54. Selenium is a test tool that allows you to write
  55. automated web application UI tests in any programming language against
  56. any HTTP website using any mainstream JavaScript-enabled browser. This module is
  57. an implementation of the client for the Remote driver that Selenium provides.
  58. You can find bindings for other languages at this location:
  59. L<http://code.google.com/p/selenium/>
  60. This module sends commands directly to the Server using HTTP. Using this module
  61. together with the Selenium Server, you can automatically control any supported
  62. browser. To use this module, you need to have already downloaded and started
  63. the Selenium Server (Selenium Server is a Java application).
  64. =cut
  65. =head1 USAGE
  66. =head2 Without Standalone Server
  67. As of v0.25, it's possible to use this module without a standalone
  68. server - that is, you would not need the JRE or the JDK to run your
  69. Selenium tests. See L<Selenium::Chrome>, L<Selenium::PhantomJS>, and
  70. L<Selenium::Firefox> for details. If you'd like additional browsers
  71. besides these, give us a holler over in
  72. L<Github|https://github.com/teodesian/Selenium-Remote-Driver/issues>.
  73. =head2 Remote Driver Response
  74. Selenium::Remote::Driver uses the
  75. L<JsonWireProtocol|http://code.google.com/p/selenium/wiki/JsonWireProtocol>
  76. And the
  77. L<WC3 WebDriver Protocol|https://www.w3.org/TR/webdriver/>
  78. to communicate with the Selenium Server. If an error occurs while
  79. executing the command then the server sends back an HTTP error code
  80. with a JSON encoded reponse that indicates the precise
  81. L<Response Error Code|http://code.google.com/p/selenium/wiki/JsonWireProtocol#Response_Status_Codes>.
  82. The module will then croak with the error message associated with this
  83. code. If no error occurred, then the subroutine called will return the
  84. value sent back from the server (if a return value was sent).
  85. So a rule of thumb while invoking methods on the driver is if the method did not
  86. croak when called, then you can safely assume the command was successful even if
  87. nothing was returned by the method.
  88. =head2 WebElement
  89. Selenium Webdriver represents all the HTML elements as WebElement, which is
  90. in turn represented by L<Selenium::Remote::WebElement> module. So any method that
  91. deals with WebElements will return and/or expect WebElement object. The POD for
  92. that module describes all the methods that perform various actions on the
  93. WebElements like click, submit etc.
  94. To interact with any WebElement you have to first "find" it, read the POD for
  95. find_element or find_elements for further info. Once you find the required element
  96. then you can perform various actions. If you don't call find_* method first, all
  97. your further actions will fail for that element. Finally, just remember that you
  98. don't have to instantiate WebElement objects at all - they will be automatically
  99. created when you use the find_* methods.
  100. A sub-class of Selenium::Remote::WebElement may be used instead of Selenium::Remote::WebElement,
  101. by providing that class name as an option the constructor:
  102. my $driver = Selenium::Remote::Driver->new( webelement_class => ... );
  103. For example, a testing-subclass may extend the web-element object with testing methods.
  104. =head2 LWP Read Timeout errors
  105. It's possible to make Selenium calls that take longer than the default
  106. L<LWP::UserAgent> timeout. For example, setting the asynchronous
  107. script timeout greater than the LWP::UserAgent timeout and then
  108. executing a long running asynchronous snippet of javascript will
  109. immediately trigger an error like:
  110. Error while executing command: executeAsyncScript: Server returned
  111. error message read timeout at...
  112. You can get around this by configuring LWP's timeout value, either by
  113. constructing your own LWP and passing it in to ::Driver during
  114. instantiation
  115. my $timeout_ua = LWP::UserAgent->new;
  116. $timeout_ua->timeout(360); # this value is in seconds!
  117. my $d = Selenium::Remote::Driver->new( ua => $timeout_ua );
  118. or by configuring the timeout on the fly as necessary:
  119. use feature qw/say/;
  120. use Selenium::Remote::Driver;
  121. my $d = Selenium::Remote::Driver->new;
  122. say $d->ua->timeout; # 180 seconds is the default
  123. $d->ua->timeout(2); # LWP wants seconds, not milliseconds!
  124. $d->set_timeout('script', 1000); # S::R::D wants milliseconds!
  125. # Async scripts only return when the callback is invoked. Since there
  126. # is no callback here, Selenium will block for the entire duration of
  127. # the async timeout script. This will hit Selenium's async script
  128. # timeout before hitting LWP::UserAgent's read timeout
  129. $d->execute_async_script('return "hello"');
  130. $d->quit;
  131. =head1 TESTING
  132. If are writing automated tests using this module, you may be
  133. interested in L<Test::Selenium::Remote::Driver> which is also included
  134. in this distribution. It includes convenience testing methods for many
  135. of the selenum methods available here.
  136. Your other option is to use this module in conjunction with your
  137. choice of testing modules, like L<Test::Spec> or L<Test::More> as
  138. you please.
  139. =head1 WC3 WEBDRIVER COMPATIBILITY
  140. WC3 Webdriver is a constantly evolving standard, so some things may or may not work at any given time.
  141. Furthermore, out of date drivers probably identify as WD3, while only implementing a few methods and retaining JSONWire functionality.
  142. One way of dealing with this is setting:
  143. $driver->{is_wd3} = 0
  144. Of course, this will prevent access of any new WC3 methods, but will probably make your tests pass until your browser's driver gets it's act together.
  145. That said, the following 'sanity tests' in the at/ (acceptance test) directory of the module passed on the following versions:
  146. =over 4
  147. =item Selenium Server: 3.8.1 - all tests
  148. =item geckodriver: 0.19.1 - at/sanity.test
  149. =item chromedriver: 2.35 - at/sanity-chrome.test
  150. =item edgedriver: 5.16299 - at/sanity-edge.test
  151. =item InternetExplorerDriver : 3.8.1 - at/sanity-ie.test (be sure to enable 'allow local files to run active content in your 'advanced settings' pane)
  152. =item safaridriver : 11.0.2 - at/sanity-safari.test (be sure to enable 'allow automated testing' in the developer menu) -- it appears WC3 spec is *unimplemented*
  153. =back
  154. These tests are intended to be run directly against a working selenium server on the local host with said drivers configured.
  155. If you are curious as to what 'works and does not' on your driver versions (and a few other quirks),
  156. it is strongly encouraged you look at where the test calls the methods you are interested in.
  157. While other browsers/drivers (especially legacy ones) likely work fine as well,
  158. any new browser/driver will likely have problems if it's not listed above.
  159. There is also a 'legacy.test' file available to run against old browsers/selenium (2.x servers, pre geckodriver).
  160. This should only be used to verify backwards-compatibility has not been broken.
  161. =head2 Firefox Notes
  162. If you are intending to pass extra_capabilities to firefox on a WD3 enabled server with geckodriver, you MUST do the following:
  163. $Selenium::Remote::Driver::FORCE_WD3=1;
  164. This is because the gecko driver prefers legacy capabilities, both of which are normally passed for compatibility reasons.
  165. =head2 Chrome Notes
  166. extra_capabilities may? not work, because chromedriver considers the chromeOptions parameter to be invalid, despite it's documentation here:
  167. https://sites.google.com/a/chromium.org/chromedriver/capabilities
  168. Other bindings get around this by just using the 'old' way of passing desired capabilities. You can do this too like so:
  169. $Selenium::Remote::Driver::FORCE_WD2=1;
  170. =head1 CONSTRUCTOR
  171. =head2 new
  172. Dies if communication with the selenium server cannot be established.
  173. Input: (all optional)
  174. Desired capabilities - HASH - Following options are accepted:
  175. =over 4
  176. =item B<remote_server_addr> - <string> - IP or FQDN of the Webdriver server machine. Default: 'localhost'
  177. =item B<port> - <string> - Port on which the Webdriver server is listening. Default: 4444
  178. =item B<browser_name> - <string> - desired browser string: {phantomjs|firefox|internet explorer|MicrosoftEdge|safari|htmlunit|iphone|chrome}
  179. =item B<version> - <string> - desired browser version number
  180. =item B<platform> - <string> - desired platform: {WINDOWS|XP|VISTA|MAC|LINUX|UNIX|ANY}
  181. =item B<accept_ssl_certs> - <boolean> - whether SSL certs should be accepted, default is true.
  182. =item B<firefox_profile> - Profile - Use Selenium::Firefox::Profile to create a Firefox profile for the browser to use. Optionally can pass a base64'd zip data of a profile directory if you don't like Selenium::Firefox::Profile.
  183. =item B<proxy> - HASH - Proxy configuration with the following keys:
  184. =item B<javascript> - <boolean> - Whether or not to use Javascript. You probably won't disable this, as you would be using L<WWW::Mechanize> instead. Default: True
  185. =item B<auto_close> - <boolean> - Whether to automatically close the browser session on the server when the object goes out of scope. Default: False.
  186. =item B<default_finder> - <string> - Default method by which to evaluate selectors. Default: 'xpath'
  187. =item B<session_id> - <string> - Provide a Session ID to highjack a browser session on the remote server. Useful for micro-optimizers. Default: undef
  188. =over 4
  189. =item B<proxyType> - <string> - REQUIRED, Possible values are:
  190. direct - A direct connection - no proxy in use,
  191. manual - Manual proxy settings configured, e.g. setting a proxy for HTTP, a proxy for FTP, etc,
  192. pac - Proxy autoconfiguration from a URL,
  193. autodetect - proxy autodetection, probably with WPAD,
  194. system - Use system settings
  195. =item B<proxyAutoconfigUrl> - <string> - REQUIRED if proxyType is 'pac', ignored otherwise. Expected format: http://hostname.com:1234/pacfile or file:///path/to/pacfile
  196. =item B<ftpProxy> - <string> - OPTIONAL, ignored if proxyType is not 'manual'. Expected format: hostname.com:1234
  197. =item B<httpProxy> - <string> - OPTIONAL, ignored if proxyType is not 'manual'. Expected format: hostname.com:1234
  198. =item B<sslProxy> - <string> - OPTIONAL, ignored if proxyType is not 'manual'. Expected format: hostname.com:1234
  199. =item B<socksProxy> - <string> - OPTIONAL, ignored if proxyType is not 'manual'. Expected format: hostname.com:1234. WebDriver 3 only.
  200. =item B<socksVersion> - <int> - OPTIONAL, ignored if proxyType is not 'manual'. WebDriver 3 only.
  201. =item B<noProxy> - <ARRAY> - OPTIONAL, list of URLs to bypass the proxy for. WebDriver3 only.
  202. =back
  203. =item B<pageLoadStrategy> - STRING - OPTIONAL, 'normal|eager|none'. default 'normal'. WebDriver3 only.
  204. =item B<extra_capabilities> - HASH - Any other extra capabilities. Accepted keys will vary by browser. If firefox_profile is passed, the args (or profile) key will be overwritten, depending on how it was passed.
  205. =back
  206. On WebDriver3 the 'extra_capabilities' will be automatically converted into the parameter needed by your browser.
  207. For example, extra_capabilities is passed to the server as the moz:firefoxOptions parameter.
  208. You can also specify some options in the constructor hash that are
  209. not part of the browser-related desired capabilities.
  210. =over 4
  211. =item B<auto_close> - <boolean> - whether driver should end session on remote server on close.
  212. =item B<base_url> - <string> - OPTIONAL, base url for the website Selenium acts on. This can save you from repeating the domain in every call to $driver->get()
  213. =item B<default_finder> - <string> - choose default finder used for find_element* {class|class_name|css|id|link|link_text|name|partial_link_text|tag_name|xpath}
  214. =item B<inner_window_size> - <aref[Int]> - An array ref [ height, width ] that the browser window should use as its initial size immediately after instantiation
  215. =item B<error_handler> - CODEREF - A CODEREF that we will call in event of any exceptions. See L</error_handler> for more details.
  216. =item B<webelement_class> - <string> - sub-class of Selenium::Remote::WebElement if you wish to use an alternate WebElement class.
  217. =item B<ua> - LWP::UserAgent instance - if you wish to use a specific $ua, like from Test::LWP::UserAgent
  218. =back
  219. Output:
  220. Remote Driver object
  221. Usage:
  222. my $driver = Selenium::Remote::Driver->new;
  223. or
  224. my $driver = Selenium::Remote::Driver->new('browser_name' => 'firefox',
  225. 'platform' => 'MAC');
  226. or (for Firefox 47 or lower on Selenium 3+)
  227. my $driver = Selenium::Remote::Driver->new('browser_name' => 'firefox',
  228. 'platform' => 'MAC',
  229. 'extra_capabilities' => {
  230. 'marionette' => \0,
  231. });
  232. or
  233. my $driver = Selenium::Remote::Driver->new('remote_server_addr' => '10.10.1.1',
  234. 'port' => '2222',
  235. 'auto_close' => 0);
  236. or
  237. my $driver = Selenium::Remote::Driver->new('browser_name' =>'chrome',
  238. 'extra_capabilities' => {
  239. 'chromeOptions' => {
  240. 'args' => [
  241. 'window-size=1260,960',
  242. 'incognito'
  243. ],
  244. 'prefs' => {
  245. 'session' => {
  246. 'restore_on_startup' => 4,
  247. 'urls_to_restore_on_startup' => [
  248. 'http://www.google.com',
  249. 'http://docs.seleniumhq.org'
  250. ]},
  251. 'first_run_tabs' => [
  252. 'http://www.google.com',
  253. 'http://docs.seleniumhq.org'
  254. ]
  255. }
  256. }
  257. });
  258. or
  259. my $driver = Selenium::Remote::Driver->new('proxy' => {'proxyType' => 'manual', 'httpProxy' => 'myproxy.com:1234'});
  260. or
  261. my $driver = Selenium::Remote::Driver->new('default_finder' => 'css');
  262. =head3 error_handler
  263. =head3 clear_error_handler
  264. OPTIONAL constructor arg & associated setter/clearer: if you wish to
  265. install your own error handler, you may pass a code ref in to
  266. C<error_handler> during instantiation like follows:
  267. my $driver = Selenium::Remote::Driver->new(
  268. error_handler => sub { print $_[1]; croak 'goodbye'; }
  269. );
  270. Additionally, you can set and/or clear it at any time on an
  271. already-instantiated driver:
  272. # later, change the error handler to something else
  273. $driver->error_handler( sub { print $_[1]; croak 'hello'; } );
  274. # stop handling errors manually and use the default S:R:D behavior
  275. # (we will croak about the exception)
  276. $driver->clear_error_handler;
  277. Your error handler will receive two arguments: the first argument is
  278. the C<$driver> object itself, and the second argument is the exception
  279. message and stack trace in one multiline string.
  280. B<N.B.>: If you set your own error handler, you are entirely
  281. responsible for handling webdriver exceptions, _including_ croaking
  282. behavior. That is, when you set an error handler, we will no longer
  283. croak on Webdriver exceptions - it's up to you to do so. For
  284. consistency with the standard S:R:D behavior, we recommend your error
  285. handler also croak when it's done, especially since your test
  286. shouldn't be running into unexpected errors. Catching specific or
  287. desired errors in your error handler makes sense, but not croaking at
  288. all can leave you in unfamiliar territory. Reaching an unexpected
  289. exception might mean your test has gone off the rails, and the further
  290. your test gets from the source of the of the exception, the harder it
  291. will be to debug.
  292. B<N.B.>: Four methods will still croak on their own: L</find_element>,
  293. L</find_elements>, L</find_child_element>, and
  294. L</find_child_elements>. If these methods throw a Webdriver Exception,
  295. your error handler _will still be_ invoked inside an C<eval>, and then
  296. they'll croak with their own error message that indicates the locator
  297. and strategy used. So, your strategies for avoiding exceptions when
  298. finding elements do not change (either use find_elements and check
  299. the returned array size, wrap your calls to find_element* in an
  300. C<eval>, or use the parameterized versions find_element_*).
  301. =head2 new_from_caps
  302. Description:
  303. For experienced users who want complete control over the desired
  304. capabilities, use this alternative constructor along with the
  305. C<desired_capabilities> hash key in the init hash. Unlike "new",
  306. this constructor will not assume any defaults for your desired
  307. capabilities.
  308. This alternate constructor IGNORES all other browser-related
  309. desiredCapability options; the only options that will be respected
  310. are those that are NOT part of the Capabilities JSON Object as
  311. described in the Json Wire Protocol.
  312. Input:
  313. The only respected keys in the input hash are:
  314. desired_capabilities - HASHREF - defaults to {}
  315. remote_server_addr - STRING - defaults to localhost
  316. port - INTEGER - defaults to 4444
  317. default_finder - STRING - defaults to xpath
  318. webelement_class - STRING - defaults to Selenium::Remote::WebElement
  319. auto_close - BOOLEAN - defaults to 1
  320. error_handler - CODEREF - defaults to croaking on exceptions
  321. Except for C<desired_capabilities>, these keys perform exactly the
  322. same as listed in the regular "new" constructor.
  323. The hashref you pass in as desired_capabilities only gets json
  324. encoded before being passed to the Selenium server; no default
  325. options of any sort will be added.
  326. This means you must handle normalization and casing of the input
  327. options (like "browser_name" vs "browserName") and take care of
  328. things like encoding the firefox profile if applicable. More
  329. information about the desired capabilities object is available on
  330. the Selenium wiki:
  331. https://code.google.com/p/selenium/wiki/JsonWireProtocol#Capabilities_JSON_Object
  332. Output:
  333. Remote Driver object
  334. Usage:
  335. my $driver = Selenium::Remote::Driver->new_from_caps(
  336. 'desired_capabilities' => {'browserName' => 'firefox'}
  337. );
  338. The above would generate a POST to the webdriver server at
  339. localhost:4444 with the exact payload of '{"desiredCapabilities":
  340. {"browserName": "firefox" }}'.
  341. =for Pod::Coverage has_base_url
  342. =for Pod::Coverage has_desired_capabilities
  343. =for Pod::Coverage has_error_handler
  344. =for Pod::Coverage has_firefox_profile
  345. =for Pod::Coverage has_inner_window_size
  346. =for Pod::Coverage has_javascript
  347. =for Pod::Coverage has_port
  348. =for Pod::Coverage has_remote_server_addr
  349. =cut
  350. has 'remote_server_addr' => (
  351. is => 'rw',
  352. coerce => sub { ( defined($_[0]) ? $_[0] : 'localhost' )},
  353. default => sub {'localhost'},
  354. predicate => 1
  355. );
  356. has 'browser_name' => (
  357. is => 'rw',
  358. coerce => sub { ( defined($_[0]) ? $_[0] : 'firefox' )},
  359. default => sub {'firefox'},
  360. );
  361. has 'base_url' => (
  362. is => 'lazy',
  363. coerce => sub {
  364. my $base_url = shift;
  365. $base_url =~ s|/$||;
  366. return $base_url;
  367. },
  368. predicate => 'has_base_url',
  369. );
  370. has 'platform' => (
  371. is => 'rw',
  372. coerce => sub { ( defined($_[0]) ? $_[0] : 'ANY' )},
  373. default => sub {'ANY'},
  374. );
  375. has 'port' => (
  376. is => 'rw',
  377. coerce => sub { ( defined($_[0]) ? $_[0] : '4444' )},
  378. default => sub {'4444'},
  379. predicate => 1
  380. );
  381. has 'version' => (
  382. is => 'rw',
  383. default => sub {''},
  384. );
  385. has 'webelement_class' => (
  386. is => 'rw',
  387. default => sub {'Selenium::Remote::WebElement'},
  388. );
  389. has 'default_finder' => (
  390. is => 'rw',
  391. coerce => sub { __PACKAGE__->FINDERS->{ $_[0] } },
  392. default => sub {'xpath'},
  393. );
  394. has 'session_id' => (
  395. is => 'rw',
  396. default => sub {undef},
  397. );
  398. has 'remote_conn' => (
  399. is => 'lazy',
  400. builder => sub {
  401. my $self = shift;
  402. return Selenium::Remote::RemoteConnection->new(
  403. remote_server_addr => $self->remote_server_addr,
  404. port => $self->port,
  405. ua => $self->ua,
  406. wd_context_prefix => $self->wd_context_prefix
  407. );
  408. },
  409. );
  410. has 'error_handler' => (
  411. is => 'rw',
  412. coerce => sub {
  413. my ($maybe_coderef) = @_;
  414. if ( ref($maybe_coderef) eq 'CODE' ) {
  415. return $maybe_coderef;
  416. }
  417. else {
  418. croak 'The error handler must be a code ref.';
  419. }
  420. },
  421. clearer => 1,
  422. predicate => 1
  423. );
  424. has 'ua' => (
  425. is => 'lazy',
  426. builder => sub { return LWP::UserAgent->new }
  427. );
  428. has 'commands' => (
  429. is => 'lazy',
  430. builder => sub {
  431. return Selenium::Remote::Commands->new;
  432. },
  433. );
  434. has 'commands_v3' => (
  435. is => 'lazy',
  436. builder => sub {
  437. return Selenium::Remote::Spec->new;
  438. },
  439. );
  440. has 'auto_close' => (
  441. is => 'rw',
  442. coerce => sub { ( defined($_[0]) ? $_[0] : 1 )},
  443. default => sub {1},
  444. );
  445. has 'pid' => (
  446. is => 'lazy',
  447. builder => sub { return $$ }
  448. );
  449. has 'javascript' => (
  450. is => 'rw',
  451. coerce => sub { $_[0] ? JSON::true : JSON::false },
  452. default => sub { return JSON::true }
  453. );
  454. has 'accept_ssl_certs' => (
  455. is => 'rw',
  456. coerce => sub { $_[0] ? JSON::true : JSON::false },
  457. default => sub { return JSON::true }
  458. );
  459. has 'proxy' => (
  460. is => 'rw',
  461. coerce => sub {
  462. my $proxy = $_[0];
  463. if ( $proxy->{proxyType} =~ /^pac$/i ) {
  464. if ( not defined $proxy->{proxyAutoconfigUrl} ) {
  465. croak "proxyAutoconfigUrl not provided\n";
  466. }
  467. elsif ( not( $proxy->{proxyAutoconfigUrl} =~ /^(http|file)/g ) ) {
  468. croak "proxyAutoconfigUrl should be of format http:// or file://";
  469. }
  470. if ( $proxy->{proxyAutoconfigUrl} =~ /^file/ ) {
  471. my $pac_url = $proxy->{proxyAutoconfigUrl};
  472. my $file = $pac_url;
  473. $file =~ s{^file://}{};
  474. if (! -e $file) {
  475. warn "proxyAutoConfigUrl file does not exist: '$pac_url'";
  476. }
  477. }
  478. }
  479. $proxy;
  480. },
  481. );
  482. has 'extra_capabilities' => (
  483. is => 'rw',
  484. default => sub { {} }
  485. );
  486. has 'firefox_profile' => (
  487. is => 'rw',
  488. coerce => sub {
  489. my $profile = shift;
  490. unless (Scalar::Util::blessed($profile)
  491. && $profile->isa('Selenium::Firefox::Profile')) {
  492. croak "firefox_profile should be a Selenium::Firefox::Profile\n";
  493. }
  494. return $profile;
  495. },
  496. predicate => 'has_firefox_profile',
  497. clearer => 1
  498. );
  499. has 'desired_capabilities' => (
  500. is => 'lazy',
  501. predicate => 'has_desired_capabilities'
  502. );
  503. has 'inner_window_size' => (
  504. is => 'lazy',
  505. predicate => 1,
  506. coerce => sub {
  507. my $size = shift;
  508. croak "inner_window_size must have two elements: [ height, width ]"
  509. unless scalar @$size == 2;
  510. foreach my $dim (@$size) {
  511. croak 'inner_window_size only accepts integers, not: ' . $dim
  512. unless Scalar::Util::looks_like_number($dim);
  513. }
  514. return $size;
  515. },
  516. );
  517. # At the time of writing, Geckodriver uses a different endpoint than
  518. # the java bindings for executing synchronous and asynchronous
  519. # scripts. As a matter of fact, Geckodriver does conform to the W3C
  520. # spec, but as are bound to support both while the java bindings
  521. # transition to full spec support, we need some way to handle the
  522. # difference.
  523. has '_execute_script_suffix' => (
  524. is => 'lazy',
  525. default => ''
  526. );
  527. with 'Selenium::Remote::Finders';
  528. with 'Selenium::Remote::Driver::CanSetWebdriverContext';
  529. sub BUILD {
  530. my $self = shift;
  531. if ( !( defined $self->session_id ) ) {
  532. if ($self->has_desired_capabilities) {
  533. $self->new_desired_session( $self->desired_capabilities );
  534. }
  535. else {
  536. # Connect to remote server & establish a new session
  537. $self->new_session( $self->extra_capabilities );
  538. }
  539. }
  540. if ( !( defined $self->session_id ) ) {
  541. croak "Could not establish a session with the remote server\n";
  542. }
  543. elsif ($self->has_inner_window_size) {
  544. my $size = $self->inner_window_size;
  545. $self->set_inner_window_size(@$size);
  546. }
  547. # Setup non-croaking, parameter versions of finders
  548. foreach my $by (keys %{ $self->FINDERS }) {
  549. my $finder_name = 'find_element_by_' . $by;
  550. # In case we get instantiated multiple times, we don't want to
  551. # install into the name space every time.
  552. unless ($self->can($finder_name)) {
  553. my $find_sub = $self->_build_find_by($by);
  554. Sub::Install::install_sub({
  555. code => $find_sub,
  556. into => __PACKAGE__,
  557. as => $finder_name,
  558. });
  559. }
  560. }
  561. }
  562. sub new_from_caps {
  563. my ($self, %args) = @_;
  564. if (not exists $args{desired_capabilities}) {
  565. $args{desired_capabilities} = {};
  566. }
  567. return $self->new(%args);
  568. }
  569. sub DEMOLISH {
  570. my ($self, $in_global_destruction) = @_;
  571. return if $$ != $self->pid;
  572. return if $in_global_destruction;
  573. $self->quit() if ( $self->auto_close && defined $self->session_id );
  574. }
  575. # We install an 'around' because we can catch more exceptions this way
  576. # than simply wrapping the explicit croaks in _execute_command.
  577. around '_execute_command' => sub {
  578. my $orig = shift;
  579. my $self = shift;
  580. # copy @_ because it gets lost in the way
  581. my @args = @_;
  582. my $return_value;
  583. try {
  584. $return_value = $orig->($self,@args);
  585. }
  586. catch {
  587. if ($self->has_error_handler) {
  588. $self->error_handler->($self,$_);
  589. }
  590. else {
  591. croak $_;
  592. }
  593. };
  594. return $return_value;
  595. };
  596. # This is an internal method used the Driver & is not supposed to be used by
  597. # end user. This method is used by Driver to set up all the parameters
  598. # (url & JSON), send commands & receive processed response from the server.
  599. sub _execute_command {
  600. my ( $self, $res, $params ) = @_;
  601. $res->{'session_id'} = $self->session_id;
  602. print "Prepping $res->{command}\n" if $self->{debug};
  603. #webdriver 3 shims
  604. return $self->{capabilities} if $res->{command} eq 'getCapabilities' && $self->{capabilities};
  605. $res->{ms} = $params->{ms} if $params->{ms};
  606. $res->{type} = $params->{type} if $params->{type};
  607. $res->{text} = $params->{text} if $params->{text};
  608. $res->{using} = $params->{using} if $params->{using};
  609. $res->{value} = $params->{value} if $params->{value};
  610. print "Executing $res->{command}\n" if $self->{debug};
  611. my $resource = $self->{is_wd3} ? $self->commands_v3->get_params($res) : $self->commands->get_params($res);
  612. #Fall-back to legacy if wd3 command doesn't exist
  613. if (!$resource && $self->{is_wd3}) {
  614. print "Falling back to legacy selenium method for $res->{command}\n" if $self->{debug};
  615. $resource = $self->commands->get_params($res);
  616. }
  617. #XXX InternetExplorerDriver quirks
  618. if ($self->{is_wd3} && $self->browser_name eq 'internet explorer') {
  619. delete $params->{ms};
  620. delete $params->{type};
  621. delete $resource->{payload}->{type};
  622. my $oldvalue = delete $params->{'page load'};
  623. $params->{pageLoad} = $oldvalue if $oldvalue;
  624. }
  625. if ($resource) {
  626. $params = {} unless $params;
  627. my $resp = $self->remote_conn->request( $resource, $params);
  628. #In general, the parse_response for v3 is better, which is why we use it *even if* we are falling back.
  629. return $self->commands_v3->parse_response($res,$resp) if $self->{is_wd3};
  630. return $self->commands->parse_response($res,$resp);
  631. }
  632. else {
  633. croak "Couldn't retrieve command settings properly\n";
  634. }
  635. }
  636. =head1 METHODS
  637. =head2 new_session (extra_capabilities)
  638. Make a new session on the server.
  639. Called by new(), not intended for regular use.
  640. Occaisonally handy for recovering from brower crashes.
  641. DANGER DANGER DANGER
  642. This will throw away your old session if you have not closed it!
  643. DANGER DANGER DANGER
  644. =cut
  645. sub new_session {
  646. my ( $self, $extra_capabilities ) = @_;
  647. $extra_capabilities ||= {};
  648. my $args = {
  649. 'desiredCapabilities' => {
  650. 'browserName' => $self->browser_name,
  651. 'platform' => $self->platform,
  652. 'javascriptEnabled' => $self->javascript,
  653. 'version' => $self->version,
  654. 'acceptSslCerts' => $self->accept_ssl_certs,
  655. %$extra_capabilities,
  656. },
  657. };
  658. $args->{'extra_capabilities'} = \%$extra_capabilities unless $FORCE_WD2;
  659. if ( defined $self->proxy ) {
  660. $args->{desiredCapabilities}->{proxy} = $self->proxy;
  661. }
  662. if ($args->{desiredCapabilities}->{browserName} =~ /firefox/i
  663. && $self->has_firefox_profile) {
  664. $args->{desiredCapabilities}->{firefox_profile} = $self->firefox_profile->_encode;
  665. }
  666. $self->_request_new_session($args);
  667. }
  668. =head2 new_desired_session(capabilities)
  669. Basically the same as new_session, but with caps.
  670. Sort of an analog to new_from_caps.
  671. =cut
  672. sub new_desired_session {
  673. my ( $self, $caps ) = @_;
  674. $self->_request_new_session({
  675. desiredCapabilities => $caps
  676. });
  677. }
  678. sub _request_new_session {
  679. my ( $self, $args ) = @_;
  680. #XXX UGLY shim for webdriver3
  681. $args->{capabilities}->{alwaysMatch} = clone($args->{desiredCapabilities});
  682. my $cmap = $self->commands_v3->get_caps_map();
  683. my $caps = $self->commands_v3->get_caps();
  684. foreach my $cap (keys(%{$args->{capabilities}->{alwaysMatch} })) {
  685. #Handle browser specific capabilities
  686. if (exists($args->{desiredCapabilities}->{browserName}) && $cap eq 'extra_capabilities') {
  687. if (exists $args->{capabilities}->{alwaysMatch}->{'moz:firefoxOptions'}->{args}) {
  688. $args->{capabilities}->{alwaysMatch}->{$cap}->{args} = $args->{capabilities}->{alwaysMatch}->{'moz:firefoxOptions'}->{args};
  689. }
  690. $args->{capabilities}->{alwaysMatch}->{'moz:firefoxOptions'} = $args->{capabilities}->{alwaysMatch}->{$cap} if $args->{desiredCapabilities}->{browserName} eq 'firefox';
  691. #XXX the chrome documentation is lies, you can't do this yet
  692. #$args->{capabilities}->{alwaysMatch}->{'chromeOptions'} = $args->{capabilities}->{alwaysMatch}->{$cap} if $args->{desiredCapabilities}->{browserName} eq 'chrome';
  693. #Does not appear there are any MSIE based options, so let's just let that be
  694. }
  695. if (exists($args->{desiredCapabilities}->{browserName}) && $args->{desiredCapabilities}->{browserName} eq 'firefox' && $cap eq 'firefox_profile') {
  696. if (ref $args->{capabilities}->{alwaysMatch}->{$cap} eq 'Selenium::Firefox::Profile') {
  697. #XXX not sure if I need to keep a ref to the File::Temp::Tempdir object to prevent reaping
  698. $args->{capabilities}->{alwaysMatch}->{'moz:firefoxOptions'}->{args} = ['-profile', $args->{capabilities}->{alwaysMatch}->{$cap}->{profile_dir}->dirname()];
  699. } else {
  700. #previously undocumented feature that we can pass the encoded profile
  701. $args->{capabilities}->{alwaysMatch}->{'moz:firefoxOptions'}->{profile} = $args->{capabilities}->{alwaysMatch}->{$cap};
  702. }
  703. }
  704. foreach my $newkey (keys(%$cmap)) {
  705. if ($newkey eq $cap) {
  706. last if $cmap->{$newkey} eq $cap;
  707. $args->{capabilities}->{alwaysMatch}->{$cmap->{$newkey}} = $args->{capabilities}->{alwaysMatch}->{$cap};
  708. delete $args->{capabilities}->{alwaysMatch}->{$cap};
  709. last;
  710. }
  711. }
  712. delete $args->{capabilities}->{alwaysMatch}->{$cap} if !any { $_ eq $cap } @$caps;
  713. }
  714. delete $args->{desiredCapabilities} if $FORCE_WD3; #XXX fork working-around busted fallback in firefox
  715. delete $args->{capabilities} if $FORCE_WD2; #XXX 'secret' feature to help the legacy unit tests to work
  716. # geckodriver has not yet implemented the GET /status endpoint
  717. # https://developer.mozilla.org/en-US/docs/Mozilla/QA/Marionette/WebDriver/status
  718. if (! $self->isa('Selenium::Firefox')) {
  719. $self->remote_conn->check_status();
  720. }
  721. # command => 'newSession' to fool the tests of commands implemented
  722. # TODO: rewrite the testing better, this is so fragile.
  723. my $resource_new_session = {
  724. method => $self->commands->get_method('newSession'),
  725. url => $self->commands->get_url('newSession'),
  726. no_content_success => $self->commands->get_no_content_success('newSession'),
  727. };
  728. my $rc = $self->remote_conn;
  729. my $resp = $rc->request(
  730. $resource_new_session,
  731. $args,
  732. );
  733. if ( ( defined $resp->{'sessionId'} ) && $resp->{'sessionId'} ne '' ) {
  734. $self->session_id( $resp->{'sessionId'} );
  735. }
  736. else {
  737. my $error = 'Could not create new session';
  738. if (ref $resp->{cmd_return} eq 'HASH') {
  739. $error .= ': ' . $resp->{cmd_return}->{message};
  740. }
  741. else {
  742. $error .= ': ' . $resp->{cmd_return};
  743. }
  744. croak $error;
  745. }
  746. #Webdriver 3 - best guess that this is 'whats goin on'
  747. if ( ref $resp->{cmd_return} eq 'HASH' && $resp->{cmd_return}->{capabilities}) {
  748. $self->{is_wd3} = 1;
  749. $self->{capabilities} = $resp->{cmd_return}->{capabilities};
  750. }
  751. #XXX chromedriver DOES NOT FOLLOW SPEC!
  752. if ( ref $resp->{cmd_return} eq 'HASH' && $resp->{cmd_return}->{chrome}) {
  753. if (defined $resp->{cmd_return}->{setWindowRect}) { #XXX i'm inferring we are wd3 based on the presence of this
  754. $self->{is_wd3} = 1;
  755. $self->{capabilities} = $resp->{cmd_return};
  756. }
  757. }
  758. #XXX unsurprisingly, neither does microsoft
  759. if ( ref $resp->{cmd_return} eq 'HASH' && $resp->{cmd_return}->{pageLoadStrategy} && $self->browser_name eq 'MicrosoftEdge') {
  760. $self->{is_wd3} = 1;
  761. $self->{capabilities} = $resp->{cmd_return};
  762. }
  763. return ($args,$resp);
  764. }
  765. =head2 is_webdriver_3
  766. Print whether the server (or browser) thinks it's implemented webdriver 3.
  767. If this returns true, webdriver 3 methods will be used in the case an action exists in L<Selenium::Remote::Spec> for the method you are trying to call.
  768. If a method you are calling has no webdriver 3 equivalent (or browser extension), the legacy commands implemented in L<Selenium::Remote::Commands> will be used.
  769. Note how I said *thinks* above. In the case you want to force usage of legacy methods, call set_webdriver_3() to work around various browser issues.
  770. =cut
  771. sub is_webdriver_3 {
  772. my $self = shift;
  773. return $self->{is_wd3};
  774. }
  775. =head2 debug_on
  776. Description:
  777. Turns on debugging mode and the driver will print extra info like request
  778. and response to stdout. Useful, when you want to see what is being sent to
  779. the server & what response you are getting back.
  780. Usage:
  781. $driver->debug_on;
  782. =cut
  783. sub debug_on {
  784. my ($self) = @_;
  785. $self->{debug} = 1;
  786. $self->remote_conn->debug(1);
  787. }
  788. =head2 debug_off
  789. Description:
  790. Turns off the debugging mode.
  791. Usage:
  792. $driver->debug_off;
  793. =cut
  794. sub debug_off {
  795. my ($self) = @_;
  796. $self->{debug} = 0;
  797. $self->remote_conn->debug(0);
  798. }
  799. =head2 get_sessions
  800. Description:
  801. Returns a list of the currently active sessions. Each session will be
  802. returned as an array of Hashes with the following keys:
  803. 'id' : The session ID
  804. 'capabilities: An object describing session's capabilities
  805. Output:
  806. Array of Hashes
  807. Usage:
  808. print Dumper $driver->get_sessions();
  809. =cut
  810. sub get_sessions {
  811. my ($self) = @_;
  812. my $res = { 'command' => 'getSessions' };
  813. return $self->_execute_command($res);
  814. }
  815. =head2 status
  816. Description:
  817. Query the server's current status. All server implementations
  818. should return two basic objects describing the server's current
  819. platform and when the server was built.
  820. Output:
  821. Hash ref
  822. Usage:
  823. print Dumper $driver->status;
  824. =cut
  825. sub status {
  826. my ($self) = @_;
  827. my $res = { 'command' => 'status' };
  828. return $self->_execute_command($res);
  829. }
  830. =head2 get_alert_text
  831. Description:
  832. Gets the text of the currently displayed JavaScript alert(), confirm()
  833. or prompt() dialog.
  834. Example
  835. my $string = $driver->get_alert_text;
  836. =cut
  837. sub get_alert_text {
  838. my ($self) = @_;
  839. my $res = { 'command' => 'getAlertText' };
  840. return $self->_execute_command($res);
  841. }
  842. =head2 send_keys_to_active_element
  843. Description:
  844. Send a sequence of key strokes to the active element. This command is
  845. similar to the send keys command in every aspect except the implicit
  846. termination: The modifiers are not released at the end of the call.
  847. Rather, the state of the modifier keys is kept between calls, so mouse
  848. interactions can be performed while modifier keys are depressed.
  849. Compatibility:
  850. On webdriver 3 servers, don't use this to send modifier keys; use send_modifier instead.
  851. Input: 1
  852. Required:
  853. {ARRAY | STRING} - Array of strings or a string.
  854. Usage:
  855. $driver->send_keys_to_active_element('abcd', 'efg');
  856. $driver->send_keys_to_active_element('hijk');
  857. or
  858. # include the WDKeys module
  859. use Selenium::Remote::WDKeys;
  860. .
  861. .
  862. $driver->send_keys_to_active_element(KEYS->{'space'}, KEYS->{'enter'});
  863. =cut
  864. sub send_keys_to_active_element {
  865. my ( $self, @strings ) = @_;
  866. if ($self->{is_wd3} && !(grep { $self->browser_name eq $_ } qw{chrome MicrosoftEdge}) ) {
  867. @strings = map { split('',$_) } @strings;
  868. my @acts = map {
  869. (
  870. {
  871. type => 'keyDown',
  872. value => $_,
  873. },
  874. {
  875. type => 'keyUp',
  876. value => $_,
  877. }
  878. )
  879. } @strings;
  880. my $action = { actions => [{
  881. id => 'key',
  882. type => 'key',
  883. actions => \@acts,
  884. }]};
  885. return $self->general_action(%$action);
  886. }
  887. my $res = { 'command' => 'sendKeysToActiveElement' };
  888. my $params = {
  889. 'value' => \@strings,
  890. };
  891. return $self->_execute_command( $res, $params );
  892. }
  893. =head2 send_keys_to_alert
  894. Synonymous with send_keys_to_prompt
  895. =cut
  896. sub send_keys_to_alert {
  897. return shift->send_keys_to_prompt(@_);
  898. }
  899. =head2 send_keys_to_prompt
  900. Description:
  901. Sends keystrokes to a JavaScript prompt() dialog.
  902. Input:
  903. {string} keys to send
  904. Example:
  905. $driver->send_keys_to_prompt('hello world');
  906. or
  907. ok($driver->get_alert_text eq 'Please Input your name','prompt appears');
  908. $driver->send_keys_to_alert("Larry Wall");
  909. $driver->accept_alert;
  910. =cut
  911. sub send_keys_to_prompt {
  912. my ( $self, $keys ) = @_;
  913. my $res = { 'command' => 'sendKeysToPrompt' };
  914. my $params = { 'text' => $keys };
  915. return $self->_execute_command( $res, $params );
  916. }
  917. =head2 accept_alert
  918. Description:
  919. Accepts the currently displayed alert dialog. Usually, this is
  920. equivalent to clicking the 'OK' button in the dialog.
  921. Example:
  922. $driver->accept_alert;
  923. =cut
  924. sub accept_alert {
  925. my ($self) = @_;
  926. my $res = { 'command' => 'acceptAlert' };
  927. return $self->_execute_command($res);
  928. }
  929. =head2 dismiss_alert
  930. Description:
  931. Dismisses the currently displayed alert dialog. For comfirm()
  932. and prompt() dialogs, this is equivalent to clicking the
  933. 'Cancel' button. For alert() dialogs, this is equivalent to
  934. clicking the 'OK' button.
  935. Example:
  936. $driver->dismiss_alert;
  937. =cut
  938. sub dismiss_alert {
  939. my ($self) = @_;
  940. my $res = { 'command' => 'dismissAlert' };
  941. return $self->_execute_command($res);
  942. }
  943. =head2 general_action
  944. Provide an 'actions definition' hash to make webdriver use input devices.
  945. Given the spec for the structure of this data is 'non normative',
  946. it is left as an exercise to the reader what that means as to how to use this function.
  947. That said, it seems most of the data looks something like this:
  948. $driver->general_action( actions => [{
  949. type => 'pointer|key|none|somethingElseSuperSpecialDefinedByYourBrowserDriver',
  950. id => MUST be mouse|key|none|other. And by 'other' I mean anything else. The first 3 are 'special' in that they are used in the global actions queue.
  951. If you want say, another mouse action to execute in parallel to other mouse actions (to simulate multi-touch, for example), call your action 'otherMouseAction' or something.
  952. parameters => {
  953. someOption => "basically these are global parameters used by all steps in the forthcoming "action chain".
  954. },
  955. actions => [
  956. {
  957. type => "keyUp|KeyDown if key, pointerUp|pointerDown|pointerMove|pointerCancel if pointer, pause if any type",
  958. key => A raw keycode or character from the keyboard if this is a key event,
  959. duration => how many 'ticks' this action should take, you probably want this to be 0 all of the time unless you are evading Software debounce.
  960. button => what number button if you are using a pointer (this sounds terribly like it might be re-purposed to be a joypad in the future sometime)
  961. origin => Point of Origin if moving a pointer around
  962. x => unit vector to travel along x-axis if pointerMove event
  963. y => unit vector to travel along y-axis if pointerMove event
  964. },
  965. ...
  966. ]
  967. },
  968. ...
  969. ]
  970. )
  971. Only available on WebDriver3 capable selenium servers.
  972. If you have called any legacy shim, such as mouse_move_to_location() previously, your actions passed will be appended to the existing actions queue.
  973. Called with no arguments, it simply executes the existing action queue.
  974. If you are looking for pre-baked action chains that aren't currently part of L<Selenium::Remote::Driver>,
  975. consider L<Selenium::ActionChains>, which is shipped with this distribution instead.
  976. =cut
  977. sub general_action {
  978. my ($self,%action) = @_;
  979. _queue_action(%action);
  980. my $res = { 'command' => 'generalAction' };
  981. my $out = $self->_execute_command( $res, \%CURRENT_ACTION_CHAIN );
  982. %CURRENT_ACTION_CHAIN = ( actions => [] );
  983. return $out;
  984. }
  985. sub _queue_action {
  986. my (%action) = @_;
  987. if (ref $action{actions} eq 'ARRAY') {
  988. foreach my $live_action (@{$action{actions}}) {
  989. my $existing_action;
  990. foreach my $global_action (@{$CURRENT_ACTION_CHAIN{actions}}) {
  991. if ($global_action->{id} eq $live_action->{id}) {
  992. $existing_action = $global_action;
  993. last;
  994. }
  995. }
  996. if ($existing_action) {
  997. push(@{$existing_action->{actions}},@{$live_action->{actions}});
  998. } else {
  999. push(@{$CURRENT_ACTION_CHAIN{actions}},$live_action);
  1000. }
  1001. }
  1002. }
  1003. }
  1004. =head2 release_general_action
  1005. Nukes *all* input device state (modifier key up/down, pointer button up/down, pointer location, and other device state) from orbit.
  1006. Call if you forget to do a *Up event in your provided action chains, or just to save time.
  1007. Also clears the current actions queue.
  1008. Only available on WebDriver3 capable selenium servers.
  1009. =cut
  1010. sub release_general_action {
  1011. my ($self) = @_;
  1012. my $res = { 'command' => 'releaseGeneralAction' };
  1013. %CURRENT_ACTION_CHAIN = ( actions => [] );
  1014. return $self->_execute_command( $res );
  1015. }
  1016. =head2 mouse_move_to_location
  1017. Description:
  1018. Move the mouse by an offset of the specificed element. If no
  1019. element is specified, the move is relative to the current mouse
  1020. cursor. If an element is provided but no offset, the mouse will be
  1021. moved to the center of the element. If the element is not visible,
  1022. it will be scrolled into view.
  1023. Compatibility:
  1024. Due to limitations in the Webdriver 3 API, mouse movements have to be executed 'lazily' e.g. only right before a click() event occurs.
  1025. This is because there is no longer any persistent mouse location state; mouse movements are now totally atomic.
  1026. This has several problematic aspects; for one, I can't think of a way to both hover an element and then do another action relying on the element staying hover()ed,
  1027. Aside from using javascript workarounds.
  1028. Output:
  1029. STRING -
  1030. Usage:
  1031. # element - the element to move to. If not specified or is null, the offset is relative to current position of the mouse.
  1032. # 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.
  1033. # 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.
  1034. print $driver->mouse_move_to_location(element => e, xoffset => x, yoffset => y);
  1035. =cut
  1036. sub mouse_move_to_location {
  1037. my ( $self, %params ) = @_;
  1038. $params{element} = $params{element}{id} if exists $params{element};
  1039. if ($self->{is_wd3} && !(grep { $self->browser_name eq $_ } qw{chrome MicrosoftEdge}) ) {
  1040. my $origin = $params{element};
  1041. my $move_action = {
  1042. type => "pointerMove",
  1043. duration => 0,
  1044. x => $params{xoffset} // 0,
  1045. y => $params{yoffset} // 0,
  1046. };
  1047. $move_action->{origin} = {'element-6066-11e4-a52e-4f735466cecf' => $origin } if $origin;
  1048. _queue_action( actions => [{
  1049. type => "pointer",
  1050. id => 'mouse',
  1051. "parameters" => { "pointerType" => "mouse" },
  1052. actions => [$move_action],
  1053. }]);
  1054. return 1;
  1055. }
  1056. my $res = { 'command' => 'mouseMoveToLocation' };
  1057. return $self->_execute_command( $res, \%params );
  1058. }
  1059. =head2 move_to
  1060. Synonymous with mouse_move_to_location
  1061. =cut
  1062. sub move_to {
  1063. return shift->mouse_move_to_location(@_);
  1064. }
  1065. =head2 get_capabilities
  1066. Description:
  1067. Retrieve the capabilities of the specified session.
  1068. Output:
  1069. HASH of all the capabilities.
  1070. Usage:
  1071. my $capab = $driver->get_capabilities();
  1072. print Dumper($capab);
  1073. =cut
  1074. sub get_capabilities {
  1075. my $self = shift;
  1076. my $res = { 'command' => 'getCapabilities' };
  1077. return $self->_execute_command($res);
  1078. }
  1079. =head2 get_timeouts
  1080. Description:
  1081. Get the currently configured values (ms) for the page load, script and implicit timeouts.
  1082. Compatibility:
  1083. Only available on WebDriver3 enabled selenium servers.
  1084. Usage:
  1085. $driver->get_timeouts();
  1086. =cut
  1087. sub get_timeouts {
  1088. my $self = shift;
  1089. my $res = { 'command' => 'getTimeouts' };
  1090. return $self->_execute_command( $res, {} );
  1091. }
  1092. =head2 set_timeout
  1093. Description:
  1094. Configure the amount of time that a particular type of operation can execute
  1095. for before they are aborted and a |Timeout| error is returned to the client.
  1096. Input:
  1097. type - <STRING> - The type of operation to set the timeout for.
  1098. Valid values are:
  1099. "script" : for script timeouts,
  1100. "implicit" : for modifying the implicit wait timeout
  1101. "page load" : for setting a page load timeout.
  1102. ms - <NUMBER> - The amount of time, in milliseconds, that time-limited
  1103. commands are permitted to run.
  1104. Usage:
  1105. $driver->set_timeout('script', 1000);
  1106. =cut
  1107. sub set_timeout {
  1108. my ( $self, $type, $ms ) = @_;
  1109. if ( not defined $type ) {
  1110. croak "Expecting type";
  1111. }
  1112. $ms = _coerce_timeout_ms( $ms );
  1113. my $res = { 'command' => 'setTimeout' };
  1114. my $params = { 'type' => $type, 'ms' => $ms, $type => $ms };
  1115. return $self->_execute_command( $res, $params );
  1116. }
  1117. =head2 set_async_script_timeout
  1118. Description:
  1119. Set the amount of time, in milliseconds, that asynchronous scripts executed
  1120. by execute_async_script() are permitted to run before they are
  1121. aborted and a |Timeout| error is returned to the client.
  1122. Input:
  1123. ms - <NUMBER> - The amount of time, in milliseconds, that time-limited
  1124. commands are permitted to run.
  1125. Usage:
  1126. $driver->set_async_script_timeout(1000);
  1127. =cut
  1128. sub set_async_script_timeout {
  1129. my ( $self, $ms ) = @_;
  1130. $ms = _coerce_timeout_ms( $ms );
  1131. my $res = { 'command' => 'setAsyncScriptTimeout' };
  1132. my $params = { 'ms' => $ms };
  1133. return $self->_execute_command( $res, $params );
  1134. }
  1135. =head2 set_implicit_wait_timeout
  1136. Description:
  1137. Set the amount of time the driver should wait when searching for elements.
  1138. When searching for a single element, the driver will poll the page until
  1139. an element is found or the timeout expires, whichever occurs first.
  1140. When searching for multiple elements, the driver should poll the page until
  1141. at least one element is found or the timeout expires, at which point it
  1142. will return an empty list. If this method is never called, the driver will
  1143. default to an implicit wait of 0ms.
  1144. This is exactly equivalent to calling L</set_timeout> with a type
  1145. arg of C<"implicit">.
  1146. Input:
  1147. Time in milliseconds.
  1148. Output:
  1149. Server Response Hash with no data returned back from the server.
  1150. Usage:
  1151. $driver->set_implicit_wait_timeout(10);
  1152. =cut
  1153. sub set_implicit_wait_timeout {
  1154. my ( $self, $ms ) = @_;
  1155. $ms = _coerce_timeout_ms( $ms );
  1156. my $res = { 'command' => 'setImplicitWaitTimeout' };
  1157. my $params = { 'ms' => $ms };
  1158. return $self->_execute_command( $res, $params );
  1159. }
  1160. =head2 pause
  1161. Description:
  1162. Pause execution for a specified interval of milliseconds.
  1163. Usage:
  1164. $driver->pause(10000); # 10 second delay
  1165. $driver->pause(); # 1 second delay default
  1166. DEPRECATED: consider using Time::HiRes instead.
  1167. =cut
  1168. sub pause {
  1169. my $self = shift;
  1170. my $timeout = ( shift // 1000 ) * 1000;
  1171. usleep($timeout);
  1172. }
  1173. =head2 close
  1174. Description:
  1175. Close the current window.
  1176. Usage:
  1177. $driver->close();
  1178. or
  1179. #close a popup window
  1180. my $handles = $driver->get_window_handles;
  1181. $driver->switch_to_window($handles->[1]);
  1182. $driver->close();
  1183. $driver->switch_to_window($handles->[0]);
  1184. =cut
  1185. sub close {
  1186. my $self = shift;
  1187. my $res = { 'command' => 'close' };
  1188. $self->_execute_command($res);
  1189. }
  1190. =head2 quit
  1191. Description:
  1192. DELETE the session, closing open browsers. We will try to call
  1193. this on our down when we get destroyed, but in the event that we
  1194. are demolished during global destruction, we will not be able to
  1195. close the browser. For your own unattended and/or complicated
  1196. tests, we recommend explicitly calling quit to make sure you're
  1197. not leaving orphan browsers around.
  1198. Note that as a Moo class, we use a subroutine called DEMOLISH that
  1199. takes the place of DESTROY; for more information, see
  1200. https://metacpan.org/pod/Moo#DEMOLISH.
  1201. Usage:
  1202. $driver->quit();
  1203. =cut
  1204. sub quit {
  1205. my $self = shift;
  1206. my $res = { 'command' => 'quit' };
  1207. $self->_execute_command($res);
  1208. $self->session_id(undef);
  1209. }
  1210. =head2 get_current_window_handle
  1211. Description:
  1212. Retrieve the current window handle.
  1213. Output:
  1214. STRING - the window handle
  1215. Usage:
  1216. print $driver->get_current_window_handle();
  1217. =cut
  1218. sub get_current_window_handle {
  1219. my $self = shift;
  1220. my $res = { 'command' => 'getCurrentWindowHandle' };
  1221. return $self->_execute_command($res);
  1222. }
  1223. =head2 get_window_handles
  1224. Description:
  1225. Retrieve the list of window handles used in the session.
  1226. Output:
  1227. ARRAY of STRING - list of the window handles
  1228. Usage:
  1229. print Dumper $driver->get_window_handles;
  1230. or
  1231. # get popup, close, then back
  1232. my $handles = $driver->get_window_handles;
  1233. $driver->switch_to_window($handles->[1]);
  1234. $driver->close;
  1235. $driver->switch_to_window($handles->[0]);
  1236. =cut
  1237. sub get_window_handles {
  1238. my $self = shift;
  1239. my $res = { 'command' => 'getWindowHandles' };
  1240. return $self->_execute_command($res);
  1241. }
  1242. =head2 get_window_size
  1243. Description:
  1244. Retrieve the window size
  1245. Compatibility:
  1246. The ability to get the size of arbitrary handles by passing input only exists in WebDriver2.
  1247. You will have to switch to the window first going forward.
  1248. Input:
  1249. STRING - <optional> - window handle (default is 'current' window)
  1250. Output:
  1251. HASH - containing keys 'height' & 'width'
  1252. Usage:
  1253. my $window_size = $driver->get_window_size();
  1254. print $window_size->{'height'}, $window_size->('width');
  1255. =cut
  1256. sub get_window_size {
  1257. my ( $self, $window ) = @_;
  1258. $window = ( defined $window ) ? $window : 'current';
  1259. my $res = { 'command' => 'getWindowSize', 'window_handle' => $window };
  1260. $res = {'command' => 'getWindowRect', handle => $window } if $self->{is_wd3} && $self->browser_name ne 'chrome';
  1261. return $self->_execute_command($res);
  1262. }
  1263. =head2 get_window_position
  1264. Description:
  1265. Retrieve the window position
  1266. Compatibility:
  1267. The ability to get the size of arbitrary handles by passing input only exists in WebDriver2.
  1268. You will have to switch to the window first going forward.
  1269. Input:
  1270. STRING - <optional> - window handle (default is 'current' window)
  1271. Output:
  1272. HASH - containing keys 'x' & 'y'
  1273. Usage:
  1274. my $window_size = $driver->get_window_position();
  1275. print $window_size->{'x'}, $window_size->('y');
  1276. =cut
  1277. sub get_window_position {
  1278. my ( $self, $window ) = @_;
  1279. $window = ( defined $window ) ? $window : 'current';
  1280. my $res = { 'command' => 'getWindowPosition', 'window_handle' => $window };
  1281. $res = {'command' => 'getWindowRect', handle => $window } if $self->{is_wd3} && $self->browser_name ne 'chrome';
  1282. return $self->_execute_command($res);
  1283. }
  1284. =head2 get_current_url
  1285. Description:
  1286. Retrieve the url of the current page
  1287. Output:
  1288. STRING - url
  1289. Usage:
  1290. print $driver->get_current_url();
  1291. =cut
  1292. sub get_current_url {
  1293. my $self = shift;
  1294. my $res = { 'command' => 'getCurrentUrl' };
  1295. return $self->_execute_command($res);
  1296. }
  1297. =head2 navigate
  1298. Description:
  1299. Navigate to a given url. This is same as get() method.
  1300. Input:
  1301. STRING - url
  1302. Usage:
  1303. $driver->navigate('http://www.google.com');
  1304. =cut
  1305. sub navigate {
  1306. my ( $self, $url ) = @_;
  1307. $self->get($url);
  1308. }
  1309. =head2 get
  1310. Description:
  1311. Navigate to a given url
  1312. Input:
  1313. STRING - url
  1314. Usage:
  1315. $driver->get('http://www.google.com');
  1316. =cut
  1317. sub get {
  1318. my ( $self, $url ) = @_;
  1319. if ($self->has_base_url && $url !~ m|://|) {
  1320. $url =~ s|^/||;
  1321. $url = $self->base_url . "/" . $url;
  1322. }
  1323. my $res = { 'command' => 'get' };
  1324. my $params = { 'url' => $url };
  1325. return $self->_execute_command( $res, $params );
  1326. }
  1327. =head2 get_title
  1328. Description:
  1329. Get the current page title
  1330. Output:
  1331. STRING - Page title
  1332. Usage:
  1333. print $driver->get_title();
  1334. =cut
  1335. sub get_title {
  1336. my $self = shift;
  1337. my $res = { 'command' => 'getTitle' };
  1338. return $self->_execute_command($res);
  1339. }
  1340. =head2 go_back
  1341. Description:
  1342. Equivalent to hitting the back button on the browser.
  1343. Usage:
  1344. $driver->go_back();
  1345. =cut
  1346. sub go_back {
  1347. my $self = shift;
  1348. my $res = { 'command' => 'goBack' };
  1349. return $self->_execute_command($res);
  1350. }
  1351. =head2 go_forward
  1352. Description:
  1353. Equivalent to hitting the forward button on the browser.
  1354. Usage:
  1355. $driver->go_forward();
  1356. =cut
  1357. sub go_forward {
  1358. my $self = shift;
  1359. my $res = { 'command' => 'goForward' };
  1360. return $self->_execute_command($res);
  1361. }
  1362. =head2 refresh
  1363. Description:
  1364. Reload the current page.
  1365. Usage:
  1366. $driver->refresh();
  1367. =cut
  1368. sub refresh {
  1369. my $self = shift;
  1370. my $res = { 'command' => 'refresh' };
  1371. return $self->_execute_command($res);
  1372. }
  1373. =head2 has_javascript
  1374. Description:
  1375. returns true if javascript is enabled in the driver.
  1376. Compatibility:
  1377. Can't be false on WebDriver 3.
  1378. Usage:
  1379. if ($driver->has_javascript) { ...; }
  1380. =cut
  1381. sub has_javascript {
  1382. my $self = shift;
  1383. return int($self->javascript);
  1384. }
  1385. =head2 execute_async_script
  1386. Description:
  1387. Inject a snippet of JavaScript into the page for execution in the context
  1388. of the currently selected frame. The executed script is assumed to be
  1389. asynchronous and must signal that is done by invoking the provided
  1390. callback, which is always provided as the final argument to the function.
  1391. The value to this callback will be returned to the client.
  1392. Asynchronous script commands may not span page loads. If an unload event
  1393. is fired while waiting for a script result, an error should be returned
  1394. to the client.
  1395. Input: 2 (1 optional)
  1396. Required:
  1397. STRING - Javascript to execute on the page
  1398. Optional:
  1399. ARRAY - list of arguments that need to be passed to the script.
  1400. Output:
  1401. {*} - Varied, depending on the type of result expected back from the script.
  1402. Usage:
  1403. my $script = q{
  1404. var arg1 = arguments[0];
  1405. var callback = arguments[arguments.length-1];
  1406. var elem = window.document.findElementById(arg1);
  1407. callback(elem);
  1408. };
  1409. my $elem = $driver->execute_async_script($script,'myid');
  1410. $elem->click;
  1411. =cut
  1412. sub execute_async_script {
  1413. my ( $self, $script, @args ) = @_;
  1414. if ( $self->has_javascript ) {
  1415. if ( not defined $script ) {
  1416. croak 'No script provided';
  1417. }
  1418. my $res = { 'command' => 'executeAsyncScript' . $self->_execute_script_suffix};
  1419. # Check the args array if the elem obj is provided & replace it with
  1420. # JSON representation
  1421. for ( my $i = 0; $i < @args; $i++ ) {
  1422. if ( Scalar::Util::blessed( $args[$i] )
  1423. and $args[$i]->isa('Selenium::Remote::WebElement') )
  1424. {
  1425. if ($self->{is_wd3}) {
  1426. $args[$i] = { 'element-6066-11e4-a52e-4f735466cecf' => ( $args[$i] )->{id} };
  1427. $args[$i]->{ELEMENT} = $args[$i]->{id} if $self->browser_name eq 'chrome'; #XXX sometimes they prefer the latter/prior
  1428. } else {
  1429. $args[$i] = { 'ELEMENT' => ( $args[$i] )->{id} };
  1430. }
  1431. }
  1432. }
  1433. my $params = { 'script' => $script, 'args' => \@args };
  1434. my $ret = $self->_execute_command( $res, $params );
  1435. # replace any ELEMENTS with WebElement
  1436. if ( ref($ret)
  1437. and ( ref($ret) eq 'HASH' )
  1438. and $self->_looks_like_element($ret) )
  1439. {
  1440. $ret = $self->webelement_class->new(
  1441. id => $ret,
  1442. driver => $self
  1443. );
  1444. }
  1445. return $ret;
  1446. }
  1447. else {
  1448. croak 'Javascript is not enabled on remote driver instance.';
  1449. }
  1450. }
  1451. =head2 execute_script
  1452. Description:
  1453. Inject a snippet of JavaScript into the page and return its result.
  1454. WebElements that should be passed to the script as an argument should be
  1455. specified in the arguments array as WebElement object. Likewise,
  1456. any WebElements in the script result will be returned as WebElement object.
  1457. Input: 2 (1 optional)
  1458. Required:
  1459. STRING - Javascript to execute on the page
  1460. Optional:
  1461. ARRAY - list of arguments that need to be passed to the script.
  1462. Output:
  1463. {*} - Varied, depending on the type of result expected back from the script.
  1464. Usage:
  1465. my $script = q{
  1466. var arg1 = arguments[0];
  1467. var elem = window.document.findElementById(arg1);
  1468. return elem;
  1469. };
  1470. my $elem = $driver->execute_script($script,'myid');
  1471. $elem->click;
  1472. =cut
  1473. sub execute_script {
  1474. my ( $self, $script, @args ) = @_;
  1475. if ( $self->has_javascript ) {
  1476. if ( not defined $script ) {
  1477. croak 'No script provided';
  1478. }
  1479. my $res = { 'command' => 'executeScript' . $self->_execute_script_suffix };
  1480. # Check the args array if the elem obj is provided & replace it with
  1481. # JSON representation
  1482. for ( my $i = 0; $i < @args; $i++ ) {
  1483. if ( Scalar::Util::blessed( $args[$i] )
  1484. and $args[$i]->isa('Selenium::Remote::WebElement') )
  1485. {
  1486. if ($self->{is_wd3}) {
  1487. $args[$i] = { 'element-6066-11e4-a52e-4f735466cecf' => ( $args[$i] )->{id} };
  1488. $args[$i]->{ELEMENT} = $args[$i]->{id} if $self->browser_name eq 'chrome'; #XXX sometimes they prefer the latter/prior
  1489. } else {
  1490. $args[$i] = { 'ELEMENT' => ( $args[$i] )->{id} };
  1491. }
  1492. }
  1493. }
  1494. my $params = { 'script' => $script, 'args' => [@args] };
  1495. my $ret = $self->_execute_command( $res, $params );
  1496. return $self->_convert_to_webelement($ret);
  1497. }
  1498. else {
  1499. croak 'Javascript is not enabled on remote driver instance.';
  1500. }
  1501. }
  1502. # _looks_like_element
  1503. # An internal method to check if a return value might be an element
  1504. sub _looks_like_element {
  1505. my ($self, $maybe_element) = @_;
  1506. return (exists $maybe_element->{ELEMENT}
  1507. or exists $maybe_element->{'element-6066-11e4-a52e-4f735466cecf'});
  1508. }
  1509. # _convert_to_webelement
  1510. # An internal method used to traverse a data structure
  1511. # and convert any ELEMENTS with WebElements
  1512. sub _convert_to_webelement {
  1513. my ( $self, $ret ) = @_;
  1514. if ( ref($ret) and ( ref($ret) eq 'HASH' ) ) {
  1515. if ( $self->_looks_like_element($ret) ) {
  1516. # replace an ELEMENT with WebElement
  1517. return $self->webelement_class->new(
  1518. id => $ret,
  1519. driver => $self
  1520. );
  1521. }
  1522. my %hash;
  1523. foreach my $key ( keys %$ret ) {
  1524. $hash{$key} = $self->_convert_to_webelement( $ret->{$key} );
  1525. }
  1526. return \%hash;
  1527. }
  1528. if ( ref($ret) and ( ref($ret) eq 'ARRAY' ) ) {
  1529. my @array = map { $self->_convert_to_webelement($_) } @$ret;
  1530. return \@array;
  1531. }
  1532. return $ret;
  1533. }
  1534. =head2 screenshot
  1535. Description:
  1536. Get a screenshot of the current page as a base64 encoded image.
  1537. Output:
  1538. STRING - base64 encoded image
  1539. Usage:
  1540. print $driver->screenshot();
  1541. To conveniently write the screenshot to a file, see L</capture_screenshot>.
  1542. =cut
  1543. sub screenshot {
  1544. my ($self) = @_;
  1545. my $res = { 'command' => 'screenshot' };
  1546. return $self->_execute_command($res);
  1547. }
  1548. =head2 capture_screenshot
  1549. Description:
  1550. Capture a screenshot and save as a PNG to provided file name.
  1551. (The method is compatible with the WWW::Selenium method of the same name)
  1552. Output:
  1553. TRUE - (Screenshot is written to file)
  1554. Usage:
  1555. $driver->capture_screenshot($filename);
  1556. =cut
  1557. sub capture_screenshot {
  1558. my ( $self, $filename ) = @_;
  1559. croak '$filename is required' unless $filename;
  1560. open( my $fh, '>', $filename );
  1561. binmode $fh;
  1562. print $fh MIME::Base64::decode_base64( $self->screenshot() );
  1563. CORE::close $fh;
  1564. return 1;
  1565. }
  1566. =head2 available_engines
  1567. Description:
  1568. List all available engines on the machine. To use an engine, it has to be present in this list.
  1569. Compatibility:
  1570. Does not appear to be available on Webdriver3 enabled selenium servers.
  1571. Output:
  1572. {Array.<string>} A list of available engines
  1573. Usage:
  1574. print Dumper $driver->available_engines;
  1575. =cut
  1576. #TODO emulate behavior on wd3?
  1577. #grep { eval { Selenium::Remote::Driver->new( browser => $_ ) } } (qw{firefox MicrosoftEdge chrome opera safari htmlunit iphone phantomjs},'internet_explorer');
  1578. #might do the trick
  1579. sub available_engines {
  1580. my ($self) = @_;
  1581. my $res = { 'command' => 'availableEngines' };
  1582. return $self->_execute_command($res);
  1583. }
  1584. =head2 switch_to_frame
  1585. Description:
  1586. Change focus to another frame on the page. If the frame ID is null, the
  1587. server will switch to the page's default content. You can also switch to a
  1588. WebElement, for e.g. you can find an iframe using find_element & then
  1589. provide that as an input to this method. Also see e.g.
  1590. Input: 1
  1591. Required:
  1592. {STRING | NUMBER | NULL | WebElement} - ID of the frame which can be one of the three
  1593. mentioned.
  1594. Usage:
  1595. $driver->switch_to_frame('frame_1');
  1596. or
  1597. $driver->switch_to_frame($driver->find_element('iframe', 'tag_name'));
  1598. =cut
  1599. sub switch_to_frame {
  1600. my ( $self, $id ) = @_;
  1601. return $self->switch_to_parent_frame() if ($self->{is_wd3} && !defined($id));
  1602. my $json_null = JSON::null;
  1603. my $params;
  1604. $id = ( defined $id ) ? $id : $json_null;
  1605. my $res = { 'command' => 'switchToFrame' };
  1606. if ( ref $id eq $self->webelement_class ) {
  1607. if ($self->{is_wd3} && $self->browser_name ne 'chrome') {
  1608. $params = { 'id' => { 'element-6066-11e4-a52e-4f735466cecf' => $id->{'id'} } };
  1609. } else {
  1610. $params = { 'id' => { 'ELEMENT' => $id->{'id'} } };
  1611. }
  1612. }
  1613. else {
  1614. $params = { 'id' => $id };
  1615. }
  1616. return $self->_execute_command( $res, $params );
  1617. }
  1618. =head2 switch_to_parent_frame
  1619. Webdriver 3 equivalent of calling switch_to_frame with no arguments (e.g. NULL frame).
  1620. This is actually called in that case, supposing you are using WD3 capable servers now.
  1621. =cut
  1622. sub switch_to_parent_frame {
  1623. my ( $self ) = @_;
  1624. my $res = { 'command' => 'switchToParentFrame' };
  1625. return $self->_execute_command( $res );
  1626. }
  1627. =head2 switch_to_window
  1628. Description:
  1629. Change focus to another window. The window to change focus to may
  1630. be specified by its server assigned window handle, or by the value
  1631. of the page's window.name attribute.
  1632. If you wish to use the window name as the target, you'll need to
  1633. have set C<window.name> on the page either in app code or via
  1634. L</execute_script>, or pass a name as the second argument to the
  1635. C<window.open()> function when opening the new window. Note that
  1636. the window name used here has nothing to do with the window title,
  1637. or the C<< <title> >> element on the page.
  1638. Otherwise, use L</get_window_handles> and select a
  1639. Webdriver-generated handle from the output of that function.
  1640. Input: 1
  1641. Required:
  1642. STRING - Window handle or the Window name
  1643. Usage:
  1644. $driver->switch_to_window('MY Homepage');
  1645. or
  1646. # close a popup window and switch back
  1647. my $handles = $driver->get_window_handles;
  1648. $driver->switch_to_window($handles->[1]);
  1649. $driver->close;
  1650. $driver->switch_to_window($handles->[0]);
  1651. =cut
  1652. sub switch_to_window {
  1653. my ( $self, $name ) = @_;
  1654. if ( not defined $name ) {
  1655. return 'Window name not provided';
  1656. }
  1657. my $res = { 'command' => 'switchToWindow' };
  1658. my $params = { 'name' => $name, 'handle' => $name };
  1659. return $self->_execute_command( $res, $params );
  1660. }
  1661. =head2 set_window_position
  1662. Description:
  1663. Set the position (on screen) where you want your browser to be displayed.
  1664. Compatibility:
  1665. In webDriver 3 enabled selenium servers, you may only operate on the focused window.
  1666. As such, the window handle argument below will be ignored in this context.
  1667. Input:
  1668. INT - x co-ordinate
  1669. INT - y co-ordinate
  1670. STRING - <optional> - window handle (default is 'current' window)
  1671. Output:
  1672. BOOLEAN - Success or failure
  1673. Usage:
  1674. $driver->set_window_position(50, 50);
  1675. =cut
  1676. sub set_window_position {
  1677. my ( $self, $x, $y, $window ) = @_;
  1678. $window = ( defined $window ) ? $window : 'current';
  1679. if ( not defined $x and not defined $y ) {
  1680. croak "X & Y co-ordinates are required";
  1681. }
  1682. my $res = { 'command' => 'setWindowPosition', 'window_handle' => $window };
  1683. my $params = { 'x' => $x, 'y' => $y };
  1684. if ( $self->{is_wd3} && $self->browser_name ne 'chrome') {
  1685. $res = {'command' => 'setWindowRect', handle => $window };
  1686. }
  1687. my $ret = $self->_execute_command( $res, $params );
  1688. return $ret ? 1 : 0;
  1689. }
  1690. =head2 set_window_size
  1691. Description:
  1692. Set the size of the browser window
  1693. Compatibility:
  1694. In webDriver 3 enabled selenium servers, you may only operate on the focused window.
  1695. As such, the window handle argument below will be ignored in this context.
  1696. Input:
  1697. INT - height of the window
  1698. INT - width of the window
  1699. STRING - <optional> - window handle (default is 'current' window)
  1700. Output:
  1701. BOOLEAN - Success or failure
  1702. Usage:
  1703. $driver->set_window_size(640, 480);
  1704. =cut
  1705. sub set_window_size {
  1706. my ( $self, $height, $width, $window ) = @_;
  1707. $window = ( defined $window ) ? $window : 'current';
  1708. if ( not defined $height and not defined $width ) {
  1709. croak "height & width of browser are required";
  1710. }
  1711. $height += 0;
  1712. $width += 0;
  1713. my $res = { 'command' => 'setWindowSize', 'window_handle' => $window };
  1714. my $params = { 'height' => $height, 'width' => $width };
  1715. if ( $self->{is_wd3} && $self->browser_name ne 'chrome') {
  1716. $res = {'command' => 'setWindowRect', handle => $window };
  1717. }
  1718. my $ret = $self->_execute_command( $res, $params );
  1719. return $ret ? 1 : 0;
  1720. }
  1721. =head2 maximize_window
  1722. Description:
  1723. Maximizes the browser window
  1724. Compatibility:
  1725. In webDriver 3 enabled selenium servers, you may only operate on the focused window.
  1726. As such, the window handle argument below will be ignored in this context.
  1727. Input:
  1728. STRING - <optional> - window handle (default is 'current' window)
  1729. Output:
  1730. BOOLEAN - Success or failure
  1731. Usage:
  1732. $driver->maximize_window();
  1733. =cut
  1734. sub maximize_window {
  1735. my ( $self, $window ) = @_;
  1736. if ($self->{is_wd3} && $self->browser_name eq 'chrome') {
  1737. return $self->execute_script(qq{window.resizeTo(screen.availWidth,screen.availHeight); return 1;});
  1738. }
  1739. $window = ( defined $window ) ? $window : 'current';
  1740. my $res = { 'command' => 'maximizeWindow', 'window_handle' => $window };
  1741. my $ret = $self->_execute_command( $res );
  1742. return $ret ? 1 : 0;
  1743. }
  1744. =head2 minimize_window
  1745. Description:
  1746. Minimizes the currently focused browser window (webdriver3 only)
  1747. Output:
  1748. BOOLEAN - Success or failure
  1749. Usage:
  1750. $driver->minimize_window();
  1751. =cut
  1752. sub minimize_window {
  1753. my ( $self, $window ) = @_;
  1754. $window = ( defined $window ) ? $window : 'current';
  1755. my $res = { 'command' => 'minimizeWindow', 'window_handle' => $window };
  1756. my $ret = $self->_execute_command( $res );
  1757. return $ret ? 1 : 0;
  1758. }
  1759. =head2 fullscreen_window
  1760. Description:
  1761. Fullscreens the currently focused browser window (webdriver3 only)
  1762. Output:
  1763. BOOLEAN - Success or failure
  1764. Usage:
  1765. $driver->fullscreen_window();
  1766. =cut
  1767. sub fullscreen_window {
  1768. my ( $self, $window ) = @_;
  1769. $window = ( defined $window ) ? $window : 'current';
  1770. my $res = { 'command' => 'fullscreenWindow', 'window_handle' => $window };
  1771. my $ret = $self->_execute_command( $res );
  1772. return $ret ? 1 : 0;
  1773. }
  1774. =head2 get_all_cookies
  1775. Description:
  1776. Retrieve all cookies visible to the current page. Each cookie will be
  1777. returned as a HASH reference with the following keys & their value types:
  1778. 'name' - STRING
  1779. 'value' - STRING
  1780. 'path' - STRING
  1781. 'domain' - STRING
  1782. 'secure' - BOOLEAN
  1783. Output:
  1784. ARRAY of HASHES - list of all the cookie hashes
  1785. Usage:
  1786. print Dumper($driver->get_all_cookies());
  1787. =cut
  1788. sub get_all_cookies {
  1789. my ($self) = @_;
  1790. my $res = { 'command' => 'getAllCookies' };
  1791. return $self->_execute_command($res);
  1792. }
  1793. =head2 add_cookie
  1794. Description:
  1795. Set a cookie on the domain.
  1796. Input: 2 (4 optional)
  1797. Required:
  1798. 'name' - STRING
  1799. 'value' - STRING
  1800. Optional:
  1801. 'path' - STRING
  1802. 'domain' - STRING
  1803. 'secure' - BOOLEAN - default false.
  1804. 'httponly' - BOOLEAN - default false.
  1805. 'expiry' - TIME_T - default 20 years in the future
  1806. Usage:
  1807. $driver->add_cookie('foo', 'bar', '/', '.google.com', 0, 1)
  1808. =cut
  1809. sub add_cookie {
  1810. my ( $self, $name, $value, $path, $domain, $secure, $httponly, $expiry ) = @_;
  1811. if ( ( not defined $name )
  1812. || ( not defined $value ))
  1813. {
  1814. croak "Missing parameters";
  1815. }
  1816. my $res = { 'command' => 'addCookie' };
  1817. my $json_false = JSON::false;
  1818. my $json_true = JSON::true;
  1819. $secure = ( defined $secure && $secure ) ? $json_true : $json_false;
  1820. my $params = {
  1821. 'cookie' => {
  1822. 'name' => $name,
  1823. 'value' => $value,
  1824. 'path' => $path,
  1825. 'secure' => $secure,
  1826. }
  1827. };
  1828. $params->{cookie}->{domain} = $domain if $domain;
  1829. $params->{cookie}->{'httponly'} = $httponly if $httponly;
  1830. $params->{cookie}->{'expiry'} = $expiry if $expiry;
  1831. return $self->_execute_command( $res, $params );
  1832. }
  1833. =head2 delete_all_cookies
  1834. Description:
  1835. Delete all cookies visible to the current page.
  1836. Usage:
  1837. $driver->delete_all_cookies();
  1838. =cut
  1839. sub delete_all_cookies {
  1840. my ($self) = @_;
  1841. my $res = { 'command' => 'deleteAllCookies' };
  1842. return $self->_execute_command($res);
  1843. }
  1844. =head2 get_cookie_named
  1845. Basically get only the cookie with the provided name.
  1846. Probably preferable to pick it out of the list unless you expect a *really* long list.
  1847. Input:
  1848. Cookie Name - STRING
  1849. Returns cookie definition hash, much like the elements in get_all_cookies();
  1850. Compatibility:
  1851. Only available on webdriver3 enabled selenium servers.
  1852. =cut
  1853. sub get_cookie_named {
  1854. my ( $self, $cookie_name ) = @_;
  1855. my $res = { 'command' => 'getCookieNamed', 'name' => $cookie_name };
  1856. return $self->_execute_command($res);
  1857. }
  1858. =head2 delete_cookie_named
  1859. Description:
  1860. Delete the cookie with the given name. This command will be a no-op if there
  1861. is no such cookie visible to the current page.
  1862. Input: 1
  1863. Required:
  1864. STRING - name of cookie to delete
  1865. Usage:
  1866. $driver->delete_cookie_named('foo');
  1867. =cut
  1868. sub delete_cookie_named {
  1869. my ( $self, $cookie_name ) = @_;
  1870. if ( not defined $cookie_name ) {
  1871. croak "Cookie name not provided";
  1872. }
  1873. my $res = { 'command' => 'deleteCookieNamed', 'name' => $cookie_name };
  1874. return $self->_execute_command($res);
  1875. }
  1876. =head2 get_page_source
  1877. Description:
  1878. Get the current page source.
  1879. Output:
  1880. STRING - The page source.
  1881. Usage:
  1882. print $driver->get_page_source();
  1883. =cut
  1884. sub get_page_source {
  1885. my ($self) = @_;
  1886. my $res = { 'command' => 'getPageSource' };
  1887. return $self->_execute_command($res);
  1888. }
  1889. =head2 find_element
  1890. Description:
  1891. Search for an element on the page, starting from the document
  1892. root. The located element will be returned as a WebElement
  1893. object. If the element cannot be found, we will CROAK, killing
  1894. your script. If you wish for a warning instead, use the
  1895. parameterized version of the finders:
  1896. find_element_by_class
  1897. find_element_by_class_name
  1898. find_element_by_css
  1899. find_element_by_id
  1900. find_element_by_link
  1901. find_element_by_link_text
  1902. find_element_by_name
  1903. find_element_by_partial_link_text
  1904. find_element_by_tag_name
  1905. find_element_by_xpath
  1906. These functions all take a single STRING argument: the locator
  1907. search target of the element you want. If the element is found, we
  1908. will receive a WebElement. Otherwise, we will return 0. Note that
  1909. invoking methods on 0 will of course kill your script.
  1910. Input: 2 (1 optional)
  1911. Required:
  1912. STRING - The search target.
  1913. Optional:
  1914. STRING - Locator scheme to use to search the element, available schemes:
  1915. {class, class_name, css, id, link, link_text, partial_link_text,
  1916. tag_name, name, xpath}
  1917. Defaults to 'xpath' if not configured global during instantiation.
  1918. Output:
  1919. Selenium::Remote::WebElement - WebElement Object
  1920. (This could be a subclass of L<Selenium::Remote::WebElement> if C<webelement_class> was set.
  1921. Usage:
  1922. $driver->find_element("//input[\@name='q']");
  1923. =cut
  1924. sub find_element {
  1925. my ( $self, $query, $method ) = @_;
  1926. if ( not defined $query ) {
  1927. croak 'Search string to find element not provided.';
  1928. }
  1929. my $res = { 'command' => 'findElement' };
  1930. my $params = $self->_build_find_params($method, $query);
  1931. my $ret_data = eval { $self->_execute_command( $res, $params ); };
  1932. if ($@) {
  1933. if ( $@
  1934. =~ /(An element could not be located on the page using the given search parameters)/
  1935. ) {
  1936. # give details on what element wasn't found
  1937. $@ = "$1: $query,$params->{using}";
  1938. local @CARP_NOT = ( "Selenium::Remote::Driver", @CARP_NOT );
  1939. croak $@;
  1940. }
  1941. else {
  1942. # re throw if the exception wasn't what we expected
  1943. die $@;
  1944. }
  1945. }
  1946. return $self->webelement_class->new(
  1947. id => $ret_data,
  1948. driver => $self
  1949. );
  1950. }
  1951. =head2 find_elements
  1952. Description:
  1953. Search for multiple elements on the page, starting from the document root.
  1954. The located elements will be returned as an array of WebElement object.
  1955. Input: 2 (1 optional)
  1956. Required:
  1957. STRING - The search target.
  1958. Optional:
  1959. STRING - Locator scheme to use to search the element, available schemes:
  1960. {class, class_name, css, id, link, link_text, partial_link_text,
  1961. tag_name, name, xpath}
  1962. Defaults to 'xpath' if not configured global during instantiation.
  1963. Output:
  1964. ARRAY or ARRAYREF of WebElement Objects
  1965. Usage:
  1966. $driver->find_elements("//input");
  1967. =cut
  1968. sub find_elements {
  1969. my ( $self, $query, $method ) = @_;
  1970. if ( not defined $query ) {
  1971. croak 'Search string to find element not provided.';
  1972. }
  1973. my $res = { 'command' => 'findElements' };
  1974. my $params = $self->_build_find_params($method, $query);
  1975. my $ret_data = eval { $self->_execute_command( $res, $params ); };
  1976. if ($@) {
  1977. if ( $@
  1978. =~ /(An element could not be located on the page using the given search parameters)/
  1979. ) {
  1980. # give details on what element wasn't found
  1981. $@ = "$1: $query,$params->{using}";
  1982. local @CARP_NOT = ( "Selenium::Remote::Driver", @CARP_NOT );
  1983. croak $@;
  1984. }
  1985. else {
  1986. # re throw if the exception wasn't what we expected
  1987. die $@;
  1988. }
  1989. }
  1990. my $elem_obj_arr = [];
  1991. foreach (@$ret_data) {
  1992. push(
  1993. @$elem_obj_arr,
  1994. $self->webelement_class->new(
  1995. id => $_,
  1996. driver => $self
  1997. )
  1998. );
  1999. }
  2000. return wantarray? @{$elem_obj_arr} : $elem_obj_arr ;
  2001. }
  2002. =head2 find_child_element
  2003. Description:
  2004. Search for an element on the page, starting from the identified element. The
  2005. located element will be returned as a WebElement object.
  2006. Input: 3 (1 optional)
  2007. Required:
  2008. Selenium::Remote::WebElement - WebElement object from where you want to
  2009. start searching.
  2010. STRING - The search target. (Do not use a double whack('//')
  2011. in an xpath to search for a child element
  2012. ex: '//option[@id="something"]'
  2013. instead use a dot whack ('./')
  2014. ex: './option[@id="something"]')
  2015. Optional:
  2016. STRING - Locator scheme to use to search the element, available schemes:
  2017. {class, class_name, css, id, link, link_text, partial_link_text,
  2018. tag_name, name, xpath}
  2019. Defaults to 'xpath' if not configured global during instantiation.
  2020. Output:
  2021. WebElement Object
  2022. Usage:
  2023. my $elem1 = $driver->find_element("//select[\@name='ned']");
  2024. # note the usage of ./ when searching for a child element instead of //
  2025. my $child = $driver->find_child_element($elem1, "./option[\@value='es_ar']");
  2026. =cut
  2027. sub find_child_element {
  2028. my ( $self, $elem, $query, $method ) = @_;
  2029. if ( ( not defined $elem ) || ( not defined $query ) ) {
  2030. croak "Missing parameters";
  2031. }
  2032. my $res = { 'command' => 'findChildElement', 'id' => $elem->{id} };
  2033. my $params = $self->_build_find_params($method, $query);
  2034. my $ret_data = eval { $self->_execute_command( $res, $params ); };
  2035. if ($@) {
  2036. if ( $@
  2037. =~ /(An element could not be located on the page using the given search parameters)/
  2038. ) {
  2039. # give details on what element wasn't found
  2040. $@ = "$1: $query,$params->{using}";
  2041. local @CARP_NOT = ( "Selenium::Remote::Driver", @CARP_NOT );
  2042. croak $@;
  2043. }
  2044. else {
  2045. # re throw if the exception wasn't what we expected
  2046. die $@;
  2047. }
  2048. }
  2049. return $self->webelement_class->new(
  2050. id => $ret_data,
  2051. driver => $self
  2052. );
  2053. }
  2054. =head2 find_child_elements
  2055. Description:
  2056. Search for multiple element on the page, starting from the identified
  2057. element. The located elements will be returned as an array of WebElement
  2058. objects.
  2059. Input: 3 (1 optional)
  2060. Required:
  2061. Selenium::Remote::WebElement - WebElement object from where you want to
  2062. start searching.
  2063. STRING - The search target.
  2064. Optional:
  2065. STRING - Locator scheme to use to search the element, available schemes:
  2066. {class, class_name, css, id, link, link_text, partial_link_text,
  2067. tag_name, name, xpath}
  2068. Defaults to 'xpath' if not configured global during instantiation.
  2069. Output:
  2070. ARRAY of WebElement Objects.
  2071. Usage:
  2072. my $elem1 = $driver->find_element("//select[\@name='ned']");
  2073. # note the usage of ./ when searching for a child element instead of //
  2074. my $child = $driver->find_child_elements($elem1, "./option");
  2075. =cut
  2076. sub find_child_elements {
  2077. my ( $self, $elem, $query, $method ) = @_;
  2078. if ( ( not defined $elem ) || ( not defined $query ) ) {
  2079. croak "Missing parameters";
  2080. }
  2081. my $res = { 'command' => 'findChildElements', 'id' => $elem->{id} };
  2082. my $params = $self->_build_find_params($method, $query);
  2083. my $ret_data = eval { $self->_execute_command( $res, $params ); };
  2084. if ($@) {
  2085. if ( $@
  2086. =~ /(An element could not be located on the page using the given search parameters)/
  2087. ) {
  2088. # give details on what element wasn't found
  2089. $@ = "$1: $query,$params->{using}";
  2090. local @CARP_NOT = ( "Selenium::Remote::Driver", @CARP_NOT );
  2091. croak $@;
  2092. }
  2093. else {
  2094. # re throw if the exception wasn't what we expected
  2095. die $@;
  2096. }
  2097. }
  2098. my $elem_obj_arr = [];
  2099. my $i = 0;
  2100. foreach (@$ret_data) {
  2101. $elem_obj_arr->[$i] =
  2102. $self->webelement_class->new(
  2103. id => $_,
  2104. driver => $self
  2105. );
  2106. $i++;
  2107. }
  2108. return wantarray ? @{$elem_obj_arr} : $elem_obj_arr;
  2109. }
  2110. =head2 find_element_by_class
  2111. See L</find_element>.
  2112. =head2 find_element_by_class_name
  2113. See L</find_element>.
  2114. =head2 find_element_by_css
  2115. See L</find_element>.
  2116. =head2 find_element_by_id
  2117. See L</find_element>.
  2118. =head2 find_element_by_link
  2119. See L</find_element>.
  2120. =head2 find_element_by_link_text
  2121. See L</find_element>.
  2122. =head2 find_element_by_name
  2123. See L</find_element>.
  2124. =head2 find_element_by_partial_link_text
  2125. See L</find_element>.
  2126. =head2 find_element_by_tag_name
  2127. See L</find_element>.
  2128. =head2 find_element_by_xpath
  2129. See L</find_element>.
  2130. =head2 get_active_element
  2131. Description:
  2132. Get the element on the page that currently has focus.. The located element
  2133. will be returned as a WebElement object.
  2134. Output:
  2135. WebElement Object
  2136. Usage:
  2137. $driver->get_active_element();
  2138. =cut
  2139. sub _build_find_params {
  2140. my ($self, $method, $query) = @_;
  2141. my $using = $self->_build_using($method);
  2142. # geckodriver doesn't accept name as a valid selector
  2143. if ($self->isa('Selenium::Firefox') && $using eq 'name') {
  2144. return {
  2145. using => 'css',
  2146. value => '[name=$query]'
  2147. };
  2148. }
  2149. else {
  2150. return {
  2151. using => $using,
  2152. value => $query
  2153. };
  2154. }
  2155. }
  2156. sub _build_using {
  2157. my ($self, $method) = @_;
  2158. if ($method) {
  2159. if ($self->FINDERS->{$method}) {
  2160. return $self->FINDERS->{$method};
  2161. }
  2162. else {
  2163. croak 'Bad method, expected: ' . join(', ', keys %{ $self->FINDERS }) . ", got $method";
  2164. }
  2165. }
  2166. else {
  2167. return $self->default_finder;
  2168. }
  2169. }
  2170. sub get_active_element {
  2171. my ($self) = @_;
  2172. my $res = { 'command' => 'getActiveElement' };
  2173. my $ret_data = eval { $self->_execute_command($res) };
  2174. if ($@) {
  2175. croak $@;
  2176. }
  2177. else {
  2178. return $self->webelement_class->new(
  2179. id => $ret_data,
  2180. driver => $self
  2181. );
  2182. }
  2183. }
  2184. =head2 cache_status
  2185. Description:
  2186. Get the status of the html5 application cache.
  2187. Usage:
  2188. print $driver->cache_status;
  2189. Output:
  2190. <number> - Status code for application cache: {UNCACHED = 0, IDLE = 1, CHECKING = 2, DOWNLOADING = 3, UPDATE_READY = 4, OBSOLETE = 5}
  2191. =cut
  2192. sub cache_status {
  2193. my ($self) = @_;
  2194. my $res = { 'command' => 'cacheStatus' };
  2195. return $self->_execute_command($res);
  2196. }
  2197. =head2 set_geolocation
  2198. Description:
  2199. Set the current geographic location - note that your driver must
  2200. implement this endpoint, or else it will crash your session. At the
  2201. very least, it works in v2.12 of Chromedriver.
  2202. Input:
  2203. Required:
  2204. HASH: A hash with key C<location> whose value is a Location hashref. See
  2205. usage section for example.
  2206. Usage:
  2207. $driver->set_geolocation( location => {
  2208. latitude => 40.714353,
  2209. longitude => -74.005973,
  2210. altitude => 0.056747
  2211. });
  2212. Output:
  2213. BOOLEAN - success or failure
  2214. =cut
  2215. sub set_geolocation {
  2216. my ( $self, %params ) = @_;
  2217. my $res = { 'command' => 'setGeolocation' };
  2218. return $self->_execute_command( $res, \%params );
  2219. }
  2220. =head2 get_geolocation
  2221. Description:
  2222. Get the current geographic location. Note that your webdriver must
  2223. implement this endpoint - otherwise, it will crash your session. At
  2224. the time of release, we couldn't get this to work on the desktop
  2225. FirefoxDriver or desktop Chromedriver.
  2226. Usage:
  2227. print $driver->get_geolocation;
  2228. Output:
  2229. { latitude: number, longitude: number, altitude: number } - The current geo location.
  2230. =cut
  2231. sub get_geolocation {
  2232. my ($self) = @_;
  2233. my $res = { 'command' => 'getGeolocation' };
  2234. return $self->_execute_command($res);
  2235. }
  2236. =head2 get_log
  2237. Description:
  2238. Get the log for a given log type. Log buffer is reset after each request.
  2239. Input:
  2240. Required:
  2241. <STRING> - Type of log to retrieve:
  2242. {client|driver|browser|server}. There may be others available; see
  2243. get_log_types for a full list for your driver.
  2244. Usage:
  2245. $driver->get_log( $log_type );
  2246. Output:
  2247. <ARRAY|ARRAYREF> - An array of log entries since the most recent request.
  2248. =cut
  2249. sub get_log {
  2250. my ( $self, $type ) = @_;
  2251. my $res = { 'command' => 'getLog' };
  2252. return $self->_execute_command( $res, { type => $type });
  2253. }
  2254. =head2 get_log_types
  2255. Description:
  2256. Get available log types. By default, every driver should have client,
  2257. driver, browser, and server types, but there may be more available,
  2258. depending on your driver.
  2259. Usage:
  2260. my @types = $driver->get_log_types;
  2261. $driver->get_log($types[0]);
  2262. Output:
  2263. <ARRAYREF> - The list of log types.
  2264. =cut
  2265. sub get_log_types {
  2266. my ($self) = @_;
  2267. my $res = { 'command' => 'getLogTypes' };
  2268. return $self->_execute_command($res);
  2269. }
  2270. =head2 set_orientation
  2271. Description:
  2272. Set the browser orientation.
  2273. Input:
  2274. Required:
  2275. <STRING> - Orientation {LANDSCAPE|PORTRAIT}
  2276. Usage:
  2277. $driver->set_orientation( $orientation );
  2278. Output:
  2279. BOOLEAN - success or failure
  2280. =cut
  2281. sub set_orientation {
  2282. my ( $self, $orientation ) = @_;
  2283. my $res = { 'command' => 'setOrientation' };
  2284. return $self->_execute_command( $res, { orientation => $orientation } );
  2285. }
  2286. =head2 get_orientation
  2287. Description:
  2288. Get the current browser orientation. Returns either LANDSCAPE|PORTRAIT.
  2289. Usage:
  2290. print $driver->get_orientation;
  2291. Output:
  2292. <STRING> - your orientation.
  2293. =cut
  2294. sub get_orientation {
  2295. my ($self) = @_;
  2296. my $res = { 'command' => 'getOrientation' };
  2297. return $self->_execute_command($res);
  2298. }
  2299. =head2 send_modifier
  2300. Description:
  2301. Send an event to the active element to depress or release a modifier key.
  2302. Input: 2
  2303. Required:
  2304. 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
  2305. isdown - Boolean/String - Whether to generate a key down or key up
  2306. Usage:
  2307. $driver->send_modifier('Alt','down');
  2308. $elem->send_keys('c');
  2309. $driver->send_modifier('Alt','up');
  2310. or
  2311. $driver->send_modifier('Alt',1);
  2312. $elem->send_keys('c');
  2313. $driver->send_modifier('Alt',0);
  2314. =cut
  2315. sub send_modifier {
  2316. my ( $self, $modifier, $isdown ) = @_;
  2317. if ( $isdown =~ /(down|up)/ ) {
  2318. $isdown = $isdown =~ /down/ ? 1 : 0;
  2319. }
  2320. if ($self->{is_wd3} && !(grep { $self->browser_name eq $_ } qw{chrome MicrosoftEdge})) {
  2321. my $acts = [
  2322. {
  2323. type => $isdown ? 'keyDown' : 'keyUp',
  2324. value => KEYS->{lc($modifier)},
  2325. },
  2326. ];
  2327. my $action = { actions => [{
  2328. id => 'key',
  2329. type => 'key',
  2330. actions => $acts,
  2331. }]};
  2332. _queue_action(%$action);
  2333. return 1;
  2334. }
  2335. my $res = { 'command' => 'sendModifier' };
  2336. my $params = {
  2337. value => $modifier,
  2338. isdown => $isdown
  2339. };
  2340. return $self->_execute_command( $res, $params );
  2341. }
  2342. =head2 compare_elements
  2343. Description:
  2344. Test if two element IDs refer to the same DOM element.
  2345. Input: 2
  2346. Required:
  2347. Selenium::Remote::WebElement - WebElement Object
  2348. Selenium::Remote::WebElement - WebElement Object
  2349. Output:
  2350. BOOLEAN
  2351. Usage:
  2352. $driver->compare_elements($elem_obj1, $elem_obj2);
  2353. =cut
  2354. sub compare_elements {
  2355. my ( $self, $elem1, $elem2 ) = @_;
  2356. my $res = {
  2357. 'command' => 'elementEquals',
  2358. 'id' => $elem1->{id},
  2359. 'other' => $elem2->{id}
  2360. };
  2361. return $self->_execute_command($res);
  2362. }
  2363. =head2 click
  2364. Description:
  2365. Click any mouse button (at the coordinates set by the last moveto command).
  2366. Input:
  2367. button - any one of 'LEFT'/0 'MIDDLE'/1 'RIGHT'/2
  2368. defaults to 'LEFT'
  2369. queue - (optional) queue the click, rather than executing it. WD3 only.
  2370. Usage:
  2371. $driver->click('LEFT');
  2372. $driver->click(1); #MIDDLE
  2373. $driver->click('RIGHT');
  2374. $driver->click; #Defaults to left
  2375. =cut
  2376. sub click {
  2377. my ( $self, $button, $append ) = @_;
  2378. $button = _get_button($button);
  2379. my $res = { 'command' => 'click' };
  2380. my $params = { 'button' => $button };
  2381. if ($self->{is_wd3} && !(grep { $self->browser_name eq $_ } qw{chrome MicrosoftEdge})) {
  2382. $params = {
  2383. actions => [{
  2384. type => "pointer",
  2385. id => 'mouse',
  2386. parameters => { "pointerType" => "mouse" },
  2387. actions => [
  2388. {
  2389. type => "pointerDown",
  2390. duration => 0,
  2391. button => $button,
  2392. },
  2393. {
  2394. type => "pointerUp",
  2395. duration => 0,
  2396. button => $button,
  2397. },
  2398. ],
  2399. }],
  2400. };
  2401. if ($append) {
  2402. _queue_action(%$params);
  2403. return 1;
  2404. }
  2405. return $self->general_action(%$params);
  2406. }
  2407. return $self->_execute_command( $res, $params );
  2408. }
  2409. sub _get_button {
  2410. my $button = shift;
  2411. my $button_enum = { LEFT => 0, MIDDLE => 1, RIGHT => 2 };
  2412. if ( defined $button && $button =~ /(LEFT|MIDDLE|RIGHT)/i ) {
  2413. return $button_enum->{ uc $1 };
  2414. }
  2415. if ( defined $button && $button =~ /(0|1|2)/ ) {
  2416. return $1;
  2417. }
  2418. return 0;
  2419. }
  2420. =head2 double_click
  2421. Description:
  2422. Double-clicks at the current mouse coordinates (set by moveto).
  2423. Compatibility:
  2424. On webdriver3 enabled servers, you can double click arbitrary mouse buttons.
  2425. Usage:
  2426. $driver->double_click(button);
  2427. =cut
  2428. sub double_click {
  2429. my ($self,$button) = @_;
  2430. $button = _get_button($button);
  2431. if ($self->{is_wd3} && !(grep { $self->browser_name eq $_ } qw{chrome MicrosoftEdge})) {
  2432. $self->click($button,1);
  2433. $self->click($button,1);
  2434. $self->general_action();
  2435. }
  2436. my $res = { 'command' => 'doubleClick' };
  2437. return $self->_execute_command($res);
  2438. }
  2439. =head2 button_down
  2440. Description:
  2441. Click and hold the left mouse button (at the coordinates set by the
  2442. last moveto command). Note that the next mouse-related command that
  2443. should follow is buttonup . Any other mouse command (such as click
  2444. or another call to buttondown) will yield undefined behaviour.
  2445. Compatibility:
  2446. On WebDriver 3 enabled servers, all this does is queue a button down action.
  2447. You will either have to call general_action() to perform the queue, or an action like click() which also clears the queue.
  2448. Usage:
  2449. $self->button_down;
  2450. =cut
  2451. sub button_down {
  2452. my ($self) = @_;
  2453. if ($self->{is_wd3} && !(grep { $self->browser_name eq $_ } qw{chrome MicrosoftEdge})) {
  2454. my $params = {
  2455. actions => [{
  2456. type => "pointer",
  2457. id => 'mouse',
  2458. parameters => { "pointerType" => "mouse" },
  2459. actions => [
  2460. {
  2461. type => "pointerDown",
  2462. duration => 0,
  2463. button => 0,
  2464. },
  2465. ],
  2466. }],
  2467. };
  2468. _queue_action(%$params);
  2469. return 1;
  2470. }
  2471. my $res = { 'command' => 'buttonDown' };
  2472. return $self->_execute_command($res);
  2473. }
  2474. =head2 button_up
  2475. Description:
  2476. Releases the mouse button previously held (where the mouse is
  2477. currently at). Must be called once for every buttondown command
  2478. issued. See the note in click and buttondown about implications of
  2479. out-of-order commands.
  2480. Compatibility:
  2481. On WebDriver 3 enabled servers, all this does is queue a button down action.
  2482. You will either have to call general_action() to perform the queue, or an action like click() which also clears the queue.
  2483. Usage:
  2484. $self->button_up;
  2485. =cut
  2486. sub button_up {
  2487. my ($self) = @_;
  2488. if ($self->{is_wd3} && !(grep { $self->browser_name eq $_ } qw{chrome MicrosoftEdge})) {
  2489. my $params = {
  2490. actions => [{
  2491. type => "pointer",
  2492. id => 'mouse',
  2493. parameters => { "pointerType" => "mouse" },
  2494. actions => [
  2495. {
  2496. type => "pointerDown",
  2497. duration => 0,
  2498. button => 0,
  2499. },
  2500. ],
  2501. }],
  2502. };
  2503. _queue_action(%$params);
  2504. return 1;
  2505. }
  2506. my $res = { 'command' => 'buttonUp' };
  2507. return $self->_execute_command($res);
  2508. }
  2509. =head2 upload_file
  2510. Description:
  2511. Upload a file from the local machine to the selenium server
  2512. machine. That file then can be used for testing file upload on web
  2513. forms. Returns the remote-server's path to the file.
  2514. Passing raw data as an argument past the filename will upload
  2515. that rather than the file's contents.
  2516. When passing raw data, be advised that it expects a zipped
  2517. and then base64 encoded version of a single file.
  2518. Multiple files and/or directories are not supported by the remote server.
  2519. Usage:
  2520. my $remote_fname = $driver->upload_file( $fname );
  2521. my $element = $driver->find_element( '//input[@id="file"]' );
  2522. $element->send_keys( $remote_fname );
  2523. =cut
  2524. # this method duplicates upload() method in the
  2525. # org.openqa.selenium.remote.RemoteWebElement java class.
  2526. sub upload_file {
  2527. my ( $self, $filename, $raw_content ) = @_;
  2528. my $params;
  2529. if (defined $raw_content) {
  2530. #If no processing is passed, send the argument raw
  2531. $params = {
  2532. file => $raw_content
  2533. };
  2534. }
  2535. else {
  2536. #Otherwise, zip/base64 it.
  2537. $params = $self->_prepare_file($filename);
  2538. }
  2539. my $res = { 'command' => 'uploadFile' }; # /session/:SessionId/file
  2540. my $ret = $self->_execute_command( $res, $params );
  2541. return $ret;
  2542. }
  2543. sub _prepare_file {
  2544. my ($self,$filename) = @_;
  2545. if ( not -r $filename ) { croak "upload_file: no such file: $filename"; }
  2546. my $string = ""; # buffer
  2547. my $zip = Archive::Zip->new();
  2548. $zip->addFile($filename, basename($filename));
  2549. if ($zip->writeToFileHandle(IO::String->new($string)) != AZ_OK) {
  2550. die 'zip failed';
  2551. }
  2552. return {
  2553. file => MIME::Base64::encode_base64($string, '')
  2554. };
  2555. }
  2556. =head2 get_text
  2557. Description:
  2558. Get the text of a particular element. Wrapper around L<find_element()>
  2559. Usage:
  2560. $text = $driver->get_text("//div[\@name='q']");
  2561. =cut
  2562. sub get_text {
  2563. my $self = shift;
  2564. return $self->find_element(@_)->get_text();
  2565. }
  2566. =head2 get_body
  2567. Description:
  2568. Get the current text for the whole body. If you want the entire raw HTML instead,
  2569. See L<get_page_source>.
  2570. Usage:
  2571. $body_text = $driver->get_body();
  2572. =cut
  2573. sub get_body {
  2574. my $self = shift;
  2575. return $self->get_text('//body', 'xpath');
  2576. }
  2577. =head2 get_path
  2578. Description:
  2579. Get the path part of the current browser location.
  2580. Usage:
  2581. $path = $driver->get_path();
  2582. =cut
  2583. sub get_path {
  2584. my $self = shift;
  2585. my $location = $self->get_current_url;
  2586. $location =~ s/\?.*//; # strip of query params
  2587. $location =~ s/#.*//; # strip of anchors
  2588. $location =~ s#^https?://[^/]+##; # strip off host
  2589. return $location;
  2590. }
  2591. =head2 get_user_agent
  2592. Description:
  2593. Convenience method to get the user agent string, according to the
  2594. browser's value for window.navigator.userAgent.
  2595. Usage:
  2596. $user_agent = $driver->get_user_agent()
  2597. =cut
  2598. sub get_user_agent {
  2599. my $self = shift;
  2600. return $self->execute_script('return window.navigator.userAgent;');
  2601. }
  2602. =head2 set_inner_window_size
  2603. Description:
  2604. Set the inner window size by closing the current window and
  2605. reopening the current page in a new window. This can be useful
  2606. when using browsers to mock as mobile devices.
  2607. This sub will be fired automatically if you set the
  2608. C<inner_window_size> hash key option during instantiation.
  2609. Input:
  2610. INT - height of the window
  2611. INT - width of the window
  2612. Output:
  2613. BOOLEAN - Success or failure
  2614. Usage:
  2615. $driver->set_inner_window_size(640, 480)
  2616. =cut
  2617. sub set_inner_window_size {
  2618. my $self = shift;
  2619. my $height = shift;
  2620. my $width = shift;
  2621. my $location = $self->get_current_url;
  2622. $self->execute_script('window.open("' . $location . '", "_blank")');
  2623. $self->close;
  2624. my @handles = @{ $self->get_window_handles };
  2625. $self->switch_to_window(pop @handles);
  2626. my @resize = (
  2627. 'window.innerHeight = ' . $height,
  2628. 'window.innerWidth = ' . $width,
  2629. 'return 1'
  2630. );
  2631. return $self->execute_script(join(';', @resize)) ? 1 : 0;
  2632. }
  2633. =head2 get_local_storage_item
  2634. Description:
  2635. Get the value of a local storage item specified by the given key.
  2636. Input: 1
  2637. Required:
  2638. STRING - name of the key to be retrieved
  2639. Output:
  2640. STRING - value of the local storage item
  2641. Usage:
  2642. $driver->get_local_storage_item('key')
  2643. =cut
  2644. sub get_local_storage_item {
  2645. my ($self, $key) = @_;
  2646. my $res = { 'command' => 'getLocalStorageItem' };
  2647. my $params = { 'key' => $key };
  2648. return $self->_execute_command($res, $params);
  2649. }
  2650. =head2 delete_local_storage_item
  2651. Description:
  2652. Get the value of a local storage item specified by the given key.
  2653. Input: 1
  2654. Required
  2655. STRING - name of the key to be deleted
  2656. Usage:
  2657. $driver->delete_local_storage_item('key')
  2658. =cut
  2659. sub delete_local_storage_item {
  2660. my ($self, $key) = @_;
  2661. my $res = { 'command' => 'deleteLocalStorageItem' };
  2662. my $params = { 'key' => $key };
  2663. return $self->_execute_command($res, $params);
  2664. }
  2665. sub _coerce_timeout_ms {
  2666. my ($ms) = @_;
  2667. if ( defined $ms ) {
  2668. return _coerce_number( $ms );
  2669. }
  2670. else {
  2671. croak 'Expecting a timeout in ms';
  2672. }
  2673. }
  2674. sub _coerce_number {
  2675. my ($maybe_number) = @_;
  2676. if ( Scalar::Util::looks_like_number( $maybe_number )) {
  2677. return $maybe_number + 0;
  2678. }
  2679. else {
  2680. croak "Expecting a number, not: $maybe_number";
  2681. }
  2682. }
  2683. 1;
  2684. __END__
  2685. =head1 SEE ALSO
  2686. L<https://github.com/SeleniumHQ/selenium> - the main selenium RC project
  2687. L<https://github.com/SeleniumHQ/selenium/wiki/JsonWireProtocol> - the "legacy" webdriver specification
  2688. L<https://www.w3.org/TR/webdriver/> - the WC3 WebDriver 3 specification
  2689. L<https://github.com/teodesian/Selenium-Remote-Driver/wiki>
  2690. Brownie
  2691. Wight
  2692. =cut