webdriver: Update for beta.
authorJames Graham <james@hoppipolla.co.uk>
Wed, 08 Apr 2015 18:53:24 +0100
changeset 428019 7cec10a6cb5bce21a3d5fd31f8bc268857b8c46c
parent 428018 f6683d0ab854176990088859420c3e7f4132b0fe
child 428020 d67257facd3ab45d2d9390aea1ca0cdcf3fb7357
push id7761
push userjlund@mozilla.com
push dateFri, 15 Sep 2017 00:19:52 +0000
treeherdermozilla-beta@c38455951db4 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
milestone57.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
webdriver: Update for beta. Source-Repo: https://github.com/mozilla/webdriver-rust Source-Revision: 435bc91f1297b957798d52bf0f445cab754fc186
testing/webdriver/Cargo.toml
testing/webdriver/src/command.rs
testing/webdriver/src/common.rs
testing/webdriver/src/error.rs
testing/webdriver/src/lib.rs
testing/webdriver/src/server.rs
--- a/testing/webdriver/Cargo.toml
+++ b/testing/webdriver/Cargo.toml
@@ -1,12 +1,12 @@
 [package]
 
 name = "webdriver"
-version = "0.0.1"
+version = "0.1.0"
 authors = ["James Graham <james@hoppipolla.co.uk>"]
 
 [dependencies]
-log = "0.2.5"
-regex = "0.1.18"
-rustc-serialize = "0.3.4"
-uuid = "0.1.11"
-hyper = "0.3.0"
+log = "0.3.1"
+regex = "0.1.27"
+rustc-serialize = "0.3.12"
+uuid = "0.1.16"
+hyper = "0.3.10"
--- a/testing/webdriver/src/command.rs
+++ b/testing/webdriver/src/command.rs
@@ -1,12 +1,11 @@
 use regex::Captures;
 use rustc_serialize::json::{ToJson, Json};
 use std::collections::BTreeMap;
-use std::str::StrExt;
 
 use common::{Date, Nullable, WebElement, FrameId, LocatorStrategy};
 use error::{WebDriverResult, WebDriverError, ErrorStatus};
 use httpapi::Route;
 
 
 #[derive(PartialEq)]
 pub enum WebDriverCommand {
@@ -543,21 +542,22 @@ impl Parameters for SendKeysParameters {
                                            "Missing 'value' parameter").as_array(),
                                   ErrorStatus::InvalidArgument,
                                   "Could not convert 'value' to array");
 
         let value = try!(value_json.iter().map(|x| {
            let str_value = try_opt!(x.as_string(),
                                     ErrorStatus::InvalidArgument,
                                     "Value was not a string");
-            if str_value.chars().collect::<Vec<char>>().len() != 1 {
+            let chars = str_value.chars().collect::<Vec<char>>();
+            if chars.len() != 1 {
                 return Err(WebDriverError::new(ErrorStatus::InvalidArgument,
                                                "Value was not a string"));
             }
-            Ok(str_value.char_at(0))
+            Ok(chars[0])
         }).collect::<Result<Vec<_>, _>>());
 
         Ok(SendKeysParameters {
             value: value
         })
     }
 }
 
--- a/testing/webdriver/src/common.rs
+++ b/testing/webdriver/src/common.rs
@@ -1,13 +1,12 @@
 use rustc_serialize::json::{Json, ToJson};
 use rustc_serialize::{Encodable, Encoder};
 use std::collections::BTreeMap;
-use std::error::{Error, FromError};
-use std::num::ToPrimitive;
+use std::error::Error;
 
 use error::{WebDriverResult, WebDriverError, ErrorStatus};
 
 pub static ELEMENT_KEY: &'static str = "element-6066-11e4-a52e-4f735466cecf";
 
 #[derive(RustcEncodable, PartialEq, Debug)]
 pub struct Date(u64);
 
