Skip to content

Commit

Permalink
refactor(session): Update ::new to accept Option<impl Into<String>>
Browse files Browse the repository at this point in the history
- Use `::new` in both session creation and reconnection paths
- Modify and return the existing client instance directly
  • Loading branch information
surajk-m committed Nov 10, 2024
1 parent 822c957 commit e0c6844
Showing 1 changed file with 22 additions and 34 deletions.
56 changes: 22 additions & 34 deletions src/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -539,14 +539,14 @@ where
rx: mpsc::UnboundedReceiver<Task>,
client: hyper_util::client::legacy::Client<C, BoxBody<hyper::body::Bytes, Infallible>>,
wdb_url: url::Url,
session_id: &str,
session_id: Option<impl Into<String>>,
) -> Self {
Session {
ongoing: Ongoing::None,
rx,
client,
wdb: wdb_url,
session: Some(session_id.to_string()),
session: session_id.map(Into::into),
is_legacy: false,
ua: None,
persist: false,
Expand Down Expand Up @@ -651,27 +651,17 @@ where
client: hyper_util::client::legacy::Client<C, BoxBody<hyper::body::Bytes, Infallible>>,
wdb: url::Url,
session_id: Option<&str>,
cap: Option<webdriver::capabilities::Capabilities>,
_cap: Option<webdriver::capabilities::Capabilities>,
) -> Result<Client, error::NewSessionError> {
// We're going to need a channel for sending requests to the WebDriver host
let (tx, rx) = mpsc::unbounded_channel();

if let Some(session_id) = session_id {
// Reconnect to an existing session
tokio::spawn(Session::new(rx, client, wdb, session_id));
} else if let Some(_capabilities) = cap {
// Spawn a new WebDriver session.
tokio::spawn(Session {
rx,
ongoing: Ongoing::None,
client,
wdb,
session: None,
is_legacy: false,
ua: None,
persist: false,
});
}
tokio::spawn(Session::new(
rx,
client,
wdb,
session_id.map(|id| id.to_string()),
));

// now that the session is running, let's do the handshake
Ok(Client {
Expand All @@ -689,6 +679,10 @@ where
let (client, wdb) = Self::create_client_and_parse_url(webdriver, connector).await?;
let mut cap = cap.to_owned();

// Create a new session for this client
// https://www.w3.org/TR/webdriver/#dfn-new-session
// https://www.w3.org/TR/webdriver/#capabilities
// - we want the browser to wait for the page to load
if !cap.contains_key("pageLoadStrategy") {
cap.insert("pageLoadStrategy".to_string(), Json::from("normal"));
}
Expand All @@ -702,11 +696,7 @@ where
.insert("w3c".to_string(), Json::from(true));
}

// Create a new session for this client
// https://www.w3.org/TR/webdriver/#dfn-new-session
// https://www.w3.org/TR/webdriver/#capabilities
// - we want the browser to wait for the page to load
let client = Self::setup_session(client, wdb, None, Some(cap.clone())).await?;
let mut client = Self::setup_session(client, wdb, None, Some(cap.clone())).await?;

let session_config = webdriver::capabilities::SpecNewSessionParameters {
alwaysMatch: cap.clone(),
Expand All @@ -719,11 +709,11 @@ where
.map(Self::map_handshake_response)
.await
{
Ok(new_session_response) => Ok(Client {
tx: client.tx,
is_legacy: false,
new_session_response: Some(wd::NewSessionResponse::from_wd(new_session_response)),
}),
Ok(new_session_response) => {
client.new_session_response =
Some(wd::NewSessionResponse::from_wd(new_session_response));
Ok(client)
}

Check warning on line 716 in src/session.rs

View check run for this annotation

Codecov / codecov/patch

src/session.rs#L715-L716

Added lines #L715 - L716 were not covered by tests
Err(error::NewSessionError::NotW3C(json)) => {
// maybe try legacy mode?
let mut legacy = false;
Expand Down Expand Up @@ -766,11 +756,9 @@ where
.map(Self::map_handshake_response)
.await?;

Ok(Client {
tx: client.tx,
is_legacy: true,
new_session_response: None,
})
client.is_legacy = true;

Check warning on line 759 in src/session.rs

View check run for this annotation

Codecov / codecov/patch

src/session.rs#L759

Added line #L759 was not covered by tests
client.new_session_response = None;
Ok(client)

Check warning on line 761 in src/session.rs

View check run for this annotation

Codecov / codecov/patch

src/session.rs#L761

Added line #L761 was not covered by tests
}
Err(e) => Err(e),
}
Expand Down

0 comments on commit e0c6844

Please sign in to comment.