diff --git a/components/script/dom/webidls/Window.webidl b/components/script/dom/webidls/Window.webidl index 2af5786b061..ae8758e3c56 100644 --- a/components/script/dom/webidls/Window.webidl +++ b/components/script/dom/webidls/Window.webidl @@ -56,9 +56,6 @@ //void postMessage(any message, DOMString targetOrigin, optional sequence transfer); - // Shouldn't be public, but just to make things work for now - void webdriverCallback(optional any result); - // also has obsolete members }; Window implements GlobalEventHandlers; @@ -131,6 +128,13 @@ partial interface Window { }; Window implements OnErrorEventHandlerForWindow; +// WebDriver extensions +partial interface Window { + // Shouldn't be public, but just to make things work for now + void webdriverCallback(optional any result); + void webdriverTimeout(); +}; + // https://html.spec.whatwg.org/multipage/#dom-sessionstorage [NoInterfaceObject] interface WindowSessionStorage { diff --git a/components/script/dom/window.rs b/components/script/dom/window.rs index 1cf2de74691..2621a9498a3 100644 --- a/components/script/dom/window.rs +++ b/components/script/dom/window.rs @@ -38,7 +38,7 @@ use timers::{IsInterval, TimerId, TimerManager, TimerCallback}; use webdriver_handlers::jsval_to_webdriver; use devtools_traits::{DevtoolsControlChan, TimelineMarker, TimelineMarkerType, TracingMetadata}; -use webdriver_traits::EvaluateJSReply; +use webdriver_traits::{WebDriverJSError, WebDriverJSResult}; use msg::compositor_msg::ScriptListener; use msg::constellation_msg::{LoadData, PipelineId, SubpageId, ConstellationChan, WindowSizeData, WorkerId}; use net_traits::ResourceTask; @@ -170,7 +170,7 @@ pub struct Window { pending_reflow_count: Cell, /// A channel for communicating results of async scripts back to the webdriver server - webdriver_script_chan: RefCell>>> + webdriver_script_chan: RefCell>> } impl Window { @@ -491,13 +491,17 @@ impl<'a> WindowMethods for JSRef<'a, Window> { fn WebdriverCallback(self, cx: *mut JSContext, val: JSVal) { let rv = jsval_to_webdriver(cx, val); - { - let opt_chan = self.webdriver_script_chan.borrow(); - if let Some(ref chan) = *opt_chan { - chan.send(rv).unwrap(); - } + let opt_chan = self.webdriver_script_chan.borrow_mut().take(); + if let Some(chan) = opt_chan { + chan.send(rv).unwrap(); + } + } + + fn WebdriverTimeout(self) { + let opt_chan = self.webdriver_script_chan.borrow_mut().take(); + if let Some(chan) = opt_chan { + chan.send(Err(WebDriverJSError::Timeout)).unwrap(); } - self.set_webdriver_script_chan(None); } } @@ -539,7 +543,7 @@ pub trait WindowHelpers { fn emit_timeline_marker(self, marker: TimelineMarker); fn set_devtools_timeline_marker(self, marker: TimelineMarkerType, reply: Sender); fn drop_devtools_timeline_markers(self); - fn set_webdriver_script_chan(self, chan: Option>>); + fn set_webdriver_script_chan(self, chan: Option>); } pub trait ScriptHelpers { @@ -898,7 +902,7 @@ impl<'a> WindowHelpers for JSRef<'a, Window> { *self.devtools_marker_sender.borrow_mut() = None; } - fn set_webdriver_script_chan(self, chan: Option>>) { + fn set_webdriver_script_chan(self, chan: Option>) { *self.webdriver_script_chan.borrow_mut() = chan; } } diff --git a/components/script/webdriver_handlers.rs b/components/script/webdriver_handlers.rs index 5ab1aa50a21..dc04e2346a0 100644 --- a/components/script/webdriver_handlers.rs +++ b/components/script/webdriver_handlers.rs @@ -2,7 +2,7 @@ * 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 webdriver_traits::{EvaluateJSReply}; +use webdriver_traits::{WebDriverJSValue, WebDriverJSError, WebDriverJSResult}; use dom::bindings::conversions::FromJSValConvertible; use dom::bindings::conversions::StringificationBehavior; use dom::bindings::codegen::InheritTypes::{NodeCast, ElementCast}; @@ -37,24 +37,24 @@ fn find_node_by_unique_id(page: &Rc, pipeline: PipelineId, node_id: String None } -pub fn jsval_to_webdriver(cx: *mut JSContext, val: JSVal) -> Result { +pub fn jsval_to_webdriver(cx: *mut JSContext, val: JSVal) -> WebDriverJSResult { if val.is_undefined() { - Ok(EvaluateJSReply::VoidValue) + Ok(WebDriverJSValue::Undefined) } else if val.is_boolean() { - Ok(EvaluateJSReply::BooleanValue(val.to_boolean())) + Ok(WebDriverJSValue::Boolean(val.to_boolean())) } else if val.is_double() { - Ok(EvaluateJSReply::NumberValue(FromJSValConvertible::from_jsval(cx, val, ()).unwrap())) + Ok(WebDriverJSValue::Number(FromJSValConvertible::from_jsval(cx, val, ()).unwrap())) } else if val.is_string() { //FIXME: use jsstring_to_str when jsval grows to_jsstring - Ok(EvaluateJSReply::StringValue(FromJSValConvertible::from_jsval(cx, val, StringificationBehavior::Default).unwrap())) + Ok(WebDriverJSValue::String(FromJSValConvertible::from_jsval(cx, val, StringificationBehavior::Default).unwrap())) } else if val.is_null() { - Ok(EvaluateJSReply::NullValue) + Ok(WebDriverJSValue::Null) } else { - Err(()) + Err(WebDriverJSError::UnknownType) } } -pub fn handle_execute_script(page: &Rc, pipeline: PipelineId, eval: String, reply: Sender>) { +pub fn handle_execute_script(page: &Rc, pipeline: PipelineId, eval: String, reply: Sender) { let page = get_page(&*page, pipeline); let window = page.window().root(); let cx = window.r().get_cx(); @@ -63,8 +63,7 @@ pub fn handle_execute_script(page: &Rc, pipeline: PipelineId, eval: String reply.send(jsval_to_webdriver(cx, rval)).unwrap(); } - -pub fn handle_execute_async_script(page: &Rc, pipeline: PipelineId, eval: String, reply: Sender>) { +pub fn handle_execute_async_script(page: &Rc, pipeline: PipelineId, eval: String, reply: Sender) { let page = get_page(&*page, pipeline); let window = page.window().root(); window.r().set_webdriver_script_chan(Some(reply)); diff --git a/components/webdriver_server/lib.rs b/components/webdriver_server/lib.rs index 57790548e27..df32cd1e4ea 100644 --- a/components/webdriver_server/lib.rs +++ b/components/webdriver_server/lib.rs @@ -22,11 +22,11 @@ extern crate webdriver_traits; use msg::constellation_msg::{ConstellationChan, LoadData, PipelineId, NavigationDirection, WebDriverCommandMsg}; use msg::constellation_msg::Msg as ConstellationMsg; use std::sync::mpsc::{channel, Receiver}; -use webdriver_traits::{WebDriverScriptCommand, EvaluateJSReply}; +use webdriver_traits::{WebDriverScriptCommand, WebDriverJSError, WebDriverJSResult}; use url::Url; use webdriver::command::{WebDriverMessage, WebDriverCommand}; -use webdriver::command::{GetParameters, JavascriptCommandParameters, LocatorParameters}; +use webdriver::command::{GetParameters, JavascriptCommandParameters, LocatorParameters, TimeoutsParameters}; use webdriver::common::{LocatorStrategy, WebElement}; use webdriver::response::{ WebDriverResponse, NewSessionResponse, ValueResponse}; @@ -58,6 +58,9 @@ struct WebdriverSession { struct Handler { session: Option, constellation_chan: ConstellationChan, + script_timeout: u32, + load_timeout: u32, + implicit_wait_timeout: u32 } impl WebdriverSession { @@ -72,7 +75,10 @@ impl Handler { pub fn new(constellation_chan: ConstellationChan) -> Handler { Handler { session: None, - constellation_chan: constellation_chan + constellation_chan: constellation_chan, + script_timeout: 30_000, + load_timeout: 300_000, + implicit_wait_timeout: 0 } } @@ -263,6 +269,19 @@ impl Handler { } } + 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; + match ¶meters.type_[..] { + "implicit" => self.implicit_wait_timeout = value, + "page load" => self.load_timeout = value, + "script" => self.script_timeout = value, + x @ _ => return Err(WebDriverError::new(ErrorStatus::InvalidSelector, + &format!("Unknown timeout type {}", x))) + } + Ok(WebDriverResponse::Void) + } + fn handle_execute_script(&self, parameters: &JavascriptCommandParameters) -> WebDriverResult { let func_body = ¶meters.script; let args_string = ""; @@ -281,17 +300,16 @@ impl Handler { let func_body = ¶meters.script; let args_string = "window.webdriverCallback"; - // This is pretty ugly; we really want something that acts like - // new Function() and then takes the resulting function and executes - // it with a vec of arguments. - let script = format!("(function(callback) {{ {} }})({})", func_body, args_string); + let script = format!( + "setTimeout(webdriverTimeout, {}); (function(callback) {{ {} }})({})", + self.script_timeout, func_body, args_string); let (sender, reciever) = channel(); let command = WebDriverScriptCommand::ExecuteAsyncScript(script, sender); self.execute_script(command, reciever) } - fn execute_script(&self, command: WebDriverScriptCommand, reciever: Receiver>) -> WebDriverResult { + fn execute_script(&self, command: WebDriverScriptCommand, reciever: Receiver) -> WebDriverResult { // TODO: This isn't really right because it always runs the script in the // root window let pipeline_id = try!(self.get_root_pipeline()); @@ -302,12 +320,12 @@ impl Handler { match reciever.recv().unwrap() { Ok(value) => Ok(WebDriverResponse::Generic(ValueResponse::new(value.to_json()))), - Err(_) => Err(WebDriverError::new(ErrorStatus::UnsupportedOperation, - "Unsupported return type")) + Err(WebDriverJSError::Timeout) => Err(WebDriverError::new(ErrorStatus::Timeout, "")), + Err(WebDriverJSError::UnknownType) => Err(WebDriverError::new( + ErrorStatus::UnsupportedOperation, "Unsupported return type")) } } - fn handle_take_screenshot(&self) -> WebDriverResult { let mut img = None; @@ -367,6 +385,7 @@ impl WebDriverHandler for Handler { WebDriverCommand::GetElementTagName(ref element) => self.handle_get_element_tag_name(element), WebDriverCommand::ExecuteScript(ref x) => self.handle_execute_script(x), WebDriverCommand::ExecuteAsyncScript(ref x) => self.handle_execute_async_script(x), + WebDriverCommand::SetTimeouts(ref x) => self.handle_set_timeouts(x), WebDriverCommand::TakeScreenshot => self.handle_take_screenshot(), _ => Err(WebDriverError::new(ErrorStatus::UnsupportedOperation, "Command not implemented")) diff --git a/components/webdriver_traits/lib.rs b/components/webdriver_traits/lib.rs index db890f6d9d4..9279e8e2dd9 100644 --- a/components/webdriver_traits/lib.rs +++ b/components/webdriver_traits/lib.rs @@ -11,8 +11,8 @@ use rustc_serialize::json::{Json, ToJson}; use std::sync::mpsc::Sender; pub enum WebDriverScriptCommand { - ExecuteScript(String, Sender>), - ExecuteAsyncScript(String, Sender>), + ExecuteScript(String, Sender), + ExecuteAsyncScript(String, Sender), FindElementCSS(String, Sender, ()>>), FindElementsCSS(String, Sender, ()>>), GetActiveElement(Sender>), @@ -21,23 +21,30 @@ pub enum WebDriverScriptCommand { GetTitle(Sender) } -pub enum EvaluateJSReply { - VoidValue, - NullValue, - BooleanValue(bool), - NumberValue(f64), - StringValue(String), +pub enum WebDriverJSValue { + Undefined, + Null, + Boolean(bool), + Number(f64), + String(String), // TODO: ObjectValue and WebElementValue } -impl ToJson for EvaluateJSReply { +pub enum WebDriverJSError { + Timeout, + UnknownType +} + +pub type WebDriverJSResult = Result; + +impl ToJson for WebDriverJSValue { fn to_json(&self) -> Json { match self { - &EvaluateJSReply::VoidValue => Json::Null, - &EvaluateJSReply::NullValue => Json::Null, - &EvaluateJSReply::BooleanValue(ref x) => x.to_json(), - &EvaluateJSReply::NumberValue(ref x) => x.to_json(), - &EvaluateJSReply::StringValue(ref x) => x.to_json() + &WebDriverJSValue::Undefined => Json::Null, + &WebDriverJSValue::Null => Json::Null, + &WebDriverJSValue::Boolean(ref x) => x.to_json(), + &WebDriverJSValue::Number(ref x) => x.to_json(), + &WebDriverJSValue::String(ref x) => x.to_json() } } }