fantoccini icon indicating copy to clipboard operation
fantoccini copied to clipboard

Wrapping capabilities for initialization

Open AminArria opened this issue 5 years ago • 7 comments

Hi I want to use with_capabilities to create a new client, but not sure if doing it right. This is what I'm doing:

extern crate tokio;
extern crate futures;
extern crate fantoccini;
extern crate webdriver;
extern crate serde_json;

use fantoccini::{Client, Locator};
use futures::future::Future;
use webdriver::capabilities::Capabilities;

fn main() {
    let mut cap = Capabilities::new();
    let arg = serde_json::from_str("{\"args\": [\"-headless\"]}").unwrap();
    cap.insert("moz:firefoxOptions".to_string(), arg);
    let c = Client::with_capabilities("http://localhost:4444", cap);
   
   // rest of the code down here...
}

If this is the right way to do it I was wondering if we could receive in the capabilities in another way that didn't involve me knowing about webdriver and serde_json and having to add them to my dependencies.

I'm open to making a PR if you prefer to discuss over actual code.

AminArria avatar Mar 14 '19 20:03 AminArria

You can construct the json::Value directly (rather than relying on from_str), but I agree with you it's pretty inconvenient. Part of the reason there isn't a better API is that the capabilities can take on different forms for different vendors, so I don't know of a good way to expose it beyond just serde_json::Value. This could perhaps be helped somewhat by fantoccini re-exporting serde_json::Value and webdriver::capabilities::Capabilities? Did you have a particular API in mind that you think would be better? I'd be happy to review a PR!

jonhoo avatar Mar 14 '19 20:03 jonhoo

Re-exporting would be a sort of quick fix, but I don't know if its considered a good or bad practice. I would be inclined to go against it.

Ideally I would just change with_capabilities to receive something like a FancocciniCapabilities and then internally make it into a Capabilities.

AminArria avatar Mar 14 '19 21:03 AminArria

I'm inclined to re-export since we don't actually need our own wrapper functions or anything here. It's unclear what we gain from exposing our own. In your particular case, the maplit crate may be a good thing to look into too. Re-exporting that seems excessive though :thinking:

jonhoo avatar Mar 14 '19 21:03 jonhoo

Thanks for the idea of not using from_str, I changed it to:

let arg = json!({"args": ["-headless"]});

Yeah, re-exporting maplit might be a bit excessive. I'll make the PR to re-export the other libs.

AminArria avatar Mar 15 '19 20:03 AminArria

Ah, yes, I'd forgotten about json!. Maybe we do something like

pub mod ext {
    pub use serde_json::{json, Value};
    pub use webdriver::capabilities::Capabilities;
}

jonhoo avatar Mar 15 '19 21:03 jonhoo

Hi I want to use with_capabilities to create a new client, but not sure if doing it right. This is what I'm doing:

extern crate tokio;
extern crate futures;
extern crate fantoccini;
extern crate webdriver;
extern crate serde_json;

use fantoccini::{Client, Locator};
use futures::future::Future;
use webdriver::capabilities::Capabilities;

fn main() {
    let mut cap = Capabilities::new();
    let arg = serde_json::from_str("{\"args\": [\"-headless\"]}").unwrap();
    cap.insert("moz:firefoxOptions".to_string(), arg);
    let c = Client::with_capabilities("http://localhost:4444", cap);
   
   // rest of the code down here...
}

If this is the right way to do it I was wondering if we could receive in the capabilities in another way that didn't involve me knowing about webdriver and serde_json and having to add them to my dependencies.

I'm open to making a PR if you prefer to discuss over actual code.

Hi Amin, Your reply was incredibly helpful to my understanding of adding capabilities but I am wondering where you found the "moz:firefoxOptions" string at. I am currently trying to replicate this but using Chromedriver and I haven't had much luck with it. Thank you for your time!

GRVYDEV avatar Dec 21 '19 03:12 GRVYDEV

Hi @Grav3y sadly I didn't document where I got it, but this links might be useful

  • https://chromedriver.chromium.org/capabilities
  • https://developer.mozilla.org/en-US/docs/Web/WebDriver/Capabilities#Vendor-specific_capabilities
  • https://developer.mozilla.org/en-US/docs/Web/WebDriver/Capabilities/firefoxOptions#Example

That project I never got to implementing the capabilities for a Chromedriver so not much else I can do to help you

AminArria avatar Dec 22 '19 22:12 AminArria