Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
SeleniumHQ
GitHub Repository: SeleniumHQ/Selenium
Path: blob/trunk/rb/lib/selenium/webdriver/bidi/network.rb
4078 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
require_relative 'network/url_pattern'
20
21
module Selenium
22
module WebDriver
23
class BiDi
24
# Implements the Navigation Module of the WebDriver-BiDi specification
25
# Continue to use functionality from existing `driver.navigate` method
26
#
27
# @api private
28
#
29
30
class Network
31
EVENTS = {
32
before_request: 'network.beforeRequestSent',
33
response_started: 'network.responseStarted',
34
response_completed: 'network.responseCompleted',
35
auth_required: 'network.authRequired',
36
fetch_error: 'network.fetchError'
37
}.freeze
38
39
PHASES = {
40
before_request: 'beforeRequestSent',
41
response_started: 'responseStarted',
42
auth_required: 'authRequired'
43
}.freeze
44
45
def initialize(bidi)
46
@bidi = bidi
47
end
48
49
def add_intercept(phases: [], contexts: nil, url_patterns: nil, pattern_type: :string)
50
url_patterns = url_patterns && pattern_type ? UrlPattern.format_pattern(url_patterns, pattern_type) : nil
51
@bidi.send_cmd('network.addIntercept',
52
phases: phases,
53
contexts: contexts,
54
urlPatterns: url_patterns)
55
end
56
57
def remove_intercept(intercept)
58
@bidi.send_cmd('network.removeIntercept', intercept: intercept)
59
end
60
61
def continue_with_auth(request_id, username, password)
62
@bidi.send_cmd(
63
'network.continueWithAuth',
64
request: request_id,
65
action: 'provideCredentials',
66
credentials: {
67
type: 'password',
68
username: username,
69
password: password
70
}
71
)
72
end
73
74
def continue_without_auth(request_id)
75
@bidi.send_cmd(
76
'network.continueWithAuth',
77
request: request_id,
78
action: 'default'
79
)
80
end
81
82
def cancel_auth(request_id)
83
@bidi.send_cmd(
84
'network.continueWithAuth',
85
request: request_id,
86
action: 'cancel'
87
)
88
end
89
90
def continue_request(**args)
91
args = {
92
request: args[:id],
93
body: args[:body],
94
cookies: args[:cookies],
95
headers: args[:headers],
96
method: args[:method],
97
url: args[:url]
98
}.compact
99
100
@bidi.send_cmd('network.continueRequest', **args)
101
end
102
103
def fail_request(request_id)
104
@bidi.send_cmd(
105
'network.failRequest',
106
request: request_id
107
)
108
end
109
110
def continue_response(**args)
111
args = {
112
request: args[:id],
113
cookies: args[:cookies],
114
credentials: args[:credentials],
115
headers: args[:headers],
116
reasonPhrase: args[:reason],
117
statusCode: args[:status]
118
}.compact
119
120
@bidi.send_cmd('network.continueResponse', **args)
121
end
122
123
def provide_response(**args)
124
args = {
125
request: args[:id],
126
body: args[:body],
127
cookies: args[:cookies],
128
headers: args[:headers],
129
reasonPhrase: args[:reason],
130
statusCode: args[:status]
131
}.compact
132
133
@bidi.send_cmd('network.provideResponse', **args)
134
end
135
136
def set_cache_behavior(behavior, *contexts)
137
@bidi.send_cmd('network.setCacheBehavior', cacheBehavior: behavior, contexts: contexts)
138
end
139
140
def on(event, &block)
141
event = EVENTS[event] if event.is_a?(Symbol)
142
@bidi.add_callback(event, &block)
143
@bidi.session.subscribe(event)
144
end
145
end # Network
146
end # BiDi
147
end # WebDriver
148
end # Selenium
149
150