|
| 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 BiDi |
| 23 | + class Network |
| 24 | + EVENTS = { |
| 25 | + before_request: 'network.beforeRequestSent', |
| 26 | + response_started: 'network.responseStarted', |
| 27 | + response_completed: 'network.responseCompleted', |
| 28 | + auth_required: 'network.authRequired', |
| 29 | + FETCH_ERROR: 'network.fetchError' |
| 30 | + }.freeze |
| 31 | + |
| 32 | + PHASES = { |
| 33 | + before_request: 'beforeRequestSent', |
| 34 | + response_started: 'responseStarted', |
| 35 | + auth_required: 'authRequired' |
| 36 | + }.freeze |
| 37 | + |
| 38 | + def initialize(bidi) |
| 39 | + @bidi = bidi |
| 40 | + end |
| 41 | + |
| 42 | + def add_intercept(phases: [], contexts: nil, url_patterns: nil) |
| 43 | + @bidi.send_cmd('network.addIntercept', phases: phases, contexts: contexts, urlPatterns: url_patterns) |
| 44 | + end |
| 45 | + |
| 46 | + def remove_intercept(intercept) |
| 47 | + @bidi.send_cmd('network.removeIntercept', intercept: intercept) |
| 48 | + end |
| 49 | + |
| 50 | + def continue_with_auth(request_id, username, password) |
| 51 | + @bidi.send_cmd( |
| 52 | + 'network.continueWithAuth', |
| 53 | + 'request' => request_id, |
| 54 | + 'action' => 'provideCredentials', |
| 55 | + 'credentials' => { |
| 56 | + 'type' => 'password', |
| 57 | + 'username' => username, |
| 58 | + 'password' => password |
| 59 | + } |
| 60 | + ) |
| 61 | + end |
| 62 | + |
| 63 | + def on(event, &) |
| 64 | + event = EVENTS[event] if event.is_a?(Symbol) |
| 65 | + @bidi.add_callback(event, &) |
| 66 | + end |
| 67 | + end # Network |
| 68 | + end # BiDi |
| 69 | + end # WebDriver |
| 70 | +end # Selenium |
0 commit comments