Bug 845805 - Add a By class to the marionette python bindings; r=dburns
authorFlorin Bogdan Strugariu <bebe@mozilla.ro>
Thu, 04 Jul 2013 14:43:54 +0300
changeset 137771 a75301db98994387658f9af4a33ac517f268d2fb
parent 137770 5af7d6736c54846c25bac2e51dad2ea72ea5b3b4
child 137772 184127374d60c909ebed0d1ae298e0bc0e2ce279
push id30692
push userdburns@mozilla.com
push dateTue, 09 Jul 2013 14:58:22 +0000
treeherdermozilla-inbound@a75301db9899 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersdburns
bugs845805
milestone25.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 845805 - Add a By class to the marionette python bindings; r=dburns
testing/marionette/client/marionette/__init__.py
testing/marionette/client/marionette/by.py
testing/marionette/client/marionette/tests/unit/test_click.py
testing/marionette/client/marionette/tests/unit/test_findelement.py
--- a/testing/marionette/client/marionette/__init__.py
+++ b/testing/marionette/client/marionette/__init__.py
@@ -1,12 +1,13 @@
 # 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 gestures import *
+from by import By
 from marionette import Marionette, HTMLElement, Actions, MultiActions
 from marionette_test import MarionetteTestCase, CommonTestCase, expectedFailure, skip, SkipTest
 from emulator import Emulator
 from runtests import MarionetteTestResult
 from runtests import MarionetteTestRunner
 from runtests import MarionetteTestOptions
 from runtests import MarionetteTextTestRunner
new file mode 100644
--- /dev/null
+++ b/testing/marionette/client/marionette/by.py
@@ -0,0 +1,25 @@
+# Copyright 2008-2009 WebDriver committers
+# Copyright 2008-2009 Google Inc.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+
+class By(object):
+    ID = "id"
+    XPATH = "xpath"
+    LINK_TEXT = "link text"
+    PARTIAL_LINK_TEXT = "partial link text"
+    NAME = "name"
+    TAG_NAME = "tag name"
+    CLASS_NAME = "class name"
+    CSS_SELECTOR = "css selector"
--- a/testing/marionette/client/marionette/tests/unit/test_click.py
+++ b/testing/marionette/client/marionette/tests/unit/test_click.py
@@ -1,30 +1,31 @@
 # 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 time
 from marionette_test import MarionetteTestCase
+from by import By
 
 
 class TestClick(MarionetteTestCase):
     def test_click(self):
         test_html = self.marionette.absolute_url("test.html")
         self.marionette.navigate(test_html)
-        link = self.marionette.find_element("id", "mozLink")
+        link = self.marionette.find_element(By.ID, "mozLink")
         link.click()
         self.assertEqual("Clicked", self.marionette.execute_script("return document.getElementById('mozLink').innerHTML;"))
 
     def testClickingALinkMadeUpOfNumbersIsHandledCorrectly(self):
         test_html = self.marionette.absolute_url("clicks.html")
         self.marionette.navigate(test_html)
-        self.marionette.find_element("link text", "333333").click()
+        self.marionette.find_element(By.LINK_TEXT, "333333").click()
         count = 0
-        while len(self.marionette.find_elements("id", "username")) == 0:
+        while len(self.marionette.find_elements(By.ID, "username")) == 0:
             count += 1
             time.sleep(1)
             if count == 30:
                 self.fail("Element id=username not found after 30 seconds")
 
         self.assertEqual(self.marionette.title, "XHTML Test Page")
 
 
