Skip to content

Commit

Permalink
webdriver: Merge pull request mozilla#30 from jgraham/new_session_cap…
Browse files Browse the repository at this point in the history
…abilities

Add support for capabilites in New Session command

Source-Repo: https://github.com/mozilla/webdriver-rust
Source-Revision: a47ba3d93ab667259c839df4e5d11bef778455a4
  • Loading branch information
jgraham committed May 23, 2016
1 parent 1736ca4 commit c275b3e
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 16 deletions.
55 changes: 51 additions & 4 deletions testing/webdriver/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use httpapi::{Route, WebDriverExtensionRoute, VoidWebDriverExtensionRoute};

#[derive(PartialEq)]
pub enum WebDriverCommand<T: WebDriverExtensionCommand> {
NewSession,
NewSession(NewSessionParameters),
DeleteSession,
Get(GetParameters),
GetCurrentUrl,
Expand Down Expand Up @@ -104,7 +104,10 @@ impl <U: WebDriverExtensionRoute> WebDriverMessage<U> {
Json::Null
};
let command = match match_type {
Route::NewSession => WebDriverCommand::NewSession,
Route::NewSession => {
let parameters: NewSessionParameters = try!(Parameters::from_json(&body_data));
WebDriverCommand::NewSession(parameters)
},
Route::DeleteSession => WebDriverCommand::DeleteSession,
Route::Get => {
let parameters: GetParameters = try!(Parameters::from_json(&body_data));
Expand Down Expand Up @@ -313,7 +316,7 @@ impl <U: WebDriverExtensionRoute> WebDriverMessage<U> {
impl <U:WebDriverExtensionRoute> ToJson for WebDriverMessage<U> {
fn to_json(&self) -> Json {
let parameters = match self.command {
WebDriverCommand::NewSession |
WebDriverCommand::NewSession(_) |
WebDriverCommand::DeleteSession | WebDriverCommand::GetCurrentUrl |
WebDriverCommand::GoBack | WebDriverCommand::GoForward | WebDriverCommand::Refresh |
WebDriverCommand::GetTitle | WebDriverCommand::GetPageSource |
Expand Down Expand Up @@ -361,6 +364,50 @@ pub trait Parameters: Sized {
fn from_json(body: &Json) -> WebDriverResult<Self>;
}

#[derive(PartialEq)]
pub struct NewSessionParameters {
pub desired: BTreeMap<String, Json>,
pub required: BTreeMap<String, Json>,
}

impl Parameters for NewSessionParameters {
fn from_json(body: &Json) -> WebDriverResult<NewSessionParameters> {
let data = try_opt!(body.as_object(),
ErrorStatus::UnknownError,
"Message body was not an object");

let desired_capabilities =
if let Some(capabilities) = data.get("desiredCapabilities") {
try_opt!(capabilities.as_object(),
ErrorStatus::InvalidArgument,
"'desiredCapabilities' parameter is not an object").clone()
} else {
BTreeMap::new()
};

let required_capabilities =
if let Some(capabilities) = data.get("requiredCapabilities") {
try_opt!(capabilities.as_object(),
ErrorStatus::InvalidArgument,
"'requiredCapabilities' parameter is not an object").clone()
} else {
BTreeMap::new()
};

Ok(NewSessionParameters {
desired: desired_capabilities,
required: required_capabilities
})
}
}

impl NewSessionParameters {
pub fn get(&self, name: &str) -> Option<&Json> {
debug!("Getting {} from capabilities", name);
self.required.get(name).or_else(|| self.desired.get(name))
}
}

#[derive(PartialEq)]
pub struct GetParameters {
pub url: String
Expand All @@ -376,7 +423,7 @@ impl Parameters for GetParameters {
"Missing 'url' parameter").as_string(),
ErrorStatus::InvalidArgument,
"'url' not a string");
return Ok(GetParameters {
Ok(GetParameters {
url: url.to_string()
})
}
Expand Down
21 changes: 18 additions & 3 deletions testing/webdriver/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use hyper::status::StatusCode;
use rustc_serialize::base64::FromBase64Error;
use rustc_serialize::json::{Json, ToJson, ParserError, DecoderError};
use std::borrow::Cow;
use std::collections::BTreeMap;
Expand Down Expand Up @@ -164,20 +165,34 @@ impl Error for WebDriverError {
impl From<ParserError> for WebDriverError {
fn from(err: ParserError) -> WebDriverError {
WebDriverError::new(ErrorStatus::UnknownError,
err.description().to_string())
err.description().to_string())
}
}

impl From<IoError> for WebDriverError {
fn from(err: IoError) -> WebDriverError {
WebDriverError::new(ErrorStatus::UnknownError,
err.description().to_string())
err.description().to_string())
}
}

impl From<DecoderError> for WebDriverError {
fn from(err: DecoderError) -> WebDriverError {
WebDriverError::new(ErrorStatus::UnknownError,
format!("Could not decode json string:\n{}", err.description()))
err.description().to_string())
}
}

impl From<FromBase64Error> for WebDriverError {
fn from(err: FromBase64Error) -> WebDriverError {
WebDriverError::new(ErrorStatus::UnknownError,
err.description().to_string())
}
}

impl From<Box<Error>> for WebDriverError {
fn from(err: Box<Error>) -> WebDriverError {
WebDriverError::new(ErrorStatus::UnknownError,
err.description().to_string())
}
}
16 changes: 7 additions & 9 deletions testing/webdriver/src/server.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use std::marker::PhantomData;
use std::io::{Write, Read};
use std::io::Read;
use std::sync::Mutex;
use std::sync::mpsc::{channel, Receiver, Sender};
use std::thread;
use std::net::SocketAddr;

use hyper::header::{ContentLength, ContentType};
use hyper::header::ContentType;
use hyper::method::Method;
use hyper::server::{Server, Handler, Request, Response};
use hyper::uri::RequestUri::AbsolutePath;
Expand Down Expand Up @@ -118,7 +118,7 @@ impl <T: WebDriverHandler<U>,
match self.session {
Some(_) => {
match msg.command {
WebDriverCommand::NewSession => {
WebDriverCommand::NewSession(_) => {
Err(WebDriverError::new(
ErrorStatus::UnsupportedOperation,
"Session is already started"))
Expand All @@ -134,7 +134,7 @@ impl <T: WebDriverHandler<U>,
},
None => {
match msg.command {
WebDriverCommand::NewSession => Ok(()),
WebDriverCommand::NewSession(_) => Ok(()),

_ => Err(WebDriverError::new(
ErrorStatus::InvalidSessionId,
Expand Down Expand Up @@ -221,11 +221,8 @@ impl <U: WebDriverExtensionRoute> Handler for HttpHandler<U> {
let resp_status = res.status_mut();
*resp_status = status;
}
res.headers_mut().set(ContentLength(resp_body.len() as u64));
res.headers_mut().set(ContentType::json());
let mut stream = res.start().unwrap();
stream.write_all(&resp_body.as_bytes()).unwrap();
stream.end().unwrap();
res.send(&resp_body.as_bytes()).unwrap();
},
_ => {}
}
Expand All @@ -240,7 +237,8 @@ pub fn start<T: 'static+WebDriverHandler<U>,

let api = WebDriverHttpApi::new(extension_routes);
let http_handler = HttpHandler::new(api, msg_send);
let server = Server::http(address).unwrap();
let mut server = Server::http(address).unwrap();
server.keep_alive(None);

let builder = thread::Builder::new().name("webdriver dispatcher".to_string());
builder.spawn(move || {
Expand Down

0 comments on commit c275b3e

Please sign in to comment.