use skyscraper::{ html::{self, trim_internal_whitespace}, xpath, }; #[test] fn contains_should_select_text() { // arrange let text = r###"
hello world
select me
"###; let document = html::parse(&text).unwrap(); let xpath_item_tree = xpath::XpathItemTree::from(&document); let xpath = xpath::parse("//div[contains(text(),'select')]").unwrap(); // act let nodes = xpath.apply(&xpath_item_tree).unwrap(); // assert assert_eq!(nodes.len(), 1); let mut nodes = nodes.into_iter(); // assert node { let tree_node = nodes.next().unwrap().extract_into_node(); let element = tree_node.extract_as_element_node(); assert_eq!(element.name, "div"); assert_eq!( trim_internal_whitespace(&tree_node.text(&xpath_item_tree).unwrap()), "select me" ); } } #[test] fn contains_should_select_attribute() { // arrange let text = r###"
hello world
select me
"###; let document = html::parse(&text).unwrap(); let xpath_item_tree = xpath::XpathItemTree::from(&document); let xpath = xpath::parse("//div[contains(@class,'select')]").unwrap(); // act let nodes = xpath.apply(&xpath_item_tree).unwrap(); // assert assert_eq!(nodes.len(), 1); let mut nodes = nodes.into_iter(); // assert node { let tree_node = nodes.next().unwrap().extract_into_node(); let element = tree_node.extract_as_element_node(); assert_eq!(element.name, "div"); assert_eq!( trim_internal_whitespace(&tree_node.text(&xpath_item_tree).unwrap()), "select me" ); } } #[test] fn contains_should_select_on_expression() { // arrange let text = r###"
other
hello world
"###; let document = html::parse(&text).unwrap(); let xpath_item_tree = xpath::XpathItemTree::from(&document); // this expression first selects the class attribute of the div with id 'select', // then uses that result to find a div with text containing that class. let xpath = xpath::parse("//div[contains(text(),//div[@id='select']/@class)]").unwrap(); // act let nodes = xpath.apply(&xpath_item_tree).unwrap(); // assert assert_eq!(nodes.len(), 1); let mut nodes = nodes.into_iter(); // assert node { let tree_node = nodes.next().unwrap().extract_into_node(); let element = tree_node.extract_as_element_node(); assert_eq!(element.name, "div"); assert_eq!( trim_internal_whitespace(&tree_node.text(&xpath_item_tree).unwrap()), "hello world" ); } }