Skip to content

Commit c145a83

Browse files
Mr0grogp0deje
authored andcommitted
Update Ruby BiDi script structs to match spec
This updates the structs used by the BiDi `Script` and `LogHandler` classes to match the W3C spec as of 2024-07-08. It also makes the tests slightly more detailed.
1 parent 1345193 commit c145a83

File tree

2 files changed

+48
-5
lines changed

2 files changed

+48
-5
lines changed

rb/lib/selenium/webdriver/bidi/log_handler.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ module Selenium
2121
module WebDriver
2222
class BiDi
2323
class LogHandler
24-
ConsoleLogEntry = BiDi::Struct.new(:level, :text, :timestamp, :method, :args, :type)
25-
JavaScriptLogEntry = BiDi::Struct.new(:level, :text, :timestamp, :stack_trace, :type)
24+
ConsoleLogEntry = BiDi::Struct.new(:level, :text, :timestamp, :stack_trace, :type, :source, :method, :args)
25+
JavaScriptLogEntry = BiDi::Struct.new(:level, :text, :timestamp, :stack_trace, :type, :source)
2626

2727
def initialize(bidi)
2828
@bidi = bidi

rb/spec/integration/selenium/webdriver/bidi/script_spec.rb

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,20 @@ module WebDriver
2525
only: {browser: %i[chrome edge firefox]} do
2626
after { |example| reset_driver!(example: example) }
2727

28+
# Helper to match the expected pattern of `script.StackFrame` objects.
29+
# https://w3c.github.io/webdriver-bidi/#type-script-StackFrame
30+
#
31+
# Pass in any fields you want to check more specific values for, e.g:
32+
# a_stack_frame('functionName' => 'someFunction')
33+
def a_stack_frame(**options)
34+
include({
35+
'columnNumber' => an_instance_of(Integer),
36+
'functionName' => an_instance_of(String),
37+
'lineNumber' => an_instance_of(Integer),
38+
'url' => an_instance_of(String)
39+
}.merge(options))
40+
end
41+
2842
it 'errors when bidi not enabled' do
2943
reset_driver!(web_socket_url: false) do |driver|
3044
msg = /BiDi must be enabled by setting #web_socket_url to true in options class/
@@ -45,10 +59,27 @@ module WebDriver
4559
expect(log_entries.size).to eq(1)
4660
log_entry = log_entries.first
4761
expect(log_entry).to be_a BiDi::LogHandler::ConsoleLogEntry
62+
expect(log_entry.type).to eq 'console'
4863
expect(log_entry.level).to eq 'info'
4964
expect(log_entry.method).to eq 'log'
5065
expect(log_entry.text).to eq 'Hello, world!'
51-
expect(log_entry.type).to eq 'console'
66+
expect(log_entry.args).to eq [
67+
{'type' => 'string', 'value' => 'Hello, world!'}
68+
]
69+
expect(log_entry.timestamp).to be_an_integer
70+
expect(log_entry.source).to match(
71+
'context' => an_instance_of(String),
72+
'realm' => an_instance_of(String)
73+
)
74+
# Stack traces on console messages are optional.
75+
expect(log_entry.stack_trace).to be_nil.or match(
76+
# Some browsers include stack traces from parts of the runtime, so we
77+
# just check the first frames that come from user code.
78+
'callFrames' => start_with(
79+
a_stack_frame('functionName' => 'helloWorld'),
80+
a_stack_frame('functionName' => 'onclick')
81+
)
82+
)
5283
end
5384

5485
it 'logs multiple console messages' do
@@ -97,10 +128,22 @@ module WebDriver
97128
expect(log_entries.size).to eq(1)
98129
log_entry = log_entries.first
99130
expect(log_entry).to be_a BiDi::LogHandler::JavaScriptLogEntry
100-
expect(log_entry.level).to eq 'error'
101131
expect(log_entry.type).to eq 'javascript'
132+
expect(log_entry.level).to eq 'error'
102133
expect(log_entry.text).to eq 'Error: Not working'
103-
expect(log_entry.stack_trace).not_to be_empty
134+
expect(log_entry.timestamp).to be_an_integer
135+
expect(log_entry.source).to match(
136+
'context' => an_instance_of(String),
137+
'realm' => an_instance_of(String)
138+
)
139+
expect(log_entry.stack_trace).to match(
140+
# Some browsers include stack traces from parts of the runtime, so we
141+
# just check the first frames that come from user code.
142+
'callFrames' => start_with(
143+
a_stack_frame('functionName' => 'createError'),
144+
a_stack_frame('functionName' => 'onclick')
145+
)
146+
)
104147
end
105148

106149
it 'errors removing non-existent handler' do

0 commit comments

Comments
 (0)