Skip to content

Commit

Permalink
Bumping version to 0.4.0
Browse files Browse the repository at this point in the history
  • Loading branch information
AutomatedTester committed Sep 28, 2015
1 parent 4bd5c36 commit 9d026f1
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 209 deletions.
67 changes: 1 addition & 66 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
@@ -1,6 +1,6 @@
[package]
name = "wires"
version = "0.3.0"
version = "0.4.0"
authors = ["James Graham <james@hoppipolla.co.uk>"]
description = "Proxy for using WebDriver clients to interact with Gecko-based browsers."
repository = "https://github.com/jgraham/wires"
Expand Down
8 changes: 3 additions & 5 deletions src/main.rs
@@ -1,12 +1,10 @@
#[macro_use]
extern crate log;
extern crate rustc_serialize;
extern crate argparse;
extern crate env_logger;
extern crate hyper;
extern crate mozprofile;
extern crate mozrunner;
extern crate regex;
extern crate rustc_serialize;
#[macro_use]
extern crate webdriver;

Expand All @@ -19,7 +17,7 @@ use std::path::Path;
use argparse::{ArgumentParser, StoreTrue, Store};
use webdriver::server::start;

use marionette::{MarionetteHandler, BrowserLauncher, MarionetteSettings, extension_routes};
use marionette::{MarionetteHandler, BrowserLauncher, MarionetteSettings};

