Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
SeleniumHQ
GitHub Repository: SeleniumHQ/Selenium
Path: blob/trunk/rb/spec/integration/selenium/webdriver/bidi/network_spec.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_relative '../spec_helper'
21
22
module Selenium
23
module WebDriver
24
class BiDi
25
describe Network, exclusive: {bidi: true, reason: 'only executed when bidi is enabled'},
26
only: {browser: %i[chrome edge firefox]} do
27
after { |example| reset_driver!(example: example) }
28
29
it 'adds an intercept' do
30
network = described_class.new(driver.bidi)
31
intercept = network.add_intercept(phases: [described_class::PHASES[:before_request]])
32
expect(intercept).not_to be_nil
33
end
34
35
it 'adds an intercept with a default pattern type' do
36
network = described_class.new(driver.bidi)
37
pattern = 'http://localhost:4444/formPage.html'
38
intercept = network.add_intercept(phases: [described_class::PHASES[:before_request]], url_patterns: pattern)
39
expect(intercept).not_to be_nil
40
end
41
42
it 'adds an intercept with a url pattern' do
43
network = described_class.new(driver.bidi)
44
pattern = 'http://localhost:4444/formPage.html'
45
intercept = network.add_intercept(phases: [described_class::PHASES[:before_request]],
46
url_patterns: pattern,
47
pattern_type: :url)
48
expect(intercept).not_to be_nil
49
end
50
51
it 'removes an intercept' do
52
network = described_class.new(driver.bidi)
53
intercept = network.add_intercept(phases: [described_class::PHASES[:before_request]])
54
expect(network.remove_intercept(intercept['intercept'])).to be_empty
55
end
56
57
it 'continues with auth' do
58
username = SpecSupport::RackServer::TestApp::BASIC_AUTH_CREDENTIALS.first
59
password = SpecSupport::RackServer::TestApp::BASIC_AUTH_CREDENTIALS.last
60
network = described_class.new(driver.bidi)
61
phases = [Selenium::WebDriver::BiDi::Network::PHASES[:auth_required]]
62
network.add_intercept(phases: phases)
63
network.on(:auth_required) do |event|
64
request_id = event['request']['request']
65
network.continue_with_auth(request_id, username, password)
66
end
67
68
driver.navigate.to url_for('basicAuth')
69
expect(driver.find_element(tag_name: 'h1').text).to eq('authorized')
70
end
71
72
it 'continues without auth' do
73
network = described_class.new(driver.bidi)
74
network.add_intercept(phases: [described_class::PHASES[:auth_required]])
75
network.on(:auth_required) do |event|
76
request_id = event['request']['request']
77
network.continue_without_auth(request_id)
78
end
79
80
expect { driver.navigate.to url_for('basicAuth') }.to raise_error(Error::WebDriverError)
81
end
82
83
it 'cancels auth' do
84
network = described_class.new(driver.bidi)
85
network.add_intercept(phases: [described_class::PHASES[:auth_required]])
86
network.on(:auth_required) do |event|
87
request_id = event['request']['request']
88
network.cancel_auth(request_id)
89
end
90
91
driver.navigate.to url_for('basicAuth')
92
expect(driver.find_element(tag_name: 'pre').text).to eq('Login please')
93
end
94
95
it 'continues request' do
96
network = described_class.new(driver.bidi)
97
network.add_intercept(phases: [described_class::PHASES[:before_request]])
98
network.on(:before_request) do |event|
99
request_id = event['request']['request']
100
network.continue_request(id: request_id)
101
end
102
103
driver.navigate.to url_for('formPage.html')
104
expect(driver.find_element(name: 'login')).to be_displayed
105
end
106
107
it 'fails request' do
108
network = described_class.new(driver.bidi)
109
network.add_intercept(phases: [described_class::PHASES[:before_request]])
110
network.on(:before_request) do |event|
111
request_id = event['request']['request']
112
network.fail_request(request_id)
113
end
114
115
expect { driver.navigate.to url_for('formPage.html') }.to raise_error(Error::WebDriverError)
116
end
117
118
it 'continues response' do
119
network = described_class.new(driver.bidi)
120
network.add_intercept(phases: [described_class::PHASES[:response_started]])
121
network.on(:response_started) do |event|
122
request_id = event['request']['request']
123
network.continue_response(id: request_id)
124
end
125
126
driver.navigate.to url_for('formPage.html')
127
expect(driver.find_element(name: 'login')).to be_displayed
128
end
129
130
it 'provides response', except: {browser: :firefox,
131
reason: 'https://github.com/w3c/webdriver-bidi/issues/747'} do
132
network = described_class.new(driver.bidi)
133
network.add_intercept(phases: [described_class::PHASES[:response_started]])
134
network.on(:response_started) do |event|
135
request_id = event['request']['request']
136
network.provide_response(
137
id: request_id,
138
status: 200,
139
headers: [
140
{
141
name: 'foo',
142
value: {
143
type: 'string',
144
value: 'bar'
145
}
146
}
147
],
148
body: {
149
type: 'string',
150
value: '<html><head><title>Hello World!</title></head><body/></html>'
151
}
152
)
153
end
154
155
driver.navigate.to url_for('formPage.html')
156
source = driver.page_source
157
expect(source).not_to include('There should be a form here:')
158
expect(source).to include('Hello World!')
159
end
160
161
it 'sets the cache to bypass' do
162
browsing_context = BrowsingContext.new(driver).create
163
network = described_class.new(driver.bidi)
164
network.set_cache_behavior('bypass', browsing_context)
165
expect(network.set_cache_behavior('bypass', browsing_context)).to be_a(Hash)
166
end
167
168
it 'sets the cache to default' do
169
browsing_context = BrowsingContext.new(driver).create
170
network = described_class.new(driver.bidi)
171
expect(network.set_cache_behavior('default', browsing_context)).to be_a(Hash)
172
end
173
end
174
end
175
end
176
end
177
178