ferridriver-bdd 0.4.0

BDD/Cucumber test framework for ferridriver. 144 built-in Gherkin steps backed by the Page API.
Documentation
//! File upload step definitions.
//!
//! Uses `locator.set_input_files(&[paths])` to attach files to `<input type="file">` elements.

use crate::step::{DataTable, StepError};
use crate::world::BrowserWorld;
use ferridriver_bdd_macros::when;

#[when("I attach file {string} to {string}")]
async fn attach_file(world: &mut BrowserWorld, file_path: String, selector: String) {
  world
    .page()
    .locator(&selector, None)
    .set_input_files(
      ferridriver::options::InputFiles::Paths(vec![std::path::PathBuf::from(&file_path)]),
      None,
    )
    .await
    .map_err(|e| StepError::wrap(format!("attach file \"{file_path}\" to \"{selector}\""), e))?;
}

#[when("I attach files to {string}")]
async fn attach_files(world: &mut BrowserWorld, selector: String, table: Option<&DataTable>) {
  let table = table.ok_or_else(|| StepError::from("attach files requires a data table of file paths"))?;

  let paths: Vec<std::path::PathBuf> = table
    .iter()
    .flat_map(|row| row.iter().cloned())
    .filter(|s| !s.is_empty())
    .map(std::path::PathBuf::from)
    .collect();

  if paths.is_empty() {
    return Err(StepError::from("data table contained no file paths"));
  }

  world
    .page()
    .locator(&selector, None)
    .set_input_files(ferridriver::options::InputFiles::Paths(paths), None)
    .await
    .map_err(|e| StepError::wrap(format!("attach files to \"{selector}\""), e))?;
}