macro_rules! try_opt {
($expr:expr, $err_type:expr, $err_msg:expr) => ({
Expand Down Expand Up @@ -102,5 +100,5 @@ fn main() {
launcher);

//TODO: what if binary isn't a valid path?
start(addr, MarionetteHandler::new(settings), extension_routes());
start(addr, MarionetteHandler::new(settings), vec![]);
}
152 changes: 15 additions & 137 deletions src/marionette.rs
Expand Up @@ -10,13 +10,11 @@ use std::net::TcpStream;
use std::path::PathBuf;
use std::sync::Mutex;
use std::thread::sleep_ms;
use hyper::method::Method;
use regex::Captures;

use mozrunner::runner::{Runner, FirefoxRunner};
use mozprofile::preferences::{PrefValue};

use webdriver::command::{WebDriverCommand, WebDriverMessage, Parameters,
WebDriverExtensionCommand};
use webdriver::command::{WebDriverMessage};
use webdriver::command::WebDriverCommand::{
NewSession, DeleteSession, Get, GetCurrentUrl,
GoBack, GoForward, Refresh, GetTitle, GetWindowHandle,
Expand All @@ -43,113 +41,6 @@ use webdriver::common::{
use webdriver::error::{
WebDriverResult, WebDriverError, ErrorStatus};
use webdriver::server::{WebDriverHandler, Session};
use webdriver::httpapi::{WebDriverExtensionRoute};

pub fn extension_routes() -> Vec<(Method, &'static str, GeckoExtensionRoute)> {
return vec![(Method::Get, "/session/{sessionId}/moz/context", GeckoExtensionRoute::GetContext),
(Method::Post, "/session/{sessionId}/moz/context", GeckoExtensionRoute::SetContext)]
}

#[derive(Clone, Copy, PartialEq)]
pub enum GeckoExtensionRoute {
GetContext,
SetContext
}

impl WebDriverExtensionRoute for GeckoExtensionRoute {
type Command = GeckoExtensionCommand;

fn command(&self,
_captures: &Captures,
body_data: &Json) -> WebDriverResult<WebDriverCommand<GeckoExtensionCommand>> {
let command = match self {
&GeckoExtensionRoute::GetContext => {
GeckoExtensionCommand::GetContext
}
&GeckoExtensionRoute::SetContext => {
let parameters: GeckoContextParameters = try!(Parameters::from_json(&body_data));
GeckoExtensionCommand::SetContext(parameters)
}
};
Ok(WebDriverCommand::Extension(command))
}
}

#[derive(Clone, PartialEq)]
pub enum GeckoExtensionCommand {
GetContext,
SetContext(GeckoContextParameters),
}

impl WebDriverExtensionCommand for GeckoExtensionCommand {
fn parameters_json(&self) -> Option<Json> {
match self {
&GeckoExtensionCommand::GetContext => None,
&GeckoExtensionCommand::SetContext(ref x) => Some(x.to_json()),
}
}
}

#[derive(Clone, Debug, PartialEq)]
enum GeckoContext {
Content,
Chrome
}

impl ToJson for GeckoContext {
fn to_json(&self) -> Json {
match self {
&GeckoContext::Content => Json::String("content".to_owned()),
&GeckoContext::Chrome => Json::String("chrome".to_owned()),
}
}
}

#[derive(Clone, Debug, PartialEq)]
struct GeckoContextParameters {
context: GeckoContext
}

impl Parameters for GeckoContextParameters {
fn from_json(body: &Json) -> WebDriverResult<GeckoContextParameters> {
let data = try!(body.as_object().ok_or(
WebDriverError::new(ErrorStatus::InvalidArgument,
"Message body was not an object")));
let context_value = try!(data.get("context").ok_or(
WebDriverError::new(ErrorStatus::InvalidArgument,
"Missing context key")));
let value = try!(context_value.as_string().ok_or(
WebDriverError::new(
ErrorStatus::InvalidArgument,
"context was not a string")));
let context = try!(match value {
"chrome" => Ok(GeckoContext::Chrome),
"content" => Ok(GeckoContext::Content),
_ => Err(WebDriverError::new(ErrorStatus::InvalidArgument,
&format!("{} is not a valid context",
value)))
});
Ok(GeckoContextParameters {
context: context
})
}
}

impl ToJson for GeckoContextParameters {
fn to_json(&self) -> Json {
let mut data = BTreeMap::new();
data.insert("context".to_owned(), self.context.to_json());
Json::Object(data)
}
}

impl ToMarionette for GeckoContextParameters {
fn to_marionette(&self) -> WebDriverResult<Json> {
let mut data = BTreeMap::new();
data.insert("value".to_owned(), self.context.to_json());
Ok(Json::Object(data))
}
}

pub static FIREFOX_PREFERENCES: [(&'static str, PrefValue); 49] = [
("app.update.enabled", PrefValue::PrefBool(false)),
Expand Down Expand Up @@ -271,8 +162,8 @@ impl MarionetteHandler {
}
}

impl WebDriverHandler<GeckoExtensionRoute> for MarionetteHandler {
fn handle_command(&mut self, _: &Option<Session>, msg: &WebDriverMessage<GeckoExtensionRoute>) -> WebDriverResult<WebDriverResponse> {
impl WebDriverHandler for MarionetteHandler {
fn handle_command(&mut self, _: &Option<Session>, msg: &WebDriverMessage) -> WebDriverResult<WebDriverResponse> {
let mut create_connection = false;
match self.connection.lock() {
Ok(ref mut connection) => {
Expand Down Expand Up @@ -349,15 +240,15 @@ impl MarionetteSession {
}
}

pub fn msg_to_marionette(&self, msg: &WebDriverMessage<GeckoExtensionRoute>) -> WebDriverResult<Json> {
pub fn msg_to_marionette(&self, msg: &WebDriverMessage) -> WebDriverResult<Json> {
let x = try!(msg.to_marionette());
let data = try_opt!(x.as_object(),
ErrorStatus::UnknownError,
"Message was not a JSON Object").clone();
Ok(Json::Object(data))
}

pub fn update(&mut self, msg: &WebDriverMessage<GeckoExtensionRoute>,
pub fn update(&mut self, msg: &WebDriverMessage,
resp: &Json) -> WebDriverResult<()> {
match msg.command {
NewSession => {
Expand Down Expand Up @@ -397,7 +288,7 @@ impl MarionetteSession {
Ok(WebElement::new(id))
}

pub fn response_from_json(&mut self, message: &WebDriverMessage<GeckoExtensionRoute>,
pub fn response_from_json(&mut self, message: &WebDriverMessage,
data: &str) -> WebDriverResult<WebDriverResponse> {

let json_data = try!(Json::from_str(data));
Expand Down Expand Up @@ -549,16 +440,8 @@ impl MarionetteSession {
DeleteSession => {
WebDriverResponse::DeleteSession
},
Extension(ref extension) => {
match extension {
&GeckoExtensionCommand::GetContext => {
let value = try_opt!(json_data.find("value"),
ErrorStatus::UnknownError,
"Failed to find value field");
WebDriverResponse::Generic(ValueResponse::new(value.clone()))
}
&GeckoExtensionCommand::SetContext(_) => WebDriverResponse::Void
}
Extension(_) => {
panic!("No extensions implemented")
}
})
}
Expand Down Expand Up @@ -724,7 +607,7 @@ impl MarionetteConnection {
format!("{}:{}", data.len(), data)
}

pub fn send_message(&mut self, msg: &WebDriverMessage<GeckoExtensionRoute>) -> WebDriverResult<WebDriverResponse> {
pub fn send_message(&mut self, msg: &WebDriverMessage) -> WebDriverResult<WebDriverResponse> {
let resp = try!(self.session.msg_to_marionette(msg));
let resp_data = try!(self.send(&resp));
self.session.response_from_json(msg, &resp_data[..])
Expand Down Expand Up @@ -815,7 +698,7 @@ trait ToMarionette {
fn to_marionette(&self) -> WebDriverResult<Json>;
}

impl ToMarionette for WebDriverMessage<GeckoExtensionRoute> {
impl ToMarionette for WebDriverMessage {
fn to_marionette(&self) -> WebDriverResult<Json> {
let (opt_name, opt_parameters) = match self.command {
NewSession => {
Expand Down Expand Up @@ -852,7 +735,7 @@ impl ToMarionette for WebDriverMessage<GeckoExtensionRoute> {

let mut data = try_opt!(body.unwrap().as_object(),
ErrorStatus::UnknownError,
"Marionette request was not an object").clone();
"Marionette response was not an object").clone();
data.insert("element".to_string(), e.id.to_json());
(Some("findElement"), Some(Ok(Json::Object(data.clone()))))
},
Expand All @@ -864,7 +747,7 @@ impl ToMarionette for WebDriverMessage<GeckoExtensionRoute> {

let mut data = try_opt!(body.unwrap().as_object(),
ErrorStatus::UnknownError,
"Marionette request was not an object").clone();
"Marionette response was not an object").clone();
data.insert("element".to_string(), e.id.to_json());
(Some("findElements"), Some(Ok(Json::Object(data.clone()))))
},
Expand Down Expand Up @@ -924,13 +807,8 @@ impl ToMarionette for WebDriverMessage<GeckoExtensionRoute> {
data.insert("full".to_string(), Json::Boolean(false));
(Some("takeScreenshot"), Some(Ok(Json::Object(data))))
},
Extension(ref extension) => {
match extension {
&GeckoExtensionCommand::GetContext => (Some("getContext"), None),
&GeckoExtensionCommand::SetContext(ref x) => {
(Some("setContext"), Some(x.to_marionette()))
}
}
Extension(_) => {
panic!("No extensions implemented");
}
};

Expand Down

0 comments on commit 9d026f1

Please sign in to comment.