Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[rb] Add target type param to devtools #15416

Merged
merged 6 commits into from
Mar 14, 2025
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file.
16 changes: 16 additions & 0 deletions common/src/web/service_worker.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<!DOCTYPE html>
<html>
<head>
<title>Service Worker Test Page</title>
<script>
if (navigator.serviceWorker) {
navigator.serviceWorker.register("/service-worker.js", { scope: "/" })
.then(() => navigator.serviceWorker.ready)
.then(() => console.debug("[Companion]", "Service worker registered!"));
}
</script>
</head>
<body>
<h1>This page loads a service worker</h1>
</body>
</html>
2 changes: 1 addition & 1 deletion rb/lib/selenium/webdriver/common/driver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ def quit
bridge.quit
ensure
@service_manager&.stop
@devtools&.close
@devtools&.values&.map(&:close)
end

#
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,13 @@ module HasDevTools
# @return [DevTools]
#

def devtools
@devtools ||= begin
def devtools(target_type: 'page')
@devtools ||= {}
@devtools[target_type] ||= begin
require 'selenium/devtools'
Selenium::DevTools.version ||= devtools_version
Selenium::DevTools.load_version
Selenium::WebDriver::DevTools.new(url: devtools_url)
Selenium::WebDriver::DevTools.new(url: devtools_url, target_type: target_type)
end
end
end # HasDevTools
Expand Down
12 changes: 7 additions & 5 deletions rb/lib/selenium/webdriver/devtools.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ class DevTools
autoload :Request, 'selenium/webdriver/devtools/request'
autoload :Response, 'selenium/webdriver/devtools/response'

def initialize(url:)
def initialize(url:, target_type:)
@ws = WebSocketConnection.new(url: url)
@session_id = nil
start_session
start_session(target_type: target_type)
end

def close
Expand Down Expand Up @@ -81,10 +81,12 @@ def respond_to_missing?(method, *_args)

private

def start_session
def start_session(target_type:)
targets = target.get_targets.dig('result', 'targetInfos')
page_target = targets.find { |target| target['type'] == 'page' }
session = target.attach_to_target(target_id: page_target['targetId'], flatten: true)
found_target = targets.find { |target| target['type'] == target_type }
raise Error::WebDriverError, "Target type '#{target_type}' not found" unless found_target

session = target.attach_to_target(target_id: found_target['targetId'], flatten: true)
@session_id = session.dig('result', 'sessionId')
end

Expand Down
20 changes: 20 additions & 0 deletions rb/spec/integration/selenium/webdriver/devtools_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,26 @@ module WebDriver
}.to raise_error(RuntimeError, 'This is fine!')
end

describe '#target' do
it 'target type defaults to page' do
driver.devtools.page.navigate(url: url_for('xhtmlTest.html'))
expect(driver.devtools.target.get_target_info.dig('result', 'targetInfo', 'type')).to eq 'page'
end

it 'target type is service_worker' do
driver.devtools.page.navigate(url: url_for('service_worker.html'))
sleep 0.5 # wait for service worker to register
target = driver.devtools(target_type: 'service_worker').target
expect(target.get_target_info.dig('result', 'targetInfo', 'type')).to eq 'service_worker'
end

it 'throws an error for unknown target type' do
driver.devtools.page.navigate(url: url_for('xhtmlTest.html'))
expect { driver.devtools(target_type: 'unknown') }
.to raise_error(Selenium::WebDriver::Error::WebDriverError, "Target type 'unknown' not found")
end
end

describe '#register' do
let(:username) { SpecSupport::RackServer::TestApp::BASIC_AUTH_CREDENTIALS.first }
let(:password) { SpecSupport::RackServer::TestApp::BASIC_AUTH_CREDENTIALS.last }
Expand Down
Loading