Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(PuppeteerRunnerExtension): improve handling of SVG elements #41

Merged
merged 2 commits into from
Mar 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 29 additions & 3 deletions src/PuppeteerRunnerExtension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -326,19 +326,45 @@ async function scrollIntoViewIfNeeded(
timeout: number
): Promise<void> {
await waitForConnected(element, timeout);
const isInViewport = await element.isIntersectingViewport({ threshold: 0 });
const isSvg = await element.evaluate((element: Element) => {
/* c8 ignore next 1 */
return element instanceof SVGElement;
});
const intersectionTarget = isSvg
? await getOwnerSVGElement(element)
: element;
const isInViewport = intersectionTarget
? await intersectionTarget.isIntersectingViewport({ threshold: 0 })
: false;
if (isInViewport) {
return;
}
await scrollIntoView(element);
if (intersectionTarget) {
await waitForInViewport(intersectionTarget, timeout);
}
}

async function getOwnerSVGElement(
element: ElementHandle<Element>
): Promise<ElementHandle<Element> | null> {
return (
await element.evaluateHandle((element: Element) => {
/* c8 ignore next 1 */
return (element as SVGElement).ownerSVGElement;
})
).asElement();
}

async function scrollIntoView(element: ElementHandle<Element>): Promise<void> {
await element.evaluate((element: Element) => {
/* c8 ignore next 1 */
/* c8 ignore next 5 */
element.scrollIntoView({
block: 'center',
inline: 'center',
behavior: 'auto',
});
});
await waitForInViewport(element, timeout);
}

async function waitForConnected(
Expand Down
7 changes: 7 additions & 0 deletions test/resources/svg.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<svg viewBox="0 0 100 100" xmlns="http://www.w3.org/2000/svg">
<path d="M 10,30
A 20,20 0,0,1 50,30
A 20,20 0,0,1 90,30
Q 90,60 50,90
Q 10,60 10,30 z"/>
</svg>
22 changes: 22 additions & 0 deletions test/runner_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,28 @@ describe('Runner', () => {
assert.strictEqual(page.url(), `${HTTP_PREFIX}/main2.html`);
});

it('should be able to replay click steps on SVG path elements', async () => {
const runner = await createRunner(
{
title: 'test',
steps: [
{
type: 'navigate',
url: `${HTTP_PREFIX}/svg.html`,
},
{
type: 'click',
selectors: ['svg > path'],
offsetX: 1,
offsetY: 1,
},
],
},
new PuppeteerRunnerExtension(browser, page)
);
await runner.run();
});

it('should be able to replay click steps on checkboxes', async () => {
const runner = await createRunner(
{
Expand Down