mirror of
https://github.com/servo/servo.git
synced 2025-06-13 02:44:29 +00:00
Add basic support for executeAsyncScript.
This relies on a global webdriverCallback function, which is visible to content. Obviously that's not a long term solution for a number of reasons, but it allows us to experiment for now
This commit is contained in:
parent
98cb65ca0a
commit
8d10fa1f2d
6 changed files with 96 additions and 35 deletions
|
@ -56,6 +56,9 @@
|
|||
|
||||
//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;
|
||||
|
|
|
@ -35,8 +35,10 @@ use script_task::{TimerSource, ScriptChan, ScriptPort, NonWorkerScriptChan};
|
|||
use script_task::ScriptMsg;
|
||||
use script_traits::ScriptControlChan;
|
||||
use timers::{IsInterval, TimerId, TimerManager, TimerCallback};
|
||||
use webdriver_handlers::jsval_to_webdriver;
|
||||
|
||||
use devtools_traits::{DevtoolsControlChan, TimelineMarker, TimelineMarkerType, TracingMetadata};
|
||||
use webdriver_traits::EvaluateJSReply;
|
||||
use msg::compositor_msg::ScriptListener;
|
||||
use msg::constellation_msg::{LoadData, PipelineId, SubpageId, ConstellationChan, WindowSizeData, WorkerId};
|
||||
use net_traits::ResourceTask;
|
||||
|
@ -166,6 +168,9 @@ pub struct Window {
|
|||
|
||||
/// A counter of the number of pending reflows for this 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, ()>>>>
|
||||
}
|
||||
|
||||
impl Window {
|
||||
|
@ -483,6 +488,17 @@ impl<'a> WindowMethods for JSRef<'a, Window> {
|
|||
let doc = self.Document().root();
|
||||
doc.r().cancel_animation_frame(ident);
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
self.set_webdriver_script_chan(None);
|
||||
}
|
||||
}
|
||||
|
||||
pub trait WindowHelpers {
|
||||
|
@ -523,6 +539,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, ()>>>);
|
||||
}
|
||||
|
||||
pub trait ScriptHelpers {
|
||||
|
@ -880,6 +897,10 @@ impl<'a> WindowHelpers for JSRef<'a, Window> {
|
|||
self.devtools_markers.borrow_mut().clear();
|
||||
*self.devtools_marker_sender.borrow_mut() = None;
|
||||
}
|
||||
|
||||
fn set_webdriver_script_chan(self, chan: Option<Sender<Result<EvaluateJSReply, ()>>>) {
|
||||
*self.webdriver_script_chan.borrow_mut() = chan;
|
||||
}
|
||||
}
|
||||
|
||||
impl Window {
|
||||
|
@ -947,6 +968,7 @@ impl Window {
|
|||
devtools_marker_sender: RefCell::new(None),
|
||||
devtools_markers: RefCell::new(HashSet::new()),
|
||||
devtools_wants_updates: Cell::new(false),
|
||||
webdriver_script_chan: RefCell::new(None),
|
||||
};
|
||||
|
||||
WindowBinding::Wrap(runtime.cx(), win)
|
||||
|
|
|
@ -316,7 +316,7 @@ pub struct ScriptTask {
|
|||
/// The JavaScript runtime.
|
||||
js_runtime: Rc<Runtime>,
|
||||
|
||||
mouse_over_targets: DOMRefCell<Vec<JS<Node>>>
|
||||
mouse_over_targets: DOMRefCell<Vec<JS<Node>>>,
|
||||
}
|
||||
|
||||
/// In the event of task failure, all data on the stack runs its destructor. However, there
|
||||
|
@ -814,7 +814,9 @@ impl ScriptTask {
|
|||
WebDriverScriptCommand::GetElementText(node_id, reply) =>
|
||||
webdriver_handlers::handle_get_text(&page, pipeline_id, node_id, reply),
|
||||
WebDriverScriptCommand::GetTitle(reply) =>
|
||||
webdriver_handlers::handle_get_title(&page, pipeline_id, reply)
|
||||
webdriver_handlers::handle_get_title(&page, pipeline_id, reply),
|
||||
WebDriverScriptCommand::ExecuteAsyncScript(script, reply) =>
|
||||
webdriver_handlers::handle_execute_async_script(&page, pipeline_id, script, reply),
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -12,8 +12,10 @@ use dom::bindings::codegen::Bindings::NodeBinding::NodeMethods;
|
|||
use dom::bindings::codegen::Bindings::NodeListBinding::NodeListMethods;
|
||||
use dom::bindings::js::{OptionalRootable, Rootable, Temporary};
|
||||
use dom::node::{Node, NodeHelpers};
|
||||
use dom::window::ScriptHelpers;
|
||||
use dom::window::{ScriptHelpers, WindowHelpers};
|
||||
use dom::document::DocumentHelpers;
|
||||
use js::jsapi::JSContext;
|
||||
use js::jsval::JSVal;
|
||||
use page::Page;
|
||||
use msg::constellation_msg::PipelineId;
|
||||
use script_task::get_page;
|
||||
|
@ -35,26 +37,38 @@ 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, ()> {
|
||||
if val.is_undefined() {
|
||||
Ok(EvaluateJSReply::VoidValue)
|
||||
} else if val.is_boolean() {
|
||||
Ok(EvaluateJSReply::BooleanValue(val.to_boolean()))
|
||||
} else if val.is_double() {
|
||||
Ok(EvaluateJSReply::NumberValue(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()))
|
||||
} else if val.is_null() {
|
||||
Ok(EvaluateJSReply::NullValue)
|
||||
} else {
|
||||
Err(())
|
||||
}
|
||||
}
|
||||
|
||||
pub fn handle_execute_script(page: &Rc<Page>, pipeline: PipelineId, eval: String, reply: Sender<Result<EvaluateJSReply, ()>>) {
|
||||
let page = get_page(&*page, pipeline);
|
||||
let window = page.window().root();
|
||||
let cx = window.r().get_cx();
|
||||
let rval = window.r().evaluate_js_on_global_with_result(&eval);
|
||||
|
||||
reply.send(if rval.is_undefined() {
|
||||
Ok(EvaluateJSReply::VoidValue)
|
||||
} else if rval.is_boolean() {
|
||||
Ok(EvaluateJSReply::BooleanValue(rval.to_boolean()))
|
||||
} else if rval.is_double() {
|
||||
Ok(EvaluateJSReply::NumberValue(FromJSValConvertible::from_jsval(cx, rval, ()).unwrap()))
|
||||
} else if rval.is_string() {
|
||||
//FIXME: use jsstring_to_str when jsval grows to_jsstring
|
||||
Ok(EvaluateJSReply::StringValue(FromJSValConvertible::from_jsval(cx, rval, StringificationBehavior::Default).unwrap()))
|
||||
} else if rval.is_null() {
|
||||
Ok(EvaluateJSReply::NullValue)
|
||||
} else {
|
||||
Err(())
|
||||
}).unwrap();
|
||||
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, ()>>) {
|
||||
let page = get_page(&*page, pipeline);
|
||||
let window = page.window().root();
|
||||
window.r().set_webdriver_script_chan(Some(reply));
|
||||
window.r().evaluate_js_on_global_with_result(&eval);
|
||||
}
|
||||
|
||||
pub fn handle_find_element_css(page: &Rc<Page>, _pipeline: PipelineId, selector: String, reply: Sender<Result<Option<String>, ()>>) {
|
||||
|
|
|
@ -21,8 +21,8 @@ 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;
|
||||
use webdriver_traits::WebDriverScriptCommand;
|
||||
use std::sync::mpsc::{channel, Receiver};
|
||||
use webdriver_traits::{WebDriverScriptCommand, EvaluateJSReply};
|
||||
|
||||
use url::Url;
|
||||
use webdriver::command::{WebDriverMessage, WebDriverCommand};
|
||||
|
@ -77,8 +77,8 @@ impl Handler {
|
|||
}
|
||||
|
||||
fn get_root_pipeline(&self) -> WebDriverResult<PipelineId> {
|
||||
let interval = Duration::milliseconds(20);
|
||||
let iterations = 30_000 / interval.num_milliseconds();
|
||||
let interval = 20;
|
||||
let iterations = 30_000 / interval;
|
||||
|
||||
for _ in 0..iterations {
|
||||
let (sender, reciever) = channel();
|
||||
|
@ -90,7 +90,7 @@ impl Handler {
|
|||
return Ok(x);
|
||||
};
|
||||
|
||||
sleep(interval)
|
||||
sleep_ms(interval)
|
||||
};
|
||||
|
||||
Err(WebDriverError::new(ErrorStatus::Timeout,
|
||||
|
@ -150,7 +150,7 @@ impl Handler {
|
|||
}
|
||||
|
||||
fn handle_get_title(&self) -> WebDriverResult<WebDriverResponse> {
|
||||
let pipeline_id = self.get_root_pipeline();
|
||||
let pipeline_id = try!(self.get_root_pipeline());
|
||||
|
||||
let (sender, reciever) = channel();
|
||||
let ConstellationChan(ref const_chan) = self.constellation_chan;
|
||||
|
@ -176,7 +176,7 @@ impl Handler {
|
|||
}
|
||||
|
||||
fn handle_find_element(&self, parameters: &LocatorParameters) -> WebDriverResult<WebDriverResponse> {
|
||||
let pipeline_id = self.get_root_pipeline();
|
||||
let pipeline_id = try!(self.get_root_pipeline());
|
||||
|
||||
if parameters.using != LocatorStrategy::CSSSelector {
|
||||
return Err(WebDriverError::new(ErrorStatus::UnsupportedOperation,
|
||||
|
@ -198,7 +198,7 @@ impl Handler {
|
|||
}
|
||||
|
||||
fn handle_find_elements(&self, parameters: &LocatorParameters) -> WebDriverResult<WebDriverResponse> {
|
||||
let pipeline_id = self.get_root_pipeline();
|
||||
let pipeline_id = try!(self.get_root_pipeline());
|
||||
|
||||
if parameters.using != LocatorStrategy::CSSSelector {
|
||||
return Err(WebDriverError::new(ErrorStatus::UnsupportedOperation,
|
||||
|
@ -222,7 +222,7 @@ impl Handler {
|
|||
}
|
||||
|
||||
fn handle_get_element_text(&self, element: &WebElement) -> WebDriverResult<WebDriverResponse> {
|
||||
let pipeline_id = self.get_root_pipeline();
|
||||
let pipeline_id = try!(self.get_root_pipeline());
|
||||
|
||||
let (sender, reciever) = channel();
|
||||
let ConstellationChan(ref const_chan) = self.constellation_chan;
|
||||
|
@ -237,7 +237,7 @@ impl Handler {
|
|||
}
|
||||
|
||||
fn handle_get_active_element(&self) -> WebDriverResult<WebDriverResponse> {
|
||||
let pipeline_id = self.get_root_pipeline();
|
||||
let pipeline_id = try!(self.get_root_pipeline());
|
||||
|
||||
let (sender, reciever) = channel();
|
||||
let ConstellationChan(ref const_chan) = self.constellation_chan;
|
||||
|
@ -249,7 +249,7 @@ impl Handler {
|
|||
}
|
||||
|
||||
fn handle_get_element_tag_name(&self, element: &WebElement) -> WebDriverResult<WebDriverResponse> {
|
||||
let pipeline_id = self.get_root_pipeline();
|
||||
let pipeline_id = try!(self.get_root_pipeline());
|
||||
|
||||
let (sender, reciever) = channel();
|
||||
let ConstellationChan(ref const_chan) = self.constellation_chan;
|
||||
|
@ -264,10 +264,6 @@ impl Handler {
|
|||
}
|
||||
|
||||
fn handle_execute_script(&self, parameters: &JavascriptCommandParameters) -> WebDriverResult<WebDriverResponse> {
|
||||
// TODO: This isn't really right because it always runs the script in the
|
||||
// root window
|
||||
let pipeline_id = self.get_root_pipeline();
|
||||
|
||||
let func_body = ¶meters.script;
|
||||
let args_string = "";
|
||||
|
||||
|
@ -277,9 +273,31 @@ impl Handler {
|
|||
let script = format!("(function() {{ {} }})({})", func_body, args_string);
|
||||
|
||||
let (sender, reciever) = channel();
|
||||
let command = WebDriverScriptCommand::ExecuteScript(script, sender);
|
||||
self.execute_script(command, reciever)
|
||||
}
|
||||
|
||||
fn handle_execute_async_script(&self, parameters: &JavascriptCommandParameters) -> WebDriverResult<WebDriverResponse> {
|
||||
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 (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> {
|
||||
// TODO: This isn't really right because it always runs the script in the
|
||||
// root window
|
||||
let pipeline_id = try!(self.get_root_pipeline());
|
||||
|
||||
let ConstellationChan(ref const_chan) = self.constellation_chan;
|
||||
let cmd = WebDriverScriptCommand::ExecuteScript(script, sender);
|
||||
let cmd_msg = WebDriverCommandMsg::ScriptCommand(pipeline_id, cmd);
|
||||
let cmd_msg = WebDriverCommandMsg::ScriptCommand(pipeline_id, command);
|
||||
const_chan.send(ConstellationMsg::WebDriverCommand(cmd_msg)).unwrap();
|
||||
|
||||
match reciever.recv().unwrap() {
|
||||
|
@ -348,6 +366,7 @@ impl WebDriverHandler for Handler {
|
|||
WebDriverCommand::GetElementText(ref element) => self.handle_get_element_text(element),
|
||||
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::TakeScreenshot => self.handle_take_screenshot(),
|
||||
_ => Err(WebDriverError::new(ErrorStatus::UnsupportedOperation,
|
||||
"Command not implemented"))
|
||||
|
|
|
@ -12,6 +12,7 @@ use std::sync::mpsc::Sender;
|
|||
|
||||
pub enum WebDriverScriptCommand {
|
||||
ExecuteScript(String, Sender<Result<EvaluateJSReply, ()>>),
|
||||
ExecuteAsyncScript(String, Sender<Result<EvaluateJSReply, ()>>),
|
||||
FindElementCSS(String, Sender<Result<Option<String>, ()>>),
|
||||
FindElementsCSS(String, Sender<Result<Vec<String>, ()>>),
|
||||
GetActiveElement(Sender<Option<String>>),
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue