mirror of
https://github.com/servo/servo.git
synced 2025-06-13 10:54:29 +00:00
Add WebDriver support for getting elements by selector.
Also adds example support for getting the name and text properties of the elements.
This commit is contained in:
parent
892a740426
commit
9e44206760
5 changed files with 163 additions and 10 deletions
|
@ -975,20 +975,17 @@ impl<'a> NodeHelpers for JSRef<'a, Node> {
|
|||
|
||||
fn get_unique_id(self) -> String {
|
||||
// FIXME(https://github.com/rust-lang/rust/issues/23338)
|
||||
if self.unique_id.borrow().is_empty() {
|
||||
let mut unique_id = self.unique_id.borrow_mut();
|
||||
*unique_id = uuid::Uuid::new_v4().to_simple_string();
|
||||
}
|
||||
let id = self.unique_id.borrow();
|
||||
id.clone()
|
||||
}
|
||||
|
||||
fn summarize(self) -> NodeInfo {
|
||||
if self.unique_id.borrow().is_empty() {
|
||||
let mut unique_id = self.unique_id.borrow_mut();
|
||||
*unique_id = uuid::Uuid::new_v4().to_simple_string();
|
||||
}
|
||||
|
||||
// FIXME(https://github.com/rust-lang/rust/issues/23338)
|
||||
let unique_id = self.unique_id.borrow();
|
||||
NodeInfo {
|
||||
uniqueId: unique_id.clone(),
|
||||
uniqueId: self.get_unique_id(),
|
||||
baseURI: self.GetBaseURI().unwrap_or("".to_owned()),
|
||||
parent: self.GetParentNode().root().map(|node| node.r().get_unique_id()).unwrap_or("".to_owned()),
|
||||
nodeType: self.NodeType(),
|
||||
|
|
|
@ -791,6 +791,14 @@ impl ScriptTask {
|
|||
match msg {
|
||||
WebDriverScriptCommand::EvaluateJS(script, reply) =>
|
||||
webdriver_handlers::handle_evaluate_js(&page, pipeline_id, script, reply),
|
||||
WebDriverScriptCommand::FindElementCSS(selector, reply) =>
|
||||
webdriver_handlers::handle_find_element_css(&page, pipeline_id, selector, reply),
|
||||
WebDriverScriptCommand::FindElementsCSS(selector, reply) =>
|
||||
webdriver_handlers::handle_find_elements_css(&page, pipeline_id, selector, reply),
|
||||
WebDriverScriptCommand::GetElementTagName(node_id, reply) =>
|
||||
webdriver_handlers::handle_get_name(&page, pipeline_id, node_id, reply),
|
||||
WebDriverScriptCommand::GetElementText(node_id, reply) =>
|
||||
webdriver_handlers::handle_get_text(&page, pipeline_id, node_id, reply),
|
||||
WebDriverScriptCommand::GetTitle(reply) =>
|
||||
webdriver_handlers::handle_get_title(&page, pipeline_id, reply)
|
||||
}
|
||||
|
|
|
@ -5,8 +5,13 @@
|
|||
use webdriver_traits::{EvaluateJSReply};
|
||||
use dom::bindings::conversions::FromJSValConvertible;
|
||||
use dom::bindings::conversions::StringificationBehavior;
|
||||
use dom::bindings::codegen::InheritTypes::{NodeCast, ElementCast};
|
||||
use dom::bindings::codegen::Bindings::DocumentBinding::DocumentMethods;
|
||||
use dom::bindings::js::{OptionalRootable, Rootable};
|
||||
use dom::bindings::codegen::Bindings::ElementBinding::ElementMethods;
|
||||
use dom::bindings::codegen::Bindings::NodeBinding::NodeMethods;
|
||||
use dom::bindings::codegen::Bindings::NodeListBinding::NodeListMethods;
|
||||
use dom::bindings::js::{OptionalRootable, Rootable, Temporary};
|
||||
use dom::node::{Node, NodeHelpers};
|
||||
use dom::window::ScriptHelpers;
|
||||
use dom::document::DocumentHelpers;
|
||||
use page::Page;
|
||||
|
@ -16,6 +21,20 @@ use script_task::get_page;
|
|||
use std::rc::Rc;
|
||||
use std::sync::mpsc::Sender;
|
||||
|
||||
fn find_node_by_unique_id(page: &Rc<Page>, pipeline: PipelineId, node_id: String) -> Option<Temporary<Node>> {
|
||||
let page = get_page(&*page, pipeline);
|
||||
let document = page.document().root();
|
||||
let node = NodeCast::from_ref(document.r());
|
||||
|
||||
for candidate in node.traverse_preorder() {
|
||||
if candidate.root().r().get_unique_id() == node_id {
|
||||
return Some(candidate);
|
||||
}
|
||||
}
|
||||
|
||||
None
|
||||
}
|
||||
|
||||
pub fn handle_evaluate_js(page: &Rc<Page>, pipeline: PipelineId, eval: String, reply: Sender<Result<EvaluateJSReply, ()>>) {
|
||||
let page = get_page(&*page, pipeline);
|
||||
let window = page.window().root();
|
||||
|
@ -38,6 +57,54 @@ pub fn handle_evaluate_js(page: &Rc<Page>, pipeline: PipelineId, eval: String, r
|
|||
}).unwrap();
|
||||
}
|
||||
|
||||
pub fn handle_find_element_css(page: &Rc<Page>, _pipeline: PipelineId, selector: String, reply: Sender<Result<Option<String>, ()>>) {
|
||||
reply.send(match page.document().root().r().QuerySelector(selector.clone()) {
|
||||
Ok(node) => {
|
||||
let result = node.map(|x| NodeCast::from_ref(x.root().r()).get_unique_id());
|
||||
Ok(result)
|
||||
}
|
||||
Err(_) => Err(())
|
||||
}).unwrap();
|
||||
}
|
||||
|
||||
pub fn handle_find_elements_css(page: &Rc<Page>, _pipeline: PipelineId, selector: String, reply: Sender<Result<Vec<String>, ()>>) {
|
||||
reply.send(match page.document().root().r().QuerySelectorAll(selector.clone()) {
|
||||
Ok(ref node_list) => {
|
||||
let nodes = node_list.root();
|
||||
let mut result = Vec::with_capacity(nodes.r().Length() as usize);
|
||||
for i in 0..nodes.r().Length() {
|
||||
if let Some(ref node) = nodes.r().Item(i) {
|
||||
result.push(node.root().r().get_unique_id());
|
||||
}
|
||||
}
|
||||
Ok(result)
|
||||
},
|
||||
Err(_) => {
|
||||
Err(())
|
||||
}
|
||||
}).unwrap();
|
||||
}
|
||||
|
||||
pub fn handle_get_title(page: &Rc<Page>, _pipeline: PipelineId, reply: Sender<String>) {
|
||||
reply.send(page.document().root().r().Title()).unwrap();
|
||||
}
|
||||
|
||||
pub fn handle_get_text(page: &Rc<Page>, pipeline: PipelineId, node_id: String, reply: Sender<Result<String, ()>>) {
|
||||
reply.send(match find_node_by_unique_id(&*page, pipeline, node_id) {
|
||||
Some(ref node) => {
|
||||
Ok(node.root().r().GetTextContent().unwrap_or("".to_owned()))
|
||||
},
|
||||
None => Err(())
|
||||
}).unwrap();
|
||||
}
|
||||
|
||||
pub fn handle_get_name(page: &Rc<Page>, pipeline: PipelineId, node_id: String, reply: Sender<Result<String, ()>>) {
|
||||
reply.send(match find_node_by_unique_id(&*page, pipeline, node_id) {
|
||||
Some(tmp_node) => {
|
||||
let node = tmp_node.root();
|
||||
let element = ElementCast::to_ref(node.r()).unwrap();
|
||||
Ok(element.TagName())
|
||||
},
|
||||
None => Err(())
|
||||
}).unwrap();
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue