Driver.pm 101 KB

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