jonhoo / fantoccini

A high-level API for programmatically interacting with web pages through WebDriver.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Adding support for headless browser?

Atsukoro1 opened this issue · comments

As described in firefoxOptions, the only way to run headless browser is by specifying headless. I have not found any way to pass arguments when creating the client with ClientBuilder.

I am currently creating client with Client builder like this ->

let cl: Client = ClientBuilder::native()
    .capabilities(
        HashMap::new()
            .insert(String::from("-headless"), String::from(""))  
            .into_iter()
            .collect::<Capabilities>()
    )
    .connect("http://127.0.0.1:4444")
    .await
    .expect(
        "Failed to connect to Webdriver, do you 
         have gecko driver and firefox binary installed?"
    );

As the Firefox docs you linked to mention, you need to pass in a capability named moz:firefoxOptions whose value should be something like:

serde_json::json!({ "args": ["--headless"] });

serde_json::json returns a serde_json::Value and it was unclear to me how to actually convert that into a Capabilities, so I ended up using serde_json::from_str:

    let client = ClientBuilder::native()
        .capabilities(serde_json::from_str(r#"{"moz:firefoxOptions": {"args": ["--headless"]}}"#)?)
        .connect("http://localhost:4444")
        .await?;

Does this look about right?

Also, we could probably use an example for this.

Capabilities is really just a type alias for serde_json::Map<String, serde_json::Value>, so you can just use it directly :)