Skip to content

Commit

Permalink
[rb] Use Bazel JDK when starting server
Browse files Browse the repository at this point in the history
This ensures the server is not started with a system JDK which might be
missing, outdated or simply inaccessible due to Bazel test sandboxing.
  • Loading branch information
p0deje committed Apr 3, 2024
1 parent ef3d9e8 commit fd0fe80
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 10 deletions.
14 changes: 13 additions & 1 deletion rb/lib/selenium/server.rb
Expand Up @@ -241,7 +241,7 @@ def process
# extract any additional_args that start with -D as options
properties = @additional_args.dup - @additional_args.delete_if { |arg| arg[/^-D/] }
args = ['-jar', @jar, @role, '--port', @port.to_s]
server_command = ['java'] + properties + args + @additional_args
server_command = [java_bin] + properties + args + @additional_args
cp = WebDriver::ChildProcess.build(*server_command)

if @log.is_a?(String)
Expand All @@ -256,6 +256,18 @@ def process
end
end

def java_bin
if ENV.key?('BAZEL_TEST') && ENV.key?('JAVA_HOME')
if WebDriver::Platform.windows?
"#{ENV['JAVA_HOME']}/bin/java.exe"
else
"#{ENV['JAVA_HOME']}/bin/java"
end
else
'java'
end
end

def poll_for_service
return if socket.connected?

Expand Down
18 changes: 9 additions & 9 deletions rb/spec/unit/selenium/server_spec.rb
Expand Up @@ -48,7 +48,7 @@ module Selenium
it 'uses the given jar file and port' do
allow(File).to receive(:exist?).with('selenium_server_deploy.jar').and_return(true)
allow(WebDriver::ChildProcess).to receive(:build)
.with('java', '-jar', 'selenium_server_deploy.jar', 'standalone', '--port', '1234')
.with(a_string_ending_with('java'), '-jar', 'selenium_server_deploy.jar', 'standalone', '--port', '1234')
.and_return(mock_process)

server = described_class.new('selenium_server_deploy.jar', port: 1234, background: true)
Expand All @@ -57,13 +57,13 @@ module Selenium
server.start
expect(File).to have_received(:exist?).with('selenium_server_deploy.jar')
expect(WebDriver::ChildProcess).to have_received(:build)
.with('java', '-jar', 'selenium_server_deploy.jar', 'standalone', '--port', '1234')
.with(a_string_ending_with('java'), '-jar', 'selenium_server_deploy.jar', 'standalone', '--port', '1234')
end

it 'waits for the server process by default' do
allow(File).to receive(:exist?).with('selenium_server_deploy.jar').and_return(true)
allow(WebDriver::ChildProcess).to receive(:build)
.with('java', '-jar', 'selenium_server_deploy.jar', 'standalone', '--port', port.to_s)
.with(a_string_ending_with('java'), '-jar', 'selenium_server_deploy.jar', 'standalone', '--port', port.to_s)
.and_return(mock_process)

server = described_class.new('selenium_server_deploy.jar', port: port)
Expand All @@ -74,14 +74,14 @@ module Selenium

expect(File).to have_received(:exist?).with('selenium_server_deploy.jar')
expect(WebDriver::ChildProcess).to have_received(:build)
.with('java', '-jar', 'selenium_server_deploy.jar', 'standalone', '--port', port.to_s)
.with(a_string_ending_with('java'), '-jar', 'selenium_server_deploy.jar', 'standalone', '--port', port.to_s)
expect(mock_process).to have_received(:wait)
end

it 'adds additional args' do
allow(File).to receive(:exist?).with('selenium_server_deploy.jar').and_return(true)
allow(WebDriver::ChildProcess).to receive(:build)
.with('java', '-jar', 'selenium_server_deploy.jar', 'standalone', '--port', port.to_s, 'foo', 'bar')
.with(a_string_ending_with('java'), '-jar', 'selenium_server_deploy.jar', 'standalone', '--port', port.to_s, 'foo', 'bar')
.and_return(mock_process)

server = described_class.new('selenium_server_deploy.jar', port: port, background: true)
Expand All @@ -92,14 +92,14 @@ module Selenium
server.start
expect(File).to have_received(:exist?).with('selenium_server_deploy.jar')
expect(WebDriver::ChildProcess).to have_received(:build)
.with('java', '-jar', 'selenium_server_deploy.jar', 'standalone',
.with(a_string_ending_with('java'), '-jar', 'selenium_server_deploy.jar', 'standalone',
'--port', port.to_s, 'foo', 'bar')
end

it 'adds additional JAVA options args' do
allow(File).to receive(:exist?).with('selenium_server_deploy.jar').and_return(true)
allow(WebDriver::ChildProcess).to receive(:build)
.with('java',
.with(a_string_ending_with('java'),
'-Dwebdriver.chrome.driver=/bin/chromedriver',
'-jar', 'selenium_server_deploy.jar',
'standalone',
Expand All @@ -117,7 +117,7 @@ module Selenium
server.start
expect(File).to have_received(:exist?).with('selenium_server_deploy.jar')
expect(WebDriver::ChildProcess).to have_received(:build)
.with('java',
.with(a_string_ending_with('java'),
'-Dwebdriver.chrome.driver=/bin/chromedriver',
'-jar', 'selenium_server_deploy.jar',
'standalone',
Expand Down Expand Up @@ -194,7 +194,7 @@ module Selenium
it 'raises Selenium::Server::Error if the server is not launched within the timeout' do
allow(File).to receive(:exist?).with('selenium_server_deploy.jar').and_return(true)
allow(WebDriver::ChildProcess).to receive(:build)
.with('java', '-jar', 'selenium_server_deploy.jar', 'standalone', '--port', port.to_s)
.with(a_string_ending_with('java'), '-jar', 'selenium_server_deploy.jar', 'standalone', '--port', port.to_s)
.and_return(mock_process)

poller = instance_double(WebDriver::SocketPoller)
Expand Down

0 comments on commit fd0fe80

Please sign in to comment.