@@ -43,12 +44,12 @@ class TestClickChrome(MarionetteTestCase
         self.marionette.switch_to_window(self.win)
         MarionetteTestCase.tearDown(self)
 
     def test_click(self):
         wins = self.marionette.window_handles
         wins.remove(self.win)
         newWin = wins.pop()
         self.marionette.switch_to_window(newWin)
-        box = self.marionette.find_element("id", "testBox")
+        box = self.marionette.find_element(By.ID, "testBox")
         self.assertFalse(self.marionette.execute_script("return arguments[0].checked;", [box]))
         box.click()
         self.assertTrue(self.marionette.execute_script("return arguments[0].checked;", [box]))
--- a/testing/marionette/client/marionette/tests/unit/test_findelement.py
+++ b/testing/marionette/client/marionette/tests/unit/test_findelement.py
@@ -1,154 +1,157 @@
 # 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 os
 from marionette_test import MarionetteTestCase
 from marionette import HTMLElement
+from by import By
 from errors import NoSuchElementException
 
+
 class TestElements(MarionetteTestCase):
     def test_id(self):
         test_html = self.marionette.absolute_url("test.html")
         self.marionette.navigate(test_html)
         el = self.marionette.execute_script("return window.document.getElementById('mozLink');")
-        found_el = self.marionette.find_element("id", "mozLink")
+        found_el = self.marionette.find_element(By.ID, "mozLink")
         self.assertEqual(HTMLElement, type(found_el))
         self.assertEqual(el, found_el)
 
     def test_child_element(self):
         test_html = self.marionette.absolute_url("test.html")
         self.marionette.navigate(test_html)
-        el = self.marionette.find_element("id", "divLink")
-        div = self.marionette.find_element("id", "testDiv")
-        found_el = div.find_element("tag name", "a")
+        el = self.marionette.find_element(By.ID, "divLink")
+        div = self.marionette.find_element(By.ID, "testDiv")
+        found_el = div.find_element(By.TAG_NAME, "a")
         self.assertEqual("a", found_el.tag_name)
         self.assertEqual(HTMLElement, type(found_el))
         self.assertEqual(el, found_el)
 
     def test_child_elements(self):
         test_html = self.marionette.absolute_url("test.html")
         self.marionette.navigate(test_html)
-        el = self.marionette.find_element("id", "divLink2")
-        div = self.marionette.find_element("id", "testDiv")
-        found_els = div.find_elements("tag name", "a")
+        el = self.marionette.find_element(By.ID, "divLink2")
+        div = self.marionette.find_element(By.ID, "testDiv")
+        found_els = div.find_elements(By.TAG_NAME, "a")
         self.assertTrue(el.id in [found_el.id for found_el in found_els])
 
     def test_tag_name(self):
         test_html = self.marionette.absolute_url("test.html")
         self.marionette.navigate(test_html)
         el = self.marionette.execute_script("return window.document.getElementsByTagName('body')[0];")
-        found_el = self.marionette.find_element("tag name", "body")
+        found_el = self.marionette.find_element(By.TAG_NAME, "body")
         self.assertEqual('body', found_el.tag_name)
         self.assertEqual(HTMLElement, type(found_el))
         self.assertEqual(el, found_el)
-        found_el = self.marionette.find_elements("tag name", "body")[0]
+        found_el = self.marionette.find_elements(By.TAG_NAME, "body")[0]
         self.assertEqual('body', found_el.tag_name)
         self.assertEqual(HTMLElement, type(found_el))
         self.assertEqual(el, found_el)
 
     def test_class_name(self):
         test_html = self.marionette.absolute_url("test.html")
         self.marionette.navigate(test_html)
         el = self.marionette.execute_script("return window.document.getElementsByClassName('linkClass')[0];")
-        found_el = self.marionette.find_element("class name", "linkClass")
-        self.assertEqual(HTMLElement, type(found_el));
+        found_el = self.marionette.find_element(By.CLASS_NAME, "linkClass")
+        self.assertEqual(HTMLElement, type(found_el))
         self.assertEqual(el, found_el)
-        found_el = self.marionette.find_elements("class name", "linkClass")[0]
-        self.assertEqual(HTMLElement, type(found_el));
+        found_el = self.marionette.find_elements(By.CLASS_NAME, "linkClass")[0]
+        self.assertEqual(HTMLElement, type(found_el))
         self.assertEqual(el, found_el)
 
     def test_by_name(self):
         test_html = self.marionette.absolute_url("test.html")
         self.marionette.navigate(test_html)
         el = self.marionette.execute_script("return window.document.getElementsByName('myInput')[0];")
-        found_el = self.marionette.find_element("name", "myInput")
+        found_el = self.marionette.find_element(By.NAME, "myInput")
         self.assertEqual(HTMLElement, type(found_el))
         self.assertEqual(el, found_el)
-        found_el = self.marionette.find_elements("name", "myInput")[0]
+        found_el = self.marionette.find_elements(By.NAME, "myInput")[0]
         self.assertEqual(HTMLElement, type(found_el))
         self.assertEqual(el, found_el)
-    
+
     def test_selector(self):
         test_html = self.marionette.absolute_url("test.html")
         self.marionette.navigate(test_html)
         el = self.marionette.execute_script("return window.document.getElementById('testh1');")
-        found_el = self.marionette.find_element("css selector", "h1")
+        found_el = self.marionette.find_element(By.CSS_SELECTOR, "h1")
         self.assertEqual(HTMLElement, type(found_el))
         self.assertEqual(el, found_el)
-        found_el = self.marionette.find_elements("css selector", "h1")[0]
+        found_el = self.marionette.find_elements(By.CSS_SELECTOR, "h1")[0]
         self.assertEqual(HTMLElement, type(found_el))
         self.assertEqual(el, found_el)
 
     def test_link_text(self):
         test_html = self.marionette.absolute_url("test.html")
         self.marionette.navigate(test_html)
         el = self.marionette.execute_script("return window.document.getElementById('mozLink');")
-        found_el = self.marionette.find_element("link text", "Click me!")
+        found_el = self.marionette.find_element(By.LINK_TEXT, "Click me!")
         self.assertEqual(HTMLElement, type(found_el))
         self.assertEqual(el, found_el)
-        found_el = self.marionette.find_elements("link text", "Click me!")[0]
+        found_el = self.marionette.find_elements(By.LINK_TEXT, "Click me!")[0]
         self.assertEqual(HTMLElement, type(found_el))
         self.assertEqual(el, found_el)
 
     def test_partial_link_text(self):
         test_html = self.marionette.absolute_url("test.html")
         self.marionette.navigate(test_html)
         el = self.marionette.execute_script("return window.document.getElementById('mozLink');")
-        found_el = self.marionette.find_element("partial link text", "Click m")
+        found_el = self.marionette.find_element(By.PARTIAL_LINK_TEXT, "Click m")
         self.assertEqual(HTMLElement, type(found_el))
         self.assertEqual(el, found_el)
-        found_el = self.marionette.find_elements("partial link text", "Click m")[0]
+        found_el = self.marionette.find_elements(By.PARTIAL_LINK_TEXT, "Click m")[0]
         self.assertEqual(HTMLElement, type(found_el))
         self.assertEqual(el, found_el)
 
     def test_xpath(self):
         test_html = self.marionette.absolute_url("test.html")
         self.marionette.navigate(test_html)
         el = self.marionette.execute_script("return window.document.getElementById('mozLink');")
-        found_el = self.marionette.find_element("xpath", "id('mozLink')")
+        found_el = self.marionette.find_element(By.XPATH, "id('mozLink')")
         self.assertEqual(HTMLElement, type(found_el))
         self.assertEqual(el, found_el)
-        found_el = self.marionette.find_elements("xpath", "id('mozLink')")[0]
+        found_el = self.marionette.find_elements(By.XPATH, "id('mozLink')")[0]
         self.assertEqual(HTMLElement, type(found_el))
         self.assertEqual(el, found_el)
 
     def test_not_found(self):
         test_html = self.marionette.absolute_url("test.html")
         self.marionette.navigate(test_html)
         self.marionette.set_search_timeout(1000)
-        self.assertRaises(NoSuchElementException, self.marionette.find_element, "id", "I'm not on the page")
+        self.assertRaises(NoSuchElementException, self.marionette.find_element, By.ID, "I'm not on the page")
         self.marionette.set_search_timeout(0)
-        self.assertRaises(NoSuchElementException, self.marionette.find_element, "id", "I'm not on the page")
+        self.assertRaises(NoSuchElementException, self.marionette.find_element, By.ID, "I'm not on the page")
 
     def test_timeout(self):
         test_html = self.marionette.absolute_url("test.html")
         self.marionette.navigate(test_html)
-        self.assertRaises(NoSuchElementException, self.marionette.find_element, "id", "newDiv")
+        self.assertRaises(NoSuchElementException, self.marionette.find_element, By.ID, "newDiv")
         self.assertTrue(True, self.marionette.set_search_timeout(4000))
         self.marionette.navigate(test_html)
-        self.assertEqual(HTMLElement, type(self.marionette.find_element("id", "newDiv")))
+        self.assertEqual(HTMLElement, type(self.marionette.find_element(By.ID, "newDiv")))
 
     def test_css_selector_scope_doesnt_start_at_rootnode(self):
         test_html = self.marionette.absolute_url("test.html")
         self.marionette.navigate(test_html)
-        el = self.marionette.find_element("id","mozLink")
-        nav_el = self.marionette.find_element("id","testDiv")
-        found_els = nav_el.find_elements("css selector", "a")
+        el = self.marionette.find_element(By.ID, "mozLink")
+        nav_el = self.marionette.find_element(By.ID, "testDiv")
+        found_els = nav_el.find_elements(By.CSS_SELECTOR, "a")
         self.assertFalse(el.id in [found_el.id for found_el in found_els])
 
     def test_finding_active_element_returns_element(self):
         test_html = self.marionette.absolute_url("test.html")
         self.marionette.navigate(test_html)
-        fbody = self.marionette.find_element('tag name', 'body')
+        fbody = self.marionette.find_element(By.TAG_NAME, 'body')
         abody = self.marionette.get_active_element()
         self.assertEqual(fbody, abody)
 
+
 class TestElementsChrome(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/test.xul', 'foo', 'chrome,centerscreen');")
         self.marionette.switch_to_window('foo')
         self.assertNotEqual(self.win, self.marionette.current_window_handle)
@@ -156,56 +159,56 @@ class TestElementsChrome(MarionetteTestC
     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 test_id(self):
         el = self.marionette.execute_script("return window.document.getElementById('textInput');")
-        found_el = self.marionette.find_element("id", "textInput")
+        found_el = self.marionette.find_element(By.ID, "textInput")
         self.assertEqual(HTMLElement, type(found_el))
         self.assertEqual(el, found_el)
 
     def test_child_element(self):
-        el = self.marionette.find_element("id", "textInput")
-        parent = self.marionette.find_element("id", "things")
-        found_el = parent.find_element("tag name", "textbox")
+        el = self.marionette.find_element(By.ID, "textInput")
+        parent = self.marionette.find_element(By.ID, "things")
+        found_el = parent.find_element(By.TAG_NAME, "textbox")
         self.assertEqual(HTMLElement, type(found_el))
         self.assertEqual(el, found_el)
 
     def test_child_elements(self):
-        el = self.marionette.find_element("id", "textInput3")
-        parent = self.marionette.find_element("id", "things")
-        found_els = parent.find_elements("tag name", "textbox")
+        el = self.marionette.find_element(By.ID, "textInput3")
+        parent = self.marionette.find_element(By.ID, "things")
+        found_els = parent.find_elements(By.TAG_NAME, "textbox")
         self.assertTrue(el.id in [found_el.id for found_el in found_els])
 
     def test_tag_name(self):
         el = self.marionette.execute_script("return window.document.getElementsByTagName('vbox')[0];")
-        found_el = self.marionette.find_element("tag name", "vbox")
+        found_el = self.marionette.find_element(By.TAG_NAME, "vbox")
         self.assertEquals('vbox', found_el.tag_name)
         self.assertEqual(HTMLElement, type(found_el))
         self.assertEqual(el, found_el)
 
     def test_class_name(self):
         el = self.marionette.execute_script("return window.document.getElementsByClassName('asdf')[0];")
-        found_el = self.marionette.find_element("class name", "asdf")
-        self.assertEqual(HTMLElement, type(found_el));
+        found_el = self.marionette.find_element(By.CLASS_NAME, "asdf")
+        self.assertEqual(HTMLElement, type(found_el))
         self.assertEqual(el, found_el)
 
     def test_xpath(self):
         el = self.marionette.execute_script("return window.document.getElementById('testBox');")
-        found_el = self.marionette.find_element("xpath", "id('testBox')")
-        self.assertEqual(HTMLElement, type(found_el));
+        found_el = self.marionette.find_element(By.XPATH, "id('testBox')")
+        self.assertEqual(HTMLElement, type(found_el))
         self.assertEqual(el, found_el)
 
     def test_not_found(self):
         self.marionette.set_search_timeout(1000)
-        self.assertRaises(NoSuchElementException, self.marionette.find_element, "id", "I'm not on the page")
+        self.assertRaises(NoSuchElementException, self.marionette.find_element, By.ID, "I'm not on the page")
         self.marionette.set_search_timeout(0)
-        self.assertRaises(NoSuchElementException, self.marionette.find_element, "id", "I'm not on the page")
+        self.assertRaises(NoSuchElementException, self.marionette.find_element, By.ID, "I'm not on the page")
 
     def test_timeout(self):
-        self.assertRaises(NoSuchElementException, self.marionette.find_element, "id", "myid")
+        self.assertRaises(NoSuchElementException, self.marionette.find_element, By.ID, "myid")
         self.assertTrue(True, self.marionette.set_search_timeout(4000))
         self.marionette.execute_script("window.setTimeout(function() {var b = window.document.createElement('button'); b.id = 'myid'; document.getElementById('things').appendChild(b);}, 1000)")
-        self.assertEqual(HTMLElement, type(self.marionette.find_element("id", "myid")))
+        self.assertEqual(HTMLElement, type(self.marionette.find_element(By.ID, "myid")))
         self.marionette.execute_script("window.document.getElementById('things').removeChild(window.document.getElementById('myid'));")