ferridriver 0.3.0

Browser automation in Rust with a Playwright-compatible API. Four pluggable backends: CDP pipe, CDP WebSocket, Playwright WebKit, Firefox BiDi.
Documentation
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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
use super::{StepCategory, StepDef, q};

pub fn register(steps: &mut Vec<Box<dyn StepDef>>) {
  // Page-level assertions FIRST (more specific, avoids regex overlap with element-level)
  steps.push(Box::new(PageNotHasText));
  steps.push(Box::new(PageHasText));
  // URL / title
  steps.push(Box::new(UrlContains));
  steps.push(Box::new(UrlExact));
  steps.push(Box::new(TitleContains));
  steps.push(Box::new(TitleExact));
  // Visibility
  steps.push(Box::new(NotVisible));
  steps.push(Box::new(Visible));
  // Element text (negated first)
  steps.push(Box::new(NotContainsText));
  steps.push(Box::new(ContainsText));
  steps.push(Box::new(TextExact));
  // Value
  steps.push(Box::new(ValueExact));
  // Attributes / classes
  steps.push(Box::new(HasAttrValue));
  steps.push(Box::new(NotHasAttr));
  steps.push(Box::new(HasAttr));
  steps.push(Box::new(NotHasClass));
  steps.push(Box::new(HasClass));
  // State
  steps.push(Box::new(Disabled));
  steps.push(Box::new(Enabled));
  steps.push(Box::new(NotChecked));
  steps.push(Box::new(Checked));
  // Count
  steps.push(Box::new(ElementCount));
}

// ── Page-level text ──

step!(PageHasText {
    category: StepCategory::Assertion,
    pattern: r"^the page should (?:have|contain) text (.+)$",
    description: "Assert page body contains text",
    example: "Then the page should contain text \"Welcome\"",
    execute(page, caps, _table, _vars) {
        let text = q(&caps[1]);
        let loc = page.locator("body", None);
        let content = loc.text_content().await?.unwrap_or_default();
        if !content.contains(&text) {
            return Err(crate::error::FerriError::backend(format!("Page does not contain text '{text}'")));
        }
        Ok(None)
    }
});

step!(PageNotHasText {
    category: StepCategory::Assertion,
    pattern: r"^the page should not (?:have|contain) text (.+)$",
    description: "Assert page body does not contain text",
    example: "Then the page should not contain text \"Error\"",
    execute(page, caps, _table, _vars) {
        let text = q(&caps[1]);
        let loc = page.locator("body", None);
        let content = loc.text_content().await?.unwrap_or_default();
        if content.contains(&text) {
            return Err(crate::error::FerriError::backend(format!("Page contains text '{text}' but should not")));
        }
        Ok(None)
    }
});

// ── URL / title ──

step!(UrlContains {
    category: StepCategory::Assertion,
    pattern: r"^the URL should contain (.+)$",
    description: "Assert URL contains substring",
    example: "Then the URL should contain \"/dashboard\"",
    execute(page, caps, _table, _vars) {
        let expected = q(&caps[1]);
        let url = page.url();
        if !url.contains(&expected) {
            return Err(crate::error::FerriError::backend(format!("URL '{url}' does not contain '{expected}'")));
        }
        Ok(None)
    }
});

step!(UrlExact {
    category: StepCategory::Assertion,
    pattern: r"^the URL should be (.+)$",
    description: "Assert exact URL",
    example: "Then the URL should be \"https://example.com/\"",
    execute(page, caps, _table, _vars) {
        let expected = q(&caps[1]);
        let url = page.url();
        if url != expected {
            return Err(crate::error::FerriError::backend(format!("URL is '{url}', expected '{expected}'")));
        }
        Ok(None)
    }
});

step!(TitleContains {
    category: StepCategory::Assertion,
    pattern: r"^the title should contain (.+)$",
    description: "Assert title contains substring",
    example: "Then the title should contain \"Dashboard\"",
    execute(page, caps, _table, _vars) {
        let expected = q(&caps[1]);
        let title = page.title().await.unwrap_or_default();
        if !title.contains(&expected) {
            return Err(crate::error::FerriError::backend(format!("Title '{title}' does not contain '{expected}'")));
        }
        Ok(None)
    }
});

step!(TitleExact {
    category: StepCategory::Assertion,
    pattern: r"^the title should be (.+)$",
    description: "Assert exact title",
    example: "Then the title should be \"My App\"",
    execute(page, caps, _table, _vars) {
        let expected = q(&caps[1]);
        let title = page.title().await.unwrap_or_default();
        if title != expected {
            return Err(crate::error::FerriError::backend(format!("Title is '{title}', expected '{expected}'")));
        }
        Ok(None)
    }
});

// ── Visibility ──

step!(Visible {
    category: StepCategory::Assertion,
    pattern: r"^(.+) should be visible$",
    description: "Assert element exists and is visible",
    example: "Then \"#dialog\" should be visible",
    execute(page, caps, _table, _vars) {
        let sel = q(&caps[1]);
        let loc = page.locator(&sel, None);
        let visible = loc.is_visible().await.unwrap_or(false);
        if !visible {
            return Err(crate::error::FerriError::backend(format!("'{sel}' is not visible")));
        }
        Ok(None)
    }
});

