Auto merge of #23951 - georgeroman:return_errorstatus_from_webdriverhandlers, r=jdm

Return ErrorStatus from webdriver_handlers

<!-- Please describe your changes on the following line: -->
With `webdriver 0.40`, `ErrorStatus` implements `Deserialize`. Now we can accommodate for multiple errors occuring in `webdriver_handlers`.

---
<!-- Thank you for contributing to Servo! Please replace each `[ ]` by `[X]` when the step is complete, and replace `___` with appropriate data: -->
- [X] `./mach build -d` does not report any errors
- [X] `./mach test-tidy` does not report any errors

<!-- Either: -->

<!-- Also, please make sure that "Allow edits from maintainers" checkbox is checked, so that we can help you if you get stuck somewhere along the way.-->

<!-- Pull requests that do not address these steps are welcome, but they will require additional verification as part of the review process. -->

<!-- Reviewable:start -->
---
This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/servo/23951)
<!-- Reviewable:end -->
This commit is contained in:
bors-servo 2019-08-19 02:10:41 -04:00 committed by GitHub
commit 00a9f30773
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
12 changed files with 482 additions and 438 deletions

View file

@ -1654,7 +1654,7 @@ impl Node {
#[allow(unrooted_must_root)]
fn new_(flags: NodeFlags, doc: Option<&Document>) -> Node {
Node {
let node = Node {
eventtarget: EventTarget::new_inherited(),
parent_node: Default::default(),
@ -1673,7 +1673,11 @@ impl Node {
style_and_layout_data: Cell::new(None),
unique_id: UniqueId::new(),
}
};
ScriptThread::save_node_id(node.unique_id());
node
}
// https://dom.spec.whatwg.org/#concept-node-adopt

View file

@ -687,6 +687,9 @@ pub struct ScriptThread {
/// A mechanism to force the compositor's event loop to process events.
event_loop_waker: Option<Box<dyn EventLoopWaker>>,
/// A set of all nodes ever created in this script thread
node_ids: DomRefCell<HashSet<String>>,
}
/// In the event of thread panic, all data on the stack runs its destructor. However, there
@ -1183,6 +1186,25 @@ impl ScriptThread {
})
}
pub fn save_node_id(node_id: String) {
SCRIPT_THREAD_ROOT.with(|root| {
if let Some(script_thread) = root.get() {
let script_thread = unsafe { &*script_thread };
script_thread.node_ids.borrow_mut().insert(node_id);
}
})
}
pub fn has_node_id(node_id: &str) -> bool {
SCRIPT_THREAD_ROOT.with(|root| match root.get() {
Some(script_thread) => {
let script_thread = unsafe { &*script_thread };
script_thread.node_ids.borrow().contains(node_id)
},
None => false,
})
}
/// Creates a new script thread.
pub fn new(
state: InitialScriptState,
@ -1315,6 +1337,8 @@ impl ScriptThread {
user_agent,
player_context: state.player_context,
event_loop_waker: state.event_loop_waker,
node_ids: Default::default(),
}
}

View file

