fix: 做完就關掉瀏覽器——Chrome 類送出關閉後等它回應再斷線;加 Chrome 類收尾實機測試
- close_chrome:Browser.close 送出後等回應(或它關掉連線)最多 3 秒再放手,不在請求還沒讀到前就斷線 - a_finished_job_quits_its_chrome(#[ignore],實機):獨立資料夾、獨立連接埠、headless;兩個任務共用時先結束的只關分頁,最後一個結束整個關掉,下次再開。Mac 上用 Brave 跑過 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
69080f9117
commit
c86fe35397
|
|
@ -792,6 +792,10 @@ fn close_chrome(port: u16) -> Result<(), String> {
|
|||
let stream = std::net::TcpStream::connect_timeout(&addr, Duration::from_secs(5)).map_err(|e| e.to_string())?;
|
||||
let (mut ws, _) = tungstenite::client::client(ws_url, stream).map_err(|e| e.to_string())?;
|
||||
ws.send(tungstenite::Message::Text(serde_json::json!({"id": 1, "method": "Browser.close"}).to_string().into())).map_err(|e| e.to_string())?;
|
||||
// Its answer (or the connection closing as it quits) before hanging up: dropped at once,
|
||||
// the request could go unread.
|
||||
let _ = ws.get_mut().set_read_timeout(Some(Duration::from_secs(3)));
|
||||
let _ = ws.read();
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
|
@ -1152,6 +1156,55 @@ mod tests {
|
|||
r.unwrap();
|
||||
}
|
||||
|
||||
/// 做完就關掉瀏覽器 with a Chrome-family browser (Chrome, Brave, Edge, Chromium), live, in a
|
||||
/// scratch data folder on a port of its own, headless:
|
||||
/// ANYBOT_DATA_DIR=$T ANYBOT_BROWSER_PORT=9399 ANYBOT_BROWSER_FLAGS=--headless=new <test binary> --ignored a_finished_job_quits_its_chrome
|
||||
#[test]
|
||||
#[ignore]
|
||||
fn a_finished_job_quits_its_chrome() {
|
||||
use std::io::{Read, Write};
|
||||
assert!(engine().is_some(), "needs Chrome, Brave, Edge or Chromium");
|
||||
assert_ne!(default_port(), PORT, "run on a port of its own (ANYBOT_BROWSER_PORT), not the real bot browser's");
|
||||
let l = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
|
||||
let port = l.local_addr().unwrap().port();
|
||||
std::thread::spawn(move || {
|
||||
for mut c in l.incoming().flatten() {
|
||||
let mut buf = [0u8; 2048];
|
||||
let _ = c.read(&mut buf);
|
||||
let body = "<html><head><title>頁面</title></head><body>hi</body></html>";
|
||||
let _ = write!(c, "HTTP/1.1 200 OK\r\nContent-Type: text/html; charset=utf-8\r\nContent-Length: {}\r\nConnection: close\r\n\r\n{body}", body.len());
|
||||
}
|
||||
});
|
||||
let url = format!("http://127.0.0.1:{port}/");
|
||||
let tidy = |job: &str| as_job(job, || crate::browser::run("browser_tabs", &serde_json::json!({"action": "tidy", "close": true, "profile": DEFAULT_PROFILE})).map(|r| r.0));
|
||||
let open_as = |job: &str| as_job(job, || open(&url, Some(DEFAULT_PROFILE)));
|
||||
let quit_within = |secs: u64| {
|
||||
let t = Instant::now();
|
||||
while alive(default_port()) && t.elapsed() < Duration::from_secs(secs) {
|
||||
std::thread::sleep(Duration::from_millis(200));
|
||||
}
|
||||
!alive(default_port())
|
||||
};
|
||||
let r = (|| -> Result<(), String> {
|
||||
open_as("j1")?;
|
||||
open_as("j2")?;
|
||||
let said = tidy("j1")?;
|
||||
assert!(said.contains("分頁") && alive(default_port()), "{said}");
|
||||
let said = tidy("j2")?;
|
||||
assert!(said.contains("關掉"), "{said}");
|
||||
assert!(quit_within(10), "the browser is still running");
|
||||
open_as("j3")?;
|
||||
assert!(alive(default_port()));
|
||||
tidy("j3")?;
|
||||
assert!(quit_within(10));
|
||||
Ok(())
|
||||
})();
|
||||
if alive(default_port()) {
|
||||
let _ = close_chrome(default_port());
|
||||
}
|
||||
r.unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_firefox_profile_answers_to_its_own_name() {
|
||||
let list = vec![DEFAULT_PROFILE.to_string(), "工作".to_string(), format!("阿金{FIREFOX_MARK}"), format!("Sunny{FIREFOX_MARK}")];
|
||||
|
|
|
|||
Loading…
Reference in New Issue