Bug 800956: adding the ability to get the size of elements to marionette; r=mdas a=NPOTB
authorDavid Burns <dburns@mozilla.com>
Fri, 19 Oct 2012 01:35:30 +0100
changeset 113775 eb1ad7ebda1802bad90b147906fee286993f3235
parent 113774 df7645bd936c9ea4cde0eb625521f46731c4c1c0
child 113776 b0635185af6a2702227bcfaa9f19c9c6db044f8c
push id2507
push userdburns@mozilla.com
push dateTue, 30 Oct 2012 17:49:51 +0000
treeherdermozilla-aurora@eb1ad7ebda18 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmdas, NPOTB
bugs800956
milestone18.0a2
Bug 800956: adding the ability to get the size of elements to marionette; r=mdas a=NPOTB
testing/marionette/client/marionette/marionette.py
testing/marionette/client/marionette/tests/unit/test_elementsize.py
testing/marionette/client/marionette/www/testSize.html
testing/marionette/marionette-actors.js
testing/marionette/marionette-listener.js
--- a/testing/marionette/client/marionette/marionette.py
+++ b/testing/marionette/client/marionette/marionette.py
@@ -70,16 +70,20 @@ class HTMLElement(object):
 
     def is_enabled(self):
         return self.marionette._send_message('isElementEnabled', 'value', element=self.id)
 
     def is_displayed(self):
         return self.marionette._send_message('isElementDisplayed', 'value', element=self.id)
 
     @property
+    def size(self):
+        return self.marionette._send_message('getElementSize', 'value', element=self.id)
+
+    @property
     def tag_name(self):
         return self.marionette._send_message('getElementTagName', 'value', element=self.id)
 
 
 class Marionette(object):
 
     CONTEXT_CHROME = 'chrome'
     CONTEXT_CONTENT = 'content'
new file mode 100644
--- /dev/null
+++ b/testing/marionette/client/marionette/tests/unit/test_elementsize.py
@@ -0,0 +1,40 @@
+# This Source Code Form is subject to the terms of the Mozilla Public
+# License, v. 2.0. If a copy of the MPL was not distributed with this
+# file, You can obtain one at http://mozilla.org/MPL/2.0/.
+
+from marionette_test import MarionetteTestCase
+
+class TestElementSize(MarionetteTestCase):
+    def testShouldReturnTheSizeOfALink(self):
+        test_html = self.marionette.absolute_url("testSize.html")
+        self.marionette.navigate(test_html)
+        shrinko = self.marionette.find_element('id', 'linkId')
+        size = shrinko.size
+        self.assertTrue(size['width'] > 0)
+        self.assertTrue(size['height'] > 0)
+
+class TestElementSizeChrome(MarionetteTestCase):
+    def setUp(self):
+        MarionetteTestCase.setUp(self)
+        self.marionette.set_context("chrome")
+        self.win = self.marionette.current_window_handle
+        self.marionette.execute_script("window.open('chrome://marionette/content/test2.xul', 'foo', 'chrome,centerscreen');")
+        self.marionette.switch_to_window('foo')
+        self.assertNotEqual(self.win, self.marionette.current_window_handle)
+
+    def tearDown(self):
+        self.assertNotEqual(self.win, self.marionette.current_window_handle)
+        self.marionette.execute_script("window.close();")
+        self.marionette.switch_to_window(self.win)
+        MarionetteTestCase.tearDown(self)
+
+    def testShouldReturnTheSizeOfAnInput(self):
+        wins = self.marionette.window_handles
+        wins.remove(self.win)
+        newWin = wins.pop()
+        self.marionette.switch_to_window(newWin)
+        shrinko = self.marionette.find_element('id', 'textInput')
+        size = shrinko.size
+        self.assertTrue(size['width'] > 0)
+        self.assertTrue(size['height'] > 0)
+
new file mode 100644
--- /dev/null
+++ b/testing/marionette/client/marionette/www/testSize.html
@@ -0,0 +1,9 @@
+<?xml version="1.0"?>
+<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
+  <head>
+    <title>Test page for element size</title>
+  </head>
+  <body>
+    <p>Let's get the size of <a href='#' id='linkId'>some really cool link</a></p>
+  </body>
+</html>
--- a/testing/marionette/marionette-actors.js
+++ b/testing/marionette/marionette-actors.js
@@ -1318,16 +1318,32 @@ MarionetteDriverActor.prototype = {
         this.sendError(e.message, e.code, e.stack);
       }
     }
     else {
       this.sendAsync("isElementSelected", {element:aRequest.element});
     }
   },
 
+  getElementSize: function MDA_getElementSize(aRequest) {
+    if (this.context == "chrome") {
+      try {
+        let el = this.curBrowser.elementManager.getKnownElement(aRequest.element, this.getCurrentWindow());
+        let clientRect = el.getBoundingClientRect();  
+        this.sendResponse({width: clientRect.width, height: clientRect.height});
+      }
+      catch (e) {
+        this.sendError(e.message, e.code, e.stack);
+      }
+    }
+    else {
+      this.sendAsync("getElementSize", {element:aRequest.element});
+    }
+  },
+
   /**
    * Send key presses to element after focusing on it
    *
    * @param object aRequest
    *        'element' member holds the reference id to
    *        the element that will be checked
    *        'value' member holds the value to send to the element
    */
