Skip to content

Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.

feat: Add support for dispatching device orientation events (#27960)

Mattias Wallander5a9fa69c6dcb
diff --git a/packages/playwright-core/src/server/injected/injectedScript.ts b/packages/playwright-core/src/server/injected/injectedScript.ts
index d19603b827..48a9a388dd 100644
--- a/packages/playwright-core/src/server/injected/injectedScript.ts
+++ b/packages/playwright-core/src/server/injected/injectedScript.ts
@@ -67,6 +67,10 @@ export type HitTargetInterceptionResult = {
stop: () => 'done' | { hitTargetDescription: string };
};
+interface WebKitLegacyDeviceOrientationEvent extends DeviceOrientationEvent {
+ readonly initDeviceOrientationEvent: (type: string, bubbles: boolean, cancelable: boolean, alpha: number, beta: number, gamma: number, absolute: boolean) => void;
+}
+
export class InjectedScript {
private _engines: Map<string, SelectorEngine>;
_evaluator: SelectorEvaluatorImpl;
@@ -1036,6 +1040,15 @@ export class InjectedScript {
case 'focus': event = new FocusEvent(type, eventInit); break;
case 'drag': event = new DragEvent(type, eventInit); break;
case 'wheel': event = new WheelEvent(type, eventInit); break;
+ case 'deviceorientation':
+ try {
+ event = new DeviceOrientationEvent(type, eventInit);
+ } catch {
+ const { bubbles, cancelable, alpha, beta, gamma, absolute } = eventInit as {bubbles: boolean, cancelable: boolean, alpha: number, beta: number, gamma: number, absolute: boolean};
+ event = this.document.createEvent('DeviceOrientationEvent') as WebKitLegacyDeviceOrientationEvent;
+ event.initDeviceOrientationEvent(type, bubbles, cancelable, alpha, beta, gamma, absolute);
+ }
+ break;
default: event = new Event(type, eventInit); break;
}
node.dispatchEvent(event);
@@ -1371,7 +1384,7 @@ function oneLine(s: string): string {
return s.replace(/\n/g, '↵').replace(/\t/g, '⇆');
}
-const eventType = new Map<string, 'mouse' | 'keyboard' | 'touch' | 'pointer' | 'focus' | 'drag' | 'wheel'>([
+const eventType = new Map<string, 'mouse' | 'keyboard' | 'touch' | 'pointer' | 'focus' | 'drag' | 'wheel' | 'deviceorientation'>([
['auxclick', 'mouse'],
['click', 'mouse'],
['dblclick', 'mouse'],
@@ -1419,6 +1432,9 @@ const eventType = new Map<string, 'mouse' | 'keyboard' | 'touch' | 'pointer' | '
['drop', 'drag'],
['wheel', 'wheel'],
+
+ ['deviceorientation', 'deviceorientation'],
+ ['deviceorientationabsolute', 'deviceorientation'],
]);
const kHoverHitTargetInterceptorEvents = new Set(['mousemove']);
diff --git a/tests/assets/device-orientation.html b/tests/assets/device-orientation.html
new file mode 100644
index 0000000000..46be1f3f7a
--- /dev/null
+++ b/tests/assets/device-orientation.html
@@ -0,0 +1,29 @@
+<!DOCTYPE html>
+<html lang="en">
+
+<head>
+ <title>Device orientation test</title>
+</head>
+
+<body>
+ <script>
+ window.result = 'Was not oriented';
+ window.alpha = undefined;
+ window.beta = undefined;
+ window.gamma = undefined;
+ window.absolute = undefined;
+
+ document.addEventListener('deviceorientation', onOrientation, false);
+ document.addEventListener('deviceorientationabsolute', onOrientation, false);
+
+ function onOrientation(event) {
+ window.result = 'Oriented';
+ window.alpha = event.alpha;
+ window.beta = event.beta;
+ window.gamma = event.gamma;
+ window.absolute = event.absolute;
+ }
+ </script>
+</body>
+
+</html>
diff --git a/tests/page/page-dispatchevent.spec.ts b/tests/page/page-dispatchevent.spec.ts
index 85b928862b..840c525788 100644
--- a/tests/page/page-dispatchevent.spec.ts
+++ b/tests/page/page-dispatchevent.spec.ts
@@ -171,3 +171,23 @@ it('should dispatch wheel event', async ({ page, server }) => {
expect(await eventsHandle.evaluate(e => e[0] instanceof WheelEvent)).toBeTruthy();
expect(await eventsHandle.evaluate(e => ({ deltaX: e[0].deltaX, deltaY: e[0].deltaY }))).toEqual({ deltaX: 100, deltaY: 200 });
});
+
+it('should dispatch device orientation event', async ({ page, server }) => {
+ await page.goto(server.PREFIX + '/device-orientation.html');
+ await page.locator('html').dispatchEvent('deviceorientation', { alpha: 10, beta: 20, gamma: 30 });
+ expect(await page.evaluate('result')).toBe('Oriented');
+ expect(await page.evaluate('alpha')).toBe(10);
+ expect(await page.evaluate('beta')).toBe(20);
+ expect(await page.evaluate('gamma')).toBe(30);
+ expect(await page.evaluate('absolute')).toBeFalsy();
+});
+
+it('should dispatch absolute device orientation event', async ({ page, server }) => {
+ await page.goto(server.PREFIX + '/device-orientation.html');
+ await page.locator('html').dispatchEvent('deviceorientationabsolute', { alpha: 10, beta: 20, gamma: 30, absolute: true });
+ expect(await page.evaluate('result')).toBe('Oriented');
+ expect(await page.evaluate('alpha')).toBe(10);
+ expect(await page.evaluate('beta')).toBe(20);
+ expect(await page.evaluate('gamma')).toBe(30);
+ expect(await page.evaluate('absolute')).toBeTruthy();
+});