Skip to content

Commit 57b7dba

Browse files
committed
feat(tools): clickElementAtOffset test helper
1 parent fbd20ff commit 57b7dba

File tree

3 files changed

+35
-4
lines changed

3 files changed

+35
-4
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
"@patternfly/pfe-tools": minor
3+
---
4+
Test: add `clickElementAtOffset` utility function for tests

.changeset/test-tools-click-el.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
---
22
"@patternfly/pfe-tools": minor
33
---
4-
Test: add `clickElementCenter` utility function for tests
4+
Test: add `clickElementAtCenter` utility function for tests

tools/pfe-tools/test/utils.ts

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import type { ReactiveElement } from 'lit';
33

44
export type Position = [x: number, y: number];
55

6-
export function getElementPosition(element: Element): Position {
6+
export function getElementCenterPosition(element: Element): Position {
77
const { x, y, width, height } = element.getBoundingClientRect();
88

99
return [
@@ -12,8 +12,35 @@ export function getElementPosition(element: Element): Position {
1212
];
1313
}
1414

15-
export async function clickElementCenter(element: Element): Promise<void> {
16-
const position = getElementPosition(element);
15+
/** @deprecated - use `getElementCenterPosition` */
16+
export const getElementPosition = getElementCenterPosition;
17+
18+
/**
19+
* Click an element at approximate center, using playwright's sendMouse command
20+
*/
21+
export async function clickElementAtCenter(element: Element): Promise<void> {
22+
const position = getElementCenterPosition(element);
23+
await sendMouse({ type: 'click', position });
24+
}
25+
26+
/**
27+
* Click an element at an offset from it's top-left corner, using playwright's sendMouse command
28+
*/
29+
export async function clickElementAtOffset(element: Element, relativeOffset: Position): Promise<void> {
30+
const { x, y, right, bottom } = element.getBoundingClientRect();
31+
const [xOffset, yOffset] = relativeOffset;
32+
const position = [
33+
Math.round(xOffset + (xOffset < 0 ? right : x)),
34+
Math.round(yOffset + (yOffset < 0 ? bottom : y)),
35+
] satisfies [number, number];
36+
const [xCoord, yCoord] = position;
37+
// NOTE: this may fail in RTL situations?
38+
if (xCoord > right) {
39+
throw new Error('X offset is outside element boundaries');
40+
}
41+
if (yCoord > bottom) {
42+
throw new Error('Y offset is outside element boundaries');
43+
}
1744
await sendMouse({ type: 'click', position });
1845
}
1946

0 commit comments

Comments
 (0)