@@ -1661,16 +1677,17 @@ MarionetteDriverActor.prototype.requestT
   "setSearchTimeout": MarionetteDriverActor.prototype.setSearchTimeout,
   "findElement": MarionetteDriverActor.prototype.findElement,
   "findElements": MarionetteDriverActor.prototype.findElements,
   "clickElement": MarionetteDriverActor.prototype.clickElement,
   "getElementAttribute": MarionetteDriverActor.prototype.getElementAttribute,
   "getElementText": MarionetteDriverActor.prototype.getElementText,
   "getElementTagName": MarionetteDriverActor.prototype.getElementTagName,
   "isElementDisplayed": MarionetteDriverActor.prototype.isElementDisplayed,
+  "getElementSize": MarionetteDriverActor.prototype.getElementSize,
   "isElementEnabled": MarionetteDriverActor.prototype.isElementEnabled,
   "isElementSelected": MarionetteDriverActor.prototype.isElementSelected,
   "sendKeysToElement": MarionetteDriverActor.prototype.sendKeysToElement,
   "clearElement": MarionetteDriverActor.prototype.clearElement,
   "getTitle": MarionetteDriverActor.prototype.getTitle,
   "getPageSource": MarionetteDriverActor.prototype.getPageSource,
   "goUrl": MarionetteDriverActor.prototype.goUrl,
   "getUrl": MarionetteDriverActor.prototype.getUrl,
--- a/testing/marionette/marionette-listener.js
+++ b/testing/marionette/marionette-listener.js
@@ -100,16 +100,17 @@ function startListeners() {
   addMessageListenerId("Marionette:refresh", refresh);
   addMessageListenerId("Marionette:findElementContent", findElementContent);
   addMessageListenerId("Marionette:findElementsContent", findElementsContent);
   addMessageListenerId("Marionette:clickElement", clickElement);
   addMessageListenerId("Marionette:getElementAttribute", getElementAttribute);
   addMessageListenerId("Marionette:getElementText", getElementText);
   addMessageListenerId("Marionette:getElementTagName", getElementTagName);
   addMessageListenerId("Marionette:isElementDisplayed", isElementDisplayed);
+  addMessageListenerId("Marionette:getElementSize", getElementSize);
   addMessageListenerId("Marionette:isElementEnabled", isElementEnabled);
   addMessageListenerId("Marionette:isElementSelected", isElementSelected);
   addMessageListenerId("Marionette:sendKeysToElement", sendKeysToElement);
   addMessageListenerId("Marionette:clearElement", clearElement);
   addMessageListenerId("Marionette:switchToFrame", switchToFrame);
   addMessageListenerId("Marionette:deleteSession", deleteSession);
   addMessageListenerId("Marionette:sleepSession", sleepSession);
   addMessageListenerId("Marionette:emulatorCmdResult", emulatorCmdResult);
@@ -161,16 +162,17 @@ function deleteSession(msg) {
   removeMessageListenerId("Marionette:goForward", goForward);
   removeMessageListenerId("Marionette:refresh", refresh);
   removeMessageListenerId("Marionette:findElementContent", findElementContent);
   removeMessageListenerId("Marionette:findElementsContent", findElementsContent);
   removeMessageListenerId("Marionette:clickElement", clickElement);
   removeMessageListenerId("Marionette:getElementAttribute", getElementAttribute);
   removeMessageListenerId("Marionette:getElementTagName", getElementTagName);
   removeMessageListenerId("Marionette:isElementDisplayed", isElementDisplayed);
+  removeMessageListenerId("Marionette:getElementSize", getElementSize);
   removeMessageListenerId("Marionette:isElementEnabled", isElementEnabled);
   removeMessageListenerId("Marionette:isElementSelected", isElementSelected);
   removeMessageListenerId("Marionette:sendKeysToElement", sendKeysToElement);
   removeMessageListenerId("Marionette:clearElement", clearElement);
   removeMessageListenerId("Marionette:switchToFrame", switchToFrame);
   removeMessageListenerId("Marionette:deleteSession", deleteSession);
   removeMessageListenerId("Marionette:sleepSession", sleepSession);
   removeMessageListenerId("Marionette:emulatorCmdResult", emulatorCmdResult);
@@ -663,16 +665,30 @@ function isElementDisplayed(msg) {
     sendResponse({value: utils.isElementDisplayed(el)});
   }
   catch (e) {
     sendError(e.message, e.code, e.stack);
   }
 }
 
 /**
+ * Get the size of the element and return it
+ */
+function getElementSize(msg){
+  try {
+    let el = elementManager.getKnownElement(msg.json.element, curWindow);
+    let clientRect = el.getBoundingClientRect();  
+    sendResponse({value: {width: clientRect.width, height: clientRect.height}});
+  }
+  catch (e) {
+    sendError(e.message, e.code, e.stack);
+  }
+}
+
+/**
  * Check if element is enabled
  */
 function isElementEnabled(msg) {
   try {
     let el = elementManager.getKnownElement(msg.json.element, curWindow);
     sendResponse({value: utils.isElementEnabled(el)});
   }
   catch (e) {