Add support for timing out scripts

This commit is contained in:
James Graham 2015-04-30 16:26:04 +01:00
parent 8d10fa1f2d
commit 50f59c8255
5 changed files with 82 additions and 49 deletions

View file

@ -56,9 +56,6 @@
//void postMessage(any message, DOMString targetOrigin, optional sequence<Transferable> 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 {

View file

@ -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<u32>,
/// A channel for communicating results of async scripts back to the webdriver server
webdriver_script_chan: RefCell<Option<Sender<Result<EvaluateJSReply, ()>>>>
webdriver_script_chan: RefCell<Option<Sender<WebDriverJSResult>>>
}
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<TimelineMarker>);
fn drop_devtools_timeline_markers(self);
fn set_webdriver_script_chan(self, chan: Option<Sender<Result<EvaluateJSReply, ()>>>);
fn set_webdriver_script_chan(self, chan: Option<Sender<WebDriverJSResult>>);
}
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<Sender<Result<EvaluateJSReply, ()>>>) {
fn set_webdriver_script_chan(self, chan: Option<Sender<WebDriverJSResult>>) {
*self.webdriver_script_chan.borrow_mut() = chan;
}
}

View file

@ -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<Page>, pipeline: PipelineId, node_id: String
None
}
pub fn jsval_to_webdriver(cx: *mut JSContext, val: JSVal) -> Result<EvaluateJSReply, ()> {
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<Page>, pipeline: PipelineId, eval: String, reply: Sender<Result<EvaluateJSReply, ()>>) {
pub fn handle_execute_script(page: &Rc<Page>, pipeline: PipelineId, eval: String, reply: Sender<WebDriverJSResult>) {
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<Page>, pipeline: PipelineId, eval: String
reply.send(jsval_to_webdriver(cx, rval)).unwrap();
}
pub fn handle_execute_async_script(page: &Rc<Page>, pipeline: PipelineId, eval: String, reply: Sender<Result<EvaluateJSReply, ()>>) {
pub fn handle_execute_async_script(page: &Rc<Page>, pipeline: PipelineId, eval: String, reply: Sender<WebDriverJSResult>) {
let page = get_page(&*page, pipeline);
let window = page.window().root();
window.r().set_webdriver_script_chan(Some(reply));

View file

@ -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<WebdriverSession>,
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<WebDriverResponse> {
//TODO: this conversion is crazy, spec should limit these to u32 and check upstream
let value = parameters.ms as u32;
match &parameters.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<WebDriverResponse> {
let func_body = &parameters.script;
let args_string = "";
@ -281,17 +300,16 @@ impl Handler {
let func_body = &parameters.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<Result<EvaluateJSReply, ()>>) -> WebDriverResult<WebDriverResponse> {
fn execute_script(&self, command: WebDriverScriptCommand, reciever: Receiver<WebDriverJSResult>) -> WebDriverResult<WebDriverResponse> {
// 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<WebDriverResponse> {
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"))

View file

@ -11,8 +11,8 @@ use rustc_serialize::json::{Json, ToJson};
use std::sync::mpsc::Sender;
pub enum WebDriverScriptCommand {
ExecuteScript(String, Sender<Result<EvaluateJSReply, ()>>),
ExecuteAsyncScript(String, Sender<Result<EvaluateJSReply, ()>>),
ExecuteScript(String, Sender<WebDriverJSResult>),
ExecuteAsyncScript(String, Sender<WebDriverJSResult>),
FindElementCSS(String, Sender<Result<Option<String>, ()>>),
FindElementsCSS(String, Sender<Result<Vec<String>, ()>>),
GetActiveElement(Sender<Option<String>>),
@ -21,23 +21,30 @@ pub enum WebDriverScriptCommand {
GetTitle(Sender<String>)
}
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<WebDriverJSValue, WebDriverJSError>;
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()
}
}
}