Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
SeleniumHQ
GitHub Repository: SeleniumHQ/Selenium
Path: blob/trunk/rb/lib/selenium/webdriver/common/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
20
require 'forwardable'
21
22
module Selenium
23
module WebDriver
24
class Network
25
extend Forwardable
26
27
attr_reader :callbacks, :network
28
alias bidi network
29
30
def_delegators :network, :continue_with_auth, :continue_with_request, :continue_with_response
31
32
def initialize(bridge)
33
@network = BiDi::Network.new(bridge.bidi)
34
@callbacks = {}
35
end
36
37
def remove_handler(id)
38
intercept = callbacks[id]
39
network.remove_intercept(intercept['intercept'])
40
callbacks.delete(id)
41
end
42
43
def clear_handlers
44
callbacks.each_key { |id| remove_handler(id) }
45
end
46
47
def add_authentication_handler(username = nil, password = nil, *filter, pattern_type: nil, &block)
48
selected_block =
49
if username && password
50
proc { |auth| auth.authenticate(username, password) }
51
else
52
block
53
end
54
55
add_handler(
56
:auth_required,
57
BiDi::Network::PHASES[:auth_required],
58
BiDi::InterceptedAuth,
59
filter,
60
pattern_type: pattern_type,
61
&selected_block
62
)
63
end
64
65
def add_request_handler(*filter, pattern_type: nil, &block)
66
add_handler(
67
:before_request,
68
BiDi::Network::PHASES[:before_request],
69
BiDi::InterceptedRequest,
70
filter,
71
pattern_type: pattern_type,
72
&block
73
)
74
end
75
76
def add_response_handler(*filter, pattern_type: nil, &block)
77
add_handler(
78
:response_started,
79
BiDi::Network::PHASES[:response_started],
80
BiDi::InterceptedResponse,
81
filter,
82
pattern_type: pattern_type,
83
&block
84
)
85
end
86
87
private
88
89
def add_handler(event_type, phase, intercept_type, filter, pattern_type: nil)
90
intercept = network.add_intercept(phases: [phase], url_patterns: [filter].flatten, pattern_type: pattern_type)
91
callback_id = network.on(event_type) do |event|
92
request = event['request']
93
intercepted_item = intercept_type.new(network, request)
94
yield(intercepted_item)
95
end
96
97
callbacks[callback_id] = intercept
98
callback_id
99
end
100
end # Network
101
end # WebDriver
102
end # Selenium
103
104