diff --git a/docker-compose.yml b/docker-compose.yml index ed83c16..7bb7313 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,5 +1,10 @@ version: '2' services: + chrome: + image: robcherry/docker-chromedriver:latest + environment: + CHROMEDRIVER_WHITELISTED_IPS: "" + geckodriver: image: quay.io/cvlibrary/geckodriver logging: @@ -15,6 +20,20 @@ services: depends_on: - geckodriver - httpd + environment: + WEBDRIVER_HOST: geckodriver + WEBDRIVER_CAPABILITIES: '{ "moz:firefoxOptions" : { "args" : ["-headless"] } }' + volumes: + - .:/root + + test-chrome: + build: . + depends_on: + - chrome + - httpd + environment: + WEBDRIVER_HOST: chrome + WEBDRIVER_CAPABILITIES: '{ "chromeOptions" : { "binary" : "/usr/bin/google-chrome", "args": ["--headless"] } }' volumes: - .:/root diff --git a/lib/WebDriver/Tiny.pm b/lib/WebDriver/Tiny.pm index fcd4fa1..ee40922 100644 --- a/lib/WebDriver/Tiny.pm +++ b/lib/WebDriver/Tiny.pm @@ -82,8 +82,10 @@ sub new($class, %args) { $args{base_url} // '', ], $class; + my $req_args = { desiredCapabilities => $args{capabilities} // {} }; + $req_args->{_need_sessionId} = 1; my $reply = $self->_req( - POST => '', { desiredCapabilities => $args{capabilities} // {} } ); + POST => '', $req_args ); $self->[1] .= '/' . $reply->{sessionId}; @@ -274,14 +276,18 @@ sub window_switch($self, $handle) { $self; } -sub _req($self, $method, $path, $args = undef) { +sub _req($self, $method, $path, $args = {}) { + my $need_sessionId = delete $args->{_need_sessionId}; + my $reply = $self->[0]->request( $method, $self->[1] . $path, - { content => JSON::PP::encode_json( $args // {} ) }, + { content => JSON::PP::encode_json( $args ) }, ); - my $value = eval { JSON::PP::decode_json( $reply->{content} )->{value} }; + my $full_value = eval { JSON::PP::decode_json( $reply->{content} ) }; + my $value = $full_value->{value}; + $value->{sessionId} //= $full_value->{sessionId} if $need_sessionId; unless ( $reply->{success} ) { my $error = $value diff --git a/run-xt b/run-xt index 40d415a..6bcb623 100755 --- a/run-xt +++ b/run-xt @@ -1,8 +1,10 @@ #!/bin/sh -docker-compose up --abort-on-container-exit --build --exit-code-from test -t0 - +docker-compose run --rm test ret=$? +docker-compose run --rm test-chrome + +ret=$? && $ret docker-compose kill &> /dev/null docker-compose rm -f &> /dev/null diff --git a/xt/alert.t b/xt/alert.t index 91a6a7d..d05fad1 100644 --- a/xt/alert.t +++ b/xt/alert.t @@ -1,12 +1,13 @@ use strict; use warnings; +use JSON::PP (); use Test::More; use WebDriver::Tiny; my $drv = WebDriver::Tiny->new( - capabilities => { 'moz:firefoxOptions' => { args => ['-headless'] } }, - host => 'geckodriver', + capabilities => JSON::PP::decode_json($ENV{WEBDRIVER_CAPABILITIES} || '{}'), + host => $ENV{WEBDRIVER_HOST}, port => 4444, ); diff --git a/xt/attr.t b/xt/attr.t index cc9a8b7..0245809 100644 --- a/xt/attr.t +++ b/xt/attr.t @@ -1,13 +1,14 @@ use strict; use warnings; +use JSON::PP (); use Test::Deep; use Test::More; use WebDriver::Tiny; my $drv = WebDriver::Tiny->new( - capabilities => { 'moz:firefoxOptions' => { args => ['-headless'] } }, - host => 'geckodriver', + capabilities => JSON::PP::decode_json($ENV{WEBDRIVER_CAPABILITIES} || '{}'), + host => $ENV{WEBDRIVER_HOST}, port => 4444, ); diff --git a/xt/basic.t b/xt/basic.t index 036e689..81c99cb 100644 --- a/xt/basic.t +++ b/xt/basic.t @@ -2,13 +2,14 @@ use strict; use utf8; use warnings; +use JSON::PP (); use Test::Deep; use Test::More; use WebDriver::Tiny; my $drv = WebDriver::Tiny->new( - capabilities => { 'moz:firefoxOptions' => { args => ['-headless'] } }, - host => 'geckodriver', + capabilities => JSON::PP::decode_json($ENV{WEBDRIVER_CAPABILITIES} || '{}'), + host => $ENV{WEBDRIVER_HOST}, port => 4444, ); diff --git a/xt/cookies.t b/xt/cookies.t index 7c5bcff..f2028d0 100644 --- a/xt/cookies.t +++ b/xt/cookies.t @@ -1,12 +1,13 @@ use strict; use warnings; +use JSON::PP (); use Test::More; use WebDriver::Tiny; my $drv = WebDriver::Tiny->new( - capabilities => { 'moz:firefoxOptions' => { args => ['-headless'] } }, - host => 'geckodriver', + capabilities => JSON::PP::decode_json($ENV{WEBDRIVER_CAPABILITIES} || '{}'), + host => $ENV{WEBDRIVER_HOST}, port => 4444, ); diff --git a/xt/form.t b/xt/form.t index 6dd453e..0dbeba6 100644 --- a/xt/form.t +++ b/xt/form.t @@ -2,6 +2,7 @@ use strict; use utf8; use warnings; +use JSON::PP (); use Test::Deep; use Test::More; use URI; @@ -15,8 +16,8 @@ sub pick { map { splice @_, rand @_, 1 } 1 .. shift } sub roll { map { $_[ rand @_ ] } 1 .. shift } my $drv = WebDriver::Tiny->new( - capabilities => { 'moz:firefoxOptions' => { args => ['-headless'] } }, - host => 'geckodriver', + capabilities => JSON::PP::decode_json($ENV{WEBDRIVER_CAPABILITIES} || '{}'), + host => $ENV{WEBDRIVER_HOST}, port => 4444, ); diff --git a/xt/ghost.t b/xt/ghost.t index 4869e61..668f375 100644 --- a/xt/ghost.t +++ b/xt/ghost.t @@ -2,12 +2,13 @@ use strict; use utf8; use warnings; +use JSON::PP (); use Test::More; use WebDriver::Tiny; my $drv = WebDriver::Tiny->new( - capabilities => { 'moz:firefoxOptions' => { args => ['-headless'] } }, - host => 'geckodriver', + capabilities => JSON::PP::decode_json($ENV{WEBDRIVER_CAPABILITIES} || '{}'), + host => $ENV{WEBDRIVER_HOST}, port => 4444, ); diff --git a/xt/javascript.t b/xt/javascript.t index b9851b7..f0c895d 100644 --- a/xt/javascript.t +++ b/xt/javascript.t @@ -1,12 +1,13 @@ use strict; use warnings; +use JSON::PP (); use Test::More; use WebDriver::Tiny; my $drv = WebDriver::Tiny->new( - capabilities => { 'moz:firefoxOptions' => { args => ['-headless'] } }, - host => 'geckodriver', + capabilities => JSON::PP::decode_json($ENV{WEBDRIVER_CAPABILITIES} || '{}'), + host => $ENV{WEBDRIVER_HOST}, port => 4444, ); diff --git a/xt/screenshot.t b/xt/screenshot.t index d7b5855..0f50c54 100644 --- a/xt/screenshot.t +++ b/xt/screenshot.t @@ -1,13 +1,14 @@ use strict; use warnings; +use JSON::PP (); use File::Temp; use Test::More; use WebDriver::Tiny; my $drv = WebDriver::Tiny->new( - capabilities => { 'moz:firefoxOptions' => { args => ['-headless'] } }, - host => 'geckodriver', + capabilities => JSON::PP::decode_json($ENV{WEBDRIVER_CAPABILITIES} || '{}'), + host => $ENV{WEBDRIVER_HOST}, port => 4444, ); diff --git a/xt/status.t b/xt/status.t index 909acc0..9324da1 100644 --- a/xt/status.t +++ b/xt/status.t @@ -1,13 +1,14 @@ use strict; use warnings; +use JSON::PP (); use Test::Deep; use Test::More; use WebDriver::Tiny; my $drv = WebDriver::Tiny->new( - capabilities => { 'moz:firefoxOptions' => { args => ['-headless'] } }, - host => 'geckodriver', + capabilities => JSON::PP::decode_json($ENV{WEBDRIVER_CAPABILITIES} || '{}'), + host => $ENV{WEBDRIVER_HOST}, port => 4444, ); diff --git a/xt/window-management.t b/xt/window-management.t index ad2e5de..b14a013 100644 --- a/xt/window-management.t +++ b/xt/window-management.t @@ -1,13 +1,14 @@ use strict; use warnings; +use JSON::PP (); use Test::Deep; use Test::More; use WebDriver::Tiny; my $drv = WebDriver::Tiny->new( - capabilities => { 'moz:firefoxOptions' => { args => ['-headless'] } }, - host => 'geckodriver', + capabilities => JSON::PP::decode_json($ENV{WEBDRIVER_CAPABILITIES} || '{}'), + host => $ENV{WEBDRIVER_HOST}, port => 4444, );