webdriver: Fix conversion of SendKeys input
authorJames Graham <james@hoppipolla.co.uk>
Mon, 02 Mar 2015 12:38:37 +0000
changeset 428008 8c40b307f6ed05598c4ab500369adc107590a82a
parent 428007 531718ef916435895d7aa40c49744e81566108a1
child 428009 0633b16221e9e6ac1867dc9e5c09de729811991f
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: Fix conversion of SendKeys input Source-Repo: https://github.com/mozilla/webdriver-rust Source-Revision: 65d155c9b4a3928445e4ba9f2ffbcb7853a57b2b
testing/webdriver/src/command.rs
testing/webdriver/src/common.rs
testing/webdriver/src/lib.rs
--- a/testing/webdriver/src/command.rs
+++ b/testing/webdriver/src/command.rs
@@ -1,11 +1,12 @@
-use std::collections::BTreeMap;
+use regex::Captures;
 use rustc_serialize::json::{ToJson, Json};
-use regex::Captures;
+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 {
@@ -455,17 +456,17 @@ impl Parameters for LocatorParameters {
         let using = try!(LocatorStrategy::from_json(
             try_opt!(data.get("using"),
                      ErrorStatus::InvalidArgument,
                      "Missing 'using' parameter")));
 
         let value = try_opt!(
             try_opt!(data.get("value"),
                      ErrorStatus::InvalidArgument,
-                     "Missing 'using' parameter").as_string(),
+                     "Missing 'value' parameter").as_string(),
             ErrorStatus::InvalidArgument,
             "Could not convert using to string").to_string();
 
         return Ok(LocatorParameters {
             using: using,
             value: value
         })
     }
@@ -505,45 +506,54 @@ impl ToJson for SwitchToFrameParameters 
         let mut data = BTreeMap::new();
         data.insert("id".to_string(), self.id.to_json());
         Json::Object(data)
     }
 }
 
 #[derive(PartialEq)]
 pub struct SendKeysParameters {
-    pub value: Nullable<Vec<Json>>
+    pub value: Vec<char>
 }
 
 impl Parameters for SendKeysParameters {
     fn from_json(body: &Json) -> WebDriverResult<SendKeysParameters> {
         let data = try_opt!(body.as_object(),
                             ErrorStatus::InvalidArgument,
                             "Message body was not an object");
-        let value_json= try_opt!(data.get("value"),
-                             ErrorStatus::InvalidArgument,
-                             "Missing 'value' parameter");
-        let value = try!(Nullable::from_json(
-            value_json,
-            |x| {
-                Ok((try_opt!(x.as_array(),
-                             ErrorStatus::InvalidArgument,
-                             "Failed to convert args to Array")).clone())
-            }));
+        let value_json = try_opt!(try_opt!(data.get("value"),
+                                           ErrorStatus::InvalidArgument,
+                                           "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 {
+                return Err(WebDriverError::new(ErrorStatus::InvalidArgument,
+                                               "Value was not a string"));
+            }
+            Ok(str_value.char_at(0))
+        }).collect::<Result<Vec<_>, _>>());
 
         Ok(SendKeysParameters {
             value: value
         })
     }
 }
 
 impl ToJson for SendKeysParameters {
     fn to_json(&self) -> Json {
         let mut data = BTreeMap::new();
-        data.insert("value".to_string(), self.value.to_json());
+        let value_string: Vec<String> = self.value.iter().map(|x| {
+            x.to_string()
+        }).collect();
+        data.insert("value".to_string(), value_string.to_json());
         Json::Object(data)
     }
 }
 
 #[derive(PartialEq)]
 pub struct JavascriptCommandParameters {
     script: String,
     args: Nullable<Vec<Json>>
--- a/testing/webdriver/src/common.rs
+++ b/testing/webdriver/src/common.rs
@@ -1,17 +1,17 @@
 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 error::{WebDriverResult, WebDriverError, ErrorStatus};
 
-static ELEMENT_KEY: &'static str = "element-6066-11e4-a52e-4f735466cecf";
+pub static ELEMENT_KEY: &'static str = "element-6066-11e4-a52e-4f735466cecf";
 
 #[derive(RustcEncodable, PartialEq, Debug)]
 pub struct Date(u64);
 
 impl Date {
     pub fn new(timestamp: u64) -> Date {
         Date(timestamp)
     }
--- a/testing/webdriver/src/lib.rs
+++ b/testing/webdriver/src/lib.rs
@@ -1,10 +1,11 @@
 #![feature(old_io)]
 #![feature(core)]
+#![feature(collections)]
 #![allow(non_snake_case)]
 
 #[macro_use]
 extern crate log;
 extern crate "rustc-serialize" as rustc_serialize;
 extern crate hyper;
 extern crate regex;