S006: mainstream user agent, heavy-web smoke on real sites
Default UA is Servo's in Firefox's exact form (Gecko/20100101, no Servo token), following Servo's version (inject::mainstream_ua, tested); a custom UA still wins. tests/smoke/heavy-web.txt (needs internet): DuckDuckGo, Wikipedia, YouTube in one go (home, search, video, ads, PiP). Selftest waitfor, locate + click @, expect > n. input.txt checks Enter submits a form and the keys a page sees. Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
parent
b3bfdb6b28
commit
7f43322ee7
4 changed files with 195 additions and 16 deletions
|
|
@ -2438,7 +2438,7 @@ impl App {
|
|||
if old.web.user_agent != new.web.user_agent
|
||||
&& let Some(servo) = &self.servo
|
||||
{
|
||||
let ua = if new.web.user_agent.trim().is_empty() { servo::UserAgentPlatform::default().to_user_agent_string() } else { new.web.user_agent.trim().to_owned() };
|
||||
let ua = if new.web.user_agent.trim().is_empty() { crate::inject::mainstream_ua(&servo::UserAgentPlatform::default().to_user_agent_string()) } else { new.web.user_agent.trim().to_owned() };
|
||||
servo.set_preference("user_agent", servo::PrefValue::Str(ua.clone()));
|
||||
*self.shared.user_agent.borrow_mut() = ua.clone();
|
||||
crate::net::fetch::set_user_agent(&ua);
|
||||
|
|
|
|||
|
|
@ -353,9 +353,7 @@ impl App {
|
|||
prefs.network_https_proxy_uri = proxy;
|
||||
prefs.network_http_no_proxy = String::new();
|
||||
}
|
||||
if !settings.web.user_agent.trim().is_empty() {
|
||||
prefs.user_agent = settings.web.user_agent.trim().to_owned();
|
||||
}
|
||||
prefs.user_agent = if settings.web.user_agent.trim().is_empty() { crate::inject::mainstream_ua(&prefs.user_agent) } else { settings.web.user_agent.trim().to_owned() };
|
||||
*self.shared.user_agent.borrow_mut() = prefs.user_agent.clone();
|
||||
crate::net::fetch::set_user_agent(&prefs.user_agent);
|
||||
prefs.dom_webxr_enabled = false;
|
||||
|
|
|
|||
|
|
@ -528,6 +528,14 @@ const GOOGLE_SIGNIN_JS: &str = r#"(() => { if (!/(^|\.)accounts\.google\.com$/.t
|
|||
/// The Firefox user agent used on Google's sign-in pages.
|
||||
pub const FIREFOX_UA: &str = "Mozilla/5.0 (X11; Linux x86_64; rv:140.0) Gecko/20100101 Firefox/140.0";
|
||||
|
||||
/// The default user agent: Servo's own, in Firefox's exact form ("... rv:N) Gecko/20100101 Firefox/N"), so sites
|
||||
/// that sniff for a known browser serve their Firefox pages; the Firefox version follows Servo's default.
|
||||
pub fn mainstream_ua(servo_default: &str) -> String {
|
||||
let without_servo: Vec<&str> = servo_default.split(' ').filter(|part| !part.starts_with("Servo/")).collect();
|
||||
let ua = without_servo.join(" ");
|
||||
if ua.contains("Gecko/") { ua } else { ua.replacen(" Firefox/", " Gecko/20100101 Firefox/", 1) }
|
||||
}
|
||||
|
||||
/// Small helpers for browser -> page calls.
|
||||
pub fn call(path: &str, args: &[Value]) -> String {
|
||||
let args: Vec<String> = args.iter().map(Value::to_string).collect();
|
||||
|
|
@ -550,6 +558,13 @@ pub fn redirect_page(to: &str, note: &str) -> Vec<u8> {
|
|||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn mainstream_user_agent() {
|
||||
let ua = mainstream_ua("Mozilla/5.0 (X11; Linux x86_64; rv:153.0) Servo/0.7.0 Firefox/153.0");
|
||||
assert_eq!(ua, "Mozilla/5.0 (X11; Linux x86_64; rv:153.0) Gecko/20100101 Firefox/153.0");
|
||||
assert_eq!(mainstream_ua(&ua), ua);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn messages() {
|
||||
let m = parse_message("abc", r#"abc{"t":"media","d":{"title":"x"}}"#).unwrap();
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@
|
|||
//!
|
||||
//! wait <seconds> let the browser run
|
||||
//! waitload [seconds] wait until the current tab finished loading (default limit 30 s)
|
||||
//! waitfor <s> <js expr> poll the expression until it gives true, at most s seconds (timeout fails the run)
|
||||
//! open <url> load in the current tab newtab <url> open in a new tab
|
||||
//! page <nacre://...> show an internal page
|
||||
//! shot <name> [pip] save the window (or the picture-in-picture window) as <name>.png
|
||||
|
|
@ -22,10 +23,10 @@
|
|||
//! js <script> run JavaScript in the current tab; the result goes to the log
|
||||
//! assert <js expression> the expression must give true, else `ASSERT FAILED` in the log fails the smoke run
|
||||
//! expect <pointer> <json> a value of the `status` JSON must equal json (e.g. `expect /tabs/1/pinned true`;
|
||||
//! `/active/...` is the active tab, `/tab_count` the number of tabs)
|
||||
//! `/active/...` is the active tab, `/tab_count` the number of tabs; `> n` a number above n)
|
||||
//! click <x> <y> click in the page at CSS-ish pixel x,y (from the page's top left), through the
|
||||
//! window's input path; `rclick` the same with the right button (context menu)
|
||||
//! locate <css selector> find an element, scroll it into view; `click @` / `rclick @` then click its centre
|
||||
//! locate <css selector> find the first visible matching element, scroll it into view; `click @` / `rclick @` then click its centre
|
||||
//! pagescroll <x> <y> <dy> turn the wheel over the page at x,y (as `click`) by dy pixels (positive: down)
|
||||
//! pagetype <text> type text into the page, if it has keyboard focus (after a `click` in a field);
|
||||
//! `{enter}` / `{escape}` in the text press Enter / Escape
|
||||
|
|
@ -67,6 +68,10 @@ pub struct SelfTest {
|
|||
held: Option<egui::Pos2>,
|
||||
/// The centre of the element the last `locate` found (page pixels), for `click @`.
|
||||
located: std::sync::Arc<std::sync::Mutex<Option<(f64, f64)>>>,
|
||||
/// `waitfor`: the expression and when to give up.
|
||||
wait_js: Option<(String, Instant)>,
|
||||
/// (an evaluation is in flight, the last one gave true)
|
||||
js_poll: std::sync::Arc<std::sync::Mutex<(bool, bool)>>,
|
||||
}
|
||||
|
||||
impl SelfTest {
|
||||
|
|
@ -75,7 +80,7 @@ impl SelfTest {
|
|||
let text = std::fs::read_to_string(&path).ok()?;
|
||||
let dir = path.parent().map(|p| p.to_owned()).unwrap_or_else(|| PathBuf::from("."));
|
||||
let lines = text.lines().map(|l| l.trim().to_owned()).filter(|l| !l.is_empty() && !l.starts_with('#')).collect();
|
||||
Some(SelfTest { lines, next: 0, wait_until: None, wait_load_until: None, dir, pending_shot: false, pending_release: None, held: None, located: Default::default() })
|
||||
Some(SelfTest { lines, next: 0, wait_until: None, wait_load_until: None, dir, pending_shot: false, pending_release: None, held: None, located: Default::default(), wait_js: None, js_poll: Default::default() })
|
||||
}
|
||||
|
||||
fn log(&self, text: &str) {
|
||||
|
|
@ -130,6 +135,30 @@ impl App {
|
|||
}
|
||||
continue;
|
||||
}
|
||||
if let Some((expr, until)) = test.wait_js.clone() {
|
||||
let (in_flight, done) = test.js_poll.lock().map(|p| *p).unwrap_or((false, false));
|
||||
if done || Instant::now() >= until {
|
||||
test.log(&if done { format!("waitfor ok: {expr}") } else { format!("ASSERT FAILED (waitfor timed out): {expr}") });
|
||||
test.wait_js = None;
|
||||
if let Ok(mut p) = test.js_poll.lock() {
|
||||
*p = (false, false);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
test.wait_until = Some(Instant::now() + Duration::from_millis(250));
|
||||
let poll = test.js_poll.clone();
|
||||
if !in_flight && let Some(web) = self.windows.get(&window).and_then(|w| w.active_webview()) {
|
||||
if let Ok(mut p) = poll.lock() {
|
||||
p.0 = true;
|
||||
}
|
||||
web.evaluate_javascript(expr, move |result| {
|
||||
if let Ok(mut p) = poll.lock() {
|
||||
*p = (false, matches!(result, Ok(servo::JSValue::Boolean(true))));
|
||||
}
|
||||
});
|
||||
}
|
||||
return;
|
||||
}
|
||||
if test.next >= test.lines.len() {
|
||||
return;
|
||||
}
|
||||
|
|
@ -161,6 +190,14 @@ impl App {
|
|||
t.wait_load_until = Some(Instant::now() + Duration::from_secs_f64(secs));
|
||||
}
|
||||
},
|
||||
"waitfor" => {
|
||||
// `waitfor <seconds> <js expression>`: poll until the expression gives true; a timeout fails the run.
|
||||
let (secs, expr) = rest.split_once(char::is_whitespace).unwrap_or(("30", rest));
|
||||
let secs: f64 = secs.parse().unwrap_or(30.0);
|
||||
if let Some(t) = self.selftest.as_mut() {
|
||||
t.wait_js = Some((expr.trim().to_owned(), Instant::now() + Duration::from_secs_f64(secs)));
|
||||
}
|
||||
},
|
||||
"open" => self.navigate(window, rest.to_owned()),
|
||||
"newtab" => self.open(window, rest.to_owned(), Disposition::NewForegroundTab, None),
|
||||
"page" => {
|
||||
|
|
@ -204,7 +241,13 @@ impl App {
|
|||
None => pointer.to_owned(),
|
||||
};
|
||||
let got = status.pointer(&pointer).cloned().unwrap_or(serde_json::Value::Null);
|
||||
if got == want {
|
||||
// `expect <pointer> > n`: a number above n.
|
||||
let above = rest.split_once(char::is_whitespace).and_then(|(_, w)| w.trim().strip_prefix('>')).and_then(|n| n.trim().parse::<f64>().ok());
|
||||
let ok = match above {
|
||||
Some(n) => got.as_f64().is_some_and(|g| g > n),
|
||||
None => got == want,
|
||||
};
|
||||
if ok {
|
||||
self.selftest_log(&format!("expect ok: {pointer} = {got}"));
|
||||
} else {
|
||||
self.selftest_log(&format!("ASSERT FAILED: {pointer} is {got}, expected {want}"));
|
||||
|
|
@ -334,7 +377,7 @@ impl App {
|
|||
}
|
||||
},
|
||||
"locate" => {
|
||||
// Find the first element matching a CSS selector and scroll it into view, for `click @`.
|
||||
// Find the first visible element matching a CSS selector and scroll it into view, for `click @`.
|
||||
let Some(web) = self.windows.get(&window).and_then(|w| w.active_webview()).cloned() else { return };
|
||||
let Some(located) = self.selftest.as_ref().map(|t| t.located.clone()) else { return };
|
||||
if let Ok(mut l) = located.lock() {
|
||||
|
|
@ -342,7 +385,7 @@ impl App {
|
|||
}
|
||||
let log_path = dir.join("selftest.log");
|
||||
let selector = serde_json::to_string(rest).unwrap_or_default();
|
||||
let script = format!("(() => {{ const e = document.querySelector({selector}); if (!e) return null; e.scrollIntoView({{block: 'center'}}); const r = e.getBoundingClientRect(); return [r.left + r.width / 2, r.top + r.height / 2]; }})()");
|
||||
let script = format!("(() => {{ const e = [...document.querySelectorAll({selector})].find(e => {{ const r = e.getBoundingClientRect(); return r.width > 0 && r.height > 0; }}); if (!e) return null; e.scrollIntoView({{block: 'center'}}); const r = e.getBoundingClientRect(); return [r.left + r.width / 2, r.top + r.height / 2]; }})()");
|
||||
web.evaluate_javascript(script, move |result| {
|
||||
use std::io::Write as _;
|
||||
let point = match &result {
|
||||
|
|
|
|||
Loading…
Reference in a new issue