Layout & viewport assertions for Playwright powered by locator.boundingBox().
- Assert element visibility in the viewport (fully or partially)
- Check relative positioning (left, right, above, below) between elements
- Assert alignment (edges or centers) on x/y axes
- Compute visible area ratio and intersection area
- Works with Playwright's
LocatorAPI
npm i -D playwright_is_located @playwright/testImport the assertions in your Playwright test files:
import {
isInViewport,
relativePosition,
areAligned,
visibleAreaRatio,
intersectionAreaRatio,
inOrder,
edgeDistance,
} from 'playwright_is_located';await expect(await isInViewport(locator)).toBe(true);
// With options:
await expect(await isInViewport(locator, { fullyVisible: true, padding: 8 })).toBe(true);
await expect(await isInViewport(locator, { threshold: 0.5 })).toBe(true); // at least 50% visible// Is A to the left of B?
await expect(await relativePosition(firstLocator, secondLocator, 'left')).toBe(true);
// Is A below B with at least 10px gap and 80% horizontal overlap?
await expect(
await relativePosition(firstLocator, secondLocator, 'below', { gap: 10, overlapRatio: 0.8 }),
).toBe(true);// Are A and B top/bottom edges aligned (x axis)?
await expect(
await areAligned(firstLocator, secondLocator, { axis: 'x', mode: 'edges', tolerance: 2 }),
).toBe(true);
// Are A and B horizontally centered?
await expect(await areAligned(firstLocator, secondLocator, { axis: 'y', mode: 'centers' })).toBe(
true,
);const ratio = await visibleAreaRatio(locator);
expect(ratio).toBeGreaterThan(0.5); // at least 50% visibleconst overlap = await intersectionAreaRatio(firstLocator, secondLocator);
expect(overlap).toBeLessThan(0.2); // less than 20% of A is overlapped by B// Are locators in left-to-right order?
await expect(await inOrder([a, b, c], 'leftToRight')).toBe(true);// Distance in px from right edge of A to left edge of B
const dist = await edgeDistance(firstLocator, secondLocator, 'left');
expect(dist).toBeGreaterThanOrEqual(0);See TypeScript types for all options and return values.
isInViewport(locator, options?)relativePosition(firstLocator, secondLocator, direction, options?)areAligned(firstLocator, secondLocator, options)visibleAreaRatio(locator)intersectionAreaRatio(firstLocator, secondLocator)inOrder(locators, order, tolerance?)edgeDistance(firstLocator, secondLocator, direction)
MIT