Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
SeleniumHQ
GitHub Repository: SeleniumHQ/Selenium
Path: blob/trunk/rb/lib/selenium/webdriver/devtools.rb
1864 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
class DevTools
23
autoload :ConsoleEvent, 'selenium/webdriver/devtools/console_event'
24
autoload :ExceptionEvent, 'selenium/webdriver/devtools/exception_event'
25
autoload :MutationEvent, 'selenium/webdriver/devtools/mutation_event'
26
autoload :NetworkInterceptor, 'selenium/webdriver/devtools/network_interceptor'
27
autoload :PinnedScript, 'selenium/webdriver/devtools/pinned_script'
28
autoload :Request, 'selenium/webdriver/devtools/request'
29
autoload :Response, 'selenium/webdriver/devtools/response'
30
31
def initialize(url:, target_type:)
32
@ws = WebSocketConnection.new(url: url)
33
@session_id = nil
34
start_session(target_type: target_type)
35
end
36
37
def close
38
@ws.close
39
end
40
41
def callbacks
42
@ws.callbacks
43
end
44
45
def send_cmd(method, **params)
46
data = {method: method, params: params.compact}
47
data[:sessionId] = @session_id if @session_id
48
message = @ws.send_cmd(**data)
49
raise Error::WebDriverError, error_message(message['error']) if message['error']
50
51
message
52
end
53
54
def method_missing(method, *_args)
55
namespace = "Selenium::DevTools::V#{Selenium::DevTools.version}"
56
methods_to_classes = "#{namespace}::METHODS_TO_CLASSES"
57
58
desired_class = if Object.const_defined?(methods_to_classes)
59
# selenium-devtools 0.113 and newer
60
"#{namespace}::#{Object.const_get(methods_to_classes)[method]}"
61
else
62
# selenium-devtools 0.112 and older
63
"#{namespace}::#{method.capitalize}"
64
end
65
66
return unless Object.const_defined?(desired_class)
67
68
self.class.class_eval do
69
define_method(method) do
70
Object.const_get(desired_class).new(self)
71
end
72
end
73
74
send(method)
75
end
76
77
def respond_to_missing?(method, *_args)
78
desired_class = "Selenium::DevTools::V#{Selenium::DevTools.version}::#{method.capitalize}"
79
Object.const_defined?(desired_class)
80
end
81
82
private
83
84
def start_session(target_type:)
85
targets = target.get_targets.dig('result', 'targetInfos')
86
found_target = targets.find { |target| target['type'] == target_type }
87
raise Error::WebDriverError, "Target type '#{target_type}' not found" unless found_target
88
89
session = target.attach_to_target(target_id: found_target['targetId'], flatten: true)
90
@session_id = session.dig('result', 'sessionId')
91
end
92
93
def error_message(error)
94
[error['code'], error['message'], error['data']].join(': ')
95
end
96
end # DevTools
97
end # WebDriver
98
end # Selenium
99
100