@@ -3,7 +3,7 @@ import type { ReactiveElement } from 'lit';
33
44export 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