@@ -117,20 +116,21 @@ pub enum FrameId {
     Element(WebElement),
     Null
 }
 
 impl FrameId {
     pub fn from_json(data: &Json) -> WebDriverResult<FrameId> {
         match data {
             &Json::U64(x) => {
-                let id = try_opt!(x.to_u16(),
-                                  ErrorStatus::NoSuchFrame,
-                                  "frame id out of range");
-                Ok(FrameId::Short(id))
+                if x > u16::max_value() as u64 || x < u16::min_value() as u64 {
+                    return Err(WebDriverError::new(ErrorStatus::NoSuchFrame,
+                                                   "frame id out of range"))
+                };
+                Ok(FrameId::Short(x as u16))
             },
             &Json::Null => Ok(FrameId::Null),
             &Json::Object(_) => Ok(FrameId::Element(
                 try!(WebElement::from_json(data)))),
             _ => Err(WebDriverError::new(ErrorStatus::NoSuchFrame,
                                          "frame id has unexpected type"))
         }
     }
--- a/testing/webdriver/src/error.rs
+++ b/testing/webdriver/src/error.rs
@@ -1,12 +1,13 @@
-use rustc_serialize::json::{Json, ToJson, ParserError};
+use rustc_serialize::json::{Json, ToJson};
 use std::collections::BTreeMap;
-use std::error::{Error, FromError};
+use std::error::Error;
 use std::fmt;
+use hyper::status::StatusCode;
 
 #[derive(PartialEq, Debug)]
 pub enum ErrorStatus {
     ElementNotSelectable,
     ElementNotVisible,
     InvalidArgument,
     InvalidCookieDomain,
     InvalidElementCoordinates,
@@ -77,42 +78,42 @@ impl WebDriverError {
             ErrorStatus::UnexpectedAlertOpen => "unexpected alert open",
             ErrorStatus::UnknownError => "unknown error",
             ErrorStatus::UnknownPath => "unknown command",
             ErrorStatus::UnknownMethod => "unknown command",
             ErrorStatus::UnsupportedOperation => "unsupported operation",
         }
     }
 
-    pub fn http_status(&self) -> u32 {
+    pub fn http_status(&self) -> StatusCode {
         match self.status {
-            ErrorStatus::ElementNotSelectable => 400,
-            ErrorStatus::ElementNotVisible => 400,
-            ErrorStatus::InvalidArgument => 400,
-            ErrorStatus::InvalidCookieDomain => 400,
-            ErrorStatus::InvalidElementCoordinates => 400,
-            ErrorStatus::InvalidElementState => 400,
-            ErrorStatus::InvalidSelector => 400,
-            ErrorStatus::InvalidSessionId => 404,
-            ErrorStatus::JavascriptError => 500,
-            ErrorStatus::MoveTargetOutOfBounds => 500,
-            ErrorStatus::NoSuchAlert => 400,
-            ErrorStatus::NoSuchElement => 404,
-            ErrorStatus::NoSuchFrame => 400,
-            ErrorStatus::NoSuchWindow => 400,
-            ErrorStatus::ScriptTimeout => 408,
-            ErrorStatus::SessionNotCreated => 500,
-            ErrorStatus::StaleElementReference => 400,
-            ErrorStatus::Timeout => 408,
-            ErrorStatus::UnableToSetCookie => 500,
-            ErrorStatus::UnexpectedAlertOpen => 500,
-            ErrorStatus::UnknownError => 500,
-            ErrorStatus::UnknownPath => 404,
-            ErrorStatus::UnknownMethod => 405,
-            ErrorStatus::UnsupportedOperation => 500,
+            ErrorStatus::ElementNotSelectable => StatusCode::BadRequest,
+            ErrorStatus::ElementNotVisible => StatusCode::BadRequest,
+            ErrorStatus::InvalidArgument => StatusCode::BadRequest,
+            ErrorStatus::InvalidCookieDomain => StatusCode::BadRequest,
+            ErrorStatus::InvalidElementCoordinates => StatusCode::BadRequest,
+            ErrorStatus::InvalidElementState => StatusCode::BadRequest,
+            ErrorStatus::InvalidSelector => StatusCode::BadRequest,
+            ErrorStatus::InvalidSessionId => StatusCode::NotFound,
+            ErrorStatus::JavascriptError => StatusCode::InternalServerError,
+            ErrorStatus::MoveTargetOutOfBounds => StatusCode::InternalServerError,
+            ErrorStatus::NoSuchAlert => StatusCode::BadRequest,
+            ErrorStatus::NoSuchElement => StatusCode::NotFound,
+            ErrorStatus::NoSuchFrame => StatusCode::BadRequest,
+            ErrorStatus::NoSuchWindow => StatusCode::BadRequest,
+            ErrorStatus::ScriptTimeout => StatusCode::RequestTimeout,
+            ErrorStatus::SessionNotCreated => StatusCode::InternalServerError,
+            ErrorStatus::StaleElementReference => StatusCode::BadRequest,
+            ErrorStatus::Timeout => StatusCode::RequestTimeout,
+            ErrorStatus::UnableToSetCookie => StatusCode::InternalServerError,
+            ErrorStatus::UnexpectedAlertOpen => StatusCode::InternalServerError,
+            ErrorStatus::UnknownError => StatusCode::InternalServerError,
+            ErrorStatus::UnknownPath => StatusCode::NotFound,
+            ErrorStatus::UnknownMethod => StatusCode::MethodNotAllowed,
+            ErrorStatus::UnsupportedOperation => StatusCode::InternalServerError,
         }
     }
 
     pub fn to_json_string(&self) -> String {
         self.to_json().to_string()
     }
 }
 
@@ -129,15 +130,8 @@ impl Error for WebDriverError {
     fn description(&self) -> &str {
         self.status_code()
     }
 
     fn cause(&self) -> Option<&Error> {
         None
     }
 }
-
-impl FromError<ParserError> for WebDriverError {
-    fn from_error(err: ParserError) -> WebDriverError {
-        let msg = format!("{:?}", err);
-        WebDriverError::new(ErrorStatus::UnknownError, &msg[..])
-    }
-}
--- a/testing/webdriver/src/lib.rs
+++ b/testing/webdriver/src/lib.rs
@@ -1,21 +1,16 @@
-#![feature(io)]
-#![feature(net)]
-#![feature(core)]
-#![feature(collections)]
 #![allow(non_snake_case)]
 
 #[macro_use]
 extern crate log;
-extern crate "rustc-serialize" as rustc_serialize;
+extern crate rustc_serialize;
 extern crate hyper;
 extern crate regex;
 
-
 #[macro_use] pub mod macros;
 mod httpapi;
 pub mod command;
 pub mod common;
 pub mod error;
 pub mod server;
 pub mod response;
 
--- a/testing/webdriver/src/server.rs
+++ b/testing/webdriver/src/server.rs
@@ -1,19 +1,18 @@
-use std::net::IpAddr;
-use std::num::FromPrimitive;
 use std::io::{Write, Read};
 use std::sync::Mutex;
 use std::sync::mpsc::{channel, Receiver, Sender};
 use std::thread;
 
 use hyper::header::ContentLength;
 use hyper::method::Method;
 use hyper::server::{Server, Handler, Request, Response};
 use hyper::uri::RequestUri::AbsolutePath;
+use hyper::status::StatusCode;
 
 use command::{WebDriverMessage, WebDriverCommand};
 use error::{WebDriverResult, WebDriverError, ErrorStatus};
 use httpapi::WebDriverHttpApi;
 use response::WebDriverResponse;
 
 enum DispatchMessage {
     HandleWebDriver(WebDriverMessage, Sender<WebDriverResult<WebDriverResponse>>),
@@ -188,52 +187,47 @@ impl Handler for HttpHandler {
                             },
                             Err(_) => {
                                 error!("Something terrible happened");
                                 return
                             }
                         }
                         match recv_res.recv() {
                             Ok(data) => match data {
-                                Ok(response) => (200, response.to_json_string()),
+                                Ok(response) => (StatusCode::Ok, response.to_json_string()),
                                 Err(err) => (err.http_status(), err.to_json_string()),
                             },
                             Err(_) => panic!("Error reading response")
                         }
                     },
                     Err(err) => {
                         (err.http_status(), err.to_json_string())
                     }
                 };
