fix: persist auth cookies on close in native mode (#650)
This commit is contained in:
@@ -146,6 +146,13 @@ impl BrowserProcess {
|
||||
BrowserProcess::Lightpanda(p) => p.kill(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn wait_or_kill(&mut self, timeout: std::time::Duration) {
|
||||
match self {
|
||||
BrowserProcess::Chrome(p) => p.wait_or_kill(timeout),
|
||||
BrowserProcess::Lightpanda(p) => p.kill(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct BrowserManager {
|
||||
@@ -583,8 +590,12 @@ impl BrowserManager {
|
||||
.send_command_no_params("Browser.close", None)
|
||||
.await;
|
||||
|
||||
if let Some(ref mut process) = self.browser_process {
|
||||
process.kill();
|
||||
if let Some(mut process) = self.browser_process.take() {
|
||||
let timeout = std::time::Duration::from_secs(5);
|
||||
let _ = tokio::task::spawn_blocking(move || {
|
||||
process.wait_or_kill(timeout);
|
||||
})
|
||||
.await;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
|
||||
@@ -16,6 +16,24 @@ impl ChromeProcess {
|
||||
let _ = self.child.kill();
|
||||
let _ = self.child.wait();
|
||||
}
|
||||
|
||||
/// Wait for Chrome to exit on its own (after Browser.close CDP command),
|
||||
/// falling back to kill() if it doesn't exit within the timeout.
|
||||
/// This allows Chrome to flush cookies and other state to the user-data-dir.
|
||||
pub fn wait_or_kill(&mut self, timeout: Duration) {
|
||||
let start = std::time::Instant::now();
|
||||
let poll_interval = Duration::from_millis(50);
|
||||
|
||||
while start.elapsed() < timeout {
|
||||
match self.child.try_wait() {
|
||||
Ok(Some(_)) => return,
|
||||
Ok(None) => std::thread::sleep(poll_interval),
|
||||
Err(_) => break,
|
||||
}
|
||||
}
|
||||
|
||||
self.kill();
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for ChromeProcess {
|
||||
|
||||
@@ -1293,3 +1293,115 @@ async fn e2e_error_handling() {
|
||||
let resp = execute_command(&json!({ "id": "99", "action": "close" }), &mut state).await;
|
||||
assert_success(&resp);
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Profile cookie persistence across restarts
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
#[tokio::test]
|
||||
#[ignore]
|
||||
async fn e2e_profile_cookie_persistence() {
|
||||
let profile_dir = std::env::temp_dir().join(format!(
|
||||
"agent-browser-e2e-profile-{}",
|
||||
uuid::Uuid::new_v4()
|
||||
));
|
||||
|
||||
// Session 1: launch with profile, set a cookie, close
|
||||
{
|
||||
let mut state = DaemonState::new();
|
||||
|
||||
let resp = execute_command(
|
||||
&json!({
|
||||
"id": "1",
|
||||
"action": "launch",
|
||||
"headless": true,
|
||||
"profile": profile_dir.to_str().unwrap()
|
||||
}),
|
||||
&mut state,
|
||||
)
|
||||
.await;
|
||||
assert_success(&resp);
|
||||
|
||||
let resp = execute_command(
|
||||
&json!({ "id": "2", "action": "navigate", "url": "https://example.com" }),
|
||||
&mut state,
|
||||
)
|
||||
.await;
|
||||
assert_success(&resp);
|
||||
|
||||
let resp = execute_command(
|
||||
&json!({
|
||||
"id": "3",
|
||||
"action": "cookies_set",
|
||||
"name": "persist_test",
|
||||
"value": "should_survive_restart",
|
||||
"domain": ".example.com",
|
||||
"path": "/",
|
||||
"expires": 2000000000
|
||||
}),
|
||||
&mut state,
|
||||
)
|
||||
.await;
|
||||
assert_success(&resp);
|
||||
|
||||
// Verify cookie is set
|
||||
let resp =
|
||||
execute_command(&json!({ "id": "4", "action": "cookies_get" }), &mut state).await;
|
||||
assert_success(&resp);
|
||||
let cookies = get_data(&resp)["cookies"].as_array().unwrap();
|
||||
let found = cookies
|
||||
.iter()
|
||||
.any(|c| c["name"] == "persist_test" && c["value"] == "should_survive_restart");
|
||||
assert!(found, "Cookie should exist before close");
|
||||
|
||||
let resp = execute_command(&json!({ "id": "5", "action": "close" }), &mut state).await;
|
||||
assert_success(&resp);
|
||||
}
|
||||
|
||||
tokio::time::sleep(tokio::time::Duration::from_secs(1)).await;
|
||||
|
||||
// Session 2: reopen with the same profile, verify cookie persisted
|
||||
{
|
||||
let mut state = DaemonState::new();
|
||||
|
||||
let resp = execute_command(
|
||||
&json!({
|
||||
"id": "10",
|
||||
"action": "launch",
|
||||
"headless": true,
|
||||
"profile": profile_dir.to_str().unwrap()
|
||||
}),
|
||||
&mut state,
|
||||
)
|
||||
.await;
|
||||
assert_success(&resp);
|
||||
|
||||
let resp = execute_command(
|
||||
&json!({ "id": "11", "action": "navigate", "url": "https://example.com" }),
|
||||
&mut state,
|
||||
)
|
||||
.await;
|
||||
assert_success(&resp);
|
||||
|
||||
let resp =
|
||||
execute_command(&json!({ "id": "12", "action": "cookies_get" }), &mut state).await;
|
||||
assert_success(&resp);
|
||||
let cookies = get_data(&resp)["cookies"].as_array().unwrap();
|
||||
let found = cookies
|
||||
.iter()
|
||||
.any(|c| c["name"] == "persist_test" && c["value"] == "should_survive_restart");
|
||||
assert!(
|
||||
found,
|
||||
"Cookie should persist across restart with --profile. Cookies found: {:?}",
|
||||
cookies
|
||||
.iter()
|
||||
.map(|c| c["name"].as_str().unwrap_or("?"))
|
||||
.collect::<Vec<_>>()
|
||||
);
|
||||
|
||||
let resp = execute_command(&json!({ "id": "99", "action": "close" }), &mut state).await;
|
||||
assert_success(&resp);
|
||||
}
|
||||
|
||||
let _ = std::fs::remove_dir_all(&profile_dir);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user