Skip to content

Commit

Permalink
[rb] add specs with bugfixes for remote execution
Browse files Browse the repository at this point in the history
  • Loading branch information
titusfortner committed Nov 27, 2019
1 parent 5296b5b commit b2b9211
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 9 deletions.
7 changes: 0 additions & 7 deletions rb/lib/selenium/webdriver/common/driver.rb
Expand Up @@ -299,13 +299,6 @@ def create_bridge(**opts)

default_caps = @bridge&.browser || :new
desired_capabilities = opts.delete(:desired_capabilities) || Remote::Capabilities.send(default_caps)
if desired_capabilities.is_a?(Symbol)
unless Remote::Capabilities.respond_to?(desired_capabilities)
raise Error::WebDriverError, "invalid desired capability: #{desired_capabilities.inspect}"
end

desired_capabilities = Remote::Capabilities.__send__(desired_capabilities)
end

options = opts.delete(:options)

Expand Down
4 changes: 2 additions & 2 deletions rb/lib/selenium/webdriver/remote/bridge.rb
Expand Up @@ -35,8 +35,8 @@ class Bridge
# @api private
#

def initialize(http_client: nil, url: nil)
uri = url.is_a?(URI) ? url : URI.parse(url || "http://#{Platform.localhost}:#{PORT}/wd/hub")
def initialize(http_client: nil, url:)
uri = url.is_a?(URI) ? url : URI.parse(url)
uri.path += '/' unless %r{\/$}.match?(uri.path)

@http = http_client || Http::Default.new
Expand Down
13 changes: 13 additions & 0 deletions rb/lib/selenium/webdriver/remote/driver.rb
Expand Up @@ -33,6 +33,19 @@ class Driver < WebDriver::Driver
include DriverExtensions::Rotatable
include DriverExtensions::HasRemoteStatus
include DriverExtensions::HasWebStorage

def initialize(bridge: nil, listener: nil, **opts)
desired_capabilities = opts[:desired_capabilities]
if desired_capabilities.is_a?(Symbol)
unless Remote::Capabilities.respond_to?(desired_capabilities)
raise Error::WebDriverError, "invalid desired capability: #{desired_capabilities.inspect}"
end

opts[:desired_capabilities] = Remote::Capabilities.__send__(desired_capabilities)
end
opts[:url] ||= "http://#{Platform.localhost}:4444/wd/hub"
super
end
end # Driver
end # Remote
end # WebDriver
Expand Down
83 changes: 83 additions & 0 deletions rb/spec/unit/selenium/webdriver/remote/driver_spec.rb
@@ -0,0 +1,83 @@
# frozen_string_literal: true

# Licensed to the Software Freedom Conservancy (SFC) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The SFC licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.

require File.expand_path('../spec_helper', __dir__)

module Selenium
module WebDriver
module Remote
describe Driver do
let(:resp) do
{status: 200,
body: "{\"value\":{\"sessionId\":\"0\",\"capabilities\":#{Remote::Capabilities.chrome.to_json}}}",
headers: {"content_type": "application/json"}}
end

def stub_response(body: nil, endpoint: nil)
body = (body || {capabilities: {firstMatch: [browserName: "chrome"]}}).to_json
endpoint ||= "http://127.0.0.1:4444/wd/hub/session"
stub_request(:post, endpoint).with(body: body).to_return(resp)
end

it 'requires default capabilities' do
stub_response(body: {capabilities: {firstMatch: [{}]}})

expect { Driver.new }.not_to raise_exception
end

it 'accepts :desired_capabilities value as a symbol' do
stub_response

expect { Driver.new(desired_capabilities: :chrome) }.not_to raise_exception
end

it 'uses provided URL' do
server = "http://example.com:4646/wd/hub"
stub_response(endpoint: "#{server}/session")

expect { Driver.new(desired_capabilities: :chrome, url: server) }.not_to raise_exception
end

it 'does not accept Options without Capabilities' do
opts = {args: ['-f']}
stub_response(body: {capabilities: {firstMatch: ["goog:chromeOptions": opts]}})

expect { Driver.new(options: Chrome::Options.new(opts)) }.not_to raise_exception
end

it 'uses provided Options' do
opts = {args: ['-f']}
stub_response(body: {capabilities: {firstMatch: [browserName: "chrome", "goog:chromeOptions": opts]}})

expect {
Driver.new(desired_capabilities: :chrome, options: Chrome::Options.new(opts))
}.not_to raise_exception
end

it 'uses provided HTTP Client' do
client = Remote::Http::Default.new
stub_response

driver = Driver.new(desired_capabilities: :chrome, http_client: client)
expect(driver.send(:bridge).http).to eq client
end
end
end # Remote
end # WebDriver
end # Selenium

0 comments on commit b2b9211

Please sign in to comment.