From 24a22f6128e49ae0ab49d6b500544ee76262c357 Mon Sep 17 00:00:00 2001 From: Ian Jackson Date: Tue, 29 Dec 2020 01:05:11 +0000 Subject: [PATCH] wip console logging Signed-off-by: Ian Jackson --- Cargo.lock.example | 2 ++ wdriver.rs | 44 ++++++++++++++++++++++++++++++++++++++------ wdriver/Cargo.toml | 2 ++ 3 files changed, 42 insertions(+), 6 deletions(-) diff --git a/Cargo.lock.example b/Cargo.lock.example index 1630ec4c..765eaa91 100644 --- a/Cargo.lock.example +++ b/Cargo.lock.example @@ -1588,6 +1588,8 @@ dependencies = [ "otter", "parking_lot", "regex", + "serde", + "serde_json", "structopt", "strum", "thirtyfour_sync", diff --git a/wdriver.rs b/wdriver.rs index 3030b2c3..f800141f 100644 --- a/wdriver.rs +++ b/wdriver.rs @@ -15,6 +15,7 @@ pub use nix::unistd::LinkatFlags; pub use num_derive::FromPrimitive; pub use parking_lot::{Mutex, MutexGuard}; pub use regex::{Captures, Regex}; +pub use serde::{Serialize, Deserialize}; pub use structopt::StructOpt; pub use strum::{EnumIter, EnumProperty, IntoEnumIterator, IntoStaticStr}; pub use thirtyfour_sync as t4; @@ -669,17 +670,19 @@ fn prepare_thirtyfour() -> (T4d, ScreenShotCount, Vec) { var saved = [ ]; var new_console = { saved: saved}; for (k of ['log','error','warn','info']) { - var orig = orig_console[k]; - new_console[k] = function() { - saved.push([k, arguments]); - orig.apply(orig_console, arguments); - } + (function(k){ + var orig = orig_console[k]; + new_console[k] = function() { + saved.push([k, arguments]); + orig.apply(orig_console, arguments); + } + })(k); } return new_console; })(); console.log('wdriver.rs console log starts'); -"#)?; + "#)?; let cons = driver.execute_script(r#"return window.console.saved;"#)?; eprintln!("{:?}", &cons.value()); @@ -774,6 +777,35 @@ impl<'g> Deref for WindowGuard<'g> { fn deref(&self) -> &T4d { &self.su.driver } } +impl<'g> Drop for WindowGuard<'g> { + fn drop(&mut self) { + (||{ + let got = self.su.driver.execute_script(r#" + var returning = window.console.saved; + window.console.saved = []; + return returning; + "#).context("get log")?; + + for ent in got.value().as_array() + .ok_or(anyhow!("saved isn't an array?"))? + { + #[derive(Deserialize)] + struct LogEnt(String, Vec); + + let ent: LogEnt = serde_json::from_value(ent.clone()) + .context("parse log entry")?; + eprint!("JS {} {}:", &self.w.name, &ent.0); + for a in ent.1 { eprint!(" {}", a); } + eprintln!(""); + } + Ok::<_,AE>(()) + })() + .with_context(|| self.w.name.clone()) + .context("update log") + .just_warn(); + } +} + pub trait Screenshottable { fn screenshot(&mut self, slug: &str) -> Result<(),AE>; } diff --git a/wdriver/Cargo.toml b/wdriver/Cargo.toml index 56ff362a..4564bb76 100644 --- a/wdriver/Cargo.toml +++ b/wdriver/Cargo.toml @@ -26,6 +26,8 @@ num-derive = "0.3" num-traits = "0.2" parking_lot = "0.11" regex = "1" +serde = { version = "1", features = ["derive","rc"] } +serde_json = "1" structopt = "0.3" strum = { version = "0.20", features = ['derive'] } thirtyfour_sync = "0.21" -- 2.30.2