diff --git a/src/lib.rs b/src/lib.rs
index 51b9122..0e5b335 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -1,39 +1,3 @@
pub mod request;
pub mod response;
pub mod selector;
-
-#[cfg(test)]
-mod tests {
- use crate::request::{Request, RequestBase};
- use crate::selector::{Selector, SelectorBase};
- use http::StatusCode;
-
- #[test]
- fn plain_text_selector() {
- let html = "
-
-
- hello world
- good bye
- simple text
-
-
-"
- .to_string();
- let sel = Selector::from_html(html);
- assert_eq!(sel.css::("h1")[0].html(), "hello world
");
- assert_eq!(sel.css::("#text")[0].content(), "good bye");
- assert_eq!(
- sel.css_once::("body > a").unwrap().content(),
- "simple text"
- );
- }
-
- #[test]
- fn simple_request() {
- let req: Request = RequestBase::new("https://httpbin.org/").unwrap();
- let resp = req.launch();
- assert_eq!(resp.status_code, StatusCode::OK);
- assert!(resp.css::("h2")[0].html().contains("httpbin.org"));
- }
-}
diff --git a/src/selector.rs b/src/selector.rs
index 7b40b02..989718f 100644
--- a/src/selector.rs
+++ b/src/selector.rs
@@ -20,12 +20,20 @@ pub trait SelectorBase {
fn content(&self) -> String {
let html = nipper::Document::from(self.html().as_str());
- html.select("*")
+ html.select("body > *")
.iter()
.map(|element| element.text().to_string())
.last()
.unwrap()
}
+
+ fn attr(&self, attribute: &'static str) -> Option {
+ let html = nipper::Document::from(self.html().as_str());
+ match html.select("body > *").attr(attribute) {
+ Some(text) => Some(format!("{}", text)),
+ None => None,
+ }
+ }
}
#[derive(Debug)]
diff --git a/tests/unit_tests.rs b/tests/unit_tests.rs
new file mode 100644
index 0000000..b96df90
--- /dev/null
+++ b/tests/unit_tests.rs
@@ -0,0 +1,64 @@
+use http::StatusCode;
+use raspa::request::{Request, RequestBase};
+use raspa::selector::{Selector, SelectorBase};
+
+#[test]
+fn plain_text_selector() {
+ let html = "
+
+
+ hello world
+ good bye
+ simple text
+
+
+"
+ .to_string();
+ let sel = Selector::from_html(html);
+ assert_eq!(sel.css::("h1")[0].html(), "hello world
");
+ assert_eq!(sel.css::("#text")[0].content(), "good bye");
+ assert_eq!(
+ sel.css_once::("body > a").unwrap().content(),
+ "simple text"
+ );
+}
+
+#[test]
+fn simple_request() {
+ let req: Request = RequestBase::new("https://httpbin.org/").unwrap();
+ let resp = req.launch();
+ assert_eq!(resp.status_code, StatusCode::OK);
+ assert!(resp.css::("h2")[0].html().contains("httpbin.org"));
+}
+
+#[test]
+fn complex_selectors() {
+ let html = "
+
+
+ good bye
+ simple text
+
+
+
+"
+ .to_string();
+ let sel = Selector::from_html(html);
+ assert_eq!(
+ sel.css_once::("p").unwrap().attr("id").unwrap(),
+ "text"
+ );
+ assert_eq!(
+ sel.css::("a")[0].attr("href").unwrap(),
+ "http://google.com"
+ );
+ for node in sel.css::("ul li").iter() {
+ let text = node.content();
+ assert_eq!(node.attr("class").unwrap(), "item");
+ assert!(node.attr("id").unwrap().contains(&text));
+ }
+}