1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
//! Multi-step flows in one call: `[{ action: "fill", target: "Email", value: "…" },
//! { action: "tap", target: "Log in" }, { action: "waitFor", text: "Welcome" }]`.
//! Each step resolves its target on a fresh, settled screen; the run stops at the first
//! step that fails and reports it with the screen as it is then, so an agent never acts
//! on a screen it has not seen.
use crate::RecordOptions;
use serde::{Deserialize, Serialize};
use std::time::Duration;
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(untagged)]
pub enum SwipeTarget {
Target(String),
Point([f64; 2]),
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(tag = "action", rename_all = "camelCase", deny_unknown_fields)]
pub enum Step {
/// `target`: an element ref from the last snapshot (`e4`), a label or identifier on
/// the current screen (`"Log in"`), or a grid cell (`C4`).
Tap { target: String },
/// Double tap a validated element ref, label, identifier, or grid cell.
DoubleTap { target: String },
/// Drag from one target to another (reorder handles, sliders): `from` and `to` are
/// refs, labels, or grid cells, resolved like `tap` resolves. The touch holds ~300 ms
/// before moving (so iOS sees a drag, not a scroll), then moves over `ms` (default 600).
Drag {
from: String,
to: String,
#[serde(default, rename = "sourceHoldMs")]
source_hold_ms: Option<u64>,
#[serde(default, rename = "moveMs", alias = "ms")]
move_ms: Option<u64>,
#[serde(default, rename = "destinationHoldMs")]
destination_hold_ms: Option<u64>,
},
/// Pinch around a target's centre (screen centre by default). A scale above 1 zooms in.
Pinch {
#[serde(default)]
target: Option<String>,
scale: f64,
},
/// Rotate around a target's centre (screen centre by default), in degrees.
Rotate {
#[serde(default)]
target: Option<String>,
degrees: f64,
},
/// Open a URL; this can switch the foreground app.
OpenUrl { url: String },
/// Launch an installed app by bundle identifier (`bundleId`).
#[serde(rename_all = "camelCase")]
Launch { bundle_id: String },
/// Terminate an app by bundle identifier (`bundleId`).
#[serde(rename_all = "camelCase")]
Terminate { bundle_id: String },
/// Set the simulator's location.
SetLocation { lat: f64, lon: f64 },
/// Set the simulator appearance to `light` or `dark`.
Appearance { appearance: String },
/// Read the simulator pasteboard; the value is included in the step report.
ClipboardRead,
/// Write text to the simulator pasteboard. An empty value clears it.
ClipboardWrite { text: String },
/// Dismiss the on-screen keyboard and confirm it is gone.
KeyboardDismiss,
/// Return using the system edge gesture or an in-app navigation button.
Back { mode: String },
/// Open the iOS app switcher with a double home-button press.
AppSwitcher,
/// Capture the whole screen as a plain PNG (no annotations) attached to the step
/// report, with target `screen`. `path` also writes it there: a file, or a directory
/// (existing, or ending in `/`) to put `screen-<unix_ms>.png` in; folders are created.
Screenshot {
#[serde(default)]
path: Option<String>,
},
/// Capture the target's frame as a cropped PNG attached to the step report.
ScreenshotElement { target: String },
/// Read, wait for, accept, or dismiss a system alert. Dismiss only uses explicit safe labels.
Alert {
op: String,
#[serde(default)]
ms: Option<u64>,
},
/// Move two fingers together around a point, with momentum disabled.
Pan {
#[serde(default)]
target: Option<String>,
dx: f64,
dy: f64,
#[serde(default)]
ms: Option<u64>,
},
/// One continuous two-finger pan, scale, and rotation stroke.
Transform {
#[serde(default)]
target: Option<String>,
#[serde(default)]
dx: f64,
#[serde(default)]
dy: f64,
scale: f64,
#[serde(default)]
degrees: f64,
},
/// Record the screen over the steps that follow (valid only as the first step): a
/// few timed frames and a contact sheet, in the report's `record`. Unset fields take
/// the recorder's defaults and limits.
#[serde(rename_all = "camelCase")]
Record {
#[serde(default)]
time_ms: Option<u64>,
#[serde(default)]
idle_ms: Option<u64>,
#[serde(default)]
full: bool,
#[serde(default)]
fps: Option<u32>,
#[serde(default)]
max_frames: Option<usize>,
},
/// Fast directional swipe with momentum.
Fling { direction: String },
/// Swipe between refs, labels, grid cells, or normalized `[x, y]` points.
Swipe {
from: SwipeTarget,
to: SwipeTarget,
#[serde(default)]
count: Option<u32>,
#[serde(default)]
#[serde(rename = "pauseMs")]
pause_ms: Option<u64>,
},
/// Press and hold `target` (a ref, label, or grid cell) — iOS's long press (context
/// menus, rearrange mode). `ms` defaults to 800.
#[serde(rename_all = "camelCase")]
LongPress {
target: String,
#[serde(default)]
ms: Option<u64>,
},
/// Tap `target` (a text field), then type `value`.
Fill {
target: String,
value: String,
#[serde(default)]
secret: bool,
},
/// Scroll to reveal what lies `direction` (`up`, `down`, `left`, `right`): `down`
/// drags the content up to show what is below. Inside `target` (a ref or label) when
/// given, else the middle of the screen.
Scroll {
direction: String,
#[serde(default)]
target: Option<String>,
},
/// Type into whatever has focus.
Type { value: String },
/// A named key: `enter`, `backspace`, `tab`, `escape`, arrows…
Key { name: String },
/// A hardware button: `home`, `lock`, `side`, `siri`, `volume-up`, `volume-down`.
Press { button: String },
/// Wait until some label or value on screen contains `text` (case-insensitive).
#[serde(rename_all = "camelCase")]
WaitFor {
text: String,
#[serde(default)]
timeout_ms: Option<u64>,
},
}
impl Step {
/// For `Record`: its options over [`RecordOptions::default`] (the recorder clamps).
pub(crate) fn record_options(&self) -> Option<RecordOptions> {
let Step::Record {
time_ms,
idle_ms,
full,
fps,
max_frames,
} = self
else {
return None;
};
let d = RecordOptions::default();
Some(RecordOptions {
time: time_ms.map_or(d.time, Duration::from_millis),
idle: idle_ms.map_or(d.idle, Duration::from_millis),
full: *full,
fps: fps.unwrap_or(d.fps),
max_frames: max_frames.unwrap_or(d.max_frames),
})
}
}
#[derive(Debug, Clone, PartialEq, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct StepFailure {
pub index: usize,
pub step: Step,
pub code: String,
pub error: String,
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn steps_parse_from_the_json_agents_write() {
let steps: Vec<Step> = serde_json::from_str(
r#"[{"action":"fill","target":"Email","value":"a@b.c"},
{"action":"tap","target":"e4"},
{"action":"waitFor","text":"Welcome","timeoutMs":2000},
{"action":"press","button":"home"}]"#,
)
.unwrap();
assert_eq!(
steps[0],
Step::Fill {
target: "Email".into(),
value: "a@b.c".into(),
secret: false
}
);
assert_eq!(
steps[2],
Step::WaitFor {
text: "Welcome".into(),
timeout_ms: Some(2000)
}
);
let steps: Vec<Step> = serde_json::from_str(
r#"[{"action":"longPress","target":"e4"},
{"action":"longPress","target":"General","ms":1500},
{"action":"drag","from":"e1","to":"C4"},
{"action":"drag","from":"Email","to":"Log in","ms":1200},
{"action":"pinch","scale":2.0},
{"action":"rotate","target":"Map","degrees":90},
{"action":"key","name":"cmd+a"},
{"action":"openUrl","url":"https://example.com"},
{"action":"setLocation","lat":6.5,"lon":3.3},
{"action":"appearance","appearance":"dark"},
{"action":"keyboardDismiss"},
{"action":"back","mode":"inApp"},
{"action":"screenshotElement","target":"e1"},
{"action":"launch","bundleId":"com.apple.Preferences"},
{"action":"swipe","from":[0.5,0.8],"to":"e2","count":2,"pauseMs":100},
{"action":"drag","from":"e1","to":"e2","sourceHoldMs":300,"moveMs":400,"destinationHoldMs":50}]"#,
)
.unwrap();
assert_eq!(
steps[0],
Step::LongPress {
target: "e4".into(),
ms: None
}
);
assert_eq!(
steps[1],
Step::LongPress {
target: "General".into(),
ms: Some(1500)
}
);
assert_eq!(
steps[2],
Step::Drag {
from: "e1".into(),
to: "C4".into(),
source_hold_ms: None,
move_ms: None,
destination_hold_ms: None
}
);
assert_eq!(
steps[3],
Step::Drag {
from: "Email".into(),
to: "Log in".into(),
source_hold_ms: None,
move_ms: Some(1200),
destination_hold_ms: None
}
);
assert_eq!(
steps[4],
Step::Pinch {
target: None,
scale: 2.0
}
);
assert_eq!(
steps[5],
Step::Rotate {
target: Some("Map".into()),
degrees: 90.0
}
);
assert_eq!(
steps[6],
Step::Key {
name: "cmd+a".into()
}
);
assert_eq!(
steps[7],
Step::OpenUrl {
url: "https://example.com".into()
}
);
assert_eq!(steps[8], Step::SetLocation { lat: 6.5, lon: 3.3 });
assert_eq!(
steps[9],
Step::Appearance {
appearance: "dark".into()
}
);
assert_eq!(steps[10], Step::KeyboardDismiss);
assert_eq!(
steps[11],
Step::Back {
mode: "inApp".into()
}
);
assert_eq!(
steps[12],
Step::ScreenshotElement {
target: "e1".into()
}
);
assert_eq!(
steps[13],
Step::Launch {
bundle_id: "com.apple.Preferences".into()
}
);
assert_eq!(
steps[14],
Step::Swipe {
from: SwipeTarget::Point([0.5, 0.8]),
to: SwipeTarget::Target("e2".into()),
count: Some(2),
pause_ms: Some(100)
}
);
assert_eq!(
steps[15],
Step::Drag {
from: "e1".into(),
to: "e2".into(),
source_hold_ms: Some(300),
move_ms: Some(400),
destination_hold_ms: Some(50)
}
);
assert!(
serde_json::from_str::<Step>(r#"{"action":"launch","bundle_id":"x"}"#).is_err(),
"camelCase only"
);
assert!(
serde_json::from_str::<Step>(r#"{"action":"tap","targt":"x"}"#).is_err(),
"typos are errors"
);
}
}