Implement GetElementRect webdriver command

Implement the webdriver Get Element Rect command
This commit is contained in:
Daniel Robertson 2016-02-20 19:45:10 +00:00
parent 875f1e92cc
commit 83b2388ef4
4 changed files with 67 additions and 1 deletions

View file

@ -1246,6 +1246,8 @@ impl ScriptThread {
webdriver_handlers::handle_get_attribute(&page, pipeline_id, node_id, name, reply),
WebDriverScriptCommand::GetElementCSS(node_id, name, reply) =>
webdriver_handlers::handle_get_css(&page, pipeline_id, node_id, name, reply),
WebDriverScriptCommand::GetElementRect(node_id, reply) =>
webdriver_handlers::handle_get_rect(&page, pipeline_id, node_id, reply),
WebDriverScriptCommand::GetElementText(node_id, reply) =>
webdriver_handlers::handle_get_text(&page, pipeline_id, node_id, reply),
WebDriverScriptCommand::GetFrameId(frame_id, reply) =>

View file

@ -22,6 +22,9 @@ use dom::htmlinputelement::HTMLInputElement;
use dom::htmloptionelement::HTMLOptionElement;
use dom::node::Node;
use dom::window::ScriptHelpers;
use euclid::point::Point2D;
use euclid::rect::Rect;
use euclid::size::Size2D;
use ipc_channel::ipc::IpcSender;
use js::jsapi::JSContext;
use js::jsapi::{HandleValue, RootedValue};
@ -185,6 +188,44 @@ pub fn handle_get_title(page: &Rc<Page>, _pipeline: PipelineId, reply: IpcSender
reply.send(String::from(page.document().Title())).unwrap();
}
pub fn handle_get_rect(page: &Rc<Page>,
pipeline: PipelineId,
element_id: String,
reply: IpcSender<Result<Rect<f64>, ()>>) {
reply.send(match find_node_by_unique_id(&*page, 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;
let mut offset_parent = html_elem.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(())
}).unwrap();
}
pub fn handle_get_text(page: &Rc<Page>,
pipeline: PipelineId,
node_id: String,