Client.pm 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525
  1. package Selenium::Client;
  2. # ABSTRACT: Module for communicating with WC3 standard selenium servers
  3. use strict;
  4. use warnings;
  5. no warnings 'experimental';
  6. use feature qw/signatures/;
  7. use JSON::MaybeXS();
  8. use HTTP::Tiny();
  9. use Carp qw{confess};
  10. use File::Path qw{make_path};
  11. use File::HomeDir();
  12. use File::Slurper();
  13. use Sub::Install();
  14. use Net::EmptyPort();
  15. use Capture::Tiny qw{capture_merged};
  16. use Selenium::Specification;
  17. =head1 CONSTRUCTOR
  18. =head2 new(%options) = Selenium::Client
  19. Either connects to a driver at the specified host and port, or spawns one locally.
  20. Spawns a server on a random port in the event the host is "localhost" (or 127.0.0.1) and nothing is reachable on the provided port.
  21. Returns a Selenium::Client object with all WC3 methods exposed.
  22. To view all available methods and their documentation, the catalog() method is provided.
  23. Remote Server options:
  24. =over 4
  25. =item C<version> ENUM (stable,draft,unstable) - WC3 Spec to use.
  26. Default: stable
  27. =item C<host> STRING - hostname of your server.
  28. Default: localhost
  29. =item C<prefix> STRING - any prefix needed to communicate with the server, such as /wd, /hub, /wd/hub, or /grid
  30. Default: ''
  31. =item C<port> INTEGER - Port which the server is listening on.
  32. Default: 4444
  33. Note: when spawning, this will be ignored and a random port chosen instead.
  34. =item C<scheme> ENUM (http,https) - HTTP scheme to use
  35. Default: http
  36. =item C<nofetch> BOOL - Do not check for a newer copy of the WC3 specifications on startup if we already have them available.
  37. Default: 1
  38. =item C<client_dir> STRING - Where to store specs and other files downloaded when spawning servers.
  39. Default: ~/.selenium
  40. =item C<debug> BOOLEAN - Whether to print out various debugging output.
  41. Default: false
  42. =item C<auto_close> BOOLEAN - Automatically close spawned selenium servers and sessions.
  43. Only turn this off when you are debugging.
  44. Default: true
  45. =item C<post_callbacks> ARRAY[CODE] - Executed after each request to the selenium server.
  46. Callbacks are passed $self, an HTTP::Tiny response hashref and the request hashref.
  47. Use this to implement custom error handlers, testing harness modifications etc.
  48. Return a truthy value to immediately exit the request subroutine after all cbs are executed.
  49. Truthy values (if any are returned) are returned in order encountered.
  50. =back
  51. When using remote servers, you should take extra care that they automatically clean up after themselves.
  52. We cannot guarantee the state of said servers after interacting with them.
  53. Spawn Options:
  54. =over 4
  55. =item C<driver> STRING - Plug-in module used to spawn drivers when needed.
  56. Included are 'Auto', 'SeleniumHQ::Jar', 'Gecko', 'Chrome', 'Edge'
  57. Default: Auto
  58. The 'Auto' Driver will pick whichever direct driver looks like it will work for your chosen browser.
  59. If we can't find one, we'll fall back to SeleniumHQ::Jar.
  60. =item C<browser> STRING - desired browser. Used by the 'Auto' Driver.
  61. Default: Blank
  62. =item C<driver_version> STRING - Version of your driver software you wish to download and run.
  63. Blank and Partial versions will return the latest sub-version available.
  64. Only relevant to Drivers which auto-download (currently only SeleniumHQ::Jar).
  65. Default: Blank
  66. =back
  67. Driver modules should be in the Selenium::Driver namespace.
  68. They may implement additional parameters which can be passed into the options hash.
  69. =cut
  70. sub new($class,%options) {
  71. $options{version} //= 'stable';
  72. $options{port} //= 4444;
  73. #XXX geckodriver doesn't bind to localhost lol
  74. $options{host} //= '127.0.0.1';
  75. $options{host} = '127.0.0.1' if $options{host} eq 'localhost';
  76. $options{nofetch} //= 1;
  77. $options{scheme} //= 'http';
  78. $options{prefix} //= '';
  79. $options{ua} //= HTTP::Tiny->new();
  80. $options{client_dir} //= File::HomeDir::my_home()."/.selenium";
  81. $options{driver} //= "SeleniumHQ::Jar";
  82. $options{post_callbacks} //= [];
  83. $options{auto_close} //= 1;
  84. $options{browser} //= '';
  85. #Grab the spec
  86. $options{spec} = Selenium::Specification::read($options{version},$options{nofetch});
  87. my $self = bless(\%options, $class);
  88. $self->{sessions} = [];
  89. $self->_build_subs();
  90. $self->_spawn() if $options{host} eq '127.0.0.1';
  91. return $self;
  92. }
  93. =head1 METHODS
  94. =head2 Most of the methods are dynamic based on the selenium spec
  95. This means that the Selenium::Client class can directly call all selenium methods.
  96. We provide a variety of subclasses as sugar around this:
  97. Selenium::Session
  98. Selenium::Capabilities
  99. Selenium::Element
  100. Which will simplify correctly passing arguments in the case of sessions and elements.
  101. However, this does not change the fact that you still must take great care.
  102. We do no validation whatsoever of the inputs, and the selenium server likes to hang when you give it an invalid input.
  103. So take great care and understand this is what "script hung and died" means -- you passed the function an unrecognized argument.
  104. This is because Selenium::Specification cannot (yet!) parse the inputs and outputs for each endpoint at this time.
  105. As such we can't just filter against the relevant prototype.
  106. In any case, all subs will look like this, for example:
  107. $client->Method( key => value, key1 => value1, ...) = (@return_per_key)
  108. The options passed in are basically JSON serialized and passed directly as a POST body (or included into the relevant URL).
  109. We return a list of items which are a hashref per item in the result (some of them blessed).
  110. For example, NewSession will return a Selenium::Capabilities and Selenium::Session object.
  111. The order in which they are returned will be ordered alphabetically.
  112. =head2 catalog(BOOL verbose=0) = HASHREF
  113. Returns the entire method catalog.
  114. Prints out every method and a link to the relevant documentation if verbose is true.
  115. =cut
  116. sub catalog($self,$printed=0) {
  117. return $self->{spec} unless $printed;
  118. foreach my $method (keys(%{$self->{spec}})) {
  119. print "$method: $self->{spec}{$method}{href}\n";
  120. }
  121. return $self->{spec};
  122. }
  123. sub _build_subs($self) {
  124. foreach my $sub (keys(%{$self->{spec}})) {
  125. Sub::Install::install_sub(
  126. {
  127. code => sub {
  128. my $self = shift;
  129. return $self->_request($sub,@_);
  130. },
  131. as => $sub,
  132. into => "Selenium::Client",
  133. }
  134. ) unless "Selenium::Client"->can($sub);
  135. }
  136. }
  137. #Check if server already up and spawn if no
  138. sub _spawn($self) {
  139. return $self->Status() if Net::EmptyPort::wait_port( $self->{port}, 1 );
  140. # Pick a random port for the new server
  141. $self->{port} = Net::EmptyPort::empty_port();
  142. my $driver_file = "Selenium/Driver/$self->{driver}.pm";
  143. $driver_file =~ s/::/\//g;
  144. eval { require $driver_file } or confess "Could not load $driver_file, check your PERL5LIB: $@";
  145. my $driver = "Selenium::Driver::$self->{driver}";
  146. $driver->build_spawn_opts($self);
  147. return $self->_do_spawn();
  148. }
  149. sub _do_spawn($self) {
  150. #XXX on windows we will *never* terminate if we are listening for *anything*
  151. #XXX so we have to just bg & ignore, unfortunately (also have to system())
  152. if (_is_windows()) {
  153. $self->{pid} = qq/$self->{driver}:$self->{port}/;
  154. my @cmdprefix = ("start /MIN", qq{"$self->{pid}"});
  155. # Selenium JAR controls it's own logging because Java
  156. my @cmdsuffix;
  157. @cmdsuffix = ('>', $self->{log_file}, '2>&1') unless $self->{driver_class} eq 'Selenium::Driver::SeleniumHQ::Jar';
  158. my $cmdstring = join(' ', @cmdprefix, @{$self->{command}}, @cmdsuffix );
  159. print "$cmdstring\n" if $self->{debug};
  160. system($cmdstring);
  161. return $self->_wait();
  162. }
  163. print "@{$self->{command}}\n" if $self->{debug};
  164. my $pid = fork // confess("Could not fork");
  165. if ($pid) {
  166. $self->{pid} = $pid;
  167. return $self->_wait();
  168. }
  169. open(my $fh, '>>', $self->{log_file});
  170. capture_merged { exec(@{$self->{command}}) } stdout => $fh;
  171. }
  172. sub _wait ($self) {
  173. print "Waiting for port to come up..." if $self->{debug};
  174. Net::EmptyPort::wait_port( $self->{port}, 30 )
  175. or confess("Server never came up on port $self->{port} after 30s!");
  176. print "done\n" if $self->{debug};
  177. return $self->Status();
  178. }
  179. sub DESTROY($self) {
  180. return unless $self->{auto_close};
  181. print "Shutting down active sessions...\n" if $self->{debug};
  182. #murder all sessions we spawned so that die() cleans up properly
  183. if ($self->{ua} && @{$self->{sessions}}) {
  184. foreach my $session (@{$self->{sessions}}) {
  185. # An attempt was made. The session *might* already be dead.
  186. eval { $self->DeleteSession( sessionid => $session ) };
  187. }
  188. }
  189. #Kill the server if we spawned one
  190. return unless $self->{pid};
  191. print "Attempting to kill server process...\n" if $self->{debug};
  192. if (_is_windows()) {
  193. my $killer = qq[taskkill /FI "WINDOWTITLE eq $self->{pid}"];
  194. print "$killer\n" if $self->{debug};
  195. #$killer .= ' > nul 2&>1' unless $self->{debug};
  196. system($killer);
  197. return 1;
  198. }
  199. my $sig = 'TERM';
  200. kill $sig, $self->{pid};
  201. print "Issued SIG$sig to $self->{pid}, waiting...\n" if $self->{debug};
  202. return waitpid( $self->{pid}, 0 );
  203. }
  204. sub _is_windows {
  205. return grep { $^O eq $_ } qw{msys MSWin32};
  206. }
  207. #XXX some of the methods require content being null, some require it to be an obj with no params LOL
  208. our @bad_methods = qw{AcceptAlert DismissAlert Back Forward Refresh ElementClick MaximizeWindow MinimizeWindow FullscreenWindow SwitchToParentFrame ElementClear};
  209. #Exempt some calls from return processing
  210. our @no_process = qw{Status GetWindowRect GetElementRect GetAllCookies};
  211. sub _request($self, $method, %params) {
  212. my $subject = $self->{spec}->{$method};
  213. #TODO handle compressed output from server
  214. my %options = (
  215. headers => {
  216. 'Content-Type' => 'application/json; charset=utf-8',
  217. 'Accept' => 'application/json; charset=utf-8',
  218. 'Accept-Encoding' => 'identity',
  219. },
  220. );
  221. $options{content} = '{}' if grep { $_ eq $method } @bad_methods;
  222. my $url = "$self->{scheme}://$self->{host}:$self->{port}$subject->{uri}";
  223. # Remove parameters to inject into child objects
  224. my $inject_key = exists $params{inject} ? delete $params{inject} : undef;
  225. my $inject_value = $inject_key ? $params{$inject_key} : '';
  226. my $inject;
  227. $inject = { to_inject => { $inject_key => $inject_value } } if $inject_key && $inject_value;
  228. # Keep sessions for passing to grandchildren
  229. $inject->{to_inject}{sessionid} = $params{sessionid} if exists $params{sessionid};
  230. foreach my $param (keys(%params)) {
  231. confess "$param is required for $method" unless exists $params{$param};
  232. delete $params{$param} if $url =~ s/{\Q$param\E}/$params{$param}/g;
  233. }
  234. if (%params) {
  235. $options{content} = JSON::MaybeXS::encode_json(\%params);
  236. $options{headers}{'Content-Length'} = length($options{content});
  237. }
  238. print "$subject->{method} $url\n" if $self->{debug};
  239. print "Body: $options{content}\n" if $self->{debug} && exists $options{content};
  240. my $res = $self->{ua}->request($subject->{method}, $url, \%options);
  241. my @cbret;
  242. foreach my $cb (@{$self->{post_callbacks}}) {
  243. if ($cb && ref $cb eq 'CODE') {
  244. @options{qw{url method}} = ($url,$subject->{method});
  245. $options{content} = \%params if %params;
  246. my $ret = $cb->($self, $res, \%options);
  247. push(@cbret,$ret) if $ret;
  248. }
  249. return $cbret[0] if @cbret == 1;
  250. return @cbret if @cbret;
  251. }
  252. print "$res->{status} : $res->{content}\n" if $self->{debug} && ref $res eq 'HASH';
  253. my $decoded_content = eval { JSON::MaybeXS::decode_json($res->{content}) };
  254. confess "$res->{reason} :\n Consult $subject->{href}\nRaw Error:\n$res->{content}\n" unless $res->{success};
  255. if (grep { $method eq $_ } @no_process) {
  256. return @{$decoded_content->{value}} if ref $decoded_content->{value} eq 'ARRAY';
  257. return $decoded_content->{value};
  258. }
  259. return $self->_objectify($decoded_content,$inject);
  260. }
  261. our %classes = (
  262. capabilities => { class => 'Selenium::Capabilities' },
  263. sessionId => {
  264. class => 'Selenium::Session',
  265. destroy_callback => sub {
  266. my $self = shift;
  267. $self->DeleteSession() unless $self->{deleted};
  268. },
  269. callback => sub {
  270. my ($self,$call) = @_;
  271. $self->{deleted} = 1 if $call eq 'DeleteSession';
  272. },
  273. },
  274. # Whoever thought this parameter name was a good idea...
  275. 'element-6066-11e4-a52e-4f735466cecf' => {
  276. class => 'Selenium::Element',
  277. },
  278. );
  279. sub _objectify($self,$result,$inject) {
  280. my $subject = $result->{value};
  281. return $subject unless grep { ref $subject eq $_ } qw{ARRAY HASH};
  282. $subject = [$subject] unless ref $subject eq 'ARRAY';
  283. my @objs;
  284. foreach my $to_objectify (@$subject) {
  285. # If we have just data return it
  286. return @$subject if ref $to_objectify ne 'HASH';
  287. my @objects = keys(%$to_objectify);
  288. foreach my $object (@objects) {
  289. my $has_class = exists $classes{$object};
  290. my $base_object = $inject // {};
  291. $base_object->{lc($object)} = $to_objectify->{$object};
  292. $base_object->{sortField} = lc($object);
  293. my $to_push = $has_class ?
  294. $classes{$object}{class}->new($self, $base_object ) :
  295. $to_objectify;
  296. $to_push->{sortField} = lc($object);
  297. # Save sessions for destructor
  298. push(@{$self->{sessions}}, $to_push->{sessionid}) if ref $to_push eq 'Selenium::Session';
  299. push(@objs,$to_push);
  300. }
  301. }
  302. @objs = sort { $a->{sortField} cmp $b->{sortField} } @objs;
  303. return $objs[0] if @objs == 1;
  304. return @objs;
  305. }
  306. 1;
  307. =head1 SUBCLASSES
  308. =head2 Selenium::Capabilities
  309. Returned as first element from NewSession().
  310. Query this object for various things about the server capabilities.
  311. =head2 Selenium::Session
  312. Returned as second element of NewSession().
  313. Has a destructor which will automatically clean itself up when we go out of scope.
  314. Alternatively, when the driver object goes out of scope, all sessions it spawned will be destroyed.
  315. You can call Selenium methods on this object which require a sessionid without passing it explicitly.
  316. =head2 Selenium::Element
  317. Returned from find element calls.
  318. You can call Selenium methods on this object which require a sessionid and elementid without passing them explicitly.
  319. =cut
  320. package Selenium::Capabilities;
  321. use parent qw{Selenium::Subclass};
  322. 1;
  323. package Selenium::Session;
  324. use parent qw{Selenium::Subclass};
  325. 1;
  326. package Selenium::Element;
  327. use parent qw{Selenium::Subclass};
  328. 1;
  329. __END__
  330. =head1 STUPID SELENIUM TRICKS
  331. There are a variety of quirks with Selenium drivers that you just have to put up with, don't log bugs on these behaviors.
  332. =head3 alerts
  333. If you have an alert() open on the page, all calls to the selenium server will 500 until you dismiss or accept it.
  334. Also be aware that chrome will re-fire alerts when you do a forward() or back() event, unlike firefox.
  335. =head3 tag names
  336. Safari returns ALLCAPS names for tags. amazing
  337. =head2 properties and attributes
  338. Many I<valid> properties/attributes will I<never> be accessible via GetProperty() or GetAttribute().
  339. For example, getting the "for" value of a <label> element is flat-out impossible using either GetProperty or GetAttribute.
  340. There are many other such cases, the most common being "non-standard" properties such as aria-* or things used by JS templating engines.
  341. You are better off using JS shims to do any element inspection.
  342. Similarly the IsElementSelected() method is quite unreliable.
  343. We can work around this however by just using the CSS :checked pseudoselector when looking for elements, as that actually works.
  344. It is this for these reasons that you should consider abandoning Selenium for something that can actually do this correctly such as L<Playwright>.
  345. =head3 windows
  346. When closing windows, be aware you will be NOT be shot back to the last window you had focused before switching to the current one.
  347. You have to manually switch back to an existing one.
  348. Opening _blank targeted links *does not* automatically switch to the new window.
  349. The procedure for handling links of such a sort to do this is as follows:
  350. # Get current handle
  351. my $handle = $session->GetWindowHandle();
  352. # Assuming the element is an href with target=_blank ...
  353. $element->ClickElement();
  354. # Get all handles and filter for the ones that we aren't currently using
  355. my @handles = $session->GetWindowHandles();
  356. my @new_handles = grep { $handle != $_ } @handles;
  357. # Use pop() as it will always be returned in the order windows are opened
  358. $session->SwitchToWindow( handle => pop(@new_handles) );
  359. Different browser drivers also handle window handles differently.
  360. Chrome in particular demands you stringify handles returned from the driver.
  361. It also seems to be a lot less cooperative than firefox when setting the WindowRect.
  362. =head3 arguments
  363. If you make a request of the server with arguments it does not understand it will hang for 30s, so set a SIGALRM handler if you insist on doing so.
  364. =head2 MSWin32 issues
  365. The default version of the Java JRE from java.com is quite simply ancient on windows, and SeleniumHQ develops against JDK 11 and better.
  366. So make sure your JDK bin dir is in your PATH I<before> the JRE path (or don't install an ancient JRE lol)
  367. If you don't, you'll probably get insta-explosions due to their usage of new language features.
  368. Kind of like how you'll die if you use a perl without signatures with this module :)
  369. Also, due to perl pseudo-forks hanging forever if anything is ever waiting on read() in windows, we don't fork to spawn binaries.
  370. Instead we use C<start> to open a new cmd.exe window, which will show up in your task tray.
  371. Don't close this or your test will fail for obvious reasons.
  372. =head1 AUTHOR
  373. George S. Baugh <george@troglodyne.net>