Rayrun
โ† Back to Discord Forum

Detached but visible component

How is possible Playwright check a given component is visible but detached from DOM? Unfortunately I don't have the scenario anymore, but these are the details:

  • Application under test: web application with Blazor Server
  • Test framework: C#, xUnit
  • Consider the same page with dynamic components: the components are visible or not (detached) considering the user interactions
  • Test A should interact with certain components
  • Test B should interact with other components (in a new window - IBrowserContext.NewPageAsync)
  • When running both tests together somehow the function Locator("component from Test A").IsVisibleAsync returns true - how??
    • then when trying to interact with that component it raises the exception - which is expected. I don't know what is happening and even what to look for to understand it better. Tips?

This thread is trying to answer question "How is it possible for Playwright to check if a given component is visible but detached from the DOM?"

3 replies

Elements must be attached to the DOM to be visible.

Collect Playwright traces to debug your tests ๐Ÿ˜‰

You will be able to travel in time in your tests and see the state of app at each step.

Answer

Interacting with Detached Components in Playwright

Playwright allows you to interact with components that are detached from the DOM. This means they're not part of the document's DOM tree, but they're still rendered and can be interacted with.

How Does Playwright Do This?

Playwright checks if a component is rendered in memory or in a virtual environment. It uses locators to identify elements on a web page, including components. This way, Playwright can find and interact with components even if they're detached from the DOM.

Working with Shadow DOM

Playwright's locators work with elements within Shadow DOM by default. However, locating by XPath doesn't pierce shadow roots, and closed-mode shadow roots aren't supported.

Checking If a Component Is Visible But Detached

You can use Playwright's API to check if a component is visible but detached from the DOM. Here's how:

// Check if a component is visible but detached from the DOM
const isVisibleButDetached = await page.isVisible('your-component-selector');

The isVisible method lets you check if an element or component is visible on the page, even if it's detached from the document structure.

Why Is This Useful?

This feature gives you flexibility in testing scenarios where components may be dynamically loaded or rendered conditionally. You can effectively test and interact with components that aren't directly attached to the browser's DOM at all times.

In short, Playwright lets you check if components that are detached from the document structure are visible, thanks to its powerful testing framework and locator capabilities. It lets you verify if a component is rendered in memory or virtually present, even when it's not attached to the DOM.

Related Discord Threads

TwitterGitHubLinkedIn
AboutQuestionsDiscord ForumBrowser ExtensionTagsQA Jobs

Rayrun is a community for QA engineers. I am constantly looking for new ways to add value to people learning Playwright and other browser automation frameworks. If you have feedback, email [email protected].