Skip to content

Commit 4092cfd

Browse files
committed
[rb][bidi] network module events
1 parent a04a22f commit 4092cfd

File tree

3 files changed

+127
-0
lines changed

3 files changed

+127
-0
lines changed

rb/lib/selenium/webdriver/bidi.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ class BiDi
2323
autoload :Session, 'selenium/webdriver/bidi/session'
2424
autoload :LogInspector, 'selenium/webdriver/bidi/log_inspector'
2525
autoload :BrowsingContext, 'selenium/webdriver/bidi/browsing_context'
26+
autoload :NetworkInspector, 'selenium/webdriver/bidi/network_inspector'
2627

2728
def initialize(url:)
2829
@ws = WebSocketConnection.new(url: url)
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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 NetworkInspector
24+
EVENTS = {
25+
before_request_sent: 'beforeRequestSent',
26+
fetch_error: 'fetchError',
27+
response_completed: 'responseCompleted',
28+
response_started: 'responseStarted'
29+
}.freeze
30+
31+
def initialize(driver, browsing_context_ids = nil)
32+
unless driver.capabilities.web_socket_url
33+
raise Error::WebDriverError,
34+
'WebDriver instance must support BiDi protocol'
35+
end
36+
37+
@bidi = driver.bidi
38+
@bidi.session.subscribe('network.beforeRequestSent', browsing_context_ids)
39+
end
40+
41+
def before_request_sent(&block)
42+
on(:before_request_sent) do |params|
43+
before_request_sent_event(params, &block)
44+
end
45+
end
46+
47+
private
48+
49+
def on(event, &block)
50+
event = EVENTS[event] if event.is_a?(Symbol)
51+
@bidi.callbacks["network.#{event}"] << block
52+
end
53+
54+
def before_request_sent_event(params)
55+
yield(params)
56+
end
57+
end # NetworkInspector
58+
end # Bidi
59+
end # WebDriver
60+
end # Selenium
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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 NetworkInspector, only: {browser: %i[chrome edge firefox]} do
26+
let(:empty_page) { '/bidi/emptyPage.html' }
27+
let(:empty_text) { '/bidi/emptyText.txt' }
28+
let(:redirected_http_equiv) { '/bidi/redirected_http_equiv.html' }
29+
30+
it 'can listen to event before request is sent' do
31+
reset_driver!(web_socket_url: true) do |driver|
32+
before_request_event = []
33+
inspector = described_class.new(driver)
34+
inspector.before_request_sent { |event| before_request_event.push(event) }
35+
36+
driver.navigate.to url_for(empty_page)
37+
38+
wait.until { !before_request_event.nil? }
39+
40+
expect(before_request_event[0].dig('request', 'method')).to eq 'GET'
41+
url = before_request_event[0].dig('request', 'url')
42+
expect(url).to eq driver.current_url
43+
end
44+
end
45+
46+
it 'can request cookies' do
47+
reset_driver!(web_socket_url: true) do |driver|
48+
before_request_event = []
49+
inspector = described_class.new(driver)
50+
inspector.before_request_sent { |event| before_request_event.push(event) }
51+
52+
driver.navigate.to url_for(empty_text)
53+
driver.manage.add_cookie name: 'north',
54+
value: 'biryani'
55+
driver.navigate.refresh
56+
wait.until { !before_request_event.nil? }
57+
58+
cookies = before_request_event[0]['request']['cookies']
59+
puts "cookies = \n", cookies # prints nil
60+
expect(before_request_event[0].dig('request', 'method')).to eq 'GET'
61+
end
62+
end
63+
end
64+
end
65+
end
66+
end

0 commit comments

Comments
 (0)