Guides
Test your embed with Playwright
Find embed regions, open popovers, check states, build metrics, move and resize cards, and reach into custom charts with selectors that stay the same across releases.
Do this with your agent
audit-databrainHelp me with this Databrain task: Test your embed with Playwright. Start with a read-only check. Explain what you can verify and show any fix for my review. Ask before applying changes.
Select the embed by its part names. They stay the same across releases; class names and markup inside the embed do not.
Before you start
- Your page renders
<dbn-dashboard>or<dbn-metric>with a test guest token. - Named parts, selector helpers and
data-dbn-filter-keyrequire the plugin release that ships with this docs update ({{PLUGIN_VERSION}}or later). - Upgrade before replacing class selectors, flags or text keys. Older plugins ignore the new names.
Steps
-
Import the selector helpers, so your tests use the same names as the embed:
import { partSelector, stateToken } from '@databrainhq/plugin/parts'; partSelector('date-filter'); // '[part~="date-filter"]' partSelector(stateToken('date-filter-preset', 'selected')); // '[part~="date-filter-preset--selected"]' -
Find a region with
[part~="name"]. Playwright CSS locators go into open shadow roots, and the embed’s shadow root is open:await expect(page.locator('[part~="card-title"]').filter({ hasText: /^Revenue$/ })).toBeVisible(); -
Open a popover and check it. Menus and the date picker open outside the card, and the same locator still finds them:
await page.locator('[part~="date-filter"]').first().click(); await expect(page.locator('[part~="date-filter-menu"]')).toBeVisible(); -
Check a state with its token. The token is there only while the state is true:
await expect(page.locator('[part~="date-filter--open"]')).toHaveCount(1); await expect(page.locator('[part~="date-filter-preset--selected"]')).toHaveCount(1);
Your test now finds the date filter, its picker and the selected preset by name.
Build, edit and save a metric
Use a test dashboard embed with metric creation and editing enabled for its guest. Use the example fields or substitute fields from your test data. The metric builder has the same part names in both designs. Drag a column from the data panel onto Dimensions or Measures, then save:
const part = (name: string) => page.locator(`[part~="${name}"]`);
await part('create-metric').click();
await expect(part('metric-builder')).toBeVisible();
await part('builder-field').filter({ hasText: 'first_name' }).dragTo(part('builder-dimensions'));
await part('builder-field').filter({ hasText: 'customer_id' }).dragTo(part('builder-measures'));
await expect(part('builder-chart-area')).toBeVisible();
await part('builder-save').click();
await part('metric-name-input').fill('Orders by customer');
await part('metric-save-confirm').click();
await expect(part('card-title').filter({ hasText: 'Orders by customer' })).toBeVisible();To edit a saved metric, open it from its card and save again. metric-save-confirm also updates an edited metric:
const card = part('card').filter({ has: part('card-title').filter({ hasText: 'Orders by customer' }) }).first();
await card.hover();
if (!(await card.locator('[part~="card-edit"]').count())) {
await card.locator('[part~="card-actions"]').last().click(); // the v2 card keeps Edit metric in its menu
}
await part('card-edit').first().click();
await expect(part('metric-builder')).toBeVisible();
// ... change the metric ...
await part('builder-save').click();
await part('metric-name-input').fill('Orders by customer, top 10');
await part('metric-save-confirm').click();Use builder-chart-type to open the chart type picker. Close the picker before saving. The builder-back part closes the builder, builder-title is the metric name at the top, and builder-filters, builder-sort and builder-appearance open their panels. builder-appearance and builder-chart-area show after the metric has a field.
Move and resize cards
Customize layout must be on for the embed. The action is in the dashboard settings menu:
await page.getByRole('button', { name: 'Settings' }).click();
await part('layout-customize').click();
await expect(part('layout-save').first()).toBeVisible();
const card = part('card').first();
const handle = part('layout-resize-handle').filter({ visible: true }).first();
await handle.scrollIntoViewIfNeeded();
const h = (await handle.boundingBox())!;
await page.mouse.move(h.x + h.width / 2, h.y + h.height / 2);
await page.mouse.down();
await page.mouse.move(h.x + 60, h.y + 90, { steps: 15 }); // resize: drag the corner handle
await page.mouse.up();
const c = (await card.boundingBox())!;
await page.mouse.move(c.x + c.width / 2, c.y + 30);
await page.mouse.down();
await page.mouse.move(c.x + c.width / 2 + 40, c.y + 40, { steps: 5 });
await page.mouse.move(c.x + c.width / 2, c.y + c.height + 220, { steps: 20 }); // move: drag the card itself
await page.mouse.up();
await part('layout-save').click(); // or part('layout-discard') to leave the layout as it wasThe card is what the viewer drags to move it; there is no separate drag handle. Use small steps in mouse.move, so the grid sees a drag and not a click.
Pick one card
A card element carries its id in the id attribute. The id comes from the dashboard and stays the same across reloads and releases, so a test can keep it:
const revenue = page.locator('[part~="card"][id="3f2b6c1e-8a4d-4c2e-9b1f-2d7e5a0c4b11"]');Or match the card by its title text, as in the examples above. Two cards can have the same title.
Pick one dashboard filter
Match data-dbn-filter-key on the filter part. The attribute uses the saved column key and stays the same after a label change or reload. A variable filter without a column can use its saved placeholder, such as {{global_region}}.
This example picks a single-select filter with the key region. If your page has more than one dashboard, start with a locator for the dashboard you want. Within that dashboard, a filter kind such as filter-select can narrow a shared key to one match.
const region = page
.locator('dbn-dashboard')
.locator('[part~="filter"][data-dbn-filter-key="region"][part~="filter-select"]');
await expect(region).toHaveCount(1);A filter that the dashboard settings put in the filter panel, not in the filter bar, shows in a panel: select Filters in the legacy design, or More filters in v2. On a small screen, open Dashboard Filters and select the Others tab. These panels open in #dbn-portal, not in dbn-dashboard. Open the panel first, then find the filter through #dbn-portal, and keep only the visible match, because a closed panel can stay in the page:
const regionInPanel = page
.locator('#dbn-portal')
.locator('[part~="filter"][data-dbn-filter-key="region"][part~="filter-select"]')
.filter({ visible: true });
await expect(regionInPanel).toHaveCount(1);Two embeds on one page share #dbn-portal, so keep only one panel open when you use this locator.
Custom charts
A custom chart draws inside two frames. Go through both with frameLocator:
const chart = page
.frameLocator('dbn-dashboard iframe') // the frame in the card
.frameLocator('iframe'); // your chart's frame
await expect(chart.locator('.my-chart-bar')).toHaveCount(5);.my-chart-bar is a class in your own chart markup. The card around it takes the card parts.
Other tools
// Cypress
cy.get('[part~="card"]', { includeShadowDom: true }).should('have.length.greaterThan', 0);// Plain DOM
const cards = document.querySelector('dbn-dashboard').shadowRoot.querySelectorAll('[part~="card"]');In the legacy design, popovers open in #dbn-portal, an element at the end of <body> with its own shadow root. With plain DOM, query document.querySelector('#dbn-portal').shadowRoot for them.
If something goes wrong
- The locator finds two elements. One element can carry two names, such as
part="menu card-menu", and a name can match text in more than one card. Use the narrower name, or filter by exact text:{ hasText: /^Revenue$/ }. - A state token is never there. Each part takes only the states Parts and states lists for it.
Never select a class name or a tag path inside the embed. They can change in any release.