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 test for detached shadow root error type #14267

Merged
merged 3 commits into from
Jul 19, 2024

Conversation

aguspe
Copy link
Contributor

@aguspe aguspe commented Jul 16, 2024

User description

Description

While investigating #13580, I noticed that the shadow root implementation done here:

d44b41b

Returns the following error:

Selenium::WebDriver::Error::DetachedShadowRootError: detached shadow root: shadow root is detached from the current frame
  (Session info: chrome=126.0.6478.127)

  0) Selenium::WebDriver::ShadowRoot raises error if the shadow root is detached
     Failure/Error: raise ex, cause: cause

     Selenium::WebDriver::Error::DetachedShadowRootError:
       detached shadow root: shadow root is detached from the current frame
         (Session info: chrome=126.0.6478.127)
     # ./lib/selenium/webdriver/remote/response.rb:62:in `add_cause'
     # ./lib/selenium/webdriver/remote/response.rb:41:in `error'
     # ./lib/selenium/webdriver/remote/response.rb:52:in `assert_ok'
     # ./lib/selenium/webdriver/remote/response.rb:34:in `initialize'
     # ./lib/selenium/webdriver/remote/http/common.rb:101:in `new'
     # ./lib/selenium/webdriver/remote/http/common.rb:101:in `create_response'
     # ./lib/selenium/webdriver/remote/http/default.rb:103:in `request'
     # ./lib/selenium/webdriver/remote/http/common.rb:67:in `call'
     # ./lib/selenium/webdriver/remote/bridge.rb:675:in `execute'
     # ./lib/selenium/webdriver/remote/bridge.rb:541:in `find_element_by'
     # ./lib/selenium/webdriver/common/search_context.rb:71:in `find_element'
     # ./spec/integration/selenium/webdriver/shadow_root_spec.rb:47:in `block (2 levels) in <module:WebDriver>'
     # ------------------
     # --- Caused by: ---
     # Selenium::WebDriver::Error::WebDriverError:
     #   0   chromedriver                        0x0000000102712a10 chromedriver + 4385296

Which is the expected DetachedShadowRootError, so I just added a test on the shadowroot expect to validate the implementation and we should remove the rb label from #13580

Motivation and Context

Tests such as this are important to validate we throw the expected error, when as in this case the shadow root gets detached from the element

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)

Checklist

  • I have read the contributing document.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have added tests to cover my changes.
  • All new and existing tests passed.

PR Type

Tests


Description

  • Added a new test case to verify that DetachedShadowRootError is raised when a shadow root is detached.
  • The test is excluded for Safari due to a NoMethodError.

Changes walkthrough 📝

Relevant files
Tests
shadow_root_spec.rb
Add test case for detached shadow root error                         

rb/spec/integration/selenium/webdriver/shadow_root_spec.rb

  • Added a new test case to check for DetachedShadowRootError.
  • The test navigates to a simple HTML page, attaches a shadow root to a
    div, removes the div, and expects an error when accessing the shadow
    root.
  • +9/-0     

    💡 PR-Agent usage:
    Comment /help on the PR to get a list of all available PR-Agent tools and their descriptions

    Copy link

    PR Reviewer Guide 🔍

    ⏱️ Estimated effort to review: 2 🔵🔵⚪⚪⚪
    🧪 PR contains tests
    🔒 No security concerns identified
    ⚡ Key issues to review

    Test Coverage
    Ensure that the new test case 'raises error if the shadow root is detached' covers all necessary scenarios and edge cases for the DetachedShadowRootError. Consider adding assertions for the error message content to ensure it matches expected output.

    Copy link

    PR Code Suggestions ✨

    CategorySuggestion                                                                                                                                    Score
    Best practice
    Verify shadow root detachment before testing its error handling

    To ensure that the shadow root is properly detached before testing its behavior, add
    an assertion to check the detachment status.

    rb/spec/integration/selenium/webdriver/shadow_root_spec.rb [46]

     driver.execute_script('arguments[0].remove();', div)
    +expect(div.shadow_root).to be_nil
     
    • Apply this suggestion
    Suggestion importance[1-10]: 9

    Why: Adding an assertion to verify the shadow root detachment ensures the test's accuracy and reliability, making it a valuable improvement.

    9
    Possible bug
    Add a nil check for the div element after it is found to ensure it exists

    It's recommended to add a check to ensure that div is not nil after finding the
    element. This will prevent potential errors if the element is not found on the page.

    rb/spec/integration/selenium/webdriver/shadow_root_spec.rb [43]

     div = driver.find_element(css: 'div')
    +raise 'Div element not found' if div.nil?
     
    • Apply this suggestion
    Suggestion importance[1-10]: 8

    Why: Adding a nil check for the div element is a good practice to prevent potential runtime errors if the element is not found, enhancing the robustness of the test.

    8
    Enhancement
    Use a before block for common setup steps to enhance test efficiency

    Consider using before block to navigate to the page and set up the shadow root once
    for tests that require similar setup, improving test efficiency and reducing
    redundancy.

    rb/spec/integration/selenium/webdriver/shadow_root_spec.rb [42-44]

    -driver.navigate.to url_for('simpleTest.html')
    -div = driver.find_element(css: 'div')
    -driver.execute_script('arguments[0].attachShadow({ mode: "open" });', div)
    +before do
    +  driver.navigate.to url_for('simpleTest.html')
    +  div = driver.find_element(css: 'div')
    +  driver.execute_script('arguments[0].attachShadow({ mode: "open" });', div)
    +end
     
    • Apply this suggestion
    Suggestion importance[1-10]: 7

    Why: Using a before block for common setup steps can improve test efficiency and reduce redundancy, although it might not be necessary if the setup is unique to this test.

    7

    @p0deje p0deje merged commit 5761c71 into SeleniumHQ:trunk Jul 19, 2024
    21 of 23 checks passed
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Projects
    None yet
    Development

    Successfully merging this pull request may close these issues.

    None yet

    2 participants