Path: blob/trunk/rb/lib/selenium/webdriver/devtools.rb
1864 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 WebDriver21class DevTools22autoload :ConsoleEvent, 'selenium/webdriver/devtools/console_event'23autoload :ExceptionEvent, 'selenium/webdriver/devtools/exception_event'24autoload :MutationEvent, 'selenium/webdriver/devtools/mutation_event'25autoload :NetworkInterceptor, 'selenium/webdriver/devtools/network_interceptor'26autoload :PinnedScript, 'selenium/webdriver/devtools/pinned_script'27autoload :Request, 'selenium/webdriver/devtools/request'28autoload :Response, 'selenium/webdriver/devtools/response'2930def initialize(url:, target_type:)31@ws = WebSocketConnection.new(url: url)32@session_id = nil33start_session(target_type: target_type)34end3536def close37@ws.close38end3940def callbacks41@ws.callbacks42end4344def send_cmd(method, **params)45data = {method: method, params: params.compact}46data[:sessionId] = @session_id if @session_id47message = @ws.send_cmd(**data)48raise Error::WebDriverError, error_message(message['error']) if message['error']4950message51end5253def method_missing(method, *_args)54namespace = "Selenium::DevTools::V#{Selenium::DevTools.version}"55methods_to_classes = "#{namespace}::METHODS_TO_CLASSES"5657desired_class = if Object.const_defined?(methods_to_classes)58# selenium-devtools 0.113 and newer59"#{namespace}::#{Object.const_get(methods_to_classes)[method]}"60else61# selenium-devtools 0.112 and older62"#{namespace}::#{method.capitalize}"63end6465return unless Object.const_defined?(desired_class)6667self.class.class_eval do68define_method(method) do69Object.const_get(desired_class).new(self)70end71end7273send(method)74end7576def respond_to_missing?(method, *_args)77desired_class = "Selenium::DevTools::V#{Selenium::DevTools.version}::#{method.capitalize}"78Object.const_defined?(desired_class)79end8081private8283def start_session(target_type:)84targets = target.get_targets.dig('result', 'targetInfos')85found_target = targets.find { |target| target['type'] == target_type }86raise Error::WebDriverError, "Target type '#{target_type}' not found" unless found_target8788session = target.attach_to_target(target_id: found_target['targetId'], flatten: true)89@session_id = session.dig('result', 'sessionId')90end9192def error_message(error)93[error['code'], error['message'], error['data']].join(': ')94end95end # DevTools96end # WebDriver97end # Selenium9899100