Path: blob/trunk/rb/lib/selenium/webdriver/remote/driver.rb
1865 views
# frozen_string_literal: true12# Licensed to the Software Freedom Conservancy (SFC) under one3# or more contributor license agreements. See the NOTICE file4# distributed with this work for additional information5# regarding copyright ownership. The SFC licenses this file6# to you under the Apache License, Version 2.0 (the7# "License"); you may not use this file except in compliance8# with the License. You may obtain a copy of the License at9#10# http://www.apache.org/licenses/LICENSE-2.011#12# Unless required by applicable law or agreed to in writing,13# software distributed under the License is distributed on an14# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY15# KIND, either express or implied. See the License for the16# specific language governing permissions and limitations17# under the License.1819module Selenium20module WebDriver21module Remote22#23# Driver implementation for remote server.24# @api private25#2627class Driver < WebDriver::Driver28include DriverExtensions::UploadsFiles29include DriverExtensions::HasSessionId30include DriverExtensions::HasFileDownloads3132def initialize(capabilities: nil, options: nil, service: nil, url: nil, **)33raise ArgumentError, "Can not set :service object on #{self.class}" if service3435url ||= "http://#{Platform.localhost}:4444/wd/hub"36caps = process_options(options, capabilities)37super(caps: caps, url: url, **)38@bridge.file_detector = ->((filename, *)) { File.exist?(filename) && filename.to_s }39command_list = @bridge.command_list40@bridge.extend(WebDriver::Remote::Features)41@bridge.add_commands(command_list)42end4344private4546def devtools_url47capabilities['se:cdp']48end4950def devtools_version51cdp_version = capabilities['se:cdpVersion']&.split('.')&.first52raise Error::WebDriverError, 'DevTools is not supported by the Remote Server' unless cdp_version5354Integer(cdp_version)55end5657def process_options(options, capabilities)58if options && capabilities59msg = "Don't use both :options and :capabilities when initializing #{self.class}, prefer :options"60raise ArgumentError, msg61elsif options.nil? && capabilities.nil?62raise ArgumentError, "#{self.class} needs :options to be set"63end64options ? options.as_json : generate_capabilities(capabilities)65end6667def generate_capabilities(capabilities)68Array(capabilities).map { |cap|69if cap.is_a? Symbol70cap = WebDriver::Options.send(cap)71elsif !cap.respond_to? :as_json72msg = ":capabilities parameter only accepts objects responding to #as_json which #{cap.class} does not"73raise ArgumentError, msg74end75cap.as_json76}.inject(:merge)77end78end # Driver79end # Remote80end # WebDriver81end # Selenium828384