step!(NotVisible {
    category: StepCategory::Assertion,
    pattern: r"^(.+) should not be visible$",
    description: "Assert element does not exist or is hidden",
    example: "Then \"#spinner\" should not be visible",
    execute(page, caps, _table, _vars) {
        let sel = q(&caps[1]);
        let loc = page.locator(&sel, None);
        let hidden = loc.is_hidden().await.unwrap_or(true);
        if !hidden {
            return Err(crate::error::FerriError::backend(format!("'{sel}' is visible but should not be")));
        }
        Ok(None)
    }
});

// ── Element text ──

step!(ContainsText {
    category: StepCategory::Assertion,
    pattern: r"^(.+) should contain text (.+)$",
    description: "Assert element contains text",
    example: "Then \"#message\" should contain text \"Success\"",
    execute(page, caps, _table, _vars) {
        let sel = q(&caps[1]);
        let expected = q(&caps[2]);
        let loc = page.locator(&sel, None);
        let text = loc.inner_text().await.map_err(|_| format!("'{sel}' not found"))?;
        if !text.contains(&expected) {
            return Err(crate::error::FerriError::backend(format!("'{sel}' text is '{text}', does not contain '{expected}'")));
        }
        Ok(None)
    }
});

step!(NotContainsText {
    category: StepCategory::Assertion,
    pattern: r"^(.+) should not contain text (.+)$",
    description: "Assert element does not contain text",
    example: "Then \"#status\" should not contain text \"Error\"",
    execute(page, caps, _table, _vars) {
        let sel = q(&caps[1]);
        let expected = q(&caps[2]);
        let loc = page.locator(&sel, None);
        let text = loc.inner_text().await.map_err(|_| format!("'{sel}' not found"))?;
        if text.contains(&expected) {
            return Err(crate::error::FerriError::backend(format!("'{sel}' text '{text}' contains '{expected}' but should not")));
        }
        Ok(None)
    }
});

step!(TextExact {
    category: StepCategory::Assertion,
    pattern: r"^(.+) should have text (.+)$",
    description: "Assert element has exact text",
    example: "Then \"h1\" should have text \"Welcome\"",
    execute(page, caps, _table, _vars) {
        let sel = q(&caps[1]);
        let expected = q(&caps[2]);
        let loc = page.locator(&sel, None);
        let text = loc.inner_text().await.map_err(|_| format!("'{sel}' not found"))?;
        if text.trim() != expected.trim() {
            return Err(crate::error::FerriError::backend(format!("'{sel}' text is '{text}', expected '{expected}'")));
        }
        Ok(None)
    }
});

step!(ValueExact {
    category: StepCategory::Assertion,
    pattern: r"^(.+) should have value (.+)$",
    description: "Assert input has value",
    example: "Then \"#email\" should have value \"test@example.com\"",
    execute(page, caps, _table, _vars) {
        let sel = q(&caps[1]);
        let expected = q(&caps[2]);
        let loc = page.locator(&sel, None);
        let val = loc.input_value().await.map_err(|_| format!("'{sel}' not found"))?;
        if val != expected {
            return Err(crate::error::FerriError::backend(format!("'{sel}' value is '{val}', expected '{expected}'")));
        }
        Ok(None)
    }
});

// ── Attributes / classes ──

step!(HasAttrValue {
    category: StepCategory::Assertion,
    pattern: r"^(.+) should have attribute (.+) with value (.+)$",
    description: "Assert attribute has value",
    example: "Then \"#link\" should have attribute \"href\" with value \"/about\"",
    execute(page, caps, _table, _vars) {
        let sel = q(&caps[1]);
        let attr = q(&caps[2]);
        let expected = q(&caps[3]);
        let loc = page.locator(&sel, None);
        let val = loc.get_attribute(&attr).await
            .map_err(|_| format!("'{sel}' not found"))?
            .unwrap_or_default();
        if val != expected {
            return Err(crate::error::FerriError::backend(format!("'{sel}' attribute '{attr}' is '{val}', expected '{expected}'")));
        }
        Ok(None)
    }
});

step!(HasAttr {
    category: StepCategory::Assertion,
    pattern: r"^(.+) should have attribute (.+)$",
    description: "Assert element has attribute",
    example: "Then \"#input\" should have attribute \"required\"",
    execute(page, caps, _table, _vars) {
        let sel = q(&caps[1]);
        let attr = q(&caps[2]);
        let loc = page.locator(&sel, None);
        let val = loc.get_attribute(&attr).await
            .map_err(|_| format!("'{sel}' not found"))?;
        if val.is_none() {
            return Err(crate::error::FerriError::backend(format!("'{sel}' does not have attribute '{attr}'")));
        }
        Ok(None)
    }
});

