Path: blob/trunk/rb/lib/selenium/webdriver/bidi/network.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.18require_relative 'network/url_pattern'1920module Selenium21module WebDriver22class BiDi23class Network24EVENTS = {25before_request: 'network.beforeRequestSent',26response_started: 'network.responseStarted',27response_completed: 'network.responseCompleted',28auth_required: 'network.authRequired',29fetch_error: 'network.fetchError'30}.freeze3132PHASES = {33before_request: 'beforeRequestSent',34response_started: 'responseStarted',35auth_required: 'authRequired'36}.freeze3738def initialize(bidi)39@bidi = bidi40end4142def add_intercept(phases: [], contexts: nil, url_patterns: nil, pattern_type: :string)43url_patterns = url_patterns && pattern_type ? UrlPattern.format_pattern(url_patterns, pattern_type) : nil44@bidi.send_cmd('network.addIntercept',45phases: phases,46contexts: contexts,47urlPatterns: url_patterns)48end4950def remove_intercept(intercept)51@bidi.send_cmd('network.removeIntercept', intercept: intercept)52end5354def continue_with_auth(request_id, username, password)55@bidi.send_cmd(56'network.continueWithAuth',57request: request_id,58action: 'provideCredentials',59credentials: {60type: 'password',61username: username,62password: password63}64)65end6667def continue_without_auth(request_id)68@bidi.send_cmd(69'network.continueWithAuth',70request: request_id,71action: 'default'72)73end7475def cancel_auth(request_id)76@bidi.send_cmd(77'network.continueWithAuth',78request: request_id,79action: 'cancel'80)81end8283def continue_request(**args)84@bidi.send_cmd(85'network.continueRequest',86request: args[:id],87body: args[:body],88cookies: args[:cookies],89headers: args[:headers],90method: args[:method],91url: args[:url]92)93end9495def fail_request(request_id)96@bidi.send_cmd(97'network.failRequest',98request: request_id99)100end101102def continue_response(**args)103@bidi.send_cmd(104'network.continueResponse',105request: args[:id],106cookies: args[:cookies],107credentials: args[:credentials],108headers: args[:headers],109reasonPhrase: args[:reason],110statusCode: args[:status]111)112end113114def provide_response(**args)115@bidi.send_cmd(116'network.provideResponse',117request: args[:id],118body: args[:body],119cookies: args[:cookies],120headers: args[:headers],121reasonPhrase: args[:reason],122statusCode: args[:status]123)124end125126def set_cache_behavior(behavior, *contexts)127@bidi.send_cmd('network.setCacheBehavior', cacheBehavior: behavior, contexts: contexts)128end129130def on(event, &block)131event = EVENTS[event] if event.is_a?(Symbol)132@bidi.add_callback(event, &block)133@bidi.session.subscribe(event)134end135end # Network136end # BiDi137end # WebDriver138end # Selenium139140141