Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
SeleniumHQ
GitHub Repository: SeleniumHQ/Selenium
Path: blob/trunk/rb/lib/selenium/webdriver/remote/driver.rb
4012 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
include DriverExtensions::HasSessionEvents
33
34
def initialize(capabilities: nil, options: nil, service: nil, url: nil, **)
35
raise ArgumentError, "Can not set :service object on #{self.class}" if service
36
37
url ||= "http://#{Platform.localhost}:4444/wd/hub"
38
caps = process_options(options, capabilities)
39
super(caps: caps, url: url, **)
40
@bridge.file_detector = ->((filename, *)) { File.exist?(filename) && filename.to_s }
41
command_list = @bridge.command_list
42
@bridge.extend(WebDriver::Remote::Features)
43
@bridge.add_commands(command_list)
44
end
45
46
private
47
48
def devtools_url
49
capabilities['se:cdp']
50
end
51
52
def devtools_version
53
cdp_version = capabilities['se:cdpVersion']&.split('.')&.first
54
raise Error::WebDriverError, 'DevTools is not supported by the Remote Server' unless cdp_version
55
56
Integer(cdp_version)
57
end
58
59
def process_options(options, capabilities)
60
if options && capabilities
61
msg = "Don't use both :options and :capabilities when initializing #{self.class}, prefer :options"
62
raise ArgumentError, msg
63
elsif options.nil? && capabilities.nil?
64
raise ArgumentError, "#{self.class} needs :options to be set"
65
end
66
options ? options.as_json : generate_capabilities(capabilities)
67
end
68
69
def generate_capabilities(capabilities)
70
Array(capabilities).map { |cap|
71
if cap.is_a? Symbol
72
cap = WebDriver::Options.send(cap)
73
elsif !cap.respond_to? :as_json
74
msg = ":capabilities parameter only accepts objects responding to #as_json which #{cap.class} does not"
75
raise ArgumentError, msg
76
end
77
cap.as_json
78
}.inject(:merge)
79
end
80
end # Driver
81
end # Remote
82
end # WebDriver
83
end # Selenium
84
85