Bug 1015242 - Provide a combined expected condition for element present and displayed. r=atolfsen
authorDave Hunt <dhunt@mozilla.com>
Wed, 26 Nov 2014 04:19:00 -0500
changeset 217640 4a64ed135ea8d6ea54eef2627ad2958bb8d6106c
parent 217639 e2ddaa3cbb7dbe63c50e9d7bcf2f277b5a4d012e
child 217641 a770fca02a0ecc9fb2fd49a686de89df98552c33
push id27886
push userryanvm@gmail.com
push dateThu, 27 Nov 2014 01:34:27 +0000
treeherdermozilla-central@159a0ec99a6d [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersatolfsen
bugs1015242
milestone36.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
Bug 1015242 - Provide a combined expected condition for element present and displayed. r=atolfsen CLOSED TREE
testing/marionette/client/marionette/expected.py
testing/marionette/client/marionette/tests/unit/test_expected.py
--- a/testing/marionette/client/marionette/expected.py
+++ b/testing/marionette/client/marionette/expected.py
@@ -1,15 +1,17 @@
 # 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/.
 
 import errors
 import types
 
+from marionette import HTMLElement
+
 """This file provides a set of expected conditions for common use
 cases when writing Marionette tests.
 
 The conditions rely on explicit waits that retries conditions a number
 of times until they are either successfully met, or they time out.
 
 """
 
@@ -154,48 +156,76 @@ class element_displayed(object):
     Visibility means that the element is not only displayed, but also
     has a height and width that is greater than 0 pixels.
 
     Stale elements, meaning elements that have been detached from the
     DOM of the current context are treated as not being displayed,
     meaning this expectation is not analogous to the behaviour of
     calling `is_displayed()` on an `HTMLElement`.
 
-    :param element: the element to perform the visibility check on
+    You can select which element to be checked for visibility by
+    supplying a locator::
+
+        displayed = Wait(marionette).until(expected.element_displayed(By.ID, "foo"))
+
+    Or by supplying an element::
+
+        el = marionette.find_element(By.ID, "foo")
+        displayed = Wait(marionette).until(expected.element_displayed(el))
+
+    :param args: locator or web element
     :returns: True if element is displayed, False if hidden
 
     """
 
-    def __init__(self, element):
-        self.el = element
+    def __init__(self, *args):
+        self.el = None
+        if len(args) == 1 and isinstance(args[0], HTMLElement):
+            self.el = args[0]
+        else:
+            self.locator = lambda m: m.find_element(*args)
 
     def __call__(self, marionette):
+        if self.el is None:
+            self.el = _find(marionette, self.locator)
+        if not self.el:
+            return False
         try:
             return self.el.is_displayed()
         except errors.StaleElementException:
             return False
 
 class element_not_displayed(element_displayed):
     """An expectation for checking that an element is not visible.
 
     Visibility means that the element is not only displayed, but also
     has a height and width that is greater than 0 pixels.
 
     Stale elements, meaning elements that have been detached fom the
     DOM of the current context are treated as not being displayed,
     meaning this expectation is not analogous to the behaviour of
     calling `is_displayed()` on an `HTMLElement`.
 
-    :param element: the element to perform the visibility check on
-    :returns: True if element is hidden, False if visible
+    You can select which element to be checked for visibility by
+    supplying a locator::
+
+        hidden = Wait(marionette).until(expected.element_not_displayed(By.ID, "foo"))
+
+    Or by supplying an element::
+
+        el = marionette.find_element(By.ID, "foo")
+        hidden = Wait(marionette).until(expected.element_not_displayed(el))
+
+    :param args: locator or web element
+    :returns: True if element is hidden, False if displayed
 
     """
 
-    def __init__(self, element):
-        super(element_not_displayed, self).__init__(element)
+    def __init__(self, *args):
+        super(element_not_displayed, self).__init__(*args)
 
     def __call__(self, marionette):
         return not super(element_not_displayed, self).__call__(marionette)
 
 class element_selected(object):
     """An expectation for checking that the given element is selected.
 
     :param element: the element to be selected
--- a/testing/marionette/client/marionette/tests/unit/test_expected.py
+++ b/testing/marionette/client/marionette/tests/unit/test_expected.py
@@ -112,41 +112,66 @@ class TestExpected(marionette_test.Mario
         self.assertFalse(r)
 
     def test_element_displayed(self):
         self.marionette.navigate(static_element)
         el = self.marionette.find_element(By.TAG_NAME, "p")
         visible = expected.element_displayed(el)(self.marionette)
         self.assertTrue(visible)
 
+    def test_element_displayed_locator(self):
+        self.marionette.navigate(static_element)
+        visible = expected.element_displayed(By.TAG_NAME, "p")(self.marionette)
+        self.assertTrue(visible)
+
     def test_element_displayed_when_hidden(self):
         self.marionette.navigate(hidden_element)
         el = self.marionette.find_element(By.TAG_NAME, "p")
-        hidden = expected.element_displayed(el)(self.marionette)
-        self.assertFalse(hidden)
+        visible = expected.element_displayed(el)(self.marionette)
+        self.assertFalse(visible)
+
+    def test_element_displayed_when_hidden_locator(self):
+        self.marionette.navigate(hidden_element)
+        visible = expected.element_displayed(By.TAG_NAME, "p")(self.marionette)
+        self.assertFalse(visible)
+
+    def test_element_displayed_when_not_present(self):
+        self.marionette.navigate("about:blank")
+        visible = expected.element_displayed(By.TAG_NAME, "p")(self.marionette)
+        self.assertFalse(visible)
 
     def test_element_displayed_when_stale_element(self):
         self.marionette.navigate(static_element)
         el = self.marionette.find_element(By.TAG_NAME, "p")
         self.marionette.navigate("about:blank")
         missing = expected.element_displayed(el)(self.marionette)
         self.assertFalse(missing)
 
     def test_element_not_displayed(self):
         self.marionette.navigate(hidden_element)
         el = self.marionette.find_element(By.TAG_NAME, "p")
-        visible = expected.element_not_displayed(el)(self.marionette)
-        self.assertTrue(visible)
+        hidden = expected.element_not_displayed(el)(self.marionette)
+        self.assertTrue(hidden)
+
+    def test_element_not_displayed_locator(self):
+        self.marionette.navigate(hidden_element)
+        hidden = expected.element_not_displayed(By.TAG_NAME, "p")(self.marionette)
+        self.assertTrue(hidden)
 
     def test_element_not_displayed_when_visible(self):
         self.marionette.navigate(static_element)
         el = self.marionette.find_element(By.TAG_NAME, "p")
         hidden = expected.element_not_displayed(el)(self.marionette)
         self.assertFalse(hidden)
 
+    def test_element_not_displayed_when_visible_locator(self):
+        self.marionette.navigate(static_element)
+        hidden = expected.element_not_displayed(By.TAG_NAME, "p")(self.marionette)
+        self.assertFalse(hidden)
+
     def test_element_not_displayed_when_stale_element(self):
         self.marionette.navigate(static_element)
         el = self.marionette.find_element(By.TAG_NAME, "p")
         self.marionette.navigate("about:blank")
         missing = expected.element_not_displayed(el)(self.marionette)
         self.assertTrue(missing)
 
     def test_element_selected(self):