| 1 | package Selenium::Driver::SeleniumHQ::Jar; | ||||||||||||||||||||
| 2 | 1 | $Selenium::Driver::SeleniumHQ::Jar::VERSION = '1.06'; | |||||||||||||||||||
| 3 | 1 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 4 | 1 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | |||||||||||||||||||||
| 6 | 1 | ▶ | use v5.28; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 7 | |||||||||||||||||||||
| 8 | 1 | ▶ | no warnings 'experimental'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 9 | 1 | ▶ | use feature qw/signatures/; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 10 | |||||||||||||||||||||
| 11 | 1 | ▶ | use Carp qw{confess}; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 12 | 1 | ▶ | use File::Basename qw{basename}; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 13 | 1 | ▶ | use File::Path qw{make_path}; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 14 | 1 | ▶ | use File::Spec(); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 15 | 1 | ▶ | use XML::LibXML(); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 16 | 1 | ▶ | use HTTP::Tiny(); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 17 | |||||||||||||||||||||
| 18 | #ABSTRACT: Download the latest version of seleniumHQ's selenium.jar, and tell Selenium::Client how to spawn it | ||||||||||||||||||||
| 19 | |||||||||||||||||||||
| 20 | |||||||||||||||||||||
| 21 | 1 | our $index = 'http://selenium-release.storage.googleapis.com'; | |||||||||||||||||||
| 22 | |||||||||||||||||||||
| 23 | ▶ | sub build_spawn_opts($class,$object) { | |||||||||||||||||||
|
Subroutine
build_spawn_opts: not called CC=7 SCAR=40.3
Pod
documented
| |||||||||||||||||||||
| 24 | 0 | $object->{driver_class} = $class; | |||||||||||||||||||
| 25 | 0 | ▶ | $object->{driver_interpreter} //= 'java'; | ||||||||||||||||||
|
Condition0%0/2
$object->{"driver_interpreter"} //= "java"
MC/DC0%0/1
$object->{"driver_interpreter"} //= "java"
$object->{"driver_interpreter"}
Truth table0%0/2
A
A= $object->{"driver_interpreter"}
| |||||||||||||||||||||
| 26 | 0 | ▶ | $object->{driver_version} //= ''; | ||||||||||||||||||
|
Condition0%0/2
$object->{"driver_version"} //= ""
MC/DC0%0/1
$object->{"driver_version"} //= ""
$object->{"driver_version"}
Truth table0%0/2
A
A= $object->{"driver_version"}
| |||||||||||||||||||||
| 27 | 0 | ▶ | $object->{log_file} //= File::Spec->catfile($object->{client_dir},"perl-client","selenium-$object->{port}.log"); | ||||||||||||||||||
|
Condition0%0/3
$object->{"log_file"} //= "File::Spec"->catfile($object->{"client_dir"}, "perl-client", qq[selenium-$object->{"port"}.log])
MC/DC0%0/2
$object->{"log_file"} //= "File::Spec"->catfile($object->{"client_dir"}, "perl-client", qq[selenium-$object->{"port"}.log])
$object->{"log_file"}
"File::Spec"->catfile($object->{"client_dir"}, "perl-client", qq[selenium-$object->{"port"}.log])
Truth table0%0/3
A //= B
A= $object->{"log_file"}B= "File::Spec"->catfile($object->{"client_dir"}, "perl-client", qq[selenium-$object->{"port"}.log])
| |||||||||||||||||||||
| 28 | 0 | ($object->{driver_file}, $object->{driver_major_version}) = find_and_fetch( File::Spec->catdir($object->{client_dir},"jars"), $object->{driver_version},$object->{ua}); | |||||||||||||||||||
| 29 | 0 | ▶ | $object->{driver_config} //= _build_config($object); | ||||||||||||||||||
|
Condition0%0/3
$object->{"driver_config"} //= _build_config($object)
MC/DC0%0/2
$object->{"driver_config"} //= _build_config($object)
$object->{"driver_config"}
_build_config($object)
Truth table0%0/3
A //= B
A= $object->{"driver_config"}B= _build_config($object)
| |||||||||||||||||||||
| 30 | |||||||||||||||||||||
| 31 | #XXX port in config is currently IGNORED | ||||||||||||||||||||
| 32 | 0 | my @java_opts; | |||||||||||||||||||
| 33 | 0 | my @config = ((qw{standalone --config}), $object->{driver_config}, '--port', $object->{port}); | |||||||||||||||||||
| 34 | |||||||||||||||||||||
| 35 | # Handle older seleniums that are WC3 compliant | ||||||||||||||||||||
| 36 | 0 | ▶ | if ( $object->{driver_major_version} < 4 ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 37 | 0 | $object->{prefix} = '/wd/hub'; | |||||||||||||||||||
| 38 | 0 | @java_opts = qw{-Dwebedriver.gecko.driver=geckodriver -Dwebdriver.chrome.driver=chromedriver}; | |||||||||||||||||||
| 39 | 0 | @config = (); | |||||||||||||||||||
| 40 | } | ||||||||||||||||||||
| 41 | |||||||||||||||||||||
| 42 | # Build command string | ||||||||||||||||||||
| 43 | # XXX relies on gecko/chromedriver in $PATH | ||||||||||||||||||||
| 44 | $object->{command} //= [ | ||||||||||||||||||||
| 45 | $object->{driver_interpreter}, | ||||||||||||||||||||
| 46 | @java_opts, | ||||||||||||||||||||
| 47 | qw{-jar}, | ||||||||||||||||||||
| 48 | $object->{driver_file}, | ||||||||||||||||||||
| 49 | 0 | ▶ | @config, | ||||||||||||||||||
|
Condition0%0/2
$object->{"command"} //= [$object->{"driver_interpreter"}, @java_opts, "-jar", $object->{"driver_file"}, @config]
MC/DC0%0/1
$object->{"command"} //= [$object->{"driver_interpreter"}, @java_opts, "-jar", $object->{"driver_file"}, @config]
$object->{"command"}
Truth table0%0/2
A
A= $object->{"command"}
| |||||||||||||||||||||
| 50 | ]; | ||||||||||||||||||||
| 51 | 0 | return $object; | |||||||||||||||||||
| 52 | } | ||||||||||||||||||||
| 53 | |||||||||||||||||||||
| 54 | ▶ | sub _build_config($self) { | |||||||||||||||||||
|
Subroutine
_build_config: not called CC=3 SCAR=24.8
| |||||||||||||||||||||
| 55 | 0 | my $dir = File::Spec->catdir($self->{client_dir},"perl-client"); | |||||||||||||||||||
| 56 | 0 | ▶ | make_path( $dir ) unless -d $dir; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 57 | |||||||||||||||||||||
| 58 | |||||||||||||||||||||
| 59 | 0 | my $file = File::Spec->catfile($dir,"config-$self->{port}.toml"); | |||||||||||||||||||
| 60 | 0 | ▶ | return $file if -f $file; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 61 | |||||||||||||||||||||
| 62 | # TODO add some self-signed SSL to this | ||||||||||||||||||||
| 63 | 0 | my $config = <<~EOF; | |||||||||||||||||||
| 64 | [node] | ||||||||||||||||||||
| 65 | detect-drivers = true | ||||||||||||||||||||
| 66 | [server] | ||||||||||||||||||||
| 67 | allow-cors = true | ||||||||||||||||||||
| 68 | hostname = "localhost" | ||||||||||||||||||||
| 69 | max-threads = 36 | ||||||||||||||||||||
| 70 | port = --PORT-- | ||||||||||||||||||||
| 71 | [logging] | ||||||||||||||||||||
| 72 | enable = true | ||||||||||||||||||||
| 73 | log-encoding = UTF-8 | ||||||||||||||||||||
| 74 | log-file = --REPLACE-- | ||||||||||||||||||||
| 75 | plain-logs = true | ||||||||||||||||||||
| 76 | structured-logs = false | ||||||||||||||||||||
| 77 | tracing = true | ||||||||||||||||||||
| 78 | EOF | ||||||||||||||||||||
| 79 | |||||||||||||||||||||
| 80 | #XXX double escape backslash because windows; like YAML, TOML is a poor choice always | ||||||||||||||||||||
| 81 | #XXX so, you'll die if there are backslashes in your username or homedir choice (lunatic) | ||||||||||||||||||||
| 82 | 0 | my $log_corrected = $self->{log_file}; | |||||||||||||||||||
| 83 | 0 | $log_corrected =~ s/\\/\\\\/g; | |||||||||||||||||||
| 84 | |||||||||||||||||||||
| 85 | 0 | $config =~ s/--REPLACE--/\"$log_corrected\"/gm; | |||||||||||||||||||
| 86 | 0 | $config =~ s/--PORT--/$self->{port}/gm; | |||||||||||||||||||
| 87 | |||||||||||||||||||||
| 88 | 0 | File::Slurper::write_text($file, $config); | |||||||||||||||||||
| 89 | 0 | return $file; | |||||||||||||||||||
| 90 | } | ||||||||||||||||||||
| 91 | |||||||||||||||||||||
| 92 | |||||||||||||||||||||
| 93 | 0 | ▶ | sub find_and_fetch($dir, $version='', $ua='') { | ||||||||||||||||||
|
Subroutine
find_and_fetch: not called CC=11 SCAR=48.8
Pod
documented
| |||||||||||||||||||||
| 94 | 0 | ▶ | $ua ||= HTTP::Tiny->new(); | ||||||||||||||||||
|
Condition0%0/3
$ua ||= "HTTP::Tiny"->new
MC/DC0%0/2
$ua ||= "HTTP::Tiny"->new
$ua
"HTTP::Tiny"->new
Truth table0%0/3
A ||= B
A= $uaB= "HTTP::Tiny"->new
| |||||||||||||||||||||
| 95 | 0 | my $res = $ua->get($index); | |||||||||||||||||||
| 96 | 0 | ▶ | confess "$res->{reason} :\n$res->{content}\n" unless $res->{success}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 97 | 0 | my $parsed = XML::LibXML->load_xml(string => $res->{content}); | |||||||||||||||||||
| 98 | |||||||||||||||||||||
| 99 | #XXX - XPATH NO WORKY, HURR DURR | ||||||||||||||||||||
| 100 | 0 | my @files; | |||||||||||||||||||
| 101 | 0 | foreach my $element ($parsed->findnodes('//*')) { | |||||||||||||||||||
| 102 | 0 | my $contents = $element->getChildrenByTagName("Contents"); | |||||||||||||||||||
| 103 | 0 | ▶ | my @candidates = sort { $b cmp $a } grep { m/selenium-server/ && m/\.jar$/ } map { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 104 | 0 | $_->getChildrenByTagName('Key')->to_literal().''; | |||||||||||||||||||
| 105 | } @$contents; | ||||||||||||||||||||
| 106 | 0 | push(@files,@candidates); | |||||||||||||||||||
| 107 | } | ||||||||||||||||||||
| 108 | |||||||||||||||||||||
| 109 | 0 | ▶ | @files = grep { m/\Q$version\E/ } @files if $version; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 110 | 0 | my $jar = shift @files; | |||||||||||||||||||
| 111 | 0 | my $url = "$index/$jar"; | |||||||||||||||||||
| 112 | |||||||||||||||||||||
| 113 | 0 | ▶ | make_path( $dir ) unless -d $dir; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 114 | 0 | my $fname = File::Spec->catfile($dir, basename($jar)); | |||||||||||||||||||
| 115 | 0 | my ($v) = $fname =~ m/-(\d)\.\d\.\d.*\.jar$/; | |||||||||||||||||||
| 116 | 0 | ▶ | return ($fname,$v) if -f $fname; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 117 | |||||||||||||||||||||
| 118 | 0 | $res = $ua->mirror($url, $fname); | |||||||||||||||||||
| 119 | |||||||||||||||||||||
| 120 | 0 | ▶ | confess "$res->{reason} :\n$res->{content}\n" unless $res->{success}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 121 | 0 | return ($fname,$v); | |||||||||||||||||||
| 122 | } | ||||||||||||||||||||
| 123 | |||||||||||||||||||||
| 124 | 1 | 1; | |||||||||||||||||||
| 125 | |||||||||||||||||||||
| 126 | __END__ | ||||||||||||||||||||
| 127 | |||||||||||||||||||||
| 128 | =pod | ||||||||||||||||||||
| 129 | |||||||||||||||||||||
| 130 | =encoding UTF-8 | ||||||||||||||||||||
| 131 | |||||||||||||||||||||
| 132 | =head1 NAME | ||||||||||||||||||||
| 133 | |||||||||||||||||||||
| 134 | Selenium::Driver::SeleniumHQ::Jar - Download the latest version of seleniumHQ's selenium.jar, and tell Selenium::Client how to spawn it | ||||||||||||||||||||
| 135 | |||||||||||||||||||||
| 136 | =head1 VERSION | ||||||||||||||||||||
| 137 | |||||||||||||||||||||
| 138 | version 1.06 | ||||||||||||||||||||
| 139 | |||||||||||||||||||||
| 140 | =head1 Mode of Operation | ||||||||||||||||||||
| 141 | |||||||||||||||||||||
| 142 | Downloads the latest Selenium JAR (or the provided driver_version). | ||||||||||||||||||||
| 143 | Expects java to already be installed. | ||||||||||||||||||||
| 144 | |||||||||||||||||||||
| 145 | Spawns a selnium server on the provided port (which the caller will assign randomly) | ||||||||||||||||||||
| 146 | Pipes log output to ~/.selenium/perl-client/$port.log | ||||||||||||||||||||
| 147 | Uses a config file ~/.selenium/perl-client/$port.toml if the selenium version supports this | ||||||||||||||||||||
| 148 | |||||||||||||||||||||
| 149 | =head1 SUBROUTINES | ||||||||||||||||||||
| 150 | |||||||||||||||||||||
| 151 | =head2 build_spawn_opts($class,$object) | ||||||||||||||||||||
| 152 | |||||||||||||||||||||
| 153 | Builds a command string which can run the driver binary. | ||||||||||||||||||||
| 154 | All driver classes must build this. | ||||||||||||||||||||
| 155 | |||||||||||||||||||||
| 156 | =head2 find_and_fetch($dir STRING, $version STRING, $user_agent HTTP::Tiny) | ||||||||||||||||||||
| 157 | |||||||||||||||||||||
| 158 | Does an index lookup of the various selenium JARs available and returns either the latest one | ||||||||||||||||||||
| 159 | or the version provided. Stores the JAR in the provided directory. | ||||||||||||||||||||
| 160 | |||||||||||||||||||||
| 161 | =head1 AUTHOR | ||||||||||||||||||||
| 162 | |||||||||||||||||||||
| 163 | George S. Baugh <george@troglodyne.net> | ||||||||||||||||||||
| 164 | |||||||||||||||||||||
| 165 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||
| 166 | |||||||||||||||||||||
| 167 | This software is Copyright (c) 2023 by George S. Baugh. | ||||||||||||||||||||
| 168 | |||||||||||||||||||||
| 169 | This is free software, licensed under: | ||||||||||||||||||||
| 170 | |||||||||||||||||||||
| 171 | The MIT (X11) License | ||||||||||||||||||||
| 172 | |||||||||||||||||||||
| 173 | =cut | ||||||||||||||||||||