diff --git a/src/chrome.rs b/src/chrome.rs index eef41d7..fb37abc 100644 --- a/src/chrome.rs +++ b/src/chrome.rs @@ -3,6 +3,7 @@ use super::*; use std::process::{Command, Child, Stdio}; +use std::sync::Arc; use std::thread; use std::time::Duration; @@ -41,15 +42,22 @@ impl ChromeDriverBuilder { // TODO: parameterize this thread::sleep(Duration::new(1, 500)); Ok(ChromeDriver { - child: child, - url: format!("http://localhost:{}", port), - kill_on_drop: self.kill_on_drop, + inner: Arc::new(ChromeDriverInner { + child: child, + url: format!("http://localhost:{}", port), + kill_on_drop: self.kill_on_drop, + }), }) } } /// A chromedriver process +#[derive(Clone)] pub struct ChromeDriver { + inner: Arc, +} + +struct ChromeDriverInner { child: Child, url: String, kill_on_drop: bool, @@ -62,9 +70,14 @@ impl ChromeDriver { pub fn build() -> ChromeDriverBuilder { ChromeDriverBuilder::new() } + + /// Start a session for this driver + pub fn session(&self, params: &NewSessionCmd) -> Result where Self : Sized + 'static { + DriverSession::create_session(Box::new(self.clone()), params) + } } -impl Drop for ChromeDriver { +impl Drop for ChromeDriverInner { fn drop(&mut self) { if self.kill_on_drop { let _ = self.child.kill(); @@ -74,6 +87,6 @@ impl Drop for ChromeDriver { impl Driver for ChromeDriver { fn url(&self) -> &str { - &self.url + &self.inner.url } } diff --git a/tests/webdriver_client_integration.rs b/tests/webdriver_client_integration.rs index 7d4e779..3711023 100644 --- a/tests/webdriver_client_integration.rs +++ b/tests/webdriver_client_integration.rs @@ -465,6 +465,26 @@ macro_rules! browser_tests { browser_tests!(firefox, TestBrowser::Firefox); browser_tests!(chrome, TestBrowser::Chrome); +#[test] +fn launch_multiple_chromes() { + ensure_logging_init(); + + let new_session_command = TestBrowser::Chrome.new_session_cmd(); + + let server = FileServer::new(); + let chromedriver = ChromeDriver::build().spawn().unwrap(); + let chrome1 = chromedriver.clone().session(&new_session_command).expect("Error starting first browser"); + let chrome2 = chromedriver.session(&new_session_command).expect("Error starting second browser"); + + let page1 = server.url("/page1.html"); + let page2 = server.url("/page2.html"); + chrome1.go(&page1).expect("Error getting page1"); + chrome2.go(&page2).expect("Error getting page2"); + + assert_eq!(&chrome1.get_current_url().expect("Error getting url 1"), &page1); + assert_eq!(&chrome2.get_current_url().expect("Error getting url 2"), &page2); +} + fn ensure_logging_init() { static DONE: Once = ONCE_INIT; DONE.call_once(|| init_logging());