playwright-touch 0.0.0-stage → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (53) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +495 -2
  3. package/dist/android.d.ts +26 -0
  4. package/dist/android.js +113 -0
  5. package/dist/chromium/clock.d.ts +12 -0
  6. package/dist/chromium/clock.js +45 -0
  7. package/dist/chromium/gestures.d.ts +56 -0
  8. package/dist/chromium/gestures.js +485 -0
  9. package/dist/chromium/steady.d.ts +16 -0
  10. package/dist/chromium/steady.js +38 -0
  11. package/dist/cli.d.ts +2 -0
  12. package/dist/cli.js +459 -0
  13. package/dist/index.d.ts +108 -0
  14. package/dist/index.js +147 -0
  15. package/dist/ios/bplist.d.ts +8 -0
  16. package/dist/ios/bplist.js +150 -0
  17. package/dist/ios/device.d.ts +30 -0
  18. package/dist/ios/device.js +48 -0
  19. package/dist/ios/inspector.d.ts +61 -0
  20. package/dist/ios/inspector.js +342 -0
  21. package/dist/ios/iphone.d.ts +27 -0
  22. package/dist/ios/iphone.js +141 -0
  23. package/dist/ios/page.d.ts +90 -0
  24. package/dist/ios/page.js +338 -0
  25. package/dist/ios/paths.d.ts +17 -0
  26. package/dist/ios/paths.js +77 -0
  27. package/dist/ios/simulator.d.ts +24 -0
  28. package/dist/ios/simulator.js +69 -0
  29. package/dist/ios/tab.d.ts +41 -0
  30. package/dist/ios/tab.js +171 -0
  31. package/dist/ios/tools.d.ts +4 -0
  32. package/dist/ios/tools.js +17 -0
  33. package/dist/ios/touch.d.ts +31 -0
  34. package/dist/ios/touch.js +178 -0
  35. package/dist/ios/usbmux.d.ts +34 -0
  36. package/dist/ios/usbmux.js +346 -0
  37. package/dist/layout.d.ts +35 -0
  38. package/dist/layout.js +164 -0
  39. package/dist/measure.d.ts +86 -0
  40. package/dist/measure.js +173 -0
  41. package/dist/own-tabs.d.ts +8 -0
  42. package/dist/own-tabs.js +242 -0
  43. package/dist/polyline.d.ts +10 -0
  44. package/dist/polyline.js +38 -0
  45. package/dist/report.d.ts +46 -0
  46. package/dist/report.js +118 -0
  47. package/dist/session.d.ts +35 -0
  48. package/dist/session.js +23 -0
  49. package/dist/show.d.ts +13 -0
  50. package/dist/show.js +51 -0
  51. package/dist/types.d.ts +58 -0
  52. package/dist/types.js +2 -0
  53. package/package.json +68 -4
