Skip to content

[java] Normalising handling of Shadow Dom elements. #16149

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

Merged
merged 1 commit into from
Aug 9, 2025

Conversation

diemol
Copy link
Member

@diemol diemol commented Aug 8, 2025

User description

🔗 Related Issues

Fixes #15961

💥 What does this PR do?

This applies the same logic we already apply
for findElement and findElements, as id,
name, and className are not W3C locators.

🔧 Implementation Notes

We probably forgot to do this when shadow root was implemented?

💡 Additional Considerations

🔄 Types of changes

  • Bug fix (backwards compatible)

PR Type

Bug fix


Description

  • Normalize shadow DOM element finding to use W3C locators

  • Apply existing parameter amendment logic to shadow root operations

  • Fix inconsistent handling between regular and shadow DOM elements


Diagram Walkthrough

flowchart LR
  A["Shadow DOM findElement/findElements"] --> B["W3C Parameter Amendment"] 
  C["Regular findElement/findElements"] --> B
  B --> D["Consistent W3C Locator Handling"]
Loading

File Walkthrough

Relevant files
Bug fix
W3CHttpCommandCodec.java
Add shadow DOM commands to parameter amendment                     

java/src/org/openqa/selenium/remote/codec/w3c/W3CHttpCommandCodec.java

  • Added FIND_ELEMENT_FROM_SHADOW_ROOT and FIND_ELEMENTS_FROM_SHADOW_ROOT
    to switch statement
  • Extended parameter amendment logic to shadow DOM operations
  • Ensures consistent W3C locator handling across all element finding
    methods
+2/-0     

This applies the same logic we already apply
for findElement and findElements, as `id`,
`name`, and `className` are not W3C locators.

Fixes #15961
@selenium-ci selenium-ci added the C-java Java Bindings label Aug 8, 2025
Copy link
Contributor

qodo-merge-pro bot commented Aug 8, 2025

PR Reviewer Guide 🔍

Here are some key observations to aid the review process:

⏱️ Estimated effort to review: 2 🔵🔵⚪⚪⚪
🧪 No relevant tests
🔒 No security concerns identified
⚡ Recommended focus areas for review

Null Handling

Ensure parameters always contains non-null using and value for new shadow root cases; otherwise casting and downstream logic could NPE or misbehave. Validate or guard before casting.

String using = (String) parameters.get("using");
Object value = parameters.get("value");

if (value instanceof String) {
  String stringValue = (String) value;
Coverage Parity

Confirm the same amendment logic applied to other find operations fully applies to shadow root variants, including any special-case mappings (e.g., mapping id/name/className to css selectors) and side effects like logging or metrics.

case FIND_ELEMENT_FROM_SHADOW_ROOT:
case FIND_ELEMENTS_FROM_SHADOW_ROOT:
  String using = (String) parameters.get("using");
  Object value = parameters.get("value");

  if (value instanceof String) {
    String stringValue = (String) value;

Copy link
Contributor

qodo-merge-pro bot commented Aug 8, 2025

PR Code Suggestions ✨

Explore these optional code suggestions:

CategorySuggestion                                                                                                                                    Impact
Possible issue
Add null/empty parameter checks

Guard against null parameters to avoid potential NullPointerException when
shadow root find commands are invoked without a params map. Add an early return
for empty input and validate required keys before casts. This keeps behavior
consistent with other branches and prevents runtime crashes.

java/src/org/openqa/selenium/remote/codec/w3c/W3CHttpCommandCodec.java [178-183]

 case FIND_ELEMENT_FROM_SHADOW_ROOT:
 case FIND_ELEMENTS_FROM_SHADOW_ROOT:
+  if (parameters == null || parameters.isEmpty()) {
+    return parameters;
+  }
   String using = (String) parameters.get("using");
   Object value = parameters.get("value");
+
+  if (using == null || value == null) {
+    return parameters;
+  }
 
   if (value instanceof String) {
     ...

[To ensure code accuracy, apply this suggestion manually]

Suggestion importance[1-10]: 6

__

Why: The suggestion correctly points out that parameters could be null, leading to a NullPointerException. Adding checks for null or empty parameters and for the presence of using and value keys improves the method's robustness, which is a valuable improvement.

Low
  • More

@diemol diemol merged commit 8b45f02 into trunk Aug 9, 2025
63 of 64 checks passed
@diemol diemol deleted the java_normalise_handling_shadow_dom_elements branch August 9, 2025 08:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[🐛 Bug]: FindElements By ClassName doesn't work inside Shadow root
2 participants