From 0e8c63fadf22f4a1bcdbe5a1007b37545b2a0795 Mon Sep 17 00:00:00 2001 From: James Graham Date: Sat, 21 Nov 2015 10:40:50 +0501 Subject: [PATCH] servo: Merge #8621 - Add support for getting an element's computed style through WebDriver (from jgraham:webdriver_css); r=jdm Source-Repo: https://github.com/servo/servo Source-Revision: 585d2d5c8c4e59be20ab9660dab75fc55438e247 --- servo/components/msg/webdriver_msg.rs | 1 + servo/components/script/script_task.rs | 2 ++ servo/components/script/webdriver_handlers.rs | 17 +++++++++++++++++ servo/components/webdriver_server/lib.rs | 16 ++++++++++++++++ 4 files changed, 36 insertions(+) diff --git a/servo/components/msg/webdriver_msg.rs b/servo/components/msg/webdriver_msg.rs index 6a537ac70f66..658df1e79e1f 100644 --- a/servo/components/msg/webdriver_msg.rs +++ b/servo/components/msg/webdriver_msg.rs @@ -16,6 +16,7 @@ pub enum WebDriverScriptCommand { FocusElement(String, IpcSender>), GetActiveElement(IpcSender>), GetElementAttribute(String, String, IpcSender, ()>>), + GetElementCSS(String, String, IpcSender>), GetElementTagName(String, IpcSender>), GetElementText(String, IpcSender>), GetFrameId(WebDriverFrameId, IpcSender, ()>>), diff --git a/servo/components/script/script_task.rs b/servo/components/script/script_task.rs index 043c0574be13..86e1493409c5 100644 --- a/servo/components/script/script_task.rs +++ b/servo/components/script/script_task.rs @@ -1108,6 +1108,8 @@ impl ScriptTask { webdriver_handlers::handle_get_name(&page, pipeline_id, node_id, reply), WebDriverScriptCommand::GetElementAttribute(node_id, name, reply) => 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::GetElementText(node_id, reply) => webdriver_handlers::handle_get_text(&page, pipeline_id, node_id, reply), WebDriverScriptCommand::GetFrameId(frame_id, reply) => diff --git a/servo/components/script/webdriver_handlers.rs b/servo/components/script/webdriver_handlers.rs index 0bf2ede1aeb6..818ad20abf9e 100644 --- a/servo/components/script/webdriver_handlers.rs +++ b/servo/components/script/webdriver_handlers.rs @@ -2,12 +2,14 @@ * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ +use dom::bindings::codegen::Bindings::CSSStyleDeclarationBinding::CSSStyleDeclarationMethods; use dom::bindings::codegen::Bindings::DocumentBinding::DocumentMethods; use dom::bindings::codegen::Bindings::ElementBinding::ElementMethods; use dom::bindings::codegen::Bindings::HTMLElementBinding::HTMLElementMethods; use dom::bindings::codegen::Bindings::HTMLIFrameElementBinding::HTMLIFrameElementMethods; use dom::bindings::codegen::Bindings::NodeBinding::NodeMethods; use dom::bindings::codegen::Bindings::NodeListBinding::NodeListMethods; +use dom::bindings::codegen::Bindings::WindowBinding::WindowMethods; use dom::bindings::conversions::{FromJSValConvertible, StringificationBehavior}; use dom::bindings::inheritance::Castable; use dom::bindings::js::Root; @@ -217,6 +219,21 @@ pub fn handle_get_attribute(page: &Rc, }).unwrap(); } +pub fn handle_get_css(page: &Rc, + pipeline: PipelineId, + node_id: String, + name: String, + reply: IpcSender>) { + reply.send(match find_node_by_unique_id(&*page, pipeline, node_id) { + Some(node) => { + let window = page.window(); + let elem = node.downcast::().unwrap(); + Ok(String::from( + window.GetComputedStyle(&elem, None).GetPropertyValue(DOMString::from(name)))) + }, + None => Err(()) + }).unwrap(); +} pub fn handle_get_url(page: &Rc, _pipeline: PipelineId, diff --git a/servo/components/webdriver_server/lib.rs b/servo/components/webdriver_server/lib.rs index 2abdb5621148..9bad724d164a 100644 --- a/servo/components/webdriver_server/lib.rs +++ b/servo/components/webdriver_server/lib.rs @@ -539,6 +539,20 @@ impl Handler { } } + fn handle_element_css(&self, element: &WebElement, name: &String) -> WebDriverResult { + let pipeline_id = try!(self.frame_pipeline()); + + let (sender, receiver) = ipc::channel().unwrap(); + let cmd = WebDriverScriptCommand::GetElementCSS(element.id.clone(), name.clone(), sender); + let cmd_msg = WebDriverCommandMsg::ScriptCommand(pipeline_id, cmd); + self.constellation_chan.send(ConstellationMsg::WebDriverCommand(cmd_msg)).unwrap(); + match receiver.recv().unwrap() { + Ok(value) => Ok(WebDriverResponse::Generic(ValueResponse::new(value.to_json()))), + Err(_) => Err(WebDriverError::new(ErrorStatus::StaleElementReference, + "Unable to find element in document")) + } + } + fn handle_set_timeouts(&mut self, parameters: &TimeoutsParameters) -> WebDriverResult { //TODO: this conversion is crazy, spec should limit these to u32 and check upstream let value = parameters.ms as u32; @@ -731,6 +745,8 @@ impl WebDriverHandler for Handler { WebDriverCommand::GetElementTagName(ref element) => self.handle_element_tag_name(element), WebDriverCommand::GetElementAttribute(ref element, ref name) => self.handle_element_attribute(element, name), + WebDriverCommand::GetCSSValue(ref element, ref name) => + self.handle_element_css(element, name), WebDriverCommand::ExecuteScript(ref x) => self.handle_execute_script(x), WebDriverCommand::ExecuteAsyncScript(ref x) => self.handle_execute_async_script(x), WebDriverCommand::ElementSendKeys(ref element, ref keys) =>