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: add hover step #127

Merged
merged 2 commits into from
May 13, 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
16 changes: 16 additions & 0 deletions src/PuppeteerRunnerExtension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,21 @@ export class PuppeteerRunnerExtension extends RunnerExtension {
await element.dispose();
}
break;
case 'hover':
{
const element = await waitForSelectors(step.selectors, localFrame, {
timeout,
visible: waitForVisible,
});
if (!element) {
throw new Error('Could not find element: ' + step.selectors[0]);
}
await scrollIntoViewIfNeeded(element, timeout);
startWaitingForEvents();
await element.hover();
await element.dispose();
}
break;
case 'emulateNetworkConditions':
{
startWaitingForEvents();
Expand Down Expand Up @@ -691,6 +706,7 @@ interface ElementHandle<ElementType extends Element>
};
}): Promise<void>;
type(input: string): Promise<void>;
hover(): Promise<void>;
focus(): Promise<void>;
$$<T extends Element = Element>(
selector: string
Expand Down
8 changes: 8 additions & 0 deletions src/PuppeteerStringifyExtension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import type {
WaitForElementStep,
WaitForExpressionStep,
DoubleClickStep,
HoverStep,
} from './Schema.js';
import { StringifyExtension } from './StringifyExtension.js';

Expand Down Expand Up @@ -167,6 +168,11 @@ export class PuppeteerStringifyExtension extends StringifyExtension {
out.appendLine('});');
}

#appendHoverStep(out: LineWriter, step: HoverStep): void {
this.#appendWaitForSelector(out, step);
out.appendLine('await element.hover();');
}

#appendChangeStep(out: LineWriter, step: ChangeStep): void {
this.#appendWaitForSelector(out, step);
out.appendLine('const type = await element.evaluate(el => el.type);');
Expand Down Expand Up @@ -248,6 +254,8 @@ export class PuppeteerStringifyExtension extends StringifyExtension {
return this.#appendClickStep(out, step);
case 'doubleClick':
return this.#appendDoubleClickStep(out, step);
case 'hover':
return this.#appendHoverStep(out, step);
case 'change':
return this.#appendChangeStep(out, step);
case 'emulateNetworkConditions':
Expand Down
17 changes: 15 additions & 2 deletions src/Schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,23 @@ export interface StepWithSelectors extends StepWithFrame {
selectors: Selector[];
}

export type PointerDeviceType = 'mouse' | 'pen' | 'touch';
export type PointerButtonType =
| 'primary'
| 'auxiliary'
| 'secondary'
| 'back'
| 'forward';

export interface ClickAttributes {
/**
* Pointer type for the event. Defaults to 'mouse'.
*/
deviceType?: 'mouse' | 'pen' | 'touch';
deviceType?: PointerDeviceType;
/**
* Defaults to 'primary' if the device type is a mouse.
*/
button?: 'primary' | 'auxiliary' | 'secondary' | 'back' | 'forward';
button?: PointerButtonType;
/**
* in px, relative to the top-left corner of the element content box. Defaults
* to the center of the element
Expand All @@ -97,6 +105,10 @@ export interface ClickStep extends ClickAttributes, StepWithSelectors {
duration?: number;
}

export interface HoverStep extends StepWithSelectors {
type: 'hover';
}

export interface ChangeStep extends StepWithSelectors {
type: 'change';
value: string;
Expand Down Expand Up @@ -167,6 +179,7 @@ export type CustomStep =
export type UserStep =
| ChangeStep
| ClickStep
| HoverStep
| CloseStep
| CustomStep
| DoubleClickStep
Expand Down
5 changes: 5 additions & 0 deletions test/resources/main.html
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
<button id="synthetic">Trigger Synthetic Event</button>
<button id="synthetic-handler">Synthetic Event Handler</button>
<button id="selector-attribute" data-devtools-test="selector-attribute">Custom selector attribute</button>
<button id="hover-button">Not hovered</button>
</div>
<script>
window.buttonClicks = [];
Expand Down Expand Up @@ -81,4 +82,8 @@
document.getElementById('form1').addEventListener('submit', () => {
document.getElementById('form1-result').textContent = 'From 1 Submitted';
});

document.getElementById('hover-button').addEventListener('pointerover', (event) => {
event.target.textContent = 'Hovered';
})
</script>
35 changes: 35 additions & 0 deletions test/runner_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -865,4 +865,39 @@ describe('Runner', () => {
);
assert.ok(frame, 'Frame that the target page navigated to is not found');
});

it('should replay hovers', async () => {
const runner = await createRunner(
{
title: 'Test Recording',
timeout: 3000,
steps: [
{
type: 'navigate',
url: `${HTTP_PREFIX}/main.html`,
assertedEvents: [
{
title: '',
type: 'navigation',
url: `${HTTP_PREFIX}/main.html`,
},
],
},
{
type: 'hover',
target: 'main',
selectors: [['#hover-button']],
},
],
},
new PuppeteerRunnerExtension(browser, page)
);
await runner.run();
assert.ok(
await page.evaluate(
() => document.getElementById('hover-button')?.textContent
),
'Hovered'
);
});
});