Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support reconnecting to an existing session #100

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,15 @@ impl Client {
Session::with_capabilities(webdriver, cap).await
}

/// Create a `Client` associated with an existing WebDriver session on the server at the given
/// URL.
pub fn from_session_id(
webdriver: &str,
session_id: &str,
) -> Result<Client, error::NewSessionError> {
Session::from_session_id(webdriver, session_id)
}

/// Get the session ID assigned by the WebDriver server to this client.
pub async fn session_id(&mut self) -> Result<Option<String>, error::CmdError> {
match self.issue(Cmd::GetSessionId).await? {
Expand Down
35 changes: 35 additions & 0 deletions src/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,41 @@ impl Session {
}
}

pub fn from_session_id(
webdriver: &str,
session_id: &str,
) -> Result<Client, error::NewSessionError> {
// Where is the WebDriver server?
let wdb = webdriver.parse::<url::Url>();

let wdb = wdb.map_err(error::NewSessionError::BadWebdriverUrl)?;

// We want a tls-enabled client
let client =
hyper::Client::builder().build::<_, hyper::Body>(hyper_tls::HttpsConnector::new());

// We're going to need a channel for sending requests to the WebDriver host
let (tx, rx) = mpsc::unbounded_channel();

// Set up our WebDriver session.
tokio::spawn(Session {
rx,
ongoing: Ongoing::None,
client,
wdb,
session: Some(session_id.to_string()),
is_legacy: false,
ua: None,
persist: false,
});

// now that the session is running, let's do the handshake
Ok(Client {
tx,
is_legacy: false,
})
}

/// Helper for determining what URL endpoint to use for various requests.
///
/// This mapping is essentially that of https://www.w3.org/TR/webdriver/#list-of-endpoints.
Expand Down
6 changes: 3 additions & 3 deletions tests/local.rs
Original file line number Diff line number Diff line change
Expand Up @@ -334,13 +334,13 @@ mod firefox {
fn double_close_window_test() {
tester!(close_window_twice_errors, "firefox")
}

#[test]
#[serial]
fn set_by_name_textarea_test() {
local_tester!(set_by_name_textarea, "firefox")
}

#[test]
#[serial]
fn stale_element_test() {
Expand Down Expand Up @@ -405,7 +405,7 @@ mod chrome {
fn double_close_window_test() {
tester!(close_window_twice_errors, "chrome")
}

#[test]
fn set_by_name_textarea_test() {
local_tester!(set_by_name_textarea, "chrome")
Expand Down