-                if status != 200 {
-                    error!("Returning status code {}", status);
-                    error!("Returning body {}", resp_body);
-                } else {
-                    debug!("Returning status code {}", status);
-                    debug!("Returning body {}", resp_body);
-                }
+                error!("Returning status {:?}", status);
+                error!("Returning body {}", resp_body);
                 {
-                    let status_code = res.status_mut();
-                    *status_code = FromPrimitive::from_u32(status).unwrap();
+                    let resp_status = res.status_mut();
+                    *resp_status = status;
                 }
                 res.headers_mut().set(ContentLength(resp_body.len() as u64));
                 let mut stream = res.start().unwrap();
                 stream.write_all(&resp_body.as_bytes()).unwrap();
                 stream.end().unwrap();
             },
             _ => {}
         }
     }
 }
 
-pub fn start<T: 'static+WebDriverHandler>(ip_address: IpAddr, port: u16, handler: T) {
+pub fn start<T: 'static+WebDriverHandler>(ip_address: &str, port: u16, handler: T) {
     let (msg_send, msg_recv) = channel();
 
     let api = WebDriverHttpApi::new();
     let http_handler = HttpHandler::new(api, msg_send);
     let server = Server::http(http_handler);
 
     thread::spawn(move || {
         let mut dispatcher = Dispatcher::new(handler);
         dispatcher.run(msg_recv)
     });
-    server.listen(ip_address, port).unwrap();
+    server.listen(&(ip_address, port)).unwrap();
 }