@@ -0,0 +1,35 @@
1
+ /** A Chrome DevTools Protocol session, as much of it as the package uses. */
2
+ export interface CDPSession {
3
+ send(method: string, params?: Record<string, unknown>): Promise<unknown>;
4
+ on(event: string, listener: (payload: any) => void): unknown;
5
+ off(event: string, listener: (payload: any) => void): unknown;
6
+ }
7
+ /** A Playwright `Page`. */
8
+ export interface Page {
9
+ viewportSize(): {
10
+ width: number;
11
+ height: number;
12
+ } | null;
13
+ evaluate<R>(pageFunction: () => R | Promise<R>): Promise<R>;
14
+ evaluate<R, A>(pageFunction: (argument: A) => R | Promise<R>, argument: A): Promise<R>;
15
+ /** A script given as text, for code that must be the same in every browser. */
16
+ evaluate<R>(script: string): Promise<R>;
17
+ context(): {
18
+ newCDPSession(page: never): Promise<CDPSession>;
19
+ };
20
+ }
21
+ /** A Playwright `Locator`. */
22
+ export interface Locator {
23
+ boundingBox(): Promise<{
24
+ x: number;
25
+ y: number;
26
+ width: number;
27
+ height: number;
28
+ } | null>;
29
+ scrollIntoViewIfNeeded(): Promise<void>;
30
+ evaluate<R>(pageFunction: (element: Element) => R): Promise<R>;
31
+ evaluate<R, A>(pageFunction: (element: Element, argument: A) => R, argument: A): Promise<R>;
32
+ page(): Page;
33
+ }
34
+ /** One DevTools session per page, opened on first use. Touch input and measurements go through it, so the browser must be Chromium. */
35
+ export declare function sessionOf(page: Page): Promise<CDPSession>;
@@ -0,0 +1,23 @@
1
+ const sessions = new WeakMap();
2
+ /** One DevTools session per page, opened on first use. Touch input and measurements go through it, so the browser must be Chromium. */
3
+ export function sessionOf(page) {
4
+ let session = sessions.get(page);
5
+ if (!session) {
6
+ session = page
7
+ .context()
8
+ .newCDPSession(page)
9
+ .catch((error) => {
10
+ // A failure is not remembered: the next gesture gets to try again.
11
+ sessions.delete(page);
12
+ const browser = page.context()
13
+ .browser?.()
14
+ ?.browserType?.()
15
+ ?.name?.();
16
+ if (browser && browser !== 'chromium')
17
+ throw new Error(`playwright-touch works through the Chrome DevTools Protocol, which ${browser} does not offer. Use a Chromium-based browser.`);
18
+ throw new Error(`playwright-touch: could not open a DevTools session for the page: ${error.message.split('\n')[0]}`);
19
+ });
20
+ sessions.set(page, session);
21
+ }
22
+ return session;
23
+ }
package/dist/show.d.ts ADDED
@@ -0,0 +1,13 @@
1
+ import type { Page } from './session.js';
2
+ /**
3
+ * The script that draws the dots, the same in every browser. It listens to the touches the page
4
+ * really gets, so a dot is proof that a finger arrived. The dots take no touches themselves and
5
+ * are not part of the layout.
6
+ */
7
+ export declare const SHOW_TOUCHES = "(() => {\n if (window.__playwrightTouchShown) return;\n window.__playwrightTouchShown = true;\n const SIZE = 36;\n const layer = document.createElement('div');\n layer.setAttribute('aria-hidden', 'true');\n layer.dataset.playwrightTouch = 'fingers';\n layer.style.cssText = 'position:fixed;inset:0;margin:0;padding:0;border:0;pointer-events:none;z-index:2147483647;overflow:hidden;contain:strict;background:none';\n const dots = new Map();\n const place = (dot, touch) => {\n dot.style.transform = 'translate(' + (touch.clientX - SIZE / 2) + 'px,' + (touch.clientY - SIZE / 2) + 'px)';\n };\n const down = (event) => {\n if (!layer.isConnected) (document.body || document.documentElement).appendChild(layer);\n for (const touch of event.changedTouches) {\n let dot = dots.get(touch.identifier);\n if (!dot) {\n dot = document.createElement('div');\n dot.style.cssText = 'position:absolute;left:0;top:0;width:' + SIZE + 'px;height:' + SIZE + 'px;box-sizing:border-box;border-radius:50%;background:rgba(30,120,255,.4);border:2px solid rgba(255,255,255,.95);box-shadow:0 0 0 1px rgba(0,0,0,.4);transition:opacity .25s;will-change:transform';\n layer.appendChild(dot);\n dots.set(touch.identifier, dot);\n }\n place(dot, touch);\n }\n };\n const up = (event) => {\n for (const touch of event.changedTouches) {\n const dot = dots.get(touch.identifier);\n if (!dot) continue;\n dots.delete(touch.identifier);\n // The dot stays for a moment, or a tap would be over before a film could show it.\n dot.style.opacity = '0';\n setTimeout(() => dot.remove(), 300);\n }\n };\n const quietly = { capture: true, passive: true };\n addEventListener('touchstart', down, quietly);\n addEventListener('touchmove', down, quietly);\n addEventListener('touchend', up, quietly);\n addEventListener('touchcancel', up, quietly);\n})()";
8
+ /** Shows the fingers on a Playwright page, now and on every page it goes to. */
9
+ export declare function showTouches(page: Page & {
10
+ addInitScript?(script: {
11
+ content: string;
12
+ }): Promise<unknown>;
13
+ }): Promise<void>;
package/dist/show.js ADDED
@@ -0,0 +1,51 @@
1
+ /**
2
+ * The script that draws the dots, the same in every browser. It listens to the touches the page
3
+ * really gets, so a dot is proof that a finger arrived. The dots take no touches themselves and
4
+ * are not part of the layout.
5
+ */
6
+ export const SHOW_TOUCHES = `(() => {
7
+ if (window.__playwrightTouchShown) return;
8
+ window.__playwrightTouchShown = true;
9
+ const SIZE = 36;
10
+ const layer = document.createElement('div');
11
+ layer.setAttribute('aria-hidden', 'true');
12
+ layer.dataset.playwrightTouch = 'fingers';
13
+ layer.style.cssText = 'position:fixed;inset:0;margin:0;padding:0;border:0;pointer-events:none;z-index:2147483647;overflow:hidden;contain:strict;background:none';
14
+ const dots = new Map();
15
+ const place = (dot, touch) => {
16
+ dot.style.transform = 'translate(' + (touch.clientX - SIZE / 2) + 'px,' + (touch.clientY - SIZE / 2) + 'px)';
17
+ };
18
+ const down = (event) => {
19
+ if (!layer.isConnected) (document.body || document.documentElement).appendChild(layer);
20
+ for (const touch of event.changedTouches) {
21
+ let dot = dots.get(touch.identifier);
22
+ if (!dot) {
23
+ dot = document.createElement('div');
24
+ dot.style.cssText = 'position:absolute;left:0;top:0;width:' + SIZE + 'px;height:' + SIZE + 'px;box-sizing:border-box;border-radius:50%;background:rgba(30,120,255,.4);border:2px solid rgba(255,255,255,.95);box-shadow:0 0 0 1px rgba(0,0,0,.4);transition:opacity .25s;will-change:transform';
25
+ layer.appendChild(dot);
26
+ dots.set(touch.identifier, dot);
27
+ }
28
+ place(dot, touch);
29
+ }
30
+ };
31
+ const up = (event) => {
32
+ for (const touch of event.changedTouches) {
33
+ const dot = dots.get(touch.identifier);
34
+ if (!dot) continue;
35
+ dots.delete(touch.identifier);
36
+ // The dot stays for a moment, or a tap would be over before a film could show it.
37
+ dot.style.opacity = '0';
38
+ setTimeout(() => dot.remove(), 300);
39
+ }
40
+ };
41
+ const quietly = { capture: true, passive: true };
42
+ addEventListener('touchstart', down, quietly);
43
+ addEventListener('touchmove', down, quietly);
44
+ addEventListener('touchend', up, quietly);
45
+ addEventListener('touchcancel', up, quietly);
46
+ })()`;
47
+ /** Shows the fingers on a Playwright page, now and on every page it goes to. */
48
+ export async function showTouches(page) {
49
+ await page.addInitScript?.({ content: SHOW_TOUCHES });
50
+ await page.evaluate(`void ${SHOW_TOUCHES}`);
51
+ }
@@ -0,0 +1,58 @@
1
+ /** A place in the page, in CSS pixels from the top left of what is on screen. */
2
+ export interface Point {
3
+ x: number;
4
+ y: number;
5
+ }
6
+ /** The way the finger travels. Content follows the finger: a swipe up scrolls the page down. */
7
+ export type Direction = 'up' | 'down' | 'left' | 'right';
8
+ /** A stroke given by where the finger lands and where it lifts. */
9
+ export interface StrokeOptions {
10
+ /** Where the finger lands, in CSS pixels of the viewport. */
11
+ from: Point;
12
+ /** Where the finger lifts. */
13
+ to: Point;
14
+ /** How long the finger travels, in milliseconds. */
15
+ duration?: number;
16
+ }
17
+ /** A stroke given by the way the finger travels across the target. */
18
+ export interface DirectedOptions {
19
+ direction: Direction;
20
+ /** Length of the stroke in CSS pixels. Default: 60 % of the element (or the viewport) along the direction. */
21
+ distance?: number;
22
+ /** How long the finger travels, in milliseconds. */
23
+ duration?: number;
24
+ }
25
+ /** How a drag is carried out. */
26
+ export interface DragOptions {
27
+ /** How long the finger rests before it starts to move, for interfaces that pick an item up on a long press. Default 0. */
28
+ hold?: number;
29
+ /** How long the finger travels, in milliseconds. Default 500. */
30
+ duration?: number;
31
+ }
32
+ /** How far two fingers spread or close. */
33
+ export interface PinchOptions {
34
+ /** How far the fingers end up relative to where they start: 2 spreads them to twice the distance (zoom in), 0.5 brings them together. */
35
+ scale: number;
36
+ /** How long the fingers travel, in milliseconds. Default 400. */
37
+ duration?: number;
38
+ }
39
+ /** How two fingers turn around the middle of an element. */
40
+ export interface RotateOptions {
41
+ /** How far the fingers turn, in degrees. Positive is clockwise. */
42
+ degrees: number;
43
+ /** How long the fingers travel, in milliseconds. Default 500. */
44
+ duration?: number;
45
+ }
46
+ /** A gesture described finger by finger, for anything the named gestures do not cover. */
47
+ export interface GestureOptions {
48
+ /**
49
+ * One list of points per finger, in CSS pixels of the viewport. Each finger lands on its first
50
+ * point, passes through the others at an even pace and lifts on the last. All fingers land and
51
+ * lift together.
52
+ */
53
+ fingers: Point[][];
54
+ /** How long the fingers travel, in milliseconds. Default 500. */
55
+ duration?: number;
56
+ /** How long the fingers rest before they move, in milliseconds. Default 0. */
57
+ hold?: number;
58
+ }
package/dist/types.js ADDED
@@ -0,0 +1,2 @@
1
+ // The shapes the gestures are described in, the same for Chrome and for Safari.
2
+ export {};
package/package.json CHANGED
@@ -1,6 +1,70 @@
1
1
  {
2
2
  "name": "playwright-touch",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.1.0",
4
+ "description": "Real touch for browser tests: tap, swipe, fling with momentum, drag, long press and pinch in Chrome and Safari, on an emulated phone, a real Android phone or an iPhone simulator, with measurements taken on the device.",
5
+ "keywords": [
6
+ "android",
7
+ "drag",
8
+ "e2e",
9
+ "fling",
10
+ "gestures",
11
+ "ios",
12
+ "long-press",
13
+ "mobile",
14
+ "momentum",
15
+ "performance",
16
+ "pinch",
17
+ "playwright",
18
+ "safari",
19
+ "swipe",
20
+ "testing",
21
+ "touch",
22
+ "touchscreen",
23
+ "web-vitals"
24
+ ],
25
+ "license": "MIT",
26
+ "author": "Adrian Zawadzki",
27
+ "type": "module",
28
+ "exports": {
29
+ ".": {
30
+ "types": "./dist/index.d.ts",
31
+ "default": "./dist/index.js"
32
+ }
33
+ },
34
+ "types": "./dist/index.d.ts",
35
+ "files": [
36
+ "dist"
37
+ ],
38
+ "sideEffects": false,
39
+ "engines": {
40
+ "node": ">=22"
41
+ },
42
+ "scripts": {
43
+ "build": "rm -rf dist && tsc -p tsconfig.build.json && node scripts/fix-types.js",
44
+ "typecheck": "tsc",
45
+ "test": "node --test --test-concurrency=1 test/*.test.ts",
46
+ "prepack": "npm run build",
47
+ "format": "prettier --write .",
48
+ "lint": "prettier --check ."
49
+ },
50
+ "peerDependencies": {
51
+ "playwright-core": ">=1.40.0"
52
+ },
53
+ "devDependencies": {
54
+ "@types/node": "^26.6.6",
55
+ "playwright-core": "^1.65.1",
56
+ "prettier": "^3.10.1",
57
+ "typescript": "^7.0.2"
58
+ },
59
+ "repository": {
60
+ "type": "git",
61
+ "url": "git+https://github.com/Had3r/playwright-touch.git"
62
+ },
63
+ "homepage": "https://github.com/Had3r/playwright-touch#readme",
64
+ "bugs": {
65
+ "url": "https://github.com/Had3r/playwright-touch/issues"
66
+ },
67
+ "bin": {
68
+ "playwright-touch": "dist/cli.js"
69
+ }
70
+ }