chiark / gitweb /
tests: provide HtmlExt and ::e_attr
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Thu, 25 Feb 2021 00:36:30 +0000 (00:36 +0000)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Thu, 25 Feb 2021 00:36:30 +0000 (00:36 +0000)
Signed-off-by: Ian Jackson <ijackson@chiark.greenend.org.uk>
apitest/at-otter.rs

index c4f18bced0e38ba2330c5e6edc21a7fbd822ddeb..d5884c489ea1be6685ec64019ea712f161014666 100644 (file)
@@ -22,10 +22,39 @@ struct Player {
 }
 
 struct Session {
-  pub dom: scraper::html::Html,
+  pub dom: scraper::Html,
   pub updates: UnixStream,
 }
 
+mod scraper_ext {
+  use super::*;
+  use scraper::*;
+  use scraper::html::*;
+
+  pub trait HtmlExt {
+    fn select<'a,'b>(&'a self, selector: &'b Selector) -> Select<'a, 'b>;
+
+    #[throws(as Option)]
+    fn e_attr<S>(&self, sel: S, attr: &str) -> &str
+    where S: TryInto<Selector>,
+          <S as TryInto<Selector>>::Error: Debug,
+    {
+      self
+        .select(&sel.try_into().unwrap())
+        .next()?
+        .value().attr(attr)?
+    }
+  }
+
+  impl HtmlExt for Html {
+    fn select<'a,'b>(&'a self, selector: &'b Selector) -> Select<'a, 'b> {
+      self.select(selector)
+    }
+  }
+}
+
+use scraper_ext::HtmlExt;
+
 impl Ctx {
   #[throws(AE)]
   fn connect_player(&self, player: &Player) -> Session {
@@ -36,11 +65,7 @@ impl Ctx {
     let body = resp.text()?;
     let loading = scraper::Html::parse_document(&body);
     //dbg!(&body, &dom);
-    let ptoken = loading
-      .select(&"#loading_token".try_into().unwrap())
-      .next().unwrap()
-      .value().attr("data-ptoken")
-      .unwrap();
+    let ptoken = loading.e_attr("#loading_token", "data-ptoken").unwrap();
     dbg!(&ptoken);
 
     let resp = client.post(&self.ds.subst("@url@/_/session/Portrait")?)