Implement WebDriver Refresh command

This commit is contained in:
James Graham 2015-06-15 14:35:36 +01:00
parent 9eade19897
commit f28219b8cb
3 changed files with 56 additions and 22 deletions

View file

@ -912,14 +912,18 @@ impl<LTF: LayoutTaskFactory, STF: ScriptTaskFactory> Constellation<LTF, STF> {
// and pass the event to that script task.
match msg {
WebDriverCommandMsg::LoadUrl(pipeline_id, load_data, reply) => {
let new_pipeline_id = self.load_url(pipeline_id, load_data);
if let Some(id) = new_pipeline_id {
self.webdriver.load_channel = Some((id, reply));
}
self.load_url_for_webdriver(pipeline_id, load_data, reply);
},
WebDriverCommandMsg::GetUrl(pipeline_id, reply) => {
let pipeline = self.pipeline(pipeline_id);
reply.send(pipeline.url.clone()).unwrap();
},
WebDriverCommandMsg::Refresh(pipeline_id, reply) => {
let load_data = {
let pipeline = self.pipeline(pipeline_id);
LoadData::new(pipeline.url.clone())
};
self.load_url_for_webdriver(pipeline_id, load_data, reply);
}
WebDriverCommandMsg::ScriptCommand(pipeline_id, cmd) => {
let pipeline = self.pipeline(pipeline_id);
@ -941,6 +945,16 @@ impl<LTF: LayoutTaskFactory, STF: ScriptTaskFactory> Constellation<LTF, STF> {
}
}
fn load_url_for_webdriver(&mut self,
pipeline_id: PipelineId,
load_data:LoadData,
reply: Sender<webdriver_msg::LoadStatus>) {
let new_pipeline_id = self.load_url(pipeline_id, load_data);
if let Some(id) = new_pipeline_id {
self.webdriver.load_channel = Some((id, reply));
}
}
fn add_or_replace_pipeline_in_frame_tree(&mut self, frame_change: FrameChange) {
// If the currently focused pipeline is the one being changed (or a child

View file

@ -337,6 +337,7 @@ impl MozBrowserEvent {
pub enum WebDriverCommandMsg {
LoadUrl(PipelineId, LoadData, IpcSender<LoadStatus>),
GetUrl(PipelineId, IpcSender<Url>),
Refresh(PipelineId, IpcSender<LoadStatus>),
ScriptCommand(PipelineId, WebDriverScriptCommand),
TakeScreenshot(PipelineId, IpcSender<Option<Image>>)
}

View file

@ -35,7 +35,7 @@ use webdriver::server::{self, WebDriverHandler, Session};
use webdriver::error::{WebDriverResult, WebDriverError, ErrorStatus};
use util::task::spawn_named;
use uuid::Uuid;
use ipc_channel::ipc::{self, IpcReceiver};
use ipc_channel::ipc::{self, IpcSender, IpcReceiver};
use std::borrow::ToOwned;
use rustc_serialize::json::{Json, ToJson};
@ -139,7 +139,7 @@ impl Handler {
const_chan.send(ConstellationMsg::GetPipeline(frame_id, sender)).unwrap();
reciever.recv().unwrap()
receiver.recv().unwrap()
}
fn handle_new_session(&mut self) -> WebDriverResult<WebDriverResponse> {
@ -176,15 +176,21 @@ impl Handler {
let cmd_msg = WebDriverCommandMsg::LoadUrl(pipeline_id, load_data, sender.clone());
const_chan.send(ConstellationMsg::WebDriverCommand(cmd_msg)).unwrap();
self.wait_for_load(sender, receiver)
}
fn wait_for_load(&self,
sender: IpcSender<LoadStatus>,
receiver: IpcReceiver<LoadStatus>) -> WebDriverResult<WebDriverResponse> {
let timeout = self.load_timeout;
let timeout_chan = sender.clone();
let timeout_chan = sender;
thread::spawn(move || {
sleep_ms(timeout);
let _ = timeout_chan.send(LoadStatus::LoadTimeout);
});
//Wait to get a load event
match reciever.recv().unwrap() {
match receiver.recv().unwrap() {
LoadStatus::LoadComplete => Ok(WebDriverResponse::Void),
LoadStatus::LoadTimeout => Err(WebDriverError::new(ErrorStatus::Timeout,
"Load timed out"))
@ -194,14 +200,14 @@ impl Handler {
fn handle_get_current_url(&self) -> WebDriverResult<WebDriverResponse> {
let pipeline_id = try!(self.get_root_pipeline());
let (sender, reciever) = channel();
let (sender, receiver) = channel();
let ConstellationChan(ref const_chan) = self.constellation_chan;
let cmd_msg = WebDriverCommandMsg::GetUrl(pipeline_id, sender);
const_chan.send(ConstellationMsg::WebDriverCommand(cmd_msg)).unwrap();
//Wait to get a load event
let url = reciever.recv().unwrap();
let url = receiver.recv().unwrap();
Ok(WebDriverResponse::Generic(ValueResponse::new(url.serialize().to_json())))
}
@ -218,6 +224,18 @@ impl Handler {
Ok(WebDriverResponse::Void)
}
fn handle_refresh(&self) -> WebDriverResult<WebDriverResponse> {
let pipeline_id = try!(self.get_root_pipeline());
let (sender, receiver) = channel();
let ConstellationChan(ref const_chan) = self.constellation_chan;
let cmd_msg = WebDriverCommandMsg::Refresh(pipeline_id, sender.clone());
const_chan.send(ConstellationMsg::WebDriverCommand(cmd_msg)).unwrap();
self.wait_for_load(sender, receiver)
}
fn handle_get_title(&self) -> WebDriverResult<WebDriverResponse> {
let pipeline_id = try!(self.get_root_pipeline());
@ -226,7 +244,7 @@ impl Handler {
let cmd_msg = WebDriverCommandMsg::ScriptCommand(pipeline_id,
WebDriverScriptCommand::GetTitle(sender));
const_chan.send(ConstellationMsg::WebDriverCommand(cmd_msg)).unwrap();
let value = reciever.recv().unwrap();
let value = receiver.recv().unwrap();
Ok(WebDriverResponse::Generic(ValueResponse::new(value.to_json())))
}
@ -257,7 +275,7 @@ impl Handler {
let cmd = WebDriverScriptCommand::FindElementCSS(parameters.value.clone(), sender);
let cmd_msg = WebDriverCommandMsg::ScriptCommand(pipeline_id, cmd);
const_chan.send(ConstellationMsg::WebDriverCommand(cmd_msg)).unwrap();
match reciever.recv().unwrap() {
match receiver.recv().unwrap() {
Ok(value) => {
let value_resp = value.map(|x| WebElement::new(x).to_json()).to_json();
Ok(WebDriverResponse::Generic(ValueResponse::new(value_resp)))
@ -300,12 +318,12 @@ impl Handler {
WebDriverCommandMsg::ScriptCommand(pipeline_id, cmd))).unwrap();
}
let frame = match reciever.recv().unwrap() {
let frame = match receiver.recv().unwrap() {
Ok(Some((pipeline_id, subpage_id))) => {
let (sender, reciever) = ipc::channel().unwrap();
let ConstellationChan(ref const_chan) = self.constellation_chan;
const_chan.send(ConstellationMsg::GetFrame(pipeline_id, subpage_id, sender)).unwrap();
reciever.recv().unwrap()
receiver.recv().unwrap()
},
Ok(None) => None,
Err(_) => {
@ -332,7 +350,7 @@ impl Handler {
let cmd = WebDriverScriptCommand::FindElementsCSS(parameters.value.clone(), sender);
let cmd_msg = WebDriverCommandMsg::ScriptCommand(pipeline_id, cmd);
const_chan.send(ConstellationMsg::WebDriverCommand(cmd_msg)).unwrap();
match reciever.recv().unwrap() {
match receiver.recv().unwrap() {
Ok(value) => {
let resp_value: Vec<Json> = value.into_iter().map(
|x| WebElement::new(x).to_json()).collect();
@ -351,7 +369,7 @@ impl Handler {
let cmd = WebDriverScriptCommand::GetElementText(element.id.clone(), sender);
let cmd_msg = WebDriverCommandMsg::ScriptCommand(pipeline_id, cmd);
const_chan.send(ConstellationMsg::WebDriverCommand(cmd_msg)).unwrap();
match reciever.recv().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"))
@ -366,7 +384,7 @@ impl Handler {
let cmd = WebDriverScriptCommand::GetActiveElement(sender);
let cmd_msg = WebDriverCommandMsg::ScriptCommand(pipeline_id, cmd);
const_chan.send(ConstellationMsg::WebDriverCommand(cmd_msg)).unwrap();
let value = reciever.recv().unwrap().map(|x| WebElement::new(x).to_json());
let value = receiver.recv().unwrap().map(|x| WebElement::new(x).to_json());
Ok(WebDriverResponse::Generic(ValueResponse::new(value.to_json())))
}
@ -378,7 +396,7 @@ impl Handler {
let cmd = WebDriverScriptCommand::GetElementTagName(element.id.clone(), sender);
let cmd_msg = WebDriverCommandMsg::ScriptCommand(pipeline_id, cmd);
const_chan.send(ConstellationMsg::WebDriverCommand(cmd_msg)).unwrap();
match reciever.recv().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"))
@ -410,7 +428,7 @@ impl Handler {
let (sender, reciever) = ipc::channel().unwrap();
let command = WebDriverScriptCommand::ExecuteScript(script, sender);
self.execute_script(command, reciever)
self.execute_script(command, receiver)
}
fn handle_execute_async_script(&self,
@ -424,7 +442,7 @@ impl Handler {
let (sender, reciever) = ipc::channel().unwrap();
let command = WebDriverScriptCommand::ExecuteAsyncScript(script, sender);
self.execute_script(command, reciever)
self.execute_script(command, receiver)
}
fn execute_script(&self,
@ -437,7 +455,7 @@ impl Handler {
let cmd_msg = WebDriverCommandMsg::ScriptCommand(pipeline_id, command);
const_chan.send(ConstellationMsg::WebDriverCommand(cmd_msg)).unwrap();
match reciever.recv().unwrap() {
match receiver.recv().unwrap() {
Ok(value) => Ok(WebDriverResponse::Generic(ValueResponse::new(value.to_json()))),
Err(WebDriverJSError::Timeout) => Err(WebDriverError::new(ErrorStatus::Timeout, "")),
Err(WebDriverJSError::UnknownType) => Err(WebDriverError::new(
@ -458,7 +476,7 @@ impl Handler {
let cmd_msg = WebDriverCommandMsg::TakeScreenshot(pipeline_id, sender);
const_chan.send(ConstellationMsg::WebDriverCommand(cmd_msg)).unwrap();
if let Some(x) = reciever.recv().unwrap() {
if let Some(x) = receiver.recv().unwrap() {
img = Some(x);
break;
};
@ -507,6 +525,7 @@ impl WebDriverHandler for Handler {
WebDriverCommand::GetCurrentUrl => self.handle_get_current_url(),
WebDriverCommand::GoBack => self.handle_go_back(),
WebDriverCommand::GoForward => self.handle_go_forward(),
WebDriverCommand::Refresh => self.handle_refresh(),
WebDriverCommand::GetTitle => self.handle_get_title(),
WebDriverCommand::GetWindowHandle => self.handle_get_window_handle(),
WebDriverCommand::GetWindowHandles => self.handle_get_window_handles(),