Hovering and hover-only content

hover() moves the mouse over an element

hover() scrolls the element into view and moves the mouse to its centre. To stop hovering, hover something else, such as the page heading.

await page.getByText('Hover over me').hover();
await expect(page.getByText('Hover state: active')).toBeVisible();

await page.getByRole('heading', { name: 'Hover', exact: true }).hover();
await expect(page.getByText('Hover state: inactive')).toBeVisible();

Menus that only appear on hover

Hidden menu items exist in the page but cannot be clicked. Hover the trigger first, then click while the menu is open. The mouse moves straight from the trigger to the option, so the menu stays open.

const option = page.getByRole('button', { name: 'Option 2' });

await expect(option).toBeHidden();
await page.getByRole('button', { name: 'Move mouse here' }).hover();
await option.click();

Tooltips

Tooltips have the tooltip role and are linked to their trigger with aria-describedby. That gives two ways to assert one:

const info = page.getByRole('button', { name: 'Shipping info' });

await info.hover();
await expect(page.getByRole('tooltip')).toHaveText('Free shipping on orders over 200 PLN');
await expect(info).toHaveAccessibleDescription('Free shipping on orders over 200 PLN');

Basic hover

Hover over me
Hover state: inactive

Hover menu

Menu choice: none

Tooltip