mirror of
https://github.com/servo/servo.git
synced 2025-08-04 21:20:23 +01:00
Add support for timing out scripts
This commit is contained in:
parent
8d10fa1f2d
commit
50f59c8255
5 changed files with 82 additions and 49 deletions
|
@ -56,9 +56,6 @@
|
||||||
|
|
||||||
//void postMessage(any message, DOMString targetOrigin, optional sequence<Transferable> transfer);
|
//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
|
// also has obsolete members
|
||||||
};
|
};
|
||||||
Window implements GlobalEventHandlers;
|
Window implements GlobalEventHandlers;
|
||||||
|
@ -131,6 +128,13 @@ partial interface Window {
|
||||||
};
|
};
|
||||||
Window implements OnErrorEventHandlerForWindow;
|
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
|
// https://html.spec.whatwg.org/multipage/#dom-sessionstorage
|
||||||
[NoInterfaceObject]
|
[NoInterfaceObject]
|
||||||
interface WindowSessionStorage {
|
interface WindowSessionStorage {
|
||||||
|
|
|
@ -38,7 +38,7 @@ use timers::{IsInterval, TimerId, TimerManager, TimerCallback};
|
||||||
use webdriver_handlers::jsval_to_webdriver;
|
use webdriver_handlers::jsval_to_webdriver;
|
||||||
|
|
||||||
use devtools_traits::{DevtoolsControlChan, TimelineMarker, TimelineMarkerType, TracingMetadata};
|
use devtools_traits::{DevtoolsControlChan, TimelineMarker, TimelineMarkerType, TracingMetadata};
|
||||||
use webdriver_traits::EvaluateJSReply;
|
use webdriver_traits::{WebDriverJSError, WebDriverJSResult};
|
||||||
use msg::compositor_msg::ScriptListener;
|
use msg::compositor_msg::ScriptListener;
|
||||||
use msg::constellation_msg::{LoadData, PipelineId, SubpageId, ConstellationChan, WindowSizeData, WorkerId};
|
use msg::constellation_msg::{LoadData, PipelineId, SubpageId, ConstellationChan, WindowSizeData, WorkerId};
|
||||||
use net_traits::ResourceTask;
|
use net_traits::ResourceTask;
|
||||||
|
@ -170,7 +170,7 @@ pub struct Window {
|
||||||
pending_reflow_count: Cell<u32>,
|
pending_reflow_count: Cell<u32>,
|
||||||
|
|
||||||
/// A channel for communicating results of async scripts back to the webdriver server
|
/// 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 {
|
impl Window {
|
||||||
|
@ -491,13 +491,17 @@ impl<'a> WindowMethods for JSRef<'a, Window> {
|
||||||
|
|
||||||
fn WebdriverCallback(self, cx: *mut JSContext, val: JSVal) {
|
fn WebdriverCallback(self, cx: *mut JSContext, val: JSVal) {
|
||||||
let rv = jsval_to_webdriver(cx, val);
|
let rv = jsval_to_webdriver(cx, val);
|
||||||
{
|
let opt_chan = self.webdriver_script_chan.borrow_mut().take();
|
||||||
let opt_chan = self.webdriver_script_chan.borrow();
|
if let Some(chan) = opt_chan {
|
||||||
if let Some(ref chan) = *opt_chan {
|
chan.send(rv).unwrap();
|
||||||
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 emit_timeline_marker(self, marker: TimelineMarker);
|
||||||
fn set_devtools_timeline_marker(self, marker: TimelineMarkerType, reply: Sender<TimelineMarker>);
|
fn set_devtools_timeline_marker(self, marker: TimelineMarkerType, reply: Sender<TimelineMarker>);
|
||||||
fn drop_devtools_timeline_markers(self);
|
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 {
|
pub trait ScriptHelpers {
|
||||||
|
@ -898,7 +902,7 @@ impl<'a> WindowHelpers for JSRef<'a, Window> {
|
||||||
*self.devtools_marker_sender.borrow_mut() = None;
|
*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;
|
*self.webdriver_script_chan.borrow_mut() = chan;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
* 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/. */
|
* 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::FromJSValConvertible;
|
||||||
use dom::bindings::conversions::StringificationBehavior;
|
use dom::bindings::conversions::StringificationBehavior;
|
||||||
use dom::bindings::codegen::InheritTypes::{NodeCast, ElementCast};
|
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
|
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() {
|
if val.is_undefined() {
|
||||||
Ok(EvaluateJSReply::VoidValue)
|
Ok(WebDriverJSValue::Undefined)
|
||||||
} else if val.is_boolean() {
|
} else if val.is_boolean() {
|
||||||
Ok(EvaluateJSReply::BooleanValue(val.to_boolean()))
|
Ok(WebDriverJSValue::Boolean(val.to_boolean()))
|
||||||
} else if val.is_double() {
|
} 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() {
|
} else if val.is_string() {
|
||||||
//FIXME: use jsstring_to_str when jsval grows to_jsstring
|
//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() {
|
} else if val.is_null() {
|
||||||
Ok(EvaluateJSReply::NullValue)
|
Ok(WebDriverJSValue::Null)
|
||||||
} else {
|
} 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 page = get_page(&*page, pipeline);
|
||||||
let window = page.window().root();
|
let window = page.window().root();
|
||||||
let cx = window.r().get_cx();
|
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();
|
reply.send(jsval_to_webdriver(cx, rval)).unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn handle_execute_async_script(page: &Rc<Page>, pipeline: PipelineId, eval: String, reply: Sender<WebDriverJSResult>) {
|
||||||
pub fn handle_execute_async_script(page: &Rc<Page>, pipeline: PipelineId, eval: String, reply: Sender<Result<EvaluateJSReply, ()>>) {
|
|
||||||
let page = get_page(&*page, pipeline);
|
let page = get_page(&*page, pipeline);
|
||||||
let window = page.window().root();
|
let window = page.window().root();
|
||||||
window.r().set_webdriver_script_chan(Some(reply));
|
window.r().set_webdriver_script_chan(Some(reply));
|
||||||
|
|
|
@ -22,11 +22,11 @@ extern crate webdriver_traits;
|
||||||
use msg::constellation_msg::{ConstellationChan, LoadData, PipelineId, NavigationDirection, WebDriverCommandMsg};
|
use msg::constellation_msg::{ConstellationChan, LoadData, PipelineId, NavigationDirection, WebDriverCommandMsg};
|
||||||
use msg::constellation_msg::Msg as ConstellationMsg;
|
use msg::constellation_msg::Msg as ConstellationMsg;
|
||||||
use std::sync::mpsc::{channel, Receiver};
|
use std::sync::mpsc::{channel, Receiver};
|
||||||
use webdriver_traits::{WebDriverScriptCommand, EvaluateJSReply};
|
use webdriver_traits::{WebDriverScriptCommand, WebDriverJSError, WebDriverJSResult};
|
||||||
|
|
||||||
use url::Url;
|
use url::Url;
|
||||||
use webdriver::command::{WebDriverMessage, WebDriverCommand};
|
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::common::{LocatorStrategy, WebElement};
|
||||||
use webdriver::response::{
|
use webdriver::response::{
|
||||||
WebDriverResponse, NewSessionResponse, ValueResponse};
|
WebDriverResponse, NewSessionResponse, ValueResponse};
|
||||||
|
@ -58,6 +58,9 @@ struct WebdriverSession {
|
||||||
struct Handler {
|
struct Handler {
|
||||||
session: Option<WebdriverSession>,
|
session: Option<WebdriverSession>,
|
||||||
constellation_chan: ConstellationChan,
|
constellation_chan: ConstellationChan,
|
||||||
|
script_timeout: u32,
|
||||||
|
load_timeout: u32,
|
||||||
|
implicit_wait_timeout: u32
|
||||||
}
|
}
|
||||||
|
|
||||||
impl WebdriverSession {
|
impl WebdriverSession {
|
||||||
|
@ -72,7 +75,10 @@ impl Handler {
|
||||||
pub fn new(constellation_chan: ConstellationChan) -> Handler {
|
pub fn new(constellation_chan: ConstellationChan) -> Handler {
|
||||||
Handler {
|
Handler {
|
||||||
session: None,
|
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 ¶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<WebDriverResponse> {
|
fn handle_execute_script(&self, parameters: &JavascriptCommandParameters) -> WebDriverResult<WebDriverResponse> {
|
||||||
let func_body = ¶meters.script;
|
let func_body = ¶meters.script;
|
||||||
let args_string = "";
|
let args_string = "";
|
||||||
|
@ -281,17 +300,16 @@ impl Handler {
|
||||||
let func_body = ¶meters.script;
|
let func_body = ¶meters.script;
|
||||||
let args_string = "window.webdriverCallback";
|
let args_string = "window.webdriverCallback";
|
||||||
|
|
||||||
// This is pretty ugly; we really want something that acts like
|
let script = format!(
|
||||||
// new Function() and then takes the resulting function and executes
|
"setTimeout(webdriverTimeout, {}); (function(callback) {{ {} }})({})",
|
||||||
// it with a vec of arguments.
|
self.script_timeout, func_body, args_string);
|
||||||
let script = format!("(function(callback) {{ {} }})({})", func_body, args_string);
|
|
||||||
|
|
||||||
let (sender, reciever) = channel();
|
let (sender, reciever) = channel();
|
||||||
let command = WebDriverScriptCommand::ExecuteAsyncScript(script, sender);
|
let command = WebDriverScriptCommand::ExecuteAsyncScript(script, sender);
|
||||||
self.execute_script(command, reciever)
|
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
|
// TODO: This isn't really right because it always runs the script in the
|
||||||
// root window
|
// root window
|
||||||
let pipeline_id = try!(self.get_root_pipeline());
|
let pipeline_id = try!(self.get_root_pipeline());
|
||||||
|
@ -302,12 +320,12 @@ impl Handler {
|
||||||
|
|
||||||
match reciever.recv().unwrap() {
|
match reciever.recv().unwrap() {
|
||||||
Ok(value) => Ok(WebDriverResponse::Generic(ValueResponse::new(value.to_json()))),
|
Ok(value) => Ok(WebDriverResponse::Generic(ValueResponse::new(value.to_json()))),
|
||||||
Err(_) => Err(WebDriverError::new(ErrorStatus::UnsupportedOperation,
|
Err(WebDriverJSError::Timeout) => Err(WebDriverError::new(ErrorStatus::Timeout, "")),
|
||||||
"Unsupported return type"))
|
Err(WebDriverJSError::UnknownType) => Err(WebDriverError::new(
|
||||||
|
ErrorStatus::UnsupportedOperation, "Unsupported return type"))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
fn handle_take_screenshot(&self) -> WebDriverResult<WebDriverResponse> {
|
fn handle_take_screenshot(&self) -> WebDriverResult<WebDriverResponse> {
|
||||||
let mut img = None;
|
let mut img = None;
|
||||||
|
|
||||||
|
@ -367,6 +385,7 @@ impl WebDriverHandler for Handler {
|
||||||
WebDriverCommand::GetElementTagName(ref element) => self.handle_get_element_tag_name(element),
|
WebDriverCommand::GetElementTagName(ref element) => self.handle_get_element_tag_name(element),
|
||||||
WebDriverCommand::ExecuteScript(ref x) => self.handle_execute_script(x),
|
WebDriverCommand::ExecuteScript(ref x) => self.handle_execute_script(x),
|
||||||
WebDriverCommand::ExecuteAsyncScript(ref x) => self.handle_execute_async_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(),
|
WebDriverCommand::TakeScreenshot => self.handle_take_screenshot(),
|
||||||
_ => Err(WebDriverError::new(ErrorStatus::UnsupportedOperation,
|
_ => Err(WebDriverError::new(ErrorStatus::UnsupportedOperation,
|
||||||
"Command not implemented"))
|
"Command not implemented"))
|
||||||
|
|
|
@ -11,8 +11,8 @@ use rustc_serialize::json::{Json, ToJson};
|
||||||
use std::sync::mpsc::Sender;
|
use std::sync::mpsc::Sender;
|
||||||
|
|
||||||
pub enum WebDriverScriptCommand {
|
pub enum WebDriverScriptCommand {
|
||||||
ExecuteScript(String, Sender<Result<EvaluateJSReply, ()>>),
|
ExecuteScript(String, Sender<WebDriverJSResult>),
|
||||||
ExecuteAsyncScript(String, Sender<Result<EvaluateJSReply, ()>>),
|
ExecuteAsyncScript(String, Sender<WebDriverJSResult>),
|
||||||
FindElementCSS(String, Sender<Result<Option<String>, ()>>),
|
FindElementCSS(String, Sender<Result<Option<String>, ()>>),
|
||||||
FindElementsCSS(String, Sender<Result<Vec<String>, ()>>),
|
FindElementsCSS(String, Sender<Result<Vec<String>, ()>>),
|
||||||
GetActiveElement(Sender<Option<String>>),
|
GetActiveElement(Sender<Option<String>>),
|
||||||
|
@ -21,23 +21,30 @@ pub enum WebDriverScriptCommand {
|
||||||
GetTitle(Sender<String>)
|
GetTitle(Sender<String>)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub enum EvaluateJSReply {
|
pub enum WebDriverJSValue {
|
||||||
VoidValue,
|
Undefined,
|
||||||
NullValue,
|
Null,
|
||||||
BooleanValue(bool),
|
Boolean(bool),
|
||||||
NumberValue(f64),
|
Number(f64),
|
||||||
StringValue(String),
|
String(String),
|
||||||
// TODO: ObjectValue and WebElementValue
|
// 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 {
|
fn to_json(&self) -> Json {
|
||||||
match self {
|
match self {
|
||||||
&EvaluateJSReply::VoidValue => Json::Null,
|
&WebDriverJSValue::Undefined => Json::Null,
|
||||||
&EvaluateJSReply::NullValue => Json::Null,
|
&WebDriverJSValue::Null => Json::Null,
|
||||||
&EvaluateJSReply::BooleanValue(ref x) => x.to_json(),
|
&WebDriverJSValue::Boolean(ref x) => x.to_json(),
|
||||||
&EvaluateJSReply::NumberValue(ref x) => x.to_json(),
|
&WebDriverJSValue::Number(ref x) => x.to_json(),
|
||||||
&EvaluateJSReply::StringValue(ref x) => x.to_json()
|
&WebDriverJSValue::String(ref x) => x.to_json()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue