webdriver: command: add window rect and remove size/position
authorAndreas Tolfsen <ato@mozilla.com>
Fri, 24 Mar 2017 15:40:56 +0000
changeset 428130 5cd31994a52b49a383802eab478f2d2c1e9246d6
parent 428129 e9b1b5d4d1779f77007021848bce43a15e73d8a1
child 428131 89d24fe294e00f2c3b64f5243fc2ffc209828dcc
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: command: add window rect and remove size/position Introduces new WebDriver commands GetWindowRect and SetWindowRect, which replace GetWindowSize, SetWindowSize, and GetWindowPosition, SetWindowPosition. The routes for the removed commands have been mapped to the new commands since their input can be used interchangably with the new commands. Source-Repo: https://github.com/mozilla/webdriver-rust Source-Revision: df8e357427b720fe0be03d4e212cf74323ceb3d3 committer: jgraham <james@hoppipolla.co.uk>
testing/webdriver/src/command.rs
--- a/testing/webdriver/src/command.rs
+++ b/testing/webdriver/src/command.rs
@@ -3,35 +3,32 @@ use capabilities::{SpecNewSessionParamet
 use common::{Date, Nullable, WebElement, FrameId, LocatorStrategy};
 use error::{WebDriverResult, WebDriverError, ErrorStatus};
 use httpapi::{Route, WebDriverExtensionRoute, VoidWebDriverExtensionRoute};
 use regex::Captures;
 use rustc_serialize::json::{ToJson, Json};
 use std::collections::BTreeMap;
 use std::default::Default;
 
-
 #[derive(PartialEq)]
 pub enum WebDriverCommand<T: WebDriverExtensionCommand> {
     NewSession(NewSessionParameters),
     DeleteSession,
     Get(GetParameters),
     GetCurrentUrl,
     GoBack,
     GoForward,
     Refresh,
     GetTitle,
     GetPageSource,
     GetWindowHandle,
     GetWindowHandles,
     CloseWindow,
-    GetWindowSize,
-    SetWindowSize(WindowSizeParameters),
-    GetWindowPosition,
-    SetWindowPosition(WindowPositionParameters),
+    GetWindowRect,
+    SetWindowRect(WindowRectParameters),
     MaximizeWindow,
 //    FullscreenWindow // Not supported in marionette
     SwitchToWindow(SwitchToWindowParameters),
     SwitchToFrame(SwitchToFrameParameters),
     SwitchToParentFrame,
     FindElement(LocatorParameters),
     FindElements(LocatorParameters),
     FindElementElement(WebElement, LocatorParameters),
@@ -131,25 +128,20 @@ impl <U: WebDriverExtensionRoute> WebDri
             Route::GetWindowHandle => WebDriverCommand::GetWindowHandle,
             Route::GetWindowHandles => WebDriverCommand::GetWindowHandles,
             Route::CloseWindow => WebDriverCommand::CloseWindow,
             Route::GetTimeouts => WebDriverCommand::GetTimeouts,
             Route::SetTimeouts => {
                 let parameters: TimeoutsParameters = try!(Parameters::from_json(&body_data));
                 WebDriverCommand::SetTimeouts(parameters)
             },
-            Route::GetWindowSize => WebDriverCommand::GetWindowSize,
-            Route::SetWindowSize => {
-                let parameters: WindowSizeParameters = try!(Parameters::from_json(&body_data));
-                WebDriverCommand::SetWindowSize(parameters)
-            },
-            Route::GetWindowPosition => WebDriverCommand::GetWindowPosition,
-            Route::SetWindowPosition => {
-                let parameters: WindowPositionParameters = try!(Parameters::from_json(&body_data));
-                WebDriverCommand::SetWindowPosition(parameters)
+            Route::GetWindowRect | Route::GetWindowPosition | Route::GetWindowSize => WebDriverCommand::GetWindowRect,
+            Route::SetWindowRect | Route::SetWindowPosition | Route::SetWindowSize => {
+                let parameters: WindowRectParameters = Parameters::from_json(&body_data)?;
+                WebDriverCommand::SetWindowRect(parameters)
             },
             Route::MaximizeWindow => WebDriverCommand::MaximizeWindow,
             Route::SwitchToWindow => {
                 let parameters: SwitchToWindowParameters = try!(Parameters::from_json(&body_data));
                 WebDriverCommand::SwitchToWindow(parameters)
             }
             Route::SwitchToFrame => {
                 let parameters: SwitchToFrameParameters = try!(Parameters::from_json(&body_data));
@@ -377,18 +369,17 @@ impl <U:WebDriverExtensionRoute> ToJson 
             WebDriverCommand::GetElementRect(_) |
             WebDriverCommand::GetElementTagName(_) |
             WebDriverCommand::GetElementText(_) |
             WebDriverCommand::GetPageSource |
             WebDriverCommand::GetTimeouts |
             WebDriverCommand::GetTitle |
             WebDriverCommand::GetWindowHandle |
             WebDriverCommand::GetWindowHandles |
-            WebDriverCommand::GetWindowPosition |
-            WebDriverCommand::GetWindowSize |
+            WebDriverCommand::GetWindowRect |
             WebDriverCommand::GoBack |
             WebDriverCommand::GoForward |
             WebDriverCommand::IsDisplayed(_) |
             WebDriverCommand::IsEnabled(_) |
             WebDriverCommand::IsSelected(_) |
             WebDriverCommand::MaximizeWindow |
             WebDriverCommand::NewSession(_) |
             WebDriverCommand::Refresh |
@@ -406,18 +397,17 @@ impl <U:WebDriverExtensionRoute> ToJson 
             WebDriverCommand::FindElementElement(_, ref x) => Some(x.to_json()),
             WebDriverCommand::FindElementElements(_, ref x) => Some(x.to_json()),
             WebDriverCommand::FindElement(ref x) => Some(x.to_json()),
             WebDriverCommand::FindElements(ref x) => Some(x.to_json()),
             WebDriverCommand::Get(ref x) => Some(x.to_json()),
             WebDriverCommand::PerformActions(ref x) => Some(x.to_json()),
             WebDriverCommand::SendAlertText(ref x) => Some(x.to_json()),
             WebDriverCommand::SetTimeouts(ref x) => Some(x.to_json()),
-            WebDriverCommand::SetWindowPosition(ref x) => Some(x.to_json()),
-            WebDriverCommand::SetWindowSize(ref x) => Some(x.to_json()),
+            WebDriverCommand::SetWindowRect(ref x) => Some(x.to_json()),
             WebDriverCommand::SwitchToFrame(ref x) => Some(x.to_json()),
             WebDriverCommand::SwitchToWindow(ref x) => Some(x.to_json()),
             WebDriverCommand::Extension(ref x) => x.parameters_json(),
         };
 
         let mut data = BTreeMap::new();
         if let Some(parameters) = parameters {
             data.insert("parameters".to_string(), parameters);
@@ -563,82 +553,87 @@ impl ToJson for TimeoutsParameters {
         }
         if let Some(ms) = self.implicit {
             data.insert("implicit".into(), ms.to_json());
         }
         Json::Object(data)
     }
 }
 
-#[derive(PartialEq)]
-pub struct WindowSizeParameters {
-    pub width: u64,
-    pub height: u64,
+#[derive(Debug, PartialEq)]
+pub struct WindowRectParameters {
+    pub x: Nullable<i64>,
+    pub y: Nullable<i64>,
+    pub width: Nullable<u64>,
+    pub height: Nullable<u64>,
 }
 
-impl Parameters for WindowSizeParameters {
-    fn from_json(body: &Json) -> WebDriverResult<WindowSizeParameters> {
-        let data = try_opt!(body.as_object(), ErrorStatus::UnknownError,
+impl Parameters for WindowRectParameters {
+    fn from_json(body: &Json) -> WebDriverResult<WindowRectParameters> {
+        let data = try_opt!(body.as_object(),
+                            ErrorStatus::UnknownError,
                             "Message body was not an object");
-        let height = try_opt!(
-            try_opt!(data.get("height"),
-                     ErrorStatus::InvalidArgument,
-                     "Missing 'height' parameter").as_u64(),
-            ErrorStatus::InvalidArgument,
-            "'height' is not a positive integer");
-        let width = try_opt!(
-            try_opt!(data.get("width"),
-                     ErrorStatus::InvalidArgument,
-                     "Missing width parameter").as_u64(),
-            ErrorStatus::InvalidArgument,
-            "'width' is not a positive integer");
-        return Ok(WindowSizeParameters {
-            height: height,
-            width: width
-        })
+
+        let x = match data.get("x") {
+            Some(json) => {
+                try!(Nullable::from_json(json, |n| {
+                    Ok((try_opt!(n.as_i64(),
+                                 ErrorStatus::InvalidArgument,
+                                 "'x' is not an integer")))
+                }))
+            }
+            None => Nullable::Null,
+        };
+        let y = match data.get("y") {
+            Some(json) => {
+                try!(Nullable::from_json(json, |n| {
+                    Ok((try_opt!(n.as_i64(),
+                                 ErrorStatus::InvalidArgument,
+                                 "'y' is not an integer")))
+                }))
+            }
+            None => Nullable::Null,
+        };
+        let width = match data.get("width") {
+            Some(json) => {
+                try!(Nullable::from_json(json, |n| {
+                    Ok((try_opt!(n.as_u64(),
+                                 ErrorStatus::InvalidArgument,
+                                 "'width' is not a positive integer")))
+                }))
+            }
+            None => Nullable::Null,
+        };
+        let height = match data.get("height") {
+            Some(json) => {
+                try!(Nullable::from_json(json, |n| {
+                    Ok((try_opt!(n.as_u64(),
+                                 ErrorStatus::InvalidArgument,
+                                 "'height' is not a positive integer")))
+                }))
+            }
+            None => Nullable::Null,
+        };
+
+        Ok(WindowRectParameters {
+               x: x,
+               y: y,
+               width: width,
+               height: height,
+           })
     }
 }
 
-impl ToJson for WindowSizeParameters {
-    fn to_json(&self) -> Json {
-        let mut data = BTreeMap::new();
-        data.insert("width".to_string(), self.width.to_json());
-        data.insert("height".to_string(), self.height.to_json());
-        Json::Object(data)
-    }
-}
-
-#[derive(PartialEq)]
-pub struct WindowPositionParameters {
-    pub x: u64,
-    pub y: u64,
-}
-
-impl Parameters for WindowPositionParameters {
-    fn from_json(body: &Json) -> WebDriverResult<WindowPositionParameters> {
-        let data = try_opt!(body.as_object(),
-            ErrorStatus::UnknownError, "Message body was not an object");
-        let xv = try_opt!(data.get("x"),
-            ErrorStatus::InvalidArgument, "Missing 'x' parameters");
-        let x = try_opt!(xv.as_u64(),
-            ErrorStatus::InvalidArgument, "'x' is not a positive integer");
-        let yv = try_opt!(data.get("y"),
-            ErrorStatus::InvalidArgument, "Missing 'y' parameters");
-        let y = try_opt!(yv.as_u64(),
-            ErrorStatus::InvalidArgument, "'y' is not a positive integer");
-        return Ok(WindowPositionParameters { x: x, y: y });
-    }
-}
-
-
-impl ToJson for WindowPositionParameters {
+impl ToJson for WindowRectParameters {
     fn to_json(&self) -> Json {
         let mut data = BTreeMap::new();
         data.insert("x".to_string(), self.x.to_json());
         data.insert("y".to_string(), self.y.to_json());
+        data.insert("width".to_string(), self.width.to_json());
+        data.insert("height".to_string(), self.height.to_json());
         Json::Object(data)
     }
 }
 
 #[derive(PartialEq)]
 pub struct SwitchToWindowParameters {
     pub handle: String
 }
@@ -1674,8 +1669,50 @@ impl ToJson for PointerMoveAction {
             data.insert("x".to_owned(), self.x.to_json());
         }
         if self.y.is_value() {
             data.insert("y".to_owned(), self.y.to_json());
         }
         Json::Object(data)
     }
 }
+
+#[cfg(test)]
+mod tests {
+    use rustc_serialize::json::Json;
+    use super::{Nullable, Parameters, WindowRectParameters};
+
+    #[test]
+    fn test_window_rect() {
+        let expected = WindowRectParameters {
+            x: Nullable::Value(0i64),
+            y: Nullable::Value(1i64),
+            width: Nullable::Value(2u64),
+            height: Nullable::Value(3u64),
+        };
+        let actual = Json::from_str(r#"{"x": 0, "y": 1, "width": 2, "height": 3}"#).unwrap();
+        assert_eq!(expected, Parameters::from_json(&actual).unwrap());
+    }
+
+    #[test]
+    fn test_window_rect_nullable() {
+        let expected = WindowRectParameters {
+            x: Nullable::Value(0i64),
+            y: Nullable::Null,
+            width: Nullable::Value(2u64),
+            height: Nullable::Null,
+        };
+        let actual = Json::from_str(r#"{"x": 0, "y": null, "width": 2, "height": null}"#).unwrap();
+        assert_eq!(expected, Parameters::from_json(&actual).unwrap());
+    }
+
+    #[test]
+    fn test_window_rect_missing_fields() {
+        let expected = WindowRectParameters {
+            x: Nullable::Value(0i64),
+            y: Nullable::Null,
+            width: Nullable::Value(2u64),
+            height: Nullable::Null,
+        };
+        let actual = Json::from_str(r#"{"x": 0, "width": 2}"#).unwrap();
+        assert_eq!(expected, Parameters::from_json(&actual).unwrap());
+    }
+}