You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
These structs include some complex collections that have defined types in the spec, but are represented here as hashes, rather than other structs. Should they be structs instead? (For context, this PR was motivated by a desire to get at the information in log_entry_instance.source['context'], which would be nicer to get as log_entry_instance.source.context.)
Caveat: args and stack_trace already existed as arrays of hashes and hashes of arrays of hashes respectively, so maybe making nice structs would be too much of a breaking change?
The spec now has a GenericLogEntry type for things that aren’t JS logs or exceptions (I think this is for various warnings and errors logged by the browser itself, like CORS blocking or content integrity problems). That’s not handled here; should I try to add it? Happy to do that work here or in a separate PR if desired.
It’s not currently a problem since Script doesn’t ever add handlers for other types of messages that would show up this way, but someone could theoretically subscribe to them via an instance of LogHandler and that might not work out so well.
Motivation and Context
The BiDi spec has added more fields since LogHandler was first implemented, and the current functionality is missing important information, like the ID of the browser context a log entry came from (in entry.source.context). This just updates the structs with the fields currently expected by the spec.
Types of changes
Bug fix (non-breaking change which fixes an issue)
New feature (non-breaking change which adds functionality)
Breaking change (fix or feature that would cause existing functionality to change)
All new and existing tests passed. (I couldn’t get all tests to pass locally even before I touched things, but all tests that I expect could have been affected were passing and still are.)
PR Type
Enhancement, Tests
Description
Updated the ConsoleLogEntry and JavaScriptLogEntry structs in LogHandler to include the source field, aligning with the W3C spec as of 2024-07-08.
Added method, args, and stack_trace fields to ConsoleLogEntry struct.
Introduced a helper method a_stack_frame in the test suite to match script.StackFrame objects.
Enhanced existing tests to validate the new fields in ConsoleLogEntry and JavaScriptLogEntry, including detailed checks for source, args, and stack_trace.
Changes walkthrough 📝
Relevant files
Enhancement
log_handler.rb
Update BiDi log entry structs to match W3C spec
rb/lib/selenium/webdriver/bidi/log_handler.rb
Updated ConsoleLogEntry and JavaScriptLogEntry structs to include source.
Added method, args, and stack_trace to ConsoleLogEntry.
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.
Possible Bug:
The PR introduces changes to the ConsoleLogEntry and JavaScriptLogEntry structs, adding new fields such as stack_trace, source, method, and args. Ensure these fields are populated correctly as per the updated W3C spec and that they integrate seamlessly with existing functionalities.
Data Structure Consistency:
The PR uses hashes to represent complex collections. Consider whether using more structured types might improve code maintainability and readability.
Add type validation to ensure ConsoleLogEntry and JavaScriptLogEntry are initialized with correct types
Consider adding a validation to ensure that ConsoleLogEntry and JavaScriptLogEntry are initialized with the correct types for each attribute. This can help catch errors early if incorrect data is passed.
-ConsoleLogEntry = BiDi::Struct.new(:level, :text, :timestamp, :stack_trace, :type, :source, :method, :args)-JavaScriptLogEntry = BiDi::Struct.new(:level, :text, :timestamp, :stack_trace, :type, :source)+ConsoleLogEntry = BiDi::Struct.new(:level, :text, :timestamp, :stack_trace, :type, :source, :method, :args) do+ def initialize(*args)+ super+ raise TypeError, "level must be a String" unless level.is_a?(String)+ raise TypeError, "timestamp must be an Integer" unless timestamp.is_a?(Integer)+ # Add more type checks as needed+ end+end+JavaScriptLogEntry = BiDi::Struct.new(:level, :text, :timestamp, :stack_trace, :type, :source) do+ def initialize(*args)+ super+ raise TypeError, "level must be a String" unless level.is_a?(String)+ raise TypeError, "timestamp must be an Integer" unless timestamp.is_a?(Integer)+ # Add more type checks as needed+ end+end+
Apply this suggestion
Suggestion importance[1-10]: 7
Why: Adding type validation is a good practice to ensure data integrity and catch errors early. The suggestion correctly targets the new code in the PR.
7
Maintainability
Extract repeated source matching code into a helper method for better readability
To improve readability and maintainability, consider extracting the repeated expect(log_entry.source).to match(...) code into a helper method.
Why: Extracting repeated code into a helper method improves maintainability and readability. The suggestion is relevant and targets new code in the PR.
6
Enhancement
Add a test to check for the presence of optional fields in JavaScriptLogEntry
Consider adding a test to check for the presence of optional fields in JavaScriptLogEntry to ensure they are handled correctly.
-expect(log_entry.stack_trace).to match(- # Some browsers include stack traces from parts of the runtime, so we- # just check the first frames that come from user code.+expect(log_entry.stack_trace).to be_nil.or match(
'callFrames' => start_with(
a_stack_frame('functionName' => 'createError'),
a_stack_frame('functionName' => 'onclick')
)
)
+# Additional test for optional fields+expect(log_entry).to respond_to(:optional_field)
Apply this suggestion
Suggestion importance[1-10]: 5
Why: The suggestion to add tests for optional fields is valid and enhances the robustness of the test suite. However, the existing code already handles optional fields in stack traces, making this a moderate improvement.
5
Best practice
Use be_a_kind_of for type checking to handle inheritance and ensure consistency
To ensure consistency, consider using be_a_kind_of instead of be_a for type checking, as it is more flexible and can handle inheritance.
Why: While using be_a_kind_of can be more flexible than be_a, this change is not crucial and the existing be_a is sufficient for the current test cases.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
User description
Description
This updates the structs used by the BiDi
Script
andLogHandler
classes to represent JS logs and errors so that they match the W3C spec as of 2024-07-08. This is meant to resolve the discussion about the context of JS Logs and errors from #13992.There are a couple open questions here:
These structs include some complex collections that have defined types in the spec, but are represented here as hashes, rather than other structs. Should they be structs instead? (For context, this PR was motivated by a desire to get at the information in
log_entry_instance.source['context']
, which would be nicer to get aslog_entry_instance.source.context
.)Caveat:
args
andstack_trace
already existed as arrays of hashes and hashes of arrays of hashes respectively, so maybe making nice structs would be too much of a breaking change?The spec now has a
GenericLogEntry
type for things that aren’t JS logs or exceptions (I think this is for various warnings and errors logged by the browser itself, like CORS blocking or content integrity problems). That’s not handled here; should I try to add it? Happy to do that work here or in a separate PR if desired.It’s not currently a problem since
Script
doesn’t ever add handlers for other types of messages that would show up this way, but someone could theoretically subscribe to them via an instance ofLogHandler
and that might not work out so well.Motivation and Context
The BiDi spec has added more fields since
LogHandler
was first implemented, and the current functionality is missing important information, like the ID of the browser context a log entry came from (inentry.source.context
). This just updates the structs with the fields currently expected by the spec.Types of changes
Checklist
PR Type
Enhancement, Tests
Description
ConsoleLogEntry
andJavaScriptLogEntry
structs inLogHandler
to include thesource
field, aligning with the W3C spec as of 2024-07-08.method
,args
, andstack_trace
fields toConsoleLogEntry
struct.a_stack_frame
in the test suite to matchscript.StackFrame
objects.ConsoleLogEntry
andJavaScriptLogEntry
, including detailed checks forsource
,args
, andstack_trace
.Changes walkthrough 📝
log_handler.rb
Update BiDi log entry structs to match W3C spec
rb/lib/selenium/webdriver/bidi/log_handler.rb
ConsoleLogEntry
andJavaScriptLogEntry
structs to includesource
.method
,args
, andstack_trace
toConsoleLogEntry
.script_spec.rb
Enhance BiDi script tests for updated log entry structs
rb/spec/integration/selenium/webdriver/bidi/script_spec.rb
a_stack_frame
for matchingscript.StackFrame
objects.
ConsoleLogEntry
andJavaScriptLogEntry
.source
,args
, andstack_trace
.