Path: blob/trunk/rb/lib/selenium/webdriver/bidi/network.rb
4078 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 BiDi23# Implements the Navigation Module of the WebDriver-BiDi specification24# Continue to use functionality from existing `driver.navigate` method25#26# @api private27#2829class Network30EVENTS = {31before_request: 'network.beforeRequestSent',32response_started: 'network.responseStarted',33response_completed: 'network.responseCompleted',34auth_required: 'network.authRequired',35fetch_error: 'network.fetchError'36}.freeze3738PHASES = {39before_request: 'beforeRequestSent',40response_started: 'responseStarted',41auth_required: 'authRequired'42}.freeze4344def initialize(bidi)45@bidi = bidi46end4748def add_intercept(phases: [], contexts: nil, url_patterns: nil, pattern_type: :string)49url_patterns = url_patterns && pattern_type ? UrlPattern.format_pattern(url_patterns, pattern_type) : nil50@bidi.send_cmd('network.addIntercept',51phases: phases,52contexts: contexts,53urlPatterns: url_patterns)54end5556def remove_intercept(intercept)57@bidi.send_cmd('network.removeIntercept', intercept: intercept)58end5960def continue_with_auth(request_id, username, password)61@bidi.send_cmd(62'network.continueWithAuth',63request: request_id,64action: 'provideCredentials',65credentials: {66type: 'password',67username: username,68password: password69}70)71end7273def continue_without_auth(request_id)74@bidi.send_cmd(75'network.continueWithAuth',76request: request_id,77action: 'default'78)79end8081def cancel_auth(request_id)82@bidi.send_cmd(83'network.continueWithAuth',84request: request_id,85action: 'cancel'86)87end8889def continue_request(**args)90args = {91request: args[:id],92body: args[:body],93cookies: args[:cookies],94headers: args[:headers],95method: args[:method],96url: args[:url]97}.compact9899@bidi.send_cmd('network.continueRequest', **args)100end101102def fail_request(request_id)103@bidi.send_cmd(104'network.failRequest',105request: request_id106)107end108109def continue_response(**args)110args = {111request: args[:id],112cookies: args[:cookies],113credentials: args[:credentials],114headers: args[:headers],115reasonPhrase: args[:reason],116statusCode: args[:status]117}.compact118119@bidi.send_cmd('network.continueResponse', **args)120end121122def provide_response(**args)123args = {124request: args[:id],125body: args[:body],126cookies: args[:cookies],127headers: args[:headers],128reasonPhrase: args[:reason],129statusCode: args[:status]130}.compact131132@bidi.send_cmd('network.provideResponse', **args)133end134135def set_cache_behavior(behavior, *contexts)136@bidi.send_cmd('network.setCacheBehavior', cacheBehavior: behavior, contexts: contexts)137end138139def on(event, &block)140event = EVENTS[event] if event.is_a?(Symbol)141@bidi.add_callback(event, &block)142@bidi.session.subscribe(event)143end144end # Network145end # BiDi146end # WebDriver147end # Selenium148149150