step!(NotHasAttr {
    category: StepCategory::Assertion,
    pattern: r"^(.+) should not have attribute (.+)$",
    description: "Assert element lacks attribute",
    example: "Then \"#input\" should not have attribute \"disabled\"",
    execute(page, caps, _table, _vars) {
        let sel = q(&caps[1]);
        let attr = q(&caps[2]);
        let loc = page.locator(&sel, None);
        let val = loc.get_attribute(&attr).await
            .map_err(|_| format!("'{sel}' not found"))?;
        if val.is_some() {
            return Err(crate::error::FerriError::backend(format!("'{sel}' has attribute '{attr}' but should not")));
        }
        Ok(None)
    }
});

step!(HasClass {
    category: StepCategory::Assertion,
    pattern: r"^(.+) should have class (.+)$",
    description: "Assert element has CSS class",
    example: "Then \"#btn\" should have class \"active\"",
    execute(page, caps, _table, _vars) {
        let sel = q(&caps[1]);
        let cls = q(&caps[2]);
        let loc = page.locator(&sel, None);
        let classes = loc.get_attribute("class").await
            .map_err(|_| format!("'{sel}' not found"))?
            .unwrap_or_default();
        if !classes.split_whitespace().any(|c| c == cls) {
            return Err(crate::error::FerriError::backend(format!("'{sel}' does not have class '{cls}'")));
        }
        Ok(None)
    }
});

step!(NotHasClass {
    category: StepCategory::Assertion,
    pattern: r"^(.+) should not have class (.+)$",
    description: "Assert element lacks CSS class",
    example: "Then \"#btn\" should not have class \"loading\"",
    execute(page, caps, _table, _vars) {
        let sel = q(&caps[1]);
        let cls = q(&caps[2]);
        let loc = page.locator(&sel, None);
        let classes = loc.get_attribute("class").await
            .map_err(|_| format!("'{sel}' not found"))?
            .unwrap_or_default();
        if classes.split_whitespace().any(|c| c == cls) {
            return Err(crate::error::FerriError::backend(format!("'{sel}' has class '{cls}' but should not")));
        }
        Ok(None)
    }
});

// ── State ──

step!(Enabled {
    category: StepCategory::Assertion,
    pattern: r"^(.+) should be enabled$",
    description: "Assert element is enabled",
    example: "Then \"#submit\" should be enabled",
    execute(page, caps, _table, _vars) {
        let sel = q(&caps[1]);
        let loc = page.locator(&sel, None);
        let enabled = loc.is_enabled().await.map_err(|_| format!("'{sel}' not found"))?;
        if !enabled {
            return Err(crate::error::FerriError::backend(format!("'{sel}' is disabled")));
        }
        Ok(None)
    }
});

step!(Disabled {
    category: StepCategory::Assertion,
    pattern: r"^(.+) should be disabled$",
    description: "Assert element is disabled",
    example: "Then \"#submit\" should be disabled",
    execute(page, caps, _table, _vars) {
        let sel = q(&caps[1]);
        let loc = page.locator(&sel, None);
        let disabled = loc.is_disabled().await.map_err(|_| format!("'{sel}' not found"))?;
        if !disabled {
            return Err(crate::error::FerriError::backend(format!("'{sel}' is not disabled")));
        }
        Ok(None)
    }
});

step!(Checked {
    category: StepCategory::Assertion,
    pattern: r"^(.+) should be checked$",
    description: "Assert checkbox is checked",
    example: "Then \"#agree\" should be checked",
    execute(page, caps, _table, _vars) {
        let sel = q(&caps[1]);
        let loc = page.locator(&sel, None);
        let checked = loc.is_checked().await.map_err(|_| format!("'{sel}' not found"))?;
        if !checked {
            return Err(crate::error::FerriError::backend(format!("'{sel}' is not checked")));
        }
        Ok(None)
    }
});

step!(NotChecked {
    category: StepCategory::Assertion,
    pattern: r"^(.+) should not be checked$",
    description: "Assert checkbox is not checked",
    example: "Then \"#agree\" should not be checked",
    execute(page, caps, _table, _vars) {
        let sel = q(&caps[1]);
        let loc = page.locator(&sel, None);
        let checked = loc.is_checked().await.map_err(|_| format!("'{sel}' not found"))?;
        if checked {
            return Err(crate::error::FerriError::backend(format!("'{sel}' is checked but should not be")));
        }
        Ok(None)
    }
});

step!(ElementCount {
    category: StepCategory::Assertion,
    pattern: r"^there should be (\d+) (.+)$",
    description: "Assert element count",
    example: "Then there should be 3 \".item\"",
    execute(page, caps, _table, _vars) {
        let expected: usize = caps[1].parse().map_err(|_| "Invalid count")?;
        let sel = q(&caps[2]);
        let loc = page.locator(&sel, None);
        let actual = loc.count().await?;
        if actual != expected {
            return Err(crate::error::FerriError::backend(format!("Found {actual} '{sel}', expected {expected}")));
        }
        Ok(None)
    }
});