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.
- package/LICENSE +21 -0
- package/README.md +495 -2
- package/dist/android.d.ts +26 -0
- package/dist/android.js +113 -0
- package/dist/chromium/clock.d.ts +12 -0
- package/dist/chromium/clock.js +45 -0
- package/dist/chromium/gestures.d.ts +56 -0
- package/dist/chromium/gestures.js +485 -0
- package/dist/chromium/steady.d.ts +16 -0
- package/dist/chromium/steady.js +38 -0
- package/dist/cli.d.ts +2 -0
- package/dist/cli.js +459 -0
- package/dist/index.d.ts +108 -0
- package/dist/index.js +147 -0
- package/dist/ios/bplist.d.ts +8 -0
- package/dist/ios/bplist.js +150 -0
- package/dist/ios/device.d.ts +30 -0
- package/dist/ios/device.js +48 -0
- package/dist/ios/inspector.d.ts +61 -0
- package/dist/ios/inspector.js +342 -0
- package/dist/ios/iphone.d.ts +27 -0
- package/dist/ios/iphone.js +141 -0
- package/dist/ios/page.d.ts +90 -0
- package/dist/ios/page.js +338 -0
- package/dist/ios/paths.d.ts +17 -0
- package/dist/ios/paths.js +77 -0
- package/dist/ios/simulator.d.ts +24 -0
- package/dist/ios/simulator.js +69 -0
- package/dist/ios/tab.d.ts +41 -0
- package/dist/ios/tab.js +171 -0
- package/dist/ios/tools.d.ts +4 -0
- package/dist/ios/tools.js +17 -0
- package/dist/ios/touch.d.ts +31 -0
- package/dist/ios/touch.js +178 -0
- package/dist/ios/usbmux.d.ts +34 -0
- package/dist/ios/usbmux.js +346 -0
- package/dist/layout.d.ts +35 -0
- package/dist/layout.js +164 -0
- package/dist/measure.d.ts +86 -0
- package/dist/measure.js +173 -0
- package/dist/own-tabs.d.ts +8 -0
- package/dist/own-tabs.js +242 -0
- package/dist/polyline.d.ts +10 -0
- package/dist/polyline.js +38 -0
- package/dist/report.d.ts +46 -0
- package/dist/report.js +118 -0
- package/dist/session.d.ts +35 -0
- package/dist/session.js +23 -0
- package/dist/show.d.ts +13 -0
- package/dist/show.js +51 -0
- package/dist/types.d.ts +58 -0
- package/dist/types.js +2 -0
- package/package.json +68 -4
package/dist/ios/tab.js
ADDED
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
// The Safari tab the package works in: one of its own, never one the user had open.
|
|
2
|
+
import { readFileSync, writeFileSync } from 'node:fs';
|
|
3
|
+
import { createServer } from 'node:http';
|
|
4
|
+
import { tmpdir } from 'node:os';
|
|
5
|
+
import { join } from 'node:path';
|
|
6
|
+
import { simctl, sleep } from './tools.js';
|
|
7
|
+
// Safari forgets a window's name when the tab goes to another site, so the name alone does not
|
|
8
|
+
// find the tab again. Every document also has its own start time, to the millisecond.
|
|
9
|
+
// The start time of the document our tab shows is written down, and a tab showing that very
|
|
10
|
+
// document is ours.
|
|
11
|
+
const noteOf = (udid) => join(tmpdir(), `playwright-touch-${udid}.json`);
|
|
12
|
+
const remembered = (udid) => {
|
|
13
|
+
try {
|
|
14
|
+
return JSON.parse(readFileSync(noteOf(udid), 'utf8')).started ?? null;
|
|
15
|
+
}
|
|
16
|
+
catch {
|
|
17
|
+
return null;
|
|
18
|
+
}
|
|
19
|
+
};
|
|
20
|
+
/** A page of our own, served from this computer, which a simulator can reach. */
|
|
21
|
+
export async function servedStart(udid) {
|
|
22
|
+
const start = createServer((_request, response) => {
|
|
23
|
+
response
|
|
24
|
+
.writeHead(200, { 'content-type': 'text/html' })
|
|
25
|
+
.end('<!doctype html><meta name="viewport" content="width=device-width, initial-scale=1"><title>playwright-touch</title><body style="margin:0;height:100vh"><script>addEventListener("touchstart",(e)=>{window.__touch={x:e.touches[0].clientX,y:e.touches[0].clientY}},{passive:true})</script>');
|
|
26
|
+
});
|
|
27
|
+
await new Promise((resolve) => start.listen(0, '127.0.0.1', resolve));
|
|
28
|
+
const url = `http://127.0.0.1:${start.address().port}/`;
|
|
29
|
+
return {
|
|
30
|
+
url,
|
|
31
|
+
open: async () => void (await simctl('openurl', udid, url)),
|
|
32
|
+
close: () => void start.close(),
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
/** Writes down which document the tab shows now, so that the next connection can recognise the tab. */
|
|
36
|
+
export async function remember(session, udid) {
|
|
37
|
+
try {
|
|
38
|
+
// Only the start time is kept. The address would say where the tab has been.
|
|
39
|
+
const started = await session.evaluate('performance.timeOrigin');
|
|
40
|
+
writeFileSync(noteOf(udid), JSON.stringify({ started }), { mode: 0o600 });
|
|
41
|
+
}
|
|
42
|
+
catch {
|
|
43
|
+
// Not being able to write it down costs one more tab next time, nothing else.
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Opens the tab to work in, or takes over the one an earlier connection left, and finds out where
|
|
48
|
+
* on the screen the page starts by touching it once.
|
|
49
|
+
*/
|
|
50
|
+
export async function openTab(inspector, touch, udid, deviceName, opener) {
|
|
51
|
+
// A page of our own opens the tab we will work in, or takes over the tab an earlier connection
|
|
52
|
+
// left. It also tells where on the screen the page starts.
|
|
53
|
+
const start = opener ?? (await servedStart(udid));
|
|
54
|
+
const startUrl = start.url;
|
|
55
|
+
/**
|
|
56
|
+
* One touch at a known place on the screen, read back in page coordinates. Null when it did not
|
|
57
|
+
* arrive. The finger is only put down when the page says it is the one on screen and, where the
|
|
58
|
+
* device can be asked, Safari is the application in front: a touch that misses the page would
|
|
59
|
+
* land on whatever else is showing.
|
|
60
|
+
*/
|
|
61
|
+
const locate = async (candidate) => {
|
|
62
|
+
const screen = await candidate.evaluate("(window.__touch = null, { w: screen.width, h: screen.height, shown: document.visibilityState === 'visible' && document.hasFocus() })");
|
|
63
|
+
if (!screen.shown || (start.inFront && !(await start.inFront())))
|
|
64
|
+
return null;
|
|
65
|
+
const probe = { x: Math.round(screen.w / 2), y: Math.round(screen.h / 2) };
|
|
66
|
+
await touch.perform([
|
|
67
|
+
[
|
|
68
|
+
{ ...probe, ms: 0 },
|
|
69
|
+
{ ...probe, ms: 60 },
|
|
70
|
+
],
|
|
71
|
+
]);
|
|
72
|
+
for (let i = 0; i < 20; i++) {
|
|
73
|
+
const seen = await candidate.evaluate('window.__touch ?? null');
|
|
74
|
+
if (seen)
|
|
75
|
+
return { x: probe.x - seen.x, y: probe.y - seen.y };
|
|
76
|
+
await sleep(100);
|
|
77
|
+
}
|
|
78
|
+
return null;
|
|
79
|
+
};
|
|
80
|
+
/** The tab left by an earlier connection, if it is still there and in front. */
|
|
81
|
+
const earlierTab = async () => {
|
|
82
|
+
await sleep(500);
|
|
83
|
+
inspector.refresh();
|
|
84
|
+
await sleep(500);
|
|
85
|
+
// Safari numbers its tabs as it opens them, and ours is among the newest. Looking at the
|
|
86
|
+
// oldest first, as this once did, stopped finding it after a dozen tabs, and every connection
|
|
87
|
+
// then opened one more.
|
|
88
|
+
const known = remembered(udid);
|
|
89
|
+
// On a phone only tabs on the start address are candidates, and they are picked out before the
|
|
90
|
+
// twelve newest are taken, or a dozen tabs of the owner's would hide ours.
|
|
91
|
+
const ours = (listed) => !start.ownOnly || (listed.url.startsWith(startUrl) && listed.bundle === 'com.apple.mobilesafari');
|
|
92
|
+
const newestFirst = inspector
|
|
93
|
+
.pages()
|
|
94
|
+
.filter(ours)
|
|
95
|
+
.sort((a, b) => Number(b.id) - Number(a.id));
|
|
96
|
+
for (const listed of newestFirst.slice(0, 12)) {
|
|
97
|
+
let candidate;
|
|
98
|
+
try {
|
|
99
|
+
candidate = await inspector.attach(listed);
|
|
100
|
+
const shows = await candidate.evaluate('({ name: window.name, started: performance.timeOrigin })');
|
|
101
|
+
// On a phone the start page lives on the phone itself, and Safari does not open a second
|
|
102
|
+
// tab for an address it already shows. A tab on that page is therefore ours, named or not.
|
|
103
|
+
const onStart = !!start.ownOnly && listed.url.startsWith(startUrl);
|
|
104
|
+
if (shows.name === 'playwright-touch' || shows.started === known || onStart) {
|
|
105
|
+
await candidate.evaluate(`void (location.href = ${JSON.stringify(startUrl)})`).catch(() => undefined);
|
|
106
|
+
for (let i = 0; i < 40; i++) {
|
|
107
|
+
await sleep(100);
|
|
108
|
+
if (await candidate
|
|
109
|
+
.evaluate(
|
|
110
|
+
// A tab that was already on the start page loads it afresh, so a new document is waited for.
|
|
111
|
+
`location.href.startsWith(${JSON.stringify(startUrl)}) && document.readyState === 'complete' && performance.timeOrigin !== ${shows.started}`)
|
|
112
|
+
.catch(() => false))
|
|
113
|
+
break;
|
|
114
|
+
}
|
|
115
|
+
await start.prepare?.(candidate);
|
|
116
|
+
const found = await locate(candidate);
|
|
117
|
+
if (found) {
|
|
118
|
+
await candidate.evaluate("void (window.name = 'playwright-touch')").catch(() => undefined);
|
|
119
|
+
await remember(candidate, udid);
|
|
120
|
+
return { session: candidate, origin: found };
|
|
121
|
+
}
|
|
122
|
+
// The tab is there but not in front, so touches go elsewhere. It gives its name back and
|
|
123
|
+
// goes to an empty page, or every later connection would try it first and fail the same way.
|
|
124
|
+
await candidate.evaluate("void (window.name = '', location.replace('about:blank'))").catch(() => undefined);
|
|
125
|
+
}
|
|
126
|
+
candidate.close();
|
|
127
|
+
}
|
|
128
|
+
catch {
|
|
129
|
+
candidate?.close();
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
return null;
|
|
133
|
+
};
|
|
134
|
+
try {
|
|
135
|
+
const earlier = await earlierTab();
|
|
136
|
+
if (earlier)
|
|
137
|
+
return earlier;
|
|
138
|
+
// Only a tab that was not there a moment ago is the one this opens.
|
|
139
|
+
inspector.refresh();
|
|
140
|
+
await sleep(300);
|
|
141
|
+
const before = new Set(inspector.pages().map((listed) => `${listed.app}/${listed.id}`));
|
|
142
|
+
await start.open();
|
|
143
|
+
await start.expect?.();
|
|
144
|
+
const isNew = (listed) => listed.url.startsWith(startUrl) && !before.has(`${listed.app}/${listed.id}`);
|
|
145
|
+
const opened = start.revive
|
|
146
|
+
? await inspector.waitForPage(isNew, 6000).catch(async () => {
|
|
147
|
+
await start.revive();
|
|
148
|
+
return inspector.waitForPage((listed) => listed.url.startsWith(startUrl), 30_000);
|
|
149
|
+
})
|
|
150
|
+
: await inspector.waitForPage(isNew, 30_000);
|
|
151
|
+
const session = await inspector.attach(opened);
|
|
152
|
+
// The page is written into only once it has finished loading, or the load would wipe it.
|
|
153
|
+
for (let i = 0; i < 50; i++) {
|
|
154
|
+
if (await session.evaluate("document.readyState === 'complete'").catch(() => false))
|
|
155
|
+
break;
|
|
156
|
+
await sleep(100);
|
|
157
|
+
}
|
|
158
|
+
await start.prepare?.(session);
|
|
159
|
+
// The name stays with the tab across navigations, so the next connection can find it again.
|
|
160
|
+
await session.evaluate("void (window.name = 'playwright-touch')");
|
|
161
|
+
const origin = await locate(session);
|
|
162
|
+
if (!origin) {
|
|
163
|
+
throw new Error(`playwright-touch: a touch through ${touch.name} did not reach the page in ${deviceName}. Is something covering Safari, or is WebDriverAgent running for a different device?`);
|
|
164
|
+
}
|
|
165
|
+
await remember(session, udid);
|
|
166
|
+
return { session, origin };
|
|
167
|
+
}
|
|
168
|
+
finally {
|
|
169
|
+
start.close();
|
|
170
|
+
}
|
|
171
|
+
}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
/** Runs a command and returns what it printed. `missing` is what to say when the command is not installed. */
|
|
2
|
+
export declare function run(command: string, args: string[], missing: string): Promise<string>;
|
|
3
|
+
export declare const simctl: (...args: string[]) => Promise<string>;
|
|
4
|
+
export declare const sleep: (ms: number) => Promise<void>;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
// Small things every part of the simulator support needs.
|
|
2
|
+
import { execFile } from 'node:child_process';
|
|
3
|
+
/** Runs a command and returns what it printed. `missing` is what to say when the command is not installed. */
|
|
4
|
+
export function run(command, args, missing) {
|
|
5
|
+
return new Promise((resolve, reject) => {
|
|
6
|
+
execFile(command, args, { timeout: 60_000, maxBuffer: 16 * 1024 * 1024 }, (error, stdout, stderr) => {
|
|
7
|
+
if (error && error.code === 'ENOENT')
|
|
8
|
+
reject(new Error(`playwright-touch: ${missing}`));
|
|
9
|
+
else if (error)
|
|
10
|
+
reject(new Error(`playwright-touch: ${command} ${args.slice(0, 3).join(' ')} failed: ${(stderr || error.message).trim().split('\n')[0]}`));
|
|
11
|
+
else
|
|
12
|
+
resolve(stdout);
|
|
13
|
+
});
|
|
14
|
+
});
|
|
15
|
+
}
|
|
16
|
+
export const simctl = (...args) => run('xcrun', ['simctl', ...args], 'xcrun was not found. Install Xcode and its command line tools.');
|
|
17
|
+
export const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { type Simulator } from './device.js';
|
|
2
|
+
/** A finger's way across the screen: where it lands, then where it is after each further stretch of time. */
|
|
3
|
+
export type Path = {
|
|
4
|
+
x: number;
|
|
5
|
+
y: number;
|
|
6
|
+
ms: number;
|
|
7
|
+
}[];
|
|
8
|
+
/** What puts fingers on the screen. Points are in screen points. */
|
|
9
|
+
export interface Touch {
|
|
10
|
+
name: 'WebDriverAgent' | 'idb';
|
|
11
|
+
/** Several paths at once are several fingers. */
|
|
12
|
+
perform(fingers: Path[]): Promise<void>;
|
|
13
|
+
/** Two quick taps at one place, the way the system itself makes them. */
|
|
14
|
+
doubleTap(at: {
|
|
15
|
+
x: number;
|
|
16
|
+
y: number;
|
|
17
|
+
}): Promise<void>;
|
|
18
|
+
/** Starts watching the screen. Each new picture is handed over as a JPEG; the returned function stops it. */
|
|
19
|
+
watch(onFrame: (jpeg: Buffer) => void): () => void;
|
|
20
|
+
/** The application in front on the device, as in "com.apple.mobilesafari", where that can be asked. */
|
|
21
|
+
front?(): Promise<string | null>;
|
|
22
|
+
close(): Promise<void>;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Touch through a running WebDriverAgent: any number of fingers, each following its path exactly.
|
|
26
|
+
* Null when none answers at `url`. One that runs for another device than `device` is refused.
|
|
27
|
+
* `pictures` is where it streams the screen, when that is not port 9100 of the same host.
|
|
28
|
+
*/
|
|
29
|
+
export declare function webDriverAgent(url: string, device: Pick<Simulator, 'name' | 'system'>, pictures?: string): Promise<Touch | null>;
|
|
30
|
+
/** Touch through idb: one finger, in a straight line at an even pace. */
|
|
31
|
+
export declare function idbTouch(idb: (...args: string[]) => Promise<string>, udid: string): Touch;
|
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
// What puts fingers on the simulator's screen: WebDriverAgent, or idb where there is none.
|
|
2
|
+
import { spawn } from 'node:child_process';
|
|
3
|
+
import { anotherDevice, describe, servedBy } from './device.js';
|
|
4
|
+
/** Cuts a stream of JPEGs written one after another into pictures, by their start and end markers. */
|
|
5
|
+
function jpegs(onFrame) {
|
|
6
|
+
let pending = Buffer.alloc(0);
|
|
7
|
+
return (chunk) => {
|
|
8
|
+
pending = Buffer.concat([pending, chunk]);
|
|
9
|
+
for (;;) {
|
|
10
|
+
const start = pending.indexOf(Buffer.from([0xff, 0xd8, 0xff]));
|
|
11
|
+
const end = start < 0 ? -1 : pending.indexOf(Buffer.from([0xff, 0xd9]), start + 3);
|
|
12
|
+
if (end < 0)
|
|
13
|
+
return;
|
|
14
|
+
onFrame(Buffer.from(pending.subarray(start, end + 2)));
|
|
15
|
+
pending = pending.subarray(end + 2);
|
|
16
|
+
}
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Touch through a running WebDriverAgent: any number of fingers, each following its path exactly.
|
|
21
|
+
* Null when none answers at `url`. One that runs for another device than `device` is refused.
|
|
22
|
+
* `pictures` is where it streams the screen, when that is not port 9100 of the same host.
|
|
23
|
+
*/
|
|
24
|
+
export async function webDriverAgent(url, device, pictures) {
|
|
25
|
+
const call = async (method, path, body, timeout = 60_000) => {
|
|
26
|
+
const response = await fetch(url + path, {
|
|
27
|
+
method,
|
|
28
|
+
signal: AbortSignal.timeout(timeout),
|
|
29
|
+
headers: { 'content-type': 'application/json' },
|
|
30
|
+
body: body ? JSON.stringify(body) : undefined,
|
|
31
|
+
});
|
|
32
|
+
const answer = (await response.json());
|
|
33
|
+
if (answer.value?.error) {
|
|
34
|
+
const reason = String(answer.value.message ?? '').split('\n')[0];
|
|
35
|
+
throw new Error(`playwright-touch: WebDriverAgent refused: ${answer.value.error} ${reason}`);
|
|
36
|
+
}
|
|
37
|
+
return answer;
|
|
38
|
+
};
|
|
39
|
+
const served = await servedBy(url);
|
|
40
|
+
if (!served)
|
|
41
|
+
return null;
|
|
42
|
+
const other = anotherDevice(served, device);
|
|
43
|
+
if (other) {
|
|
44
|
+
throw new Error(`playwright-touch: the WebDriverAgent at ${url} is running for ${other}, not for ${describe(device)}. Stop it, or start one for this device.`);
|
|
45
|
+
}
|
|
46
|
+
const running = served.session;
|
|
47
|
+
// WebDriverAgent keeps one session. Whoever comes second joins it, and nobody ends it, so two
|
|
48
|
+
// clients do not cut each other off.
|
|
49
|
+
const created = running ? null : await call('POST', '/session', { capabilities: { alwaysMatch: {} } });
|
|
50
|
+
const session = `/session/${running ?? created?.value?.sessionId ?? created?.sessionId}`;
|
|
51
|
+
// After a gesture WebDriverAgent waits for the app to come to rest, which after a fling on a
|
|
52
|
+
// real phone took nineteen seconds. The page says itself when it has stopped; nothing here waits.
|
|
53
|
+
await call('POST', `${session}/appium/settings`, { settings: { waitForIdleTimeout: 0, animationCoolOffTimeout: 0 } }, 5000).catch(() => undefined);
|
|
54
|
+
return {
|
|
55
|
+
name: 'WebDriverAgent',
|
|
56
|
+
async perform(fingers) {
|
|
57
|
+
const actions = fingers.map((path, index) => ({
|
|
58
|
+
type: 'pointer',
|
|
59
|
+
id: `finger${index}`,
|
|
60
|
+
parameters: { pointerType: 'touch' },
|
|
61
|
+
actions: [
|
|
62
|
+
{ type: 'pointerMove', duration: 0, x: Math.round(path[0].x), y: Math.round(path[0].y) },
|
|
63
|
+
{ type: 'pointerDown', button: 0 },
|
|
64
|
+
// A rest is a move to the same place. A "pause" before a move is not honoured: the finger
|
|
65
|
+
// starts to creep at once.
|
|
66
|
+
...path.slice(1).map((step) => ({
|
|
67
|
+
type: 'pointerMove',
|
|
68
|
+
duration: Math.round(step.ms),
|
|
69
|
+
x: Math.round(step.x),
|
|
70
|
+
y: Math.round(step.y),
|
|
71
|
+
})),
|
|
72
|
+
{ type: 'pointerUp', button: 0 },
|
|
73
|
+
],
|
|
74
|
+
}));
|
|
75
|
+
await call('POST', `${session}/actions`, { actions });
|
|
76
|
+
},
|
|
77
|
+
async doubleTap(at) {
|
|
78
|
+
// Two taps written out as pointer actions arrive as one tap; WebDriverAgent's own double tap
|
|
79
|
+
// is what Safari takes for one.
|
|
80
|
+
await call('POST', `${session}/wda/doubleTap`, { x: Math.round(at.x), y: Math.round(at.y) });
|
|
81
|
+
},
|
|
82
|
+
watch(onFrame) {
|
|
83
|
+
// WebDriverAgent serves the screen as a stream of pictures on its own port, at a steady
|
|
84
|
+
// rate. Only pictures that differ from the one before are passed on.
|
|
85
|
+
const stream = new URL(pictures ?? url);
|
|
86
|
+
if (!pictures)
|
|
87
|
+
stream.port = '9100';
|
|
88
|
+
const stop = new AbortController();
|
|
89
|
+
let last;
|
|
90
|
+
const cut = (chunk) => {
|
|
91
|
+
// Each picture is announced with its length, which is safer than looking for an end marker.
|
|
92
|
+
pending = Buffer.concat([pending, chunk]);
|
|
93
|
+
for (;;) {
|
|
94
|
+
const head = pending.indexOf('\r\n\r\n');
|
|
95
|
+
if (head < 0)
|
|
96
|
+
return;
|
|
97
|
+
const length = Number(/Content-Length: (\d+)/i.exec(pending.subarray(0, head).toString('latin1'))?.[1] ?? -1);
|
|
98
|
+
if (length < 0) {
|
|
99
|
+
pending = pending.subarray(head + 4);
|
|
100
|
+
continue;
|
|
101
|
+
}
|
|
102
|
+
if (pending.length < head + 4 + length)
|
|
103
|
+
return;
|
|
104
|
+
const jpeg = Buffer.from(pending.subarray(head + 4, head + 4 + length));
|
|
105
|
+
pending = pending.subarray(head + 4 + length);
|
|
106
|
+
if (!last || !last.equals(jpeg))
|
|
107
|
+
onFrame(jpeg);
|
|
108
|
+
last = jpeg;
|
|
109
|
+
}
|
|
110
|
+
};
|
|
111
|
+
let pending = Buffer.alloc(0);
|
|
112
|
+
void (async () => {
|
|
113
|
+
try {
|
|
114
|
+
await call('POST', `${session}/appium/settings`, { settings: { mjpegServerFramerate: 30, mjpegScalingFactor: 30, mjpegServerScreenshotQuality: 60 } }, 5000).catch(() => undefined);
|
|
115
|
+
const response = await fetch(stream, { signal: stop.signal });
|
|
116
|
+
// A web stream is read with a reader; it can also be looped over, which the types here do not know yet.
|
|
117
|
+
const reader = response.body.getReader();
|
|
118
|
+
for (let part = await reader.read(); !part.done; part = await reader.read())
|
|
119
|
+
cut(Buffer.from(part.value));
|
|
120
|
+
}
|
|
121
|
+
catch {
|
|
122
|
+
// Stopped, or the stream is not there; the caller notices that no picture came.
|
|
123
|
+
}
|
|
124
|
+
})();
|
|
125
|
+
return () => stop.abort();
|
|
126
|
+
},
|
|
127
|
+
async front() {
|
|
128
|
+
const answer = (await call('GET', '/wda/activeAppInfo', undefined, 5000));
|
|
129
|
+
return answer.value?.bundleId ?? null;
|
|
130
|
+
},
|
|
131
|
+
async close() { },
|
|
132
|
+
};
|
|
133
|
+
}
|
|
134
|
+
/** Touch through idb: one finger, in a straight line at an even pace. */
|
|
135
|
+
export function idbTouch(idb, udid) {
|
|
136
|
+
return {
|
|
137
|
+
name: 'idb',
|
|
138
|
+
async perform(fingers) {
|
|
139
|
+
if (fingers.length > 1)
|
|
140
|
+
throw new Error('playwright-touch: two fingers need WebDriverAgent running for the simulator; idb takes one finger.');
|
|
141
|
+
const path = fingers[0];
|
|
142
|
+
const first = path[0];
|
|
143
|
+
const last = path.at(-1);
|
|
144
|
+
const seconds = (path.reduce((sum, step) => sum + step.ms, 0) / 1000).toFixed(3);
|
|
145
|
+
const at = (point) => [String(Math.round(point.x)), String(Math.round(point.y))];
|
|
146
|
+
// Straight within half a point: the points of a stroke are worked out, not typed in, so they
|
|
147
|
+
// never lie on the line exactly.
|
|
148
|
+
const length = Math.hypot(last.x - first.x, last.y - first.y);
|
|
149
|
+
const off = (step) => length === 0
|
|
150
|
+
? Math.hypot(step.x - first.x, step.y - first.y)
|
|
151
|
+
: Math.abs((step.x - first.x) * (last.y - first.y) - (step.y - first.y) * (last.x - first.x)) / length;
|
|
152
|
+
if (path.some((step) => off(step) > 0.5)) {
|
|
153
|
+
throw new Error('playwright-touch: a path that is not a straight line needs WebDriverAgent; idb moves a finger straight.');
|
|
154
|
+
}
|
|
155
|
+
if (first.x === last.x && first.y === last.y)
|
|
156
|
+
await idb('ui', 'tap', ...at(first), '--duration', seconds);
|
|
157
|
+
else
|
|
158
|
+
await idb('ui', 'swipe', ...at(first), ...at(last), '--duration', seconds, '--delta', '4');
|
|
159
|
+
},
|
|
160
|
+
async doubleTap(at) {
|
|
161
|
+
// Each call starts a helper, so the taps are as close together as idb gets them.
|
|
162
|
+
for (let tap = 0; tap < 2; tap++)
|
|
163
|
+
await this.perform([
|
|
164
|
+
[
|
|
165
|
+
{ ...at, ms: 0 },
|
|
166
|
+
{ ...at, ms: 50 },
|
|
167
|
+
],
|
|
168
|
+
]);
|
|
169
|
+
},
|
|
170
|
+
watch(onFrame) {
|
|
171
|
+
// The simulator streams a JPEG whenever the picture changes.
|
|
172
|
+
const stream = spawn('idb', ['video-stream', '--format', 'mjpeg', '--compression-quality', '0.6', '--scale-factor', '0.3', '--udid', udid], { stdio: ['ignore', 'pipe', 'ignore'] });
|
|
173
|
+
stream.stdout.on('data', jpegs(onFrame));
|
|
174
|
+
return () => void stream.kill();
|
|
175
|
+
},
|
|
176
|
+
async close() { },
|
|
177
|
+
};
|
|
178
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { type Socket } from 'node:net';
|
|
2
|
+
import type { Duplex } from 'node:stream';
|
|
3
|
+
import { type Plist } from './bplist.js';
|
|
4
|
+
/** Writes a property list as XML. */
|
|
5
|
+
export declare function toXml(value: Plist): Buffer;
|
|
6
|
+
/** Reads a property list written as XML. Dates are kept as text. */
|
|
7
|
+
export declare function fromXml(text: string): Plist;
|
|
8
|
+
/** An iPhone on the cable. */
|
|
9
|
+
export interface Iphone {
|
|
10
|
+
udid: string;
|
|
11
|
+
/** The number usbmuxd gave it for as long as it stays plugged in. */
|
|
12
|
+
id: number;
|
|
13
|
+
}
|
|
14
|
+
/** The iPhones connected by cable. */
|
|
15
|
+
export declare function iphones(): Promise<Iphone[]>;
|
|
16
|
+
/** A connection to a port on the phone, carried over the cable. */
|
|
17
|
+
export declare function tunnel(phone: Iphone, port: number): Promise<Socket>;
|
|
18
|
+
/** Offers a port of the phone on this computer. Returns the local port and a way to stop. */
|
|
19
|
+
export declare function forward(phone: Iphone, port: number): Promise<{
|
|
20
|
+
port: number;
|
|
21
|
+
close(): void;
|
|
22
|
+
}>;
|
|
23
|
+
/** The phone's lockdown service, with a session open: reads values and starts services. */
|
|
24
|
+
export interface Lockdown {
|
|
25
|
+
value(key: string): Promise<Plist>;
|
|
26
|
+
/** Starts a service on the phone and returns the connection to it. */
|
|
27
|
+
service(name: string): Promise<Duplex>;
|
|
28
|
+
close(): void;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Opens a session with the phone's lockdown service, as the Mac the owner trusted. The proof is
|
|
32
|
+
* the pairing record macOS keeps for the phone, which usbmuxd hands over.
|
|
33
|
+
*/
|
|
34
|
+
export declare function lockdown(phone: Iphone): Promise<Lockdown>;
|