@ -33,7 +33,7 @@ use crate::dom::nodelist::NodeList;
use crate::dom::window::Window;
use crate::dom::xmlserializer::XMLSerializer;
use crate::script_runtime::JSContext as SafeJSContext;
use crate::script_thread::Documents;
use crate::script_thread::{Documents, ScriptThread};
use cookie::Cookie;
use euclid::default::{Point2D, Rect, Size2D};
use hyper_serde::Serde;
@ -52,18 +52,28 @@ use script_traits::webdriver_msg::{
};
use servo_url::ServoUrl;
use webdriver::common::WebElement;
use webdriver::error::ErrorStatus;
fn find_node_by_unique_id(
documents: &Documents,
pipeline: PipelineId,
node_id: String,
) -> Option<DomRoot<Node>> {
documents.find_document(pipeline).and_then(|document| {
) -> Result<DomRoot<Node>, ErrorStatus> {
match documents.find_document(pipeline).and_then(|document| {
document
.upcast::<Node>()
.traverse_preorder(ShadowIncluding::Yes)
.find(|candidate| candidate.unique_id() == node_id)
})
.find(|node| node.unique_id() == node_id)
}) {
Some(node) => Ok(node),
None => {
if ScriptThread::has_node_id(&node_id) {
Err(ErrorStatus::StaleElementReference)
} else {
Err(ErrorStatus::NoSuchElement)
}
},
}
}
fn matching_links<'a>(
@ -92,10 +102,10 @@ fn all_matching_links(
root_node: &Node,
link_text: String,
partial: bool,
) -> Result<Vec<String>, ()> {
) -> Result<Vec<String>, ErrorStatus> {
root_node
.query_selector_all(DOMString::from("a"))
.map_err(|_| ())
.map_err(|_| ErrorStatus::UnknownError)
.map(|nodes| matching_links(&nodes, link_text, partial).collect())
}
@ -103,10 +113,10 @@ fn first_matching_link(
root_node: &Node,
link_text: String,
partial: bool,
) -> Result<Option<String>, ()> {
) -> Result<Option<String>, ErrorStatus> {
root_node
.query_selector_all(DOMString::from("a"))
.map_err(|_| ())
.map_err(|_| ErrorStatus::UnknownError)
.map(|nodes| matching_links(&nodes, link_text, partial).take(1).next())
}
@ -243,45 +253,53 @@ pub fn handle_get_browsing_context_id(
documents: &Documents,
pipeline: PipelineId,
webdriver_frame_id: WebDriverFrameId,
reply: IpcSender<Result<BrowsingContextId, ()>>,
reply: IpcSender<Result<BrowsingContextId, ErrorStatus>>,
) {
let result = match webdriver_frame_id {
WebDriverFrameId::Short(_) => {
// This isn't supported yet
Err(())
},
WebDriverFrameId::Element(x) => find_node_by_unique_id(documents, pipeline, x)
.and_then(|node| {
node.downcast::<HTMLIFrameElement>()
.and_then(|elem| elem.browsing_context_id())
})
.ok_or(()),
WebDriverFrameId::Parent => documents
.find_window(pipeline)
.and_then(|window| {
window
.window_proxy()
.parent()
.map(|parent| parent.browsing_context_id())
})
.ok_or(()),
};
reply.send(result).unwrap()
reply
.send(match webdriver_frame_id {
WebDriverFrameId::Short(_) => {
// This isn't supported yet
Err(ErrorStatus::UnsupportedOperation)
},
WebDriverFrameId::Element(element_id) => {
find_node_by_unique_id(documents, pipeline, element_id).and_then(|node| {
node.downcast::<HTMLIFrameElement>()
.and_then(|element| element.browsing_context_id())
.ok_or(ErrorStatus::NoSuchFrame)
})
},
WebDriverFrameId::Parent => documents
.find_window(pipeline)
.and_then(|window| {
window
.window_proxy()
.parent()
.map(|parent| parent.browsing_context_id())
})
.ok_or(ErrorStatus::NoSuchFrame),
})
.unwrap();
}
pub fn handle_find_element_css(
documents: &Documents,
pipeline: PipelineId,
selector: String,
reply: IpcSender<Result<Option<String>, ()>>,
reply: IpcSender<Result<Option<String>, ErrorStatus>>,
) {
let node_id = documents
.find_document(pipeline)
.ok_or(())
.and_then(|doc| doc.QuerySelector(DOMString::from(selector)).map_err(|_| ()))
.map(|node| node.map(|x| x.upcast::<Node>().unique_id()));
reply.send(node_id).unwrap();
reply
.send(
documents
.find_document(pipeline)
.ok_or(ErrorStatus::UnknownError)
.and_then(|document| {
document
.QuerySelector(DOMString::from(selector))
.map_err(|_| ErrorStatus::InvalidSelector)
})
.map(|node| node.map(|x| x.upcast::<Node>().unique_id())),
)
.unwrap();
}
pub fn handle_find_element_link_text(
@ -289,54 +307,66 @@ pub fn handle_find_element_link_text(
pipeline: PipelineId,
selector: String,
partial: bool,
reply: IpcSender<Result<Option<String>, ()>>,
reply: IpcSender<Result<Option<String>, ErrorStatus>>,
) {
let node_id = documents
.find_document(pipeline)
.ok_or(())
.and_then(|doc| first_matching_link(&doc.upcast::<Node>(), selector.clone(), partial));
reply.send(node_id).unwrap();
reply
.send(
documents
.find_document(pipeline)
.ok_or(ErrorStatus::UnknownError)
.and_then(|document| {
first_matching_link(&document.upcast::<Node>(), selector.clone(), partial)
}),
)
.unwrap();
}
pub fn handle_find_element_tag_name(
documents: &Documents,
pipeline: PipelineId,
selector: String,
reply: IpcSender<Result<Option<String>, ()>>,
reply: IpcSender<Result<Option<String>, ErrorStatus>>,
) {
let node_id = documents
.find_document(pipeline)
.ok_or(())
.and_then(|doc| {
Ok(doc
.GetElementsByTagName(DOMString::from(selector))
.elements_iter()
.next())
})
.map(|node| node.map(|x| x.upcast::<Node>().unique_id()));
reply.send(node_id).unwrap();
reply
.send(
documents
.find_document(pipeline)
.ok_or(ErrorStatus::UnknownError)
.and_then(|document| {
Ok(document
.GetElementsByTagName(DOMString::from(selector))
.elements_iter()
.next())
})
.map(|node| node.map(|x| x.upcast::<Node>().unique_id())),
)
.unwrap();
}
pub fn handle_find_elements_css(
documents: &Documents,
pipeline: PipelineId,
selector: String,
reply: IpcSender<Result<Vec<String>, ()>>,
reply: IpcSender<Result<Vec<String>, ErrorStatus>>,
) {
let node_ids = documents
.find_document(pipeline)
.ok_or(())
.and_then(|doc| {
doc.QuerySelectorAll(DOMString::from(selector))
.map_err(|_| ())
})
.map(|nodes| {
nodes
.iter()
.map(|x| x.upcast::<Node>().unique_id())
.collect()
});
reply.send(node_ids).unwrap();
reply
.send(
documents
.find_document(pipeline)
.ok_or(ErrorStatus::UnknownError)
.and_then(|document| {
document
.QuerySelectorAll(DOMString::from(selector))
.map_err(|_| ErrorStatus::InvalidSelector)
})
.map(|nodes| {
nodes
.iter()
.map(|x| x.upcast::<Node>().unique_id())
.collect()
}),
)
.unwrap();
}
pub fn handle_find_elements_link_text(
@ -344,32 +374,40 @@ pub fn handle_find_elements_link_text(
pipeline: PipelineId,
selector: String,
partial: bool,
reply: IpcSender<Result<Vec<String>, ()>>,
reply: IpcSender<Result<Vec<String>, ErrorStatus>>,
) {
let node_ids = documents
.find_document(pipeline)
.ok_or(())
.and_then(|doc| all_matching_links(&doc.upcast::<Node>(), selector.clone(), partial));
reply.send(node_ids).unwrap();
reply
.send(
documents
.find_document(pipeline)
.ok_or(ErrorStatus::UnknownError)
.and_then(|document| {
all_matching_links(&document.upcast::<Node>(), selector.clone(), partial)
}),
)
.unwrap();
}
pub fn handle_find_elements_tag_name(
documents: &Documents,
pipeline: PipelineId,
selector: String,
reply: IpcSender<Result<Vec<String>, ()>>,
reply: IpcSender<Result<Vec<String>, ErrorStatus>>,
) {
let node_ids = documents
.find_document(pipeline)
.ok_or(())
.and_then(|doc| Ok(doc.GetElementsByTagName(DOMString::from(selector))))
.map(|nodes| {
nodes
.elements_iter()
.map(|x| x.upcast::<Node>().unique_id())
.collect::<Vec<String>>()
});
reply.send(node_ids).unwrap();
reply
.send(
documents
.find_document(pipeline)
.ok_or(ErrorStatus::UnknownError)
.and_then(|document| Ok(document.GetElementsByTagName(DOMString::from(selector))))
.map(|nodes| {
nodes
.elements_iter()
.map(|x| x.upcast::<Node>().unique_id())
.collect::<Vec<String>>()
}),
)
.unwrap();
}
pub fn handle_find_element_element_css(
@ -377,16 +415,17 @@ pub fn handle_find_element_element_css(
pipeline: PipelineId,
element_id: String,
selector: String,
reply: IpcSender<Result<Option<String>, ()>>,
reply: IpcSender<Result<Option<String>, ErrorStatus>>,
) {
let node_id = find_node_by_unique_id(documents, pipeline, element_id)
.ok_or(())
.and_then(|node| {
node.query_selector(DOMString::from(selector))
.map_err(|_| ())
})
.map(|node| node.map(|x| x.upcast::<Node>().unique_id()));
reply.send(node_id).unwrap();
reply
.send(
find_node_by_unique_id(documents, pipeline, element_id).and_then(|node| {
node.query_selector(DOMString::from(selector))
.map_err(|_| ErrorStatus::InvalidSelector)
.map(|node| node.map(|x| x.upcast::<Node>().unique_id()))
}),
)
.unwrap();
}
pub fn handle_find_element_element_link_text(
@ -395,12 +434,14 @@ pub fn handle_find_element_element_link_text(
element_id: String,
selector: String,
partial: bool,
reply: IpcSender<Result<Option<String>, ()>>,
reply: IpcSender<Result<Option<String>, ErrorStatus>>,
) {
let node_id = find_node_by_unique_id(documents, pipeline, element_id)
.ok_or(())
.and_then(|node| first_matching_link(&node, selector.clone(), partial));
reply.send(node_id).unwrap();
reply
.send(
find_node_by_unique_id(documents, pipeline, element_id)
.and_then(|node| first_matching_link(&node, selector.clone(), partial)),
)
.unwrap();
}
pub fn handle_find_element_element_tag_name(
@ -408,19 +449,22 @@ pub fn handle_find_element_element_tag_name(
pipeline: PipelineId,
element_id: String,
selector: String,
reply: IpcSender<Result<Option<String>, ()>>,
reply: IpcSender<Result<Option<String>, ErrorStatus>>,
) {
let node_id = find_node_by_unique_id(documents, pipeline, element_id)
.ok_or(())
.and_then(|node| match node.downcast::<Element>() {
Some(elem) => Ok(elem
.GetElementsByTagName(DOMString::from(selector))
.elements_iter()
.next()),
None => Err(()),
})
.map(|node| node.map(|x| x.upcast::<Node>().unique_id()));
reply.send(node_id).unwrap();
reply
.send(
find_node_by_unique_id(documents, pipeline, element_id).and_then(|node| match node
.downcast::<Element>(
) {
Some(element) => Ok(element
.GetElementsByTagName(DOMString::from(selector))
.elements_iter()
.next()
.map(|x| x.upcast::<Node>().unique_id())),
None => Err(ErrorStatus::UnknownError),
}),
)
.unwrap();
}
pub fn handle_find_element_elements_css(
@ -428,21 +472,22 @@ pub fn handle_find_element_elements_css(
pipeline: PipelineId,
element_id: String,
selector: String,
reply: IpcSender<Result<Vec<String>, ()>>,
reply: IpcSender<Result<Vec<String>, ErrorStatus>>,
) {
let node_ids = find_node_by_unique_id(documents, pipeline, element_id)
.ok_or(())
.and_then(|node| {
node.query_selector_all(DOMString::from(selector))
.map_err(|_| ())
})
.map(|nodes| {
nodes
.iter()
.map(|x| x.upcast::<Node>().unique_id())
.collect()
});
reply.send(node_ids).unwrap();
reply
.send(
find_node_by_unique_id(documents, pipeline, element_id).and_then(|node| {
node.query_selector_all(DOMString::from(selector))
.map_err(|_| ErrorStatus::InvalidSelector)
.map(|nodes| {
nodes
.iter()
.map(|x| x.upcast::<Node>().unique_id())
.collect()
})
}),
)
.unwrap();
}
pub fn handle_find_element_elements_link_text(
@ -451,12 +496,14 @@ pub fn handle_find_element_elements_link_text(
element_id: String,
selector: String,
partial: bool,
reply: IpcSender<Result<Vec<String>, ()>>,
reply: IpcSender<Result<Vec<String>, ErrorStatus>>,
) {
let node_ids = find_node_by_unique_id(documents, pipeline, element_id)
.ok_or(())
.and_then(|node| all_matching_links(&node, selector.clone(), partial));
reply.send(node_ids).unwrap();
reply
.send(
find_node_by_unique_id(documents, pipeline, element_id)
.and_then(|node| all_matching_links(&node, selector.clone(), partial)),
)
.unwrap();
}
pub fn handle_find_element_elements_tag_name(
@ -464,44 +511,42 @@ pub fn handle_find_element_elements_tag_name(
pipeline: PipelineId,
element_id: String,
selector: String,
reply: IpcSender<Result<Vec<String>, ()>>,
reply: IpcSender<Result<Vec<String>, ErrorStatus>>,
) {
let node_ids = find_node_by_unique_id(documents, pipeline, element_id)
.ok_or(())
.and_then(|node| match node.downcast::<Element>() {
Some(elem) => Ok(elem.GetElementsByTagName(DOMString::from(selector))),
None => Err(()),
})
.map(|nodes| {
nodes
.elements_iter()
.map(|x| x.upcast::<Node>().unique_id())
.collect::<Vec<String>>()
});
reply.send(node_ids).unwrap();
reply
.send(
find_node_by_unique_id(documents, pipeline, element_id).and_then(|node| match node
.downcast::<Element>(
) {
Some(element) => Ok(element
.GetElementsByTagName(DOMString::from(selector))
.elements_iter()
.map(|x| x.upcast::<Node>().unique_id())
.collect::<Vec<String>>()),
None => Err(ErrorStatus::UnknownError),
}),
)
.unwrap();
}
pub fn handle_focus_element(
documents: &Documents,
pipeline: PipelineId,
element_id: String,
reply: IpcSender<Result<(), ()>>,
reply: IpcSender<Result<(), ErrorStatus>>,
) {
reply
.send(
match find_node_by_unique_id(documents, pipeline, element_id) {
Some(ref node) => {
match node.downcast::<HTMLElement>() {
Some(ref elem) => {
// Need a way to find if this actually succeeded
elem.Focus();
Ok(())
},
None => Err(()),
}
},
None => Err(()),
},
find_node_by_unique_id(documents, pipeline, element_id).and_then(|node| {
match node.downcast::<HTMLElement>() {
Some(element) => {
// Need a way to find if this actually succeeded
element.Focus();
Ok(())
},
None => Err(ErrorStatus::UnknownError),
}
}),
)
.unwrap();
}
@ -515,8 +560,8 @@ pub fn handle_get_active_element(
.send(
documents
.find_document(pipeline)
.and_then(|doc| doc.GetActiveElement())
.map(|elem| elem.upcast::<Node>().unique_id()),
.and_then(|document| document.GetActiveElement())
.map(|element| element.upcast::<Node>().unique_id()),
)
.unwrap();
}
@ -524,25 +569,28 @@ pub fn handle_get_active_element(
pub fn handle_get_page_source(
documents: &Documents,
pipeline: PipelineId,
reply: IpcSender<Result<String, ()>>,
reply: IpcSender<Result<String, ErrorStatus>>,
) {
reply
.send(documents.find_document(pipeline).ok_or(()).and_then(|doc| {
match doc.GetDocumentElement() {
Some(elem) => match elem.GetOuterHTML() {
Ok(source) => Ok(source.to_string()),
Err(_) => {
match XMLSerializer::new(doc.window())
.SerializeToString(elem.upcast::<Node>())
{
Ok(source) => Ok(source.to_string()),
Err(_) => Err(()),
}
.send(
documents
.find_document(pipeline)
.ok_or(ErrorStatus::UnknownError)
.and_then(|document| match document.GetDocumentElement() {
Some(element) => match element.GetOuterHTML() {
Ok(source) => Ok(source.to_string()),
Err(_) => {
match XMLSerializer::new(document.window())
.SerializeToString(element.upcast::<Node>())
{
Ok(source) => Ok(source.to_string()),
Err(_) => Err(ErrorStatus::UnknownError),
}
},
},
},
None => Err(()),
}
}))
None => Err(ErrorStatus::UnknownError),
}),
)
.unwrap();
}
@ -551,21 +599,24 @@ pub fn handle_get_cookies(
pipeline: PipelineId,
reply: IpcSender<Vec<Serde<Cookie<'static>>>>,
) {
// TODO: Return an error if the pipeline doesn't exist?
let cookies = match documents.find_document(pipeline) {
None => Vec::new(),
Some(document) => {
let url = document.url();
let (sender, receiver) = ipc::channel().unwrap();
let _ = document
.window()
.upcast::<GlobalScope>()
.resource_threads()
.send(GetCookiesDataForUrl(url, sender, NonHTTP));
receiver.recv().unwrap()
},
};
reply.send(cookies).unwrap();
reply
.send(
// TODO: Return an error if the pipeline doesn't exist
match documents.find_document(pipeline) {
Some(document) => {
let url = document.url();
let (sender, receiver) = ipc::channel().unwrap();
let _ = document
.window()
.upcast::<GlobalScope>()
.resource_threads()
.send(GetCookiesDataForUrl(url, sender, NonHTTP));
receiver.recv().unwrap()
},
None => Vec::new(),
},
)
.unwrap();
}
// https://w3c.github.io/webdriver/webdriver-spec.html#get-cookie
@ -575,22 +626,27 @@ pub fn handle_get_cookie(
name: String,
reply: IpcSender<Vec<Serde<Cookie<'static>>>>,
) {
// TODO: Return an error if the pipeline doesn't exist?
let cookies = match documents.find_document(pipeline) {
None => Vec::new(),
Some(document) => {
let url = document.url();
let (sender, receiver) = ipc::channel().unwrap();
let _ = document
.window()
.upcast::<GlobalScope>()
.resource_threads()
.send(GetCookiesDataForUrl(url, sender, NonHTTP));
receiver.recv().unwrap()
},
};
reply
.send(cookies.into_iter().filter(|c| c.name() == &*name).collect())
.send(
// TODO: Return an error if the pipeline doesn't exist
match documents.find_document(pipeline) {
Some(document) => {
let url = document.url();
let (sender, receiver) = ipc::channel().unwrap();
let _ = document
.window()
.upcast::<GlobalScope>()
.resource_threads()
.send(GetCookiesDataForUrl(url, sender, NonHTTP));
let cookies = receiver.recv().unwrap();
cookies
.into_iter()
.filter(|cookie| cookie.name() == &*name)
.collect()
},
None => Vec::new(),
},
)
.unwrap();
}
@ -601,7 +657,7 @@ pub fn handle_add_cookie(
cookie: Cookie<'static>,
reply: IpcSender<Result<(), WebDriverCookieError>>,
) {
// TODO: Return a different error if the pipeline doesn't exist?
// TODO: Return a different error if the pipeline doesn't exist
let document = match documents.find_document(pipeline) {
Some(document) => document,
None => {
@ -645,12 +701,12 @@ pub fn handle_add_cookie(
pub fn handle_delete_cookies(
documents: &Documents,
pipeline: PipelineId,
reply: IpcSender<Result<(), ()>>,
reply: IpcSender<Result<(), ErrorStatus>>,
) {
let document = match documents.find_document(pipeline) {
Some(document) => document,
None => {
return reply.send(Err(())).unwrap();
return reply.send(Err(ErrorStatus::UnknownError)).unwrap();
},
};
let url = document.url();
@ -660,62 +716,62 @@ pub fn handle_delete_cookies(
.resource_threads()
.send(DeleteCookies(url))
.unwrap();
let _ = reply.send(Ok(()));
reply.send(Ok(())).unwrap();
}
pub fn handle_get_title(documents: &Documents, pipeline: PipelineId, reply: IpcSender<String>) {
// TODO: Return an error if the pipeline doesn't exist.
let title = documents
.find_document(pipeline)
.map(|doc| String::from(doc.Title()))
.unwrap_or_default();
reply.send(title).unwrap();
reply
.send(
// TODO: Return an error if the pipeline doesn't exist
documents
.find_document(pipeline)
.map(|document| String::from(document.Title()))
.unwrap_or_default(),
)
.unwrap();
}
pub fn handle_get_rect(
documents: &Documents,
pipeline: PipelineId,
element_id: String,
reply: IpcSender<Result<Rect<f64>, ()>>,
reply: IpcSender<Result<Rect<f64>, ErrorStatus>>,
) {
reply
.send(
match find_node_by_unique_id(documents, pipeline, element_id) {
Some(elem) => {
// https://w3c.github.io/webdriver/webdriver-spec.html#dfn-calculate-the-absolute-position
match elem.downcast::<HTMLElement>() {
Some(html_elem) => {
// Step 1
let mut x = 0;
let mut y = 0;
find_node_by_unique_id(documents, pipeline, element_id).and_then(|node| {
// https://w3c.github.io/webdriver/webdriver-spec.html#dfn-calculate-the-absolute-position
match node.downcast::<HTMLElement>() {
Some(html_element) => {
// Step 1
let mut x = 0;
let mut y = 0;
let mut offset_parent = html_elem.GetOffsetParent();
let mut offset_parent = html_element.GetOffsetParent();
// Step 2
while let Some(element) = offset_parent {
offset_parent = match element.downcast::<HTMLElement>() {
Some(elem) => {
x += elem.OffsetLeft();
y += elem.OffsetTop();
elem.GetOffsetParent()
},
None => None,
};
}
// Step 3
Ok(Rect::new(
Point2D::new(x as f64, y as f64),
Size2D::new(
html_elem.OffsetWidth() as f64,
html_elem.OffsetHeight() as f64,
),
))
},
None => Err(()),
}
},
None => Err(()),
},
// Step 2
while let Some(element) = offset_parent {
offset_parent = match element.downcast::<HTMLElement>() {
Some(elem) => {
x += elem.OffsetLeft();
y += elem.OffsetTop();
elem.GetOffsetParent()
},
None => None,
};
}
// Step 3
Ok(Rect::new(
Point2D::new(x as f64, y as f64),
Size2D::new(
html_element.OffsetWidth() as f64,
html_element.OffsetHeight() as f64,
),
))
},
None => Err(ErrorStatus::UnknownError),
}
}),
)
.unwrap();
}
@ -724,13 +780,13 @@ pub fn handle_get_text(
documents: &Documents,
pipeline: PipelineId,
node_id: String,
reply: IpcSender<Result<String, ()>>,
reply: IpcSender<Result<String, ErrorStatus>>,
) {
reply
.send(match find_node_by_unique_id(documents, pipeline, node_id) {
Some(ref node) => Ok(node.GetTextContent().map_or("".to_owned(), String::from)),
None => Err(()),
})
.send(
find_node_by_unique_id(documents, pipeline, node_id)
.and_then(|node| Ok(node.GetTextContent().map_or("".to_owned(), String::from))),
)
.unwrap();
}
@ -738,13 +794,13 @@ pub fn handle_get_name(
documents: &Documents,
pipeline: PipelineId,
node_id: String,
reply: IpcSender<Result<String, ()>>,
reply: IpcSender<Result<String, ErrorStatus>>,
) {
reply
.send(match find_node_by_unique_id(documents, pipeline, node_id) {
Some(node) => Ok(String::from(node.downcast::<Element>().unwrap().TagName())),
None => Err(()),
})
.send(
find_node_by_unique_id(documents, pipeline, node_id)
.and_then(|node| Ok(String::from(node.downcast::<Element>().unwrap().TagName()))),
)
.unwrap();
}
@ -753,17 +809,18 @@ pub fn handle_get_attribute(
pipeline: PipelineId,
node_id: String,
name: String,
reply: IpcSender<Result<Option<String>, ()>>,
reply: IpcSender<Result<Option<String>, ErrorStatus>>,
) {
reply
.send(match find_node_by_unique_id(documents, pipeline, node_id) {
Some(node) => Ok(node
.downcast::<Element>()
.unwrap()
.GetAttribute(DOMString::from(name))
.map(String::from)),
None => Err(()),
})
.send(
find_node_by_unique_id(documents, pipeline, node_id).and_then(|node| {
Ok(node
.downcast::<Element>()
.unwrap()
.GetAttribute(DOMString::from(name))
.map(String::from))
}),
)
.unwrap();
}
@ -773,11 +830,11 @@ pub fn handle_get_property(
pipeline: PipelineId,
node_id: String,
name: String,
reply: IpcSender<Result<WebDriverJSValue, ()>>,
reply: IpcSender<Result<WebDriverJSValue, ErrorStatus>>,
) {
reply
.send(match find_node_by_unique_id(documents, pipeline, node_id) {
Some(node) => {
.send(
find_node_by_unique_id(documents, pipeline, node_id).and_then(|node| {
let cx = documents.find_document(pipeline).unwrap().window().get_cx();
rooted!(in(*cx) let mut property = UndefinedValue());
@ -800,9 +857,8 @@ pub fn handle_get_property(
Ok(WebDriverJSValue::Undefined)
},
}
},
None => Err(()),
})
}),
)
.unwrap();
}
@ -811,48 +867,49 @@ pub fn handle_get_css(
pipeline: PipelineId,
node_id: String,
name: String,
reply: IpcSender<Result<String, ()>>,
reply: IpcSender<Result<String, ErrorStatus>>,
) {
reply
.send(match find_node_by_unique_id(documents, pipeline, node_id) {
Some(node) => {
.send(
find_node_by_unique_id(documents, pipeline, node_id).and_then(|node| {
let window = window_from_node(&*node);
let elem = node.downcast::<Element>().unwrap();
let element = node.downcast::<Element>().unwrap();
Ok(String::from(
window
.GetComputedStyle(&elem, None)
.GetComputedStyle(&element, None)
.GetPropertyValue(DOMString::from(name)),
))
},
None => Err(()),
})
}),
)
.unwrap();
}
pub fn handle_get_url(documents: &Documents, pipeline: PipelineId, reply: IpcSender<ServoUrl>) {
// TODO: Return an error if the pipeline doesn't exist.
let url = documents
.find_document(pipeline)
.map(|document| document.url())
.unwrap_or_else(|| ServoUrl::parse("about:blank").expect("infallible"));
reply.send(url).unwrap();
reply
.send(
// TODO: Return an error if the pipeline doesn't exist.
documents
.find_document(pipeline)
.map(|document| document.url())
.unwrap_or_else(|| ServoUrl::parse("about:blank").expect("infallible")),
)
.unwrap();
}
pub fn handle_is_enabled(
documents: &Documents,
pipeline: PipelineId,
element_id: String,
reply: IpcSender<Result<bool, ()>>,
reply: IpcSender<Result<bool, ErrorStatus>>,
) {
reply
.send(
match find_node_by_unique_id(&documents, pipeline, element_id) {
Some(ref node) => match node.downcast::<Element>() {
Some(elem) => Ok(elem.enabled_state()),
None => Err(()),
},
None => Err(()),
},
find_node_by_unique_id(&documents, pipeline, element_id).and_then(|node| match node
.downcast::<Element>(
) {
Some(element) => Ok(element.enabled_state()),
None => Err(ErrorStatus::UnknownError),
}),
)
.unwrap();
}
@ -861,24 +918,21 @@ pub fn handle_is_selected(
documents: &Documents,
pipeline: PipelineId,
element_id: String,
reply: IpcSender<Result<bool, ()>>,
reply: IpcSender<Result<bool, ErrorStatus>>,
) {
reply
.send(
match find_node_by_unique_id(documents, pipeline, element_id) {
Some(ref node) => {
if let Some(input_element) = node.downcast::<HTMLInputElement>() {
Ok(input_element.Checked())
} else if let Some(option_element) = node.downcast::<HTMLOptionElement>() {
Ok(option_element.Selected())
} else if node.is::<HTMLElement>() {
Ok(false) // regular elements are not selectable
} else {
Err(())
}
},
None => Err(()),
},
find_node_by_unique_id(documents, pipeline, element_id).and_then(|node| {
if let Some(input_element) = node.downcast::<HTMLInputElement>() {
Ok(input_element.Checked())
} else if let Some(option_element) = node.downcast::<HTMLOptionElement>() {
Ok(option_element.Selected())
} else if node.is::<HTMLElement>() {
Ok(false) // regular elements are not selectable
} else {
Err(ErrorStatus::UnknownError)
}
}),
)
.unwrap();
}

View file

@ -11,6 +11,7 @@ use ipc_channel::ipc::IpcSender;
use msg::constellation_msg::BrowsingContextId;
use servo_url::ServoUrl;
use webdriver::common::WebElement;
use webdriver::error::ErrorStatus;
#[derive(Debug, Deserialize, Serialize)]
pub enum WebDriverScriptCommand {
@ -22,36 +23,65 @@ pub enum WebDriverScriptCommand {
Cookie<'static>,
IpcSender<Result<(), WebDriverCookieError>>,
),
DeleteCookies(IpcSender<Result<(), ()>>),
DeleteCookies(IpcSender<Result<(), ErrorStatus>>),
ExecuteScript(String, IpcSender<WebDriverJSResult>),
ExecuteAsyncScript(String, IpcSender<WebDriverJSResult>),
FindElementCSS(String, IpcSender<Result<Option<String>, ()>>),
FindElementLinkText(String, bool, IpcSender<Result<Option<String>, ()>>),
FindElementTagName(String, IpcSender<Result<Option<String>, ()>>),
FindElementsCSS(String, IpcSender<Result<Vec<String>, ()>>),
FindElementsLinkText(String, bool, IpcSender<Result<Vec<String>, ()>>),
FindElementsTagName(String, IpcSender<Result<Vec<String>, ()>>),
FindElementElementCSS(String, String, IpcSender<Result<Option<String>, ()>>),
FindElementElementLinkText(String, String, bool, IpcSender<Result<Option<String>, ()>>),
FindElementElementTagName(String, String, IpcSender<Result<Option<String>, ()>>),
FindElementElementsCSS(String, String, IpcSender<Result<Vec<String>, ()>>),
FindElementElementsLinkText(String, String, bool, IpcSender<Result<Vec<String>, ()>>),
FindElementElementsTagName(String, String, IpcSender<Result<Vec<String>, ()>>),
FocusElement(String, IpcSender<Result<(), ()>>),
FindElementCSS(String, IpcSender<Result<Option<String>, ErrorStatus>>),
FindElementLinkText(String, bool, IpcSender<Result<Option<String>, ErrorStatus>>),
FindElementTagName(String, IpcSender<Result<Option<String>, ErrorStatus>>),
FindElementsCSS(String, IpcSender<Result<Vec<String>, ErrorStatus>>),
FindElementsLinkText(String, bool, IpcSender<Result<Vec<String>, ErrorStatus>>),
FindElementsTagName(String, IpcSender<Result<Vec<String>, ErrorStatus>>),
FindElementElementCSS(
String,
String,
IpcSender<Result<Option<String>, ErrorStatus>>,
),
FindElementElementLinkText(
String,
String,
bool,
IpcSender<Result<Option<String>, ErrorStatus>>,
),
FindElementElementTagName(
String,
String,
IpcSender<Result<Option<String>, ErrorStatus>>,
),
FindElementElementsCSS(String, String, IpcSender<Result<Vec<String>, ErrorStatus>>),
FindElementElementsLinkText(
String,
String,
bool,
IpcSender<Result<Vec<String>, ErrorStatus>>,
),
FindElementElementsTagName(String, String, IpcSender<Result<Vec<String>, ErrorStatus>>),
FocusElement(String, IpcSender<Result<(), ErrorStatus>>),
GetActiveElement(IpcSender<Option<String>>),
GetCookie(String, IpcSender<Vec<Serde<Cookie<'static>>>>),
GetCookies(IpcSender<Vec<Serde<Cookie<'static>>>>),
GetElementAttribute(String, String, IpcSender<Result<Option<String>, ()>>),
GetElementProperty(String, String, IpcSender<Result<WebDriverJSValue, ()>>),
GetElementCSS(String, String, IpcSender<Result<String, ()>>),
GetElementRect(String, IpcSender<Result<Rect<f64>, ()>>),
GetElementTagName(String, IpcSender<Result<String, ()>>),
GetElementText(String, IpcSender<Result<String, ()>>),
GetBrowsingContextId(WebDriverFrameId, IpcSender<Result<BrowsingContextId, ()>>),
GetElementAttribute(
String,
String,
IpcSender<Result<Option<String>, ErrorStatus>>,
),
GetElementProperty(
String,
String,
IpcSender<Result<WebDriverJSValue, ErrorStatus>>,
),
GetElementCSS(String, String, IpcSender<Result<String, ErrorStatus>>),
GetElementRect(String, IpcSender<Result<Rect<f64>, ErrorStatus>>),
GetElementTagName(String, IpcSender<Result<String, ErrorStatus>>),
GetElementText(String, IpcSender<Result<String, ErrorStatus>>),
GetBrowsingContextId(
WebDriverFrameId,
IpcSender<Result<BrowsingContextId, ErrorStatus>>,
),
GetUrl(IpcSender<ServoUrl>),
GetPageSource(IpcSender<Result<String, ()>>),
IsEnabled(String, IpcSender<Result<bool, ()>>),
IsSelected(String, IpcSender<Result<bool, ()>>),
GetPageSource(IpcSender<Result<String, ErrorStatus>>),
IsEnabled(String, IpcSender<Result<bool, ErrorStatus>>),
IsSelected(String, IpcSender<Result<bool, ErrorStatus>>),
GetTitle(IpcSender<String>),
}

View file

@ -735,10 +735,7 @@ impl Handler {
Ok(is_enabled) => Ok(WebDriverResponse::Generic(ValueResponse(
serde_json::to_value(is_enabled)?,
))),
Err(_) => Err(WebDriverError::new(
ErrorStatus::StaleElementReference,
"Element not found",
)),
Err(error) => Err(WebDriverError::new(error, "")),
}
}
@ -754,10 +751,7 @@ impl Handler {
Ok(is_selected) => Ok(WebDriverResponse::Generic(ValueResponse(
serde_json::to_value(is_selected)?,
))),
Err(_) => Err(WebDriverError::new(
ErrorStatus::StaleElementReference,
"Element not found",
)),
Err(error) => Err(WebDriverError::new(error, "")),
}
}
@ -860,10 +854,7 @@ impl Handler {
)?;
Ok(WebDriverResponse::Generic(ValueResponse(value_resp)))
},
Err(_) => Err(WebDriverError::new(
ErrorStatus::InvalidSelector,
"Invalid selector",
)),
Err(error) => Err(WebDriverError::new(error, "")),
}
}
@ -922,13 +913,13 @@ impl Handler {
let cmd = WebDriverScriptCommand::GetBrowsingContextId(frame_id, sender);
self.browsing_context_script_command(cmd)?;
let browsing_context_id = receiver.recv().unwrap().or(Err(WebDriverError::new(
ErrorStatus::NoSuchFrame,
"Frame does not exist",
)))?;
self.session_mut()?.browsing_context_id = browsing_context_id;
Ok(WebDriverResponse::Void)
match receiver.recv().unwrap() {
Ok(browsing_context_id) => {
self.session_mut()?.browsing_context_id = browsing_context_id;
Ok(WebDriverResponse::Void)
},
Err(error) => Err(WebDriverError::new(error, "")),
}
}
// https://w3c.github.io/webdriver/#find-elements
@ -974,10 +965,7 @@ impl Handler {
serde_json::to_value(resp_value)?,
)))
},
Err(_) => Err(WebDriverError::new(
ErrorStatus::InvalidSelector,
"Invalid selector",
)),
Err(error) => Err(WebDriverError::new(error, "")),
}
}
@ -1030,10 +1018,7 @@ impl Handler {
)?;
Ok(WebDriverResponse::Generic(ValueResponse(value_resp)))
},
Err(_) => Err(WebDriverError::new(
ErrorStatus::InvalidSelector,
"Invalid selector",
)),
Err(error) => Err(WebDriverError::new(error, "")),
}
}
@ -1089,10 +1074,7 @@ impl Handler {
serde_json::to_value(resp_value)?,
)))
},
Err(_) => Err(WebDriverError::new(
ErrorStatus::InvalidSelector,
"Invalid selector",
)),
Err(error) => Err(WebDriverError::new(error, "")),
}
}
@ -1111,10 +1093,7 @@ impl Handler {
};
Ok(WebDriverResponse::ElementRect(response))
},
Err(_) => Err(WebDriverError::new(
ErrorStatus::StaleElementReference,
"Unable to find element in document",
)),
Err(error) => Err(WebDriverError::new(error, "")),
}
}
@ -1126,10 +1105,7 @@ impl Handler {
Ok(value) => Ok(WebDriverResponse::Generic(ValueResponse(
serde_json::to_value(value)?,
))),
Err(_) => Err(WebDriverError::new(
ErrorStatus::StaleElementReference,
"Unable to find element in document",
)),
Err(error) => Err(WebDriverError::new(error, "")),
}
}
@ -1154,10 +1130,7 @@ impl Handler {
Ok(value) => Ok(WebDriverResponse::Generic(ValueResponse(
serde_json::to_value(value)?,
))),
Err(_) => Err(WebDriverError::new(
ErrorStatus::StaleElementReference,
"Unable to find element in document",
)),
Err(error) => Err(WebDriverError::new(error, "")),
}
}
@ -1177,10 +1150,7 @@ impl Handler {
Ok(value) => Ok(WebDriverResponse::Generic(ValueResponse(
serde_json::to_value(value)?,
))),
Err(_) => Err(WebDriverError::new(
ErrorStatus::StaleElementReference,
"Unable to find element in document",
)),
Err(error) => Err(WebDriverError::new(error, "")),
}
}
@ -1202,10 +1172,7 @@ impl Handler {
Ok(value) => Ok(WebDriverResponse::Generic(ValueResponse(
serde_json::to_value(SendableWebDriverJSValue(value))?,
))),
Err(_) => Err(WebDriverError::new(
ErrorStatus::StaleElementReference,
"Unable to find element in document",
)),
Err(error) => Err(WebDriverError::new(error, "")),
}
}
@ -1222,10 +1189,7 @@ impl Handler {
Ok(value) => Ok(WebDriverResponse::Generic(ValueResponse(
serde_json::to_value(value)?,
))),
Err(_) => Err(WebDriverError::new(
ErrorStatus::StaleElementReference,
"Unable to find element in document",
)),
Err(error) => Err(WebDriverError::new(error, "")),
}
}
@ -1295,10 +1259,7 @@ impl Handler {
self.browsing_context_script_command(cmd)?;
match receiver.recv().unwrap() {
Ok(_) => Ok(WebDriverResponse::Void),
Err(_) => Err(WebDriverError::new(
ErrorStatus::NoSuchWindow,
"No such window found.",
)),
Err(error) => Err(WebDriverError::new(error, "")),
}
}
@ -1355,10 +1316,7 @@ impl Handler {
Ok(source) => Ok(WebDriverResponse::Generic(ValueResponse(
serde_json::to_value(source)?,
))),
Err(_) => Err(WebDriverError::new(
ErrorStatus::UnknownError,
"Unknown error",
)),
Err(error) => Err(WebDriverError::new(error, "")),
}
}
@ -1469,12 +1427,10 @@ impl Handler {
.unwrap();
// TODO: distinguish the not found and not focusable cases
receiver.recv().unwrap().or_else(|_| {
Err(WebDriverError::new(
ErrorStatus::StaleElementReference,
"Element not found or not focusable",
))
})?;
receiver
.recv()
.unwrap()
.or_else(|error| Err(WebDriverError::new(error, "")))?;
let input_events = send_keys(&keys.text);

View file

@ -2,9 +2,6 @@
[test_boolean_attribute[video-attrs17\]]
expected: FAIL
[test_element_not_found]
expected: FAIL
[test_global_boolean_attributes]
expected: FAIL

View file

@ -1,7 +1,4 @@
[get.py]
[test_element_not_found]
expected: FAIL
[test_no_browsing_context]
expected: ERROR

View file

@ -5,9 +5,6 @@
[test_idl_attribute]
expected: FAIL
[test_element_not_found]
expected: FAIL
[test_primitives_set_by_execute_script[42-42\]]
expected: FAIL

View file

@ -1,7 +1,4 @@
[get.py]
[test_element_not_found]
expected: FAIL
[test_no_browsing_context]
expected: ERROR

View file

@ -1,7 +1,4 @@
[get.py]
[test_element_not_found]
expected: FAIL
[test_no_browsing_context]
expected: ERROR

View file

@ -1,7 +1,4 @@
[get.py]
[test_getting_text_of_a_non_existant_element_is_an_error]
expected: FAIL
[test_no_browsing_context]
expected: ERROR

View file

@ -8,18 +8,12 @@
[test_frame_id_webelement_frame[0-foo\]]
expected: FAIL
[test_frame_id_webelement_no_element_reference]
expected: FAIL
[test_frame_id_number_index_out_of_bounds]
expected: FAIL
[test_frame_id_number_index[0-foo\]]
expected: FAIL
[test_frame_id_webelement_stale_reference]
expected: FAIL
[test_no_browsing_context]
expected: ERROR