0
0
Selenium Javatesting~10 mins

Shadow DOM element access in Selenium Java - Test Execution Trace

Choose your learning style9 modes available
Test Overview

This test opens a web page with a Shadow DOM element, accesses the shadow root, finds a button inside it, clicks the button, and verifies the button's text changes as expected.

Test Code - JUnit with Selenium WebDriver
Selenium Java
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;

public class ShadowDomTest {
    WebDriver driver;

    @BeforeEach
    public void setUp() {
        driver = new ChromeDriver();
    }

    @AfterEach
    public void tearDown() {
        if (driver != null) {
            driver.quit();
        }
    }

    @Test
    public void testShadowDomButtonClick() {
        driver.get("https://example.com/shadowdom");

        // Locate shadow host element
        WebElement shadowHost = driver.findElement(By.cssSelector("#shadow-host"));

        // Access shadow root using JavaScript
        JavascriptExecutor js = (JavascriptExecutor) driver;
        WebElement shadowRoot = (WebElement) js.executeScript("return arguments[0].shadowRoot", shadowHost);

        // Find button inside shadow root
        WebElement shadowButton = shadowRoot.findElement(By.cssSelector("button#shadow-button"));

        // Click the button
        shadowButton.click();

        // Verify button text changed
        String buttonText = shadowButton.getText();
        assertEquals("Clicked", buttonText, "Button text should change to 'Clicked' after click.");
    }
}
Execution Trace - 8 Steps
StepActionSystem StateAssertionResult
1Test starts and Chrome browser opensBrowser window is open, ready to navigate-PASS
2Navigates to https://example.com/shadowdomPage with shadow DOM loaded-PASS
3Finds shadow host element with CSS selector '#shadow-host'Shadow host element located in DOMElement '#shadow-host' is presentPASS
4Executes JavaScript to get shadow root from shadow hostShadow root accessed successfullyShadow root is not nullPASS
5Finds button inside shadow root with CSS selector 'button#shadow-button'Button inside shadow DOM locatedButton element is present inside shadow rootPASS
6Clicks the shadow DOM buttonButton clicked, UI updates accordingly-PASS
7Gets button text after clickButton text updated to 'Clicked'Button text equals 'Clicked'PASS
8Test ends and browser closesBrowser closed cleanly-PASS
Failure Scenario
Failing Condition: Shadow host element '#shadow-host' is not found or shadow root is null
Execution Trace Quiz - 3 Questions
Test your understanding
Which Selenium method is used to access the shadow root in this test?
AJavascriptExecutor executing 'return arguments[0].shadowRoot'
Bdriver.findElement(By.shadowRoot())
Cdriver.switchTo().shadowRoot()
Ddriver.getShadowRoot()
Key Result
When testing Shadow DOM elements with Selenium, use JavaScript execution to access the shadow root before locating elements inside it. This ensures reliable interaction with shadow DOM content.