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