Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
SeleniumHQ
GitHub Repository: SeleniumHQ/Selenium
Path: blob/trunk/rb/lib/selenium/webdriver/remote/driver.rb
1865 views
1
# frozen_string_literal: true
2
3
# Licensed to the Software Freedom Conservancy (SFC) under one
4
# or more contributor license agreements. See the NOTICE file
5
# distributed with this work for additional information
6
# regarding copyright ownership. The SFC licenses this file
7
# to you under the Apache License, Version 2.0 (the
8
# "License"); you may not use this file except in compliance
9
# with the License. You may obtain a copy of the License at
10
#
11
# http://www.apache.org/licenses/LICENSE-2.0
12
#
13
# Unless required by applicable law or agreed to in writing,
14
# software distributed under the License is distributed on an
15
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16
# KIND, either express or implied. See the License for the
17
# specific language governing permissions and limitations
18
# under the License.
19
20
module Selenium
21
module WebDriver
22
module Remote
23
#
24
# Driver implementation for remote server.
25
# @api private
26
#
27
28
class Driver < WebDriver::Driver
29
include DriverExtensions::UploadsFiles
30
include DriverExtensions::HasSessionId
31
include DriverExtensions::HasFileDownloads
32
33
def initialize(capabilities: nil, options: nil, service: nil, url: nil, **)
34
raise ArgumentError, "Can not set :service object on #{self.class}" if service
35
36
url ||= "http://#{Platform.localhost}:4444/wd/hub"
37
caps = process_options(options, capabilities)
38
super(caps: caps, url: url, **)
39
@bridge.file_detector = ->((filename, *)) { File.exist?(filename) && filename.to_s }
40
command_list = @bridge.command_list
41
@bridge.extend(WebDriver::Remote::Features)
42
@bridge.add_commands(command_list)
43
end
44
45
private
46
47
def devtools_url
48
capabilities['se:cdp']
49
end
50
51
def devtools_version
52
cdp_version = capabilities['se:cdpVersion']&.split('.')&.first
53
raise Error::WebDriverError, 'DevTools is not supported by the Remote Server' unless cdp_version
54
55
Integer(cdp_version)
56
end
57
58
def process_options(options, capabilities)
59
if options && capabilities
60
msg = "Don't use both :options and :capabilities when initializing #{self.class}, prefer :options"
61
raise ArgumentError, msg
62
elsif options.nil? && capabilities.nil?
63
raise ArgumentError, "#{self.class} needs :options to be set"
64
end
65
options ? options.as_json : generate_capabilities(capabilities)
66
end
67
68
def generate_capabilities(capabilities)
69
Array(capabilities).map { |cap|
70
if cap.is_a? Symbol
71
cap = WebDriver::Options.send(cap)
72
elsif !cap.respond_to? :as_json
73
msg = ":capabilities parameter only accepts objects responding to #as_json which #{cap.class} does not"
74
raise ArgumentError, msg
75
end
76
cap.as_json
77
}.inject(:merge)
78
end
79
end # Driver
80
end # Remote
81
end # WebDriver
82
end # Selenium
83
84