fix(browser): the bot opens its pages in windows of their own behind, so it never takes the focus

On the shared screen a tab brought to the front makes Chrome bring its window forward and take the
keyboard focus from the game or program the user is in (seen on the game PC). The botBrowser tool
now opens each person's page in a window of its own, behind and at the place and size of the bot
browser's window (Target.createTarget with newWindow and background), and closes it again if the
connection to it fails. While the browser runs on the virtual monitor, the app looks for new Chrome
windows every 200 ms and settles each like the first (tool window, fitted to the work area, without
activating it), the newest in front of the older ones, so the shared screen shows the page the bot
opened last.

Checked on the game PC: the bot's window loading a public page comes in front of the first, both
out of the taskbar and Alt+Tab, the focus staying in Discord also with the user idle for hours.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
javis-bot
2026-11-15 16:15:36 +09:00
parent cb2dda25b6
commit bd7b69d4f4
8 changed files with 221 additions and 55 deletions

View File

@@ -444,7 +444,11 @@ the browser runs headless with a note.
area (Windows may give the monitor a taskbar of its own) and goes behind all other windows, all
without activating it. It keeps drawing when covered (`--disable-backgrounding-occluded-windows`,
`--disable-renderer-backgrounding`, `--disable-background-timer-throttling`). The window holds
the first tab, which the bot never closes; the bot's tabs open in it, the newest in front.
the first page, which the bot never closes. The bot's pages open in windows of their own
(bot_browser.spec.md), behind and at this window's place, because a tab brought to the front
takes the keyboard focus. While the browser runs, the app looks for new windows every 200 ms
and settles each the same way, the newest in front of the older ones, so the shared screen shows
the page the bot opened last.
- If the monitor or the window cannot be made, the browser runs headless and the dashboard's
`browser` note says why ("가상 모니터 없이 실행: ...").
@@ -811,8 +815,9 @@ started elevated, the same program is the elevated half; `tests/bot_browser.rs`
driver installed through the elevated start (Windows' SignPath question answered once) with the
settings folder's permissions, a declined install leaving no device, package, registry key or
settings folder and the next install giving one working device, the monitor off after install, on at the rightmost monitor's corner
while the bot browser runs and off after, one Chrome window there filling the work area and holding
the bot's tab, the focus staying in the program the user types into, no taskbar button, no Chrome
while the bot browser runs and off after, one Chrome window there filling the work area, then the bot's own
window (opened as the tool opens it, loading a public page) in front of it, the focus staying in the program the user types into
also with the user idle past Windows' focus lock, no taskbar or Alt+Tab entry for either, no Chrome
left after the stop, and the uninstall leaving no device, driver package, registry key or settings
folder. Game detection (`tests/game_watch.rs`): a program started from a Steam game folder (also under
a path with non-ASCII letters) or from a Battle.net game's install folder (a test uninstall entry)
@@ -848,4 +853,4 @@ after OK, and starts normally once released; `/S /RUN` (fresh, and over a runnin
and the finish page's "Jarvis 실행" start the app's window; a silent install while the uninstaller's
window is open, and a silent uninstall while the installer's is, stop with code 5 and change
nothing. On Linux the same window test runs on an X display (one window at the given place and
size, the bot's tab in it).
size, the bot's own window at the same place).

View File

@@ -4,7 +4,8 @@
use std::net::{IpAddr, SocketAddr};
use std::path::{Path, PathBuf};
use std::sync::Mutex;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::{Arc, Mutex};
use std::time::Duration;
use tokio::io::{AsyncReadExt, AsyncWriteExt};
@@ -16,6 +17,8 @@ const HEAD_TIMEOUT: Duration = Duration::from_secs(30);
const MAX_HEAD: usize = 64 * 1024;
const READY_TIMEOUT: Duration = Duration::from_secs(30);
const CLOSE_WAIT: Duration = Duration::from_secs(5);
/// How often the windows the bot opens on the virtual monitor are looked for.
const WATCH_EVERY: Duration = Duration::from_millis(200);
/// Whether an address is on the public internet: not this PC, the local network or a special range.
pub fn is_public(ip: &IpAddr) -> bool {
@@ -255,6 +258,14 @@ pub struct BotBrowser {
child: Mutex<Option<tokio::process::Child>>,
proxy: EgressProxy,
pub cdp_port: u16,
/// While set (with a screen, on Windows), a thread settles the windows the bot opens.
watching: Arc<AtomicBool>,
}
impl Drop for BotBrowser {
fn drop(&mut self) {
self.watching.store(false, Ordering::Relaxed);
}
}
impl BotBrowser {
@@ -293,12 +304,25 @@ impl BotBrowser {
}
tokio::time::sleep(Duration::from_millis(200)).await;
};
let b = Self { child: Mutex::new(Some(child)), proxy, cdp_port };
let b = Self { child: Mutex::new(Some(child)), proxy, cdp_port, watching: Arc::new(AtomicBool::new(false)) };
if let Some(r) = screen {
if let Err(e) = b.open_window(r).await {
b.stop().await;
return Err(e);
}
if cfg!(windows) {
// The bot opens a window per person (bot_browser.spec.md), each settled like the first.
b.watching.store(true, Ordering::Relaxed);
let (on, pid) = (b.watching.clone(), b.pid());
std::thread::spawn(move || {
while on.load(Ordering::Relaxed) {
if crate::vdisplay::settle_windows(pid) > 0 {
crate::debug_log!("browser", "a new bot browser window settled on the virtual monitor");
}
std::thread::sleep(WATCH_EVERY);
}
});
}
}
crate::info_log!("browser", "bot browser ready (CDP on 127.0.0.1:{cdp_port}{})",
if screen.is_some() { ", on the virtual monitor" } else { "" });
@@ -307,14 +331,15 @@ impl BotBrowser {
/// The window on `r`: a background page in a new window (CDP), then out of the window switcher
/// and fitted to the monitor's work area.
/// It holds the first tab, which the bot never closes, so the window stays.
/// It holds the first page, which the bot never closes, so the window stays; the bot's own
/// windows open where it is.
async fn open_window(&self, r: crate::vdisplay::Rect) -> Result<(), String> {
let ws = version(self.cdp_port).await.and_then(|v| v["webSocketDebuggerUrl"].as_str().map(str::to_owned))
.ok_or("Chrome의 CDP 주소를 읽지 못했습니다")?;
cdp_call(&ws, serde_json::json!({"id": 1, "method": "Target.createTarget", "params": {"url": "about:blank",
"newWindow": true, "background": true, "left": r.x, "top": r.y, "width": r.w, "height": r.h}})).await
.map_err(|e| format!("Chrome 창을 열지 못했습니다: {e}"))?;
let pid = self.child.lock().unwrap().as_ref().and_then(|c| c.id()).unwrap_or(0);
let pid = self.pid();
for _ in 0..25 {
if cfg!(not(windows)) || crate::vdisplay::settle_windows(pid) > 0 {
return Ok(());
@@ -324,6 +349,11 @@ impl BotBrowser {
Err("Chrome 창을 찾지 못했습니다".into())
}
/// Chrome's main process (0 once it ended), the one owning its windows.
pub fn pid(&self) -> u32 {
self.child.lock().unwrap().as_ref().and_then(|c| c.id()).unwrap_or(0)
}
pub fn cdp_url(&self) -> String {
format!("http://127.0.0.1:{}", self.cdp_port)
}
@@ -335,6 +365,7 @@ impl BotBrowser {
/// `Browser.close`, then up to 5 s for Chrome to end, else it is killed; the proxy ends too.
pub async fn stop(&self) {
self.watching.store(false, Ordering::Relaxed);
let child = self.child.lock().unwrap().take();
if let Some(mut child) = child {
if let Some(ws) = version(self.cdp_port).await.and_then(|v| v["webSocketDebuggerUrl"].as_str().map(str::to_owned)) {

View File

@@ -330,26 +330,26 @@ mod win {
1
}
/// Make process `pid`'s top-level Chrome windows tool windows (not in Alt+Tab, Win+Tab or the
/// taskbar), fill their monitor's work area (Windows may give the virtual monitor a taskbar of
/// its own, which would cover the page's bottom) and put them behind all others, all without
/// activating them. Returns how many it made tool windows.
/// Make process `pid`'s new top-level Chrome windows tool windows (not in Alt+Tab, Win+Tab or
/// the taskbar) and fill their monitor's work area (Windows may give the virtual monitor a
/// taskbar of its own, which would cover the page's bottom), all without activating them. The
/// bot's windows stay behind all others, the newest in front of the older ones. Returns how many
/// windows were new.
pub fn settle_windows(pid: u32) -> usize {
let mut found = Found { pid, windows: vec![] };
// SAFETY: `collect` only reads windows and writes into `found`, which lives through the call.
unsafe { EnumWindows(Some(collect), &mut found as *mut Found as LPARAM) };
let mut changed = 0;
for h in found.windows {
// SAFETY: a plain query on a window handle EnumWindows gave.
let (old, new): (Vec<HWND>, Vec<HWND>) = found.windows.into_iter()
.partition(|&h| unsafe { GetWindowLongPtrW(h, GWL_EXSTYLE) } as u32 & WS_EX_TOOLWINDOW != 0);
for &h in &new {
// SAFETY: style changes on a window of our own child process; hiding and showing it
// again is what makes the taskbar notice the new style.
unsafe {
let ex = GetWindowLongPtrW(h, GWL_EXSTYLE) as u32;
if ex & WS_EX_TOOLWINDOW == 0 {
ShowWindow(h, SW_HIDE);
SetWindowLongPtrW(h, GWL_EXSTYLE, ((ex | WS_EX_TOOLWINDOW) & !WS_EX_APPWINDOW) as isize);
ShowWindow(h, SW_SHOWNOACTIVATE);
changed += 1;
}
ShowWindow(h, SW_HIDE);
SetWindowLongPtrW(h, GWL_EXSTYLE, ((ex | WS_EX_TOOLWINDOW) & !WS_EX_APPWINDOW) as isize);
ShowWindow(h, SW_SHOWNOACTIVATE);
let mut mi = MONITORINFO { cbSize: std::mem::size_of::<MONITORINFO>() as u32, ..Default::default() };
if GetMonitorInfoW(MonitorFromWindow(h, MONITOR_DEFAULTTONEAREST), &mut mi) != 0 {
let w = mi.rcWork;
@@ -359,7 +359,14 @@ mod win {
}
}
}
changed
if !new.is_empty() {
// Top first, each to the bottom: they keep their order, all behind the new ones.
for &h in &old {
// SAFETY: a z-order change of our own child process's window, without activating it.
unsafe { SetWindowPos(h, HWND_BOTTOM, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE) };
}
}
new.len()
}
/// The root devices with the driver's hardware ID, in any class (a device whose driver install

View File

@@ -96,10 +96,55 @@ fn foreground_pid() -> u32 {
pid
}
/// The visible top-level Chrome windows of process `pid`, top first: (title, tool window).
#[cfg(windows)]
fn chrome_windows(pid: u32) -> Vec<(String, bool)> {
use windows_sys::Win32::Foundation::{HWND, LPARAM};
use windows_sys::Win32::UI::WindowsAndMessaging::*;
unsafe extern "system" fn each(h: HWND, l: LPARAM) -> windows_sys::core::BOOL {
// SAFETY: `l` is the `(pid, Vec)` below, alive through EnumWindows; plain queries on `h`.
unsafe {
let (pid, out) = &mut *(l as *mut (u32, Vec<(String, bool)>));
let (mut p, mut class, mut title) = (0u32, [0u16; 64], [0u16; 256]);
GetWindowThreadProcessId(h, &mut p);
let n = GetClassNameW(h, class.as_mut_ptr(), 64) as usize;
if p == *pid && IsWindowVisible(h) != 0 && GetWindow(h, GW_OWNER).is_null()
&& String::from_utf16_lossy(&class[..n]) == "Chrome_WidgetWin_1" {
let t = GetWindowTextW(h, title.as_mut_ptr(), 256) as usize;
let tool = GetWindowLongPtrW(h, GWL_EXSTYLE) as u32 & WS_EX_TOOLWINDOW != 0;
out.push((String::from_utf16_lossy(&title[..t]), tool));
}
}
1
}
let mut found = (pid, vec![]);
// SAFETY: `each` writes only into `found`, which outlives the call.
unsafe { EnumWindows(Some(each), &mut found as *mut _ as LPARAM) };
found.1
}
/// One call on the browser's CDP connection.
async fn cdp<S>(s: &mut S, id: u64, method: &str, params: serde_json::Value) -> serde_json::Value
where S: futures_util::Sink<tokio_tungstenite::tungstenite::Message> + futures_util::Stream<Item =
Result<tokio_tungstenite::tungstenite::Message, tokio_tungstenite::tungstenite::Error>> + Unpin,
S::Error: std::fmt::Debug {
use futures_util::{SinkExt, StreamExt};
let msg = serde_json::json!({"id": id, "method": method, "params": params});
s.send(tokio_tungstenite::tungstenite::Message::text(msg.to_string())).await.unwrap();
loop {
let v: serde_json::Value = serde_json::from_str(s.next().await.unwrap().unwrap().to_text().unwrap()).unwrap();
if v["id"] == id {
break v["result"].clone();
}
}
}
/// With a screen, Chrome starts without a window and the app opens exactly one, at the screen's
/// place and size, without taking the focus; the page tab the bot opens joins it. Needs a display
/// on Linux (`DISPLAY`); on Windows the screen is the virtual monitor (its driver installed), on the
/// desktop only meanwhile.
/// place and size, without taking the focus. The bot's page then opens the way the botBrowser tool
/// opens it (a window of its own behind, at that window's place): also without taking the focus,
/// and on Windows the app makes it a tool window in front of the first. Needs a display on Linux
/// (`DISPLAY`); on Windows the screen is the virtual monitor (its driver installed), on the desktop
/// only meanwhile.
#[tokio::test]
async fn on_a_screen_the_bot_browser_has_one_window_at_the_monitor() {
let Ok(exe) = std::env::var("JARVIS_TEST_CHROME") else {
@@ -130,25 +175,43 @@ async fn on_a_screen_the_bot_browser_has_one_window_at_the_monitor() {
.text().await.map(|t| serde_json::from_str(&t).unwrap()).unwrap();
let first = pages(list);
assert_eq!(first.len(), 1, "one page in one window");
client.put(format!("http://127.0.0.1:{}/json/new?about:blank", b.cdp_port)).send().await.unwrap();
// Each page's window over CDP: both tabs share the one window, at the screen's rectangle.
let ws: serde_json::Value = client.get(format!("http://127.0.0.1:{}/json/version", b.cdp_port)).send().await.unwrap()
.text().await.map(|t| serde_json::from_str(&t).unwrap()).unwrap();
let (mut s, _) = tokio_tungstenite::connect_async(ws["webSocketDebuggerUrl"].as_str().unwrap()).await.unwrap();
let list: serde_json::Value = client.get(format!("http://127.0.0.1:{}/json/list", b.cdp_port)).send().await.unwrap()
.text().await.map(|t| serde_json::from_str(&t).unwrap()).unwrap();
let ids = pages(list);
assert_eq!(ids.len(), 2);
// As the botBrowser tool does it.
let at = cdp(&mut s, 1, "Browser.getWindowForTarget", serde_json::json!({"targetId": first[0]})).await["bounds"].clone();
let bot = cdp(&mut s, 2, "Target.createTarget", serde_json::json!({"url": "about:blank",
"newWindow": true, "background": true, "left": at["left"], "top": at["top"], "width": at["width"],
"height": at["height"]})).await["targetId"].as_str().unwrap().to_owned();
// A page loading in it, as the tool's `open` does.
let (mut page, _) = tokio_tungstenite::connect_async(format!("ws://127.0.0.1:{}/devtools/page/{bot}", b.cdp_port))
.await.unwrap();
cdp(&mut page, 1, "Page.navigate", serde_json::json!({"url": "https://example.com/"})).await;
let mut loaded = false;
for _ in 0..40 {
loaded = titles(&client, b.cdp_port).await.iter().any(|(_, t)| t == "Example Domain");
if loaded {
break;
}
tokio::time::sleep(Duration::from_millis(250)).await;
}
assert!(loaded, "the bot's page loads in its window");
tokio::time::sleep(Duration::from_secs(1)).await;
#[cfg(windows)]
{
assert_eq!(foreground_pid(), focused, "the bot's window leaves the focus where it was");
let w = chrome_windows(b.pid());
eprintln!("chrome windows, top first: {w:?}");
assert_eq!(w.len(), 2, "{w:?}");
assert!(w[0].0.starts_with("Example Domain"), "the bot's window is in front: {w:?}");
assert!(w.iter().all(|(_, tool)| *tool), "no window in the taskbar or Alt+Tab: {w:?}");
}
// Each page's window over CDP: a window each, at the screen's rectangle.
let mut windows = std::collections::HashSet::new();
for (i, id) in ids.iter().enumerate() {
use futures_util::{SinkExt, StreamExt};
let msg = serde_json::json!({"id": i + 1, "method": "Browser.getWindowForTarget", "params": {"targetId": id}});
s.send(tokio_tungstenite::tungstenite::Message::text(msg.to_string())).await.unwrap();
let r = loop {
let v: serde_json::Value = serde_json::from_str(s.next().await.unwrap().unwrap().to_text().unwrap()).unwrap();
if v["id"] == i + 1 { break v["result"].clone(); }
};
for (i, id) in [first[0].clone(), bot].iter().enumerate() {
let r = cdp(&mut s, 10 + i as u64, "Browser.getWindowForTarget", serde_json::json!({"targetId": id})).await;
eprintln!("window: {r}");
windows.insert(r["windowId"].as_i64().unwrap());
let b = &r["bounds"];
@@ -163,7 +226,7 @@ async fn on_a_screen_the_bot_browser_has_one_window_at_the_monitor() {
assert_eq!((left, top, width, height), (x, y, w, h));
}
}
assert_eq!(windows.len(), 1, "the bot's tab opens in the same window");
assert_eq!(windows.len(), 2, "the bot's page has a window of its own");
if std::env::var_os("JARVIS_TEST_HOLD").is_some() {
// Time for a look at the screen (the Windows check takes a screenshot meanwhile).
tokio::time::sleep(std::time::Duration::from_secs(8)).await;

View File

@@ -282,6 +282,7 @@ LLM과 STT를 함께 켠 측정(M0 `gamemode_stt`, 게임 없음). STT를 먼저
- 디스코드에서는 "화면" 탭에서 그 가상 모니터를 고릅니다.
- Alt+Tab에서는 그대로 빠지고, 실제 모니터에도 보이지 않습니다.
- 크롬 창을 처음부터 띄우면 사용자가 하던 게임의 포커스를 가져갈 수 있어서, 창 없이 켠 뒤 앱이 CDP로 창을 뒤에서 엽니다. Windows가 가상 모니터에도 작업 표시줄을 두면 페이지 아래를 가리지 않게 창을 작업 영역에 맞춥니다.
- 봇이 페이지를 열 때도 탭이 아니라 사람마다 창을 하나씩 뒤에서 엽니다. 크롬은 탭을 앞으로 보여 줄 때 창을 앞으로 꺼내려 하고, Windows가 이를 허용하는 때에는 키보드 포커스를 가져갑니다(게임PC에서 관찰, 2026-11-15). 뒤에서 연 창은 포커스를 가져가지 않으며, 앱이 새 창을 찾아 같은 방식으로 정리하고 가장 최근에 연 창을 가상 모니터 맨 앞에 둡니다.
- **서버 Windows 가상머신 확인(2026-11-14)**: 드라이버 설치(SignPath 질문 한 번)와 제거(장치·드라이버 패키지·레지스트리·설정 폴더 남지 않음), 설치 직후 모니터 꺼짐, 봇용 크롬을 켜는 동안만 실제 모니터 오른쪽 아래 구석(1024,768)에 1280x720으로 켜짐, 크롬 창 하나가 그 모니터의 작업 영역을 채우고 봇의 탭도 그 창에 열림, 메모장에 입력하는 동안 포커스를 뺏지 않음, 작업 표시줄에 버튼 없음, 정지 뒤 크롬 프로세스 0개와 모니터 꺼짐. 남은 확인: 디스코드 "화면" 탭에 그 모니터가 나오고 공유되는지, 커서, 게임PC(실제 GPU)에서의 동작.
## 다른 사람의 디스코드 화면공유 보기 (창 분리 방식)

View File

@@ -70,8 +70,8 @@ class BrowserError(Exception):
class BrowserSession:
"""One CDP connection to a tab of its own in the bot browser. Every wait ends at `deadline`
(a monotonic time the caller sets for each action)."""
"""One CDP connection to a page of its own (in a window of its own) in the bot browser. Every
wait ends at `deadline` (a monotonic time the caller sets for each action)."""
def __init__(self, cdp_url: str, deadline: float):
from websockets.sync.client import connect
@@ -79,18 +79,36 @@ class BrowserSession:
self.deadline = deadline
self._cdp = cdp_url
self._opener = urllib.request.build_opener(urllib.request.ProxyHandler({})) # CDP is local: never a proxy
req = urllib.request.Request(f"{cdp_url}/json/new?about:blank", method="PUT")
page = json.loads(self._opener.open(req, timeout=self._left()).read())
self.target_id = page["id"]
# Entered like a context manager (newer websockets warns otherwise); close() ends it.
self._ws = connect(page["webSocketDebuggerUrl"], open_timeout=self._left(),
max_size=16 * 1024 * 1024).__enter__()
self._id = 0
self._events: deque = deque()
self._call("Page.enable")
version = json.loads(self._opener.open(f"{cdp_url}/json/version", timeout=self._left()).read())
with connect(version["webSocketDebuggerUrl"], open_timeout=self._left()) as browser:
self._ws = browser
self.target_id = self._call("Target.createTarget", self._new_window())["targetId"]
# Entered like a context manager (newer websockets warns otherwise); close() ends it.
host = urllib.parse.urlsplit(cdp_url).netloc
try:
self._ws = connect(f"ws://{host}/devtools/page/{self.target_id}", open_timeout=self._left(),
max_size=16 * 1024 * 1024).__enter__()
self._call("Page.enable")
except Exception:
self.close() # no window left behind on the shared screen
raise
def _new_window(self) -> dict:
"""A window of its own, opened behind (a tab brought to the front takes the keyboard focus
from the program the user is in), at the place and size of the bot browser's window."""
params = {"url": "about:blank", "newWindow": True, "background": True}
try:
page = next(t for t in self._call("Target.getTargets")["targetInfos"] if t["type"] == "page")
b = self._call("Browser.getWindowForTarget", {"targetId": page["targetId"]})["bounds"]
params.update({k: b[k] for k in ("left", "top", "width", "height")})
except (BrowserError, StopIteration, KeyError):
debug_log("bot browser: no window to take the place from", "browser")
return params
def close(self) -> None:
"""Close the connection and the tab."""
"""Close the connection and the page (and so its window)."""
try:
self._ws.close()
except Exception: # noqa: BLE001
@@ -205,7 +223,7 @@ class BrowserSession:
session_factory = BrowserSession
# One tab per person (Discord user ID), at most MAX_TABS; the least recently used closes first.
# One page (window) per person (Discord user ID), at most MAX_TABS; the least recently used closes first.
_sessions: Dict[str, Any] = {}
MAX_TABS = 5
_lock = threading.Lock()

View File

@@ -20,9 +20,12 @@ read it, click a link or button, type into a field, go back. Product rules: `doc
- **Bounded.** One deadline of 15 s covers an action: connecting, the action and waiting for the
page; events arriving in between do not extend it. Text is cut at 6,000 characters and the list
of links and buttons at 40.
- **A tab per person.** Each asker (`ToolContext.user_id`) has a tab and connection of their own,
so one person's page and numbered list are never another's. At most 5 tabs; the least recently
used one is closed first.
- **A window per person.** Each asker (`ToolContext.user_id`) has a page and connection of their
own, so one person's page and numbered list are never another's. The page opens in a window of
its own, behind, at the place and size of the bot browser's window: a tab brought to the front
would take the keyboard focus from the game or program the user is in, while a window opened
behind does not, and on the shared screen the app puts the newest window in front (admin.spec.md,
"Virtual monitor"). At most 5; the least recently used one is closed first.
- **One tab, also for "new tab" links.** Before a click or typing, links, forms and `<base>` lose
their `target` and `window.open` loads the address in the same tab, so what opens is what the
tool reads next.
@@ -50,8 +53,11 @@ buttons, all inside the fence. Failures come back as `Error: ...` with `success=
## Backend
`BrowserSession(cdp_url, deadline)` opens a tab of its own (`/json/new`) and speaks CDP over its
WebSocket (`websockets` sync client); `close()` closes the tab: `Page.navigate`, `Runtime.evaluate` (reading, finding and clicking elements),
`BrowserSession(cdp_url, deadline)` opens its page on the browser's connection
(`Target.createTarget` with `newWindow` and `background`, at the window bounds of the first page
there, `Browser.getWindowForTarget`; without them where Chrome puts it) and speaks CDP over the
page's WebSocket (`websockets` sync client); a failure after the page opened closes it again;
`close()` closes the page and with it its window: `Page.navigate`, `Runtime.evaluate` (reading, finding and clicking elements),
`Input.insertText` and `Input.dispatchKeyEvent` (typing, Enter). The tool keeps a session per person
and makes a new one when the old one fails. Tests and evals replace `session_factory`.

View File

@@ -9,12 +9,14 @@ browser off local addresses is the app's test (app/tests/bot_browser.rs).
from __future__ import annotations
import http.server
import json
import os
import subprocess
import tempfile
import threading
import time
import urllib.parse
import urllib.request
from pathlib import Path
import pytest
@@ -332,6 +334,39 @@ def test_links_that_would_open_a_new_tab_open_in_the_same_tab(chrome):
assert s.click("Pop up")["title"] == "Second page"
_local = urllib.request.build_opener(urllib.request.ProxyHandler({}))
def _windows(cdp, ids):
"""Each target's (window id, bounds) over the browser connection."""
from websockets.sync.client import connect
ws = json.loads(_local.open(f"{cdp}/json/version").read())["webSocketDebuggerUrl"]
out = []
with connect(ws) as c:
for i, t in enumerate(ids, 1):
c.send(json.dumps({"id": i, "method": "Browser.getWindowForTarget", "params": {"targetId": t}}))
while (m := json.loads(c.recv(timeout=5))).get("id") != i:
pass
b = m["result"]["bounds"]
out.append((m["result"]["windowId"], (b["left"], b["top"], b["width"], b["height"])))
return out
def test_each_session_has_a_window_of_its_own_where_the_bot_browser_window_is(chrome):
cdp, site, _ = chrome
first = [t["id"] for t in json.loads(_local.open(f"{cdp}/json/list").read()) if t["type"] == "page"]
a, b = BrowserSession(cdp, time.monotonic() + 15), BrowserSession(cdp, time.monotonic() + 15)
windows = _windows(cdp, first + [a.target_id, b.target_id])
assert len({w for w, _ in windows}) == 3, windows
assert {bounds for _, bounds in windows} == {windows[0][1]}, windows
assert a.open(site + "/")["title"] == "Home page"
a.close()
b.close()
left = [t["id"] for t in json.loads(_local.open(f"{cdp}/json/list").read()) if t["type"] == "page"]
assert left == first
def test_two_sessions_have_their_own_tabs(chrome):
cdp, site, _ = chrome
a, b = BrowserSession(cdp, time.monotonic() + 15), BrowserSession(cdp, time.monotonic() + 15)