add 'attr' method in the SelectorBase

master
kirbylife 2021-01-30 21:11:54 -06:00
parent d02da8ff20
commit 1cf010b5a0
3 changed files with 73 additions and 37 deletions

View File

@ -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 = "
<html>
<body>
<h1>hello world</h1>
<p id='text'>good bye</p>
<a>simple text</a>
</body>
</html>
"
.to_string();
let sel = Selector::from_html(html);
assert_eq!(sel.css::<Selector>("h1")[0].html(), "<h1>hello world</h1>");
assert_eq!(sel.css::<Selector>("#text")[0].content(), "good bye");
assert_eq!(
sel.css_once::<Selector>("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::<Selector>("h2")[0].html().contains("httpbin.org"));
}
}

View File

@ -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<String> {
let html = nipper::Document::from(self.html().as_str());
match html.select("body > *").attr(attribute) {
Some(text) => Some(format!("{}", text)),
None => None,
}
}
}
#[derive(Debug)]

View File

@ -0,0 +1,64 @@
use http::StatusCode;
use raspa::request::{Request, RequestBase};
use raspa::selector::{Selector, SelectorBase};
#[test]
fn plain_text_selector() {
let html = "
<html>
<body>
<h1>hello world</h1>
<p id='text'>good bye</p>
<a>simple text</a>
</body>
</html>
"
.to_string();
let sel = Selector::from_html(html);
assert_eq!(sel.css::<Selector>("h1")[0].html(), "<h1>hello world</h1>");
assert_eq!(sel.css::<Selector>("#text")[0].content(), "good bye");
assert_eq!(
sel.css_once::<Selector>("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::<Selector>("h2")[0].html().contains("httpbin.org"));
}
#[test]
fn complex_selectors() {
let html = "
<html>
<body>
<p id='text'>good bye</p>
<a href='http://google.com'>simple text</a>
<ul>
<li class='item' id='item-1'>1</li>
<li class='item' id='item-2'>2</li>
<li class='item' id='item-3'>3</li>
</ul>
</body>
</html>
"
.to_string();
let sel = Selector::from_html(html);
assert_eq!(
sel.css_once::<Selector>("p").unwrap().attr("id").unwrap(),
"text"
);
assert_eq!(
sel.css::<Selector>("a")[0].attr("href").unwrap(),
"http://google.com"
);
for node in sel.css::<Selector>("ul li").iter() {
let text = node.content();
assert_eq!(node.attr("class").unwrap(), "item");
assert!(node.attr("id").unwrap().contains(&text));
}
}