ferridriver-test 0.4.0

E2E test runner for ferridriver. Playwright-compatible API, parallel workers, auto-retrying expect, fixtures, snapshots.
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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
#![allow(clippy::unwrap_used, clippy::doc_markdown)]
//! E2E tests for all Playwright-compatible features:
//! - Retry with flaky detection
//! - All expect matchers (visibility, text, value, attributes, CSS, count, focused, etc.)
//! - expect.poll() and toPass()
//! - Screenshot capture on failure
//! - SuiteMode (parallel/serial)
//! - repeatEach

use std::sync::Arc;
use std::sync::atomic::{AtomicU32, Ordering};
use std::time::Duration;

use ferridriver_test::config::{CliOverrides, TestConfig};
use ferridriver_test::model::*;
use ferridriver_test::runner::TestRunner;

fn data_url(html: &str) -> String {
  format!(
    "data:text/html,{}",
    html
      .bytes()
      .map(|b| match b {
        b'A'..=b'Z' | b'a'..=b'z' | b'0'..=b'9' | b'-' | b'_' | b'.' | b'~' => {
          (b as char).to_string()
        },
        _ => format!("%{b:02X}"),
      })
      .collect::<String>()
  )
}

// ── Retry + flaky detection test ──

/// A test that fails on the first attempt and passes on the second.
fn make_flaky_test() -> TestCase {
  static ATTEMPT_COUNTER: AtomicU32 = AtomicU32::new(0);
  // Reset for this test run.
  ATTEMPT_COUNTER.store(0, Ordering::SeqCst);

  TestCase {
    id: TestId {
      file: "features_e2e.rs".into(),
      suite: Some("retry".into()),
      name: "flaky_test_passes_on_retry".into(),
      line: None,
    },
    test_fn: Arc::new(|_pool| {
      Box::pin(async move {
        let attempt = ATTEMPT_COUNTER.fetch_add(1, Ordering::SeqCst) + 1;
        if attempt == 1 {
          Err(TestFailure {
            message: "intentional first-attempt failure".into(),
            stack: None,
            diff: None,
            screenshot: None,
          })
        } else {
          Ok(())
        }
      })
    }),
    fixture_requests: vec![],
    annotations: Vec::new(),
    timeout: Some(Duration::from_secs(5)),
    retries: Some(1), // Allow 1 retry.
    expected_status: ExpectedStatus::Pass,
    use_options: None,
  }
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn test_retry_with_flaky_detection() {
  let plan = TestPlan {
    suites: vec![TestSuite {
      name: "retry".into(),
      file: "features_e2e.rs".into(),
      tests: vec![make_flaky_test()],
      hooks: Hooks::default(),
      annotations: Vec::new(),
      mode: SuiteMode::default(),
    }],
    total_tests: 1,
    shard: None,
  };

  let config = TestConfig {
    workers: 1,
    timeout: 10_000,
    ..Default::default()
  };
  let mut runner = TestRunner::new(config, CliOverrides::default());
  let exit_code = runner.run(plan).await;
  // Flaky tests count as passed (exit code 0).
  assert_eq!(exit_code, 0, "flaky test should pass after retry");
}

// ── All locator matchers test ──

async fn assert_visibility_state(page: &Arc<ferridriver::Page>) -> Result<(), TestFailure> {
  ferridriver_test::expect(&page.locator("#visible", None))
    .to_be_visible()
    .await?;
  ferridriver_test::expect(&page.locator("#hidden", None))
    .to_be_hidden()
    .await?;
  ferridriver_test::expect(&page.locator("#visible", None))
    .not()
    .to_be_hidden()
    .await?;
  ferridriver_test::expect(&page.locator("#btn", None))
    .to_be_disabled()
    .await?;
  ferridriver_test::expect(&page.locator("#inp", None))
    .to_be_enabled()
    .await?;
  ferridriver_test::expect(&page.locator("#check", None))
    .to_be_checked()
    .await?;
  ferridriver_test::expect(&page.locator("#area", None))
    .to_be_editable()
    .await?;
  ferridriver_test::expect(&page.locator("#visible", None))
    .to_be_attached()
    .await?;
  ferridriver_test::expect(&page.locator("#empty", None))
    .to_be_empty()
    .await?;
  ferridriver_test::expect(&page.locator("#visible", None))
    .not()
    .to_be_empty()
    .await?;
  Ok(())
}

async fn assert_text_and_attributes(page: &Arc<ferridriver::Page>) -> Result<(), TestFailure> {
  ferridriver_test::expect(&page.locator("#visible", None))
    .to_have_text("Visible")
    .await?;
  ferridriver_test::expect(&page.locator("#btn", None))
    .to_contain_text("Submit")
    .await?;
  ferridriver_test::expect(&page.locator("#inp", None))
    .to_have_value("hello")
    .await?;
  ferridriver_test::expect(&page.locator("#inp", None))
    .to_have_attribute("type", "text")
    .await?;
  ferridriver_test::expect(&page.locator("#btn", None))
    .to_have_class("primary large")
    .await?;
  ferridriver_test::expect(&page.locator("#btn", None))
    .to_contain_class("primary")
    .await?;
  ferridriver_test::expect(&page.locator("#btn", None))
    .to_contain_class("large")
    .await?;
  ferridriver_test::expect(&page.locator("#btn", None))
    .not()
    .to_contain_class("secondary")
    .await?;
  Ok(())
}

async fn assert_identity_and_structure(page: &Arc<ferridriver::Page>) -> Result<(), TestFailure> {
  ferridriver_test::expect(&page.locator("#btn", None))
    .to_have_id("btn")
    .await?;
  ferridriver_test::expect(&page.locator("#btn", None))
    .to_have_role("button")
    .await?;
  ferridriver_test::expect(&page.locator("#btn", None))
    .to_have_accessible_name("Submit Form")
    .await?;
  ferridriver_test::expect(&page.locator("#btn", None))
    .to_have_accessible_description("Submits the form")
    .await?;
  ferridriver_test::expect(&page.locator("div", None))
    .to_have_count(4)
    .await?;
  ferridriver_test::expect(&page.locator("#styled", None))
    .to_have_css("color", "rgb(255, 0, 0)")
    .await?;
  ferridriver_test::expect(&page.locator("#inp", None))
    .to_have_js_property("type", serde_json::json!("text"))
    .await?;
  ferridriver_test::expect(&page.locator("#multi", None))
    .to_have_values(&["a", "b"])
    .await?;
  Ok(())
}

async fn assert_all_matchers(page: &Arc<ferridriver::Page>) -> Result<(), TestFailure> {
  let html = r#"
    <div id="visible" style="display:block">Visible</div>
    <div id="hidden" style="display:none">Hidden</div>
    <button id="btn" disabled class="primary large" role="button"
            aria-label="Submit Form" aria-description="Submits the form">
      Submit
    </button>
    <input id="inp" type="text" value="hello" />
    <input id="check" type="checkbox" checked />
    <textarea id="area" contenteditable="true">Editable</textarea>
    <select id="multi" multiple>
      <option value="a" selected>A</option>
      <option value="b" selected>B</option>
      <option value="c">C</option>
    </select>
    <div id="empty"></div>
    <div id="styled" style="color: rgb(255, 0, 0);">Red</div>
  "#;
  let url = data_url(html);
  page.goto(&url, None).await.map_err(make_failure)?;

  assert_visibility_state(page).await?;
  assert_text_and_attributes(page).await?;
  assert_identity_and_structure(page).await?;

  Ok(())
}

fn make_matchers_test() -> TestCase {
  TestCase {
    id: TestId {
      file: "features_e2e.rs".into(),
      suite: Some("matchers".into()),
      name: "all_locator_matchers".into(),
      line: None,
    },
    test_fn: Arc::new(|pool| {
      Box::pin(async move {
        let page: Arc<ferridriver::Page> = pool.get("page").await.map_err(make_failure)?;
        assert_all_matchers(&page).await
      })
    }),
    fixture_requests: vec!["page".into()],
    annotations: Vec::new(),
    timeout: Some(Duration::from_secs(30)),
    retries: None,
    expected_status: ExpectedStatus::Pass,
    use_options: None,
  }
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn test_all_locator_matchers() {
  let plan = TestPlan {
    suites: vec![TestSuite {
      name: "matchers".into(),
      file: "features_e2e.rs".into(),
      tests: vec![make_matchers_test()],
      hooks: Hooks::default(),
      annotations: Vec::new(),
      mode: SuiteMode::default(),
    }],
    total_tests: 1,
    shard: None,
  };

  let config = TestConfig {
    workers: 1,
    timeout: 30_000,
    ..Default::default()
  };
  let mut runner = TestRunner::new(config, CliOverrides::default());
  let exit_code = runner.run(plan).await;
  assert_eq!(exit_code, 0, "all matchers should pass");
}

// ── expect.poll() test ──

fn make_poll_test() -> TestCase {
  TestCase {
    id: TestId {
      file: "features_e2e.rs".into(),
      suite: Some("expect_poll".into()),
      name: "poll_until_value_matches".into(),
      line: None,
    },
    test_fn: Arc::new(|_pool| {
      Box::pin(async move {
        let counter = Arc::new(AtomicU32::new(0));
        let counter_clone = Arc::clone(&counter);

        // Spawn a task that increments counter every 100ms.
        let handle = tokio::spawn(async move {
          for _ in 0..10 {
            tokio::time::sleep(Duration::from_millis(50)).await;
            counter_clone.fetch_add(1, Ordering::SeqCst);
          }
        });

        // Poll until counter reaches at least 5.
        let counter_ref = Arc::clone(&counter);
        ferridriver_test::expect_poll(
          move || {
            let c = counter_ref.load(Ordering::SeqCst);
            async move { c }
          },
          Duration::from_secs(5),
        )
        .to_satisfy(|v| *v >= 5, "counter should reach >= 5")
        .await?;

        handle.await.ok();
        Ok(())
      })
    }),
    fixture_requests: vec![],
    annotations: Vec::new(),
    timeout: Some(Duration::from_secs(10)),
    retries: None,
    expected_status: ExpectedStatus::Pass,
    use_options: None,
  }
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn test_expect_poll() {
  let plan = TestPlan {
    suites: vec![TestSuite {
      name: "expect_poll".into(),
      file: "features_e2e.rs".into(),
      tests: vec![make_poll_test()],
      hooks: Hooks::default(),
      annotations: Vec::new(),
      mode: SuiteMode::default(),
    }],
    total_tests: 1,
    shard: None,
  };

  let config = TestConfig {
    workers: 1,
    timeout: 15_000,
    ..Default::default()
  };
  let mut runner = TestRunner::new(config, CliOverrides::default());
  let exit_code = runner.run(plan).await;
  assert_eq!(exit_code, 0, "expect.poll should pass");
}

// ── toPass() test ──

fn make_to_pass_test() -> TestCase {
  TestCase {
    id: TestId {
      file: "features_e2e.rs".into(),
      suite: Some("to_pass".into()),
      name: "retries_block_until_success".into(),
      line: None,
    },
    test_fn: Arc::new(|pool| {
      Box::pin(async move {
        let page: Arc<ferridriver::Page> = pool.get("page").await.map_err(make_failure)?;
        // Page with a button that reveals text after click.
        let html = r#"
          <div id="status">loading</div>
          <script>setTimeout(() => document.getElementById('status').textContent = 'ready', 300)</script>
        "#;
        page.goto(&data_url(html), None).await.map_err(make_failure)?;

        // toPass retries the block until it succeeds. The closure
        // returns an `AssertionFailure` (the shared assertion error
        // type); the test-runner adapter converts to `TestFailure` at
        // the call site below.
        ferridriver_test::to_pass(Duration::from_secs(5), || {
          let page = Arc::clone(&page);
          async move {
            let text = page
              .locator("#status", None)
              .text_content()
              .await
              .map_err(|e| ferridriver_test::expect::AssertionFailure::new(e.to_string(), None))?
              .unwrap_or_default();
            if text != "ready" {
              return Err(ferridriver_test::expect::AssertionFailure::new(
                format!("expected 'ready', got '{text}'"),
                None,
              ));
            }
            Ok(())
          }
        })
        .await
        .map_err(TestFailure::from)?;

        Ok(())
      })
    }),
    fixture_requests: vec!["page".into()],
    annotations: Vec::new(),
    timeout: Some(Duration::from_secs(15)),
    retries: None,
    expected_status: ExpectedStatus::Pass,
    use_options: None,
  }
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn test_to_pass() {
  let plan = TestPlan {
    suites: vec![TestSuite {
      name: "to_pass".into(),
      file: "features_e2e.rs".into(),
      tests: vec![make_to_pass_test()],
      hooks: Hooks::default(),
      annotations: Vec::new(),
      mode: SuiteMode::default(),
    }],
    total_tests: 1,
    shard: None,
  };

  let config = TestConfig {
    workers: 1,
    timeout: 15_000,
    ..Default::default()
  };
  let mut runner = TestRunner::new(config, CliOverrides::default());
  let exit_code = runner.run(plan).await;
  assert_eq!(exit_code, 0, "toPass should succeed");
}

// ── Page assertions test ──

fn make_page_assertions_test() -> TestCase {
  TestCase {
    id: TestId {
      file: "features_e2e.rs".into(),
      suite: Some("page".into()),
      name: "page_title_and_url".into(),
      line: None,
    },
    test_fn: Arc::new(|pool| {
      Box::pin(async move {
        let page: Arc<ferridriver::Page> = pool.get("page").await.map_err(make_failure)?;
        let url = data_url("<title>My Title</title><body>Hello</body>");
        page.goto(&url, None).await.map_err(make_failure)?;

        ferridriver_test::expect(&page).to_have_title("My Title").await?;
        ferridriver_test::expect(&page)
          .not()
          .to_have_title("Wrong Title")
          .await?;
        ferridriver_test::expect(&page)
          .to_have_url(regex::Regex::new("^data:").unwrap())
          .await?;

        Ok(())
      })
    }),
    fixture_requests: vec!["page".into()],
    annotations: Vec::new(),
    timeout: Some(Duration::from_secs(15)),
    retries: None,
    expected_status: ExpectedStatus::Pass,
    use_options: None,
  }
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn test_page_assertions() {
  let plan = TestPlan {
    suites: vec![TestSuite {
      name: "page".into(),
      file: "features_e2e.rs".into(),
      tests: vec![make_page_assertions_test()],
      hooks: Hooks::default(),
      annotations: Vec::new(),
      mode: SuiteMode::default(),
    }],
    total_tests: 1,
    shard: None,
  };

  let config = TestConfig {
    workers: 1,
    timeout: 15_000,
    ..Default::default()
  };
  let mut runner = TestRunner::new(config, CliOverrides::default());
  let exit_code = runner.run(plan).await;
  assert_eq!(exit_code, 0, "page assertions should pass");
}

// ── Helper ──

fn make_failure(e: impl std::fmt::Display) -> TestFailure {
  TestFailure {
    message: e.to_string(),
    stack: None,
    diff: None,
    screenshot: None,
  }
}