Bug 845805 - Add a By class to the marionette python bindings; r=dburns

This commit is contained in:
Florin Bogdan Strugariu 2013-07-04 14:43:54 +03:00
parent a8f2479515
commit 672b4e079d
4 changed files with 82 additions and 52 deletions

View File

@ -3,6 +3,7 @@
# file, You can obtain one at http://mozilla.org/MPL/2.0/. # file, You can obtain one at http://mozilla.org/MPL/2.0/.
from gestures import * from gestures import *
from by import By
from marionette import Marionette, HTMLElement, Actions, MultiActions from marionette import Marionette, HTMLElement, Actions, MultiActions
from marionette_test import MarionetteTestCase, CommonTestCase, expectedFailure, skip, SkipTest from marionette_test import MarionetteTestCase, CommonTestCase, expectedFailure, skip, SkipTest
from emulator import Emulator from emulator import Emulator

View File

@ -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"

View File

@ -4,22 +4,23 @@
import time import time
from marionette_test import MarionetteTestCase from marionette_test import MarionetteTestCase
from by import By
class TestClick(MarionetteTestCase): class TestClick(MarionetteTestCase):
def test_click(self): def test_click(self):
test_html = self.marionette.absolute_url("test.html") test_html = self.marionette.absolute_url("test.html")
self.marionette.navigate(test_html) self.marionette.navigate(test_html)
link = self.marionette.find_element("id", "mozLink") link = self.marionette.find_element(By.ID, "mozLink")
link.click() link.click()
self.assertEqual("Clicked", self.marionette.execute_script("return document.getElementById('mozLink').innerHTML;")) self.assertEqual("Clicked", self.marionette.execute_script("return document.getElementById('mozLink').innerHTML;"))
def testClickingALinkMadeUpOfNumbersIsHandledCorrectly(self): def testClickingALinkMadeUpOfNumbersIsHandledCorrectly(self):
test_html = self.marionette.absolute_url("clicks.html") test_html = self.marionette.absolute_url("clicks.html")
self.marionette.navigate(test_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 count = 0
while len(self.marionette.find_elements("id", "username")) == 0: while len(self.marionette.find_elements(By.ID, "username")) == 0:
count += 1 count += 1
time.sleep(1) time.sleep(1)
if count == 30: if count == 30:
@ -48,7 +49,7 @@ class TestClickChrome(MarionetteTestCase):
wins.remove(self.win) wins.remove(self.win)
newWin = wins.pop() newWin = wins.pop()
self.marionette.switch_to_window(newWin) 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])) self.assertFalse(self.marionette.execute_script("return arguments[0].checked;", [box]))
box.click() box.click()
self.assertTrue(self.marionette.execute_script("return arguments[0].checked;", [box])) self.assertTrue(self.marionette.execute_script("return arguments[0].checked;", [box]))

View File

@ -5,23 +5,25 @@
import os import os
from marionette_test import MarionetteTestCase from marionette_test import MarionetteTestCase
from marionette import HTMLElement from marionette import HTMLElement
from by import By
from errors import NoSuchElementException from errors import NoSuchElementException
class TestElements(MarionetteTestCase): class TestElements(MarionetteTestCase):
def test_id(self): def test_id(self):
test_html = self.marionette.absolute_url("test.html") test_html = self.marionette.absolute_url("test.html")
self.marionette.navigate(test_html) self.marionette.navigate(test_html)
el = self.marionette.execute_script("return window.document.getElementById('mozLink');") 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(HTMLElement, type(found_el))
self.assertEqual(el, found_el) self.assertEqual(el, found_el)
def test_child_element(self): def test_child_element(self):
test_html = self.marionette.absolute_url("test.html") test_html = self.marionette.absolute_url("test.html")
self.marionette.navigate(test_html) self.marionette.navigate(test_html)
el = self.marionette.find_element("id", "divLink") el = self.marionette.find_element(By.ID, "divLink")
div = self.marionette.find_element("id", "testDiv") div = self.marionette.find_element(By.ID, "testDiv")
found_el = div.find_element("tag name", "a") found_el = div.find_element(By.TAG_NAME, "a")
self.assertEqual("a", found_el.tag_name) self.assertEqual("a", found_el.tag_name)
self.assertEqual(HTMLElement, type(found_el)) self.assertEqual(HTMLElement, type(found_el))
self.assertEqual(el, found_el) self.assertEqual(el, found_el)
@ -29,20 +31,20 @@ class TestElements(MarionetteTestCase):
def test_child_elements(self): def test_child_elements(self):
test_html = self.marionette.absolute_url("test.html") test_html = self.marionette.absolute_url("test.html")
self.marionette.navigate(test_html) self.marionette.navigate(test_html)
el = self.marionette.find_element("id", "divLink2") el = self.marionette.find_element(By.ID, "divLink2")
div = self.marionette.find_element("id", "testDiv") div = self.marionette.find_element(By.ID, "testDiv")
found_els = div.find_elements("tag name", "a") found_els = div.find_elements(By.TAG_NAME, "a")
self.assertTrue(el.id in [found_el.id for found_el in found_els]) self.assertTrue(el.id in [found_el.id for found_el in found_els])
def test_tag_name(self): def test_tag_name(self):
test_html = self.marionette.absolute_url("test.html") test_html = self.marionette.absolute_url("test.html")
self.marionette.navigate(test_html) self.marionette.navigate(test_html)
el = self.marionette.execute_script("return window.document.getElementsByTagName('body')[0];") 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('body', found_el.tag_name)
self.assertEqual(HTMLElement, type(found_el)) self.assertEqual(HTMLElement, type(found_el))
self.assertEqual(el, 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('body', found_el.tag_name)
self.assertEqual(HTMLElement, type(found_el)) self.assertEqual(HTMLElement, type(found_el))
self.assertEqual(el, found_el) self.assertEqual(el, found_el)
@ -51,21 +53,21 @@ class TestElements(MarionetteTestCase):
test_html = self.marionette.absolute_url("test.html") test_html = self.marionette.absolute_url("test.html")
self.marionette.navigate(test_html) self.marionette.navigate(test_html)
el = self.marionette.execute_script("return window.document.getElementsByClassName('linkClass')[0];") el = self.marionette.execute_script("return window.document.getElementsByClassName('linkClass')[0];")
found_el = self.marionette.find_element("class name", "linkClass") found_el = self.marionette.find_element(By.CLASS_NAME, "linkClass")
self.assertEqual(HTMLElement, type(found_el)); self.assertEqual(HTMLElement, type(found_el))
self.assertEqual(el, found_el) self.assertEqual(el, found_el)
found_el = self.marionette.find_elements("class name", "linkClass")[0] found_el = self.marionette.find_elements(By.CLASS_NAME, "linkClass")[0]
self.assertEqual(HTMLElement, type(found_el)); self.assertEqual(HTMLElement, type(found_el))
self.assertEqual(el, found_el) self.assertEqual(el, found_el)
def test_by_name(self): def test_by_name(self):
test_html = self.marionette.absolute_url("test.html") test_html = self.marionette.absolute_url("test.html")
self.marionette.navigate(test_html) self.marionette.navigate(test_html)
el = self.marionette.execute_script("return window.document.getElementsByName('myInput')[0];") 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(HTMLElement, type(found_el))
self.assertEqual(el, 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(HTMLElement, type(found_el))
self.assertEqual(el, found_el) self.assertEqual(el, found_el)
@ -73,10 +75,10 @@ class TestElements(MarionetteTestCase):
test_html = self.marionette.absolute_url("test.html") test_html = self.marionette.absolute_url("test.html")
self.marionette.navigate(test_html) self.marionette.navigate(test_html)
el = self.marionette.execute_script("return window.document.getElementById('testh1');") 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(HTMLElement, type(found_el))
self.assertEqual(el, 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(HTMLElement, type(found_el))
self.assertEqual(el, found_el) self.assertEqual(el, found_el)
@ -84,10 +86,10 @@ class TestElements(MarionetteTestCase):
test_html = self.marionette.absolute_url("test.html") test_html = self.marionette.absolute_url("test.html")
self.marionette.navigate(test_html) self.marionette.navigate(test_html)
el = self.marionette.execute_script("return window.document.getElementById('mozLink');") 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(HTMLElement, type(found_el))
self.assertEqual(el, 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(HTMLElement, type(found_el))
self.assertEqual(el, found_el) self.assertEqual(el, found_el)
@ -95,10 +97,10 @@ class TestElements(MarionetteTestCase):
test_html = self.marionette.absolute_url("test.html") test_html = self.marionette.absolute_url("test.html")
self.marionette.navigate(test_html) self.marionette.navigate(test_html)
el = self.marionette.execute_script("return window.document.getElementById('mozLink');") 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(HTMLElement, type(found_el))
self.assertEqual(el, 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(HTMLElement, type(found_el))
self.assertEqual(el, found_el) self.assertEqual(el, found_el)
@ -106,10 +108,10 @@ class TestElements(MarionetteTestCase):
test_html = self.marionette.absolute_url("test.html") test_html = self.marionette.absolute_url("test.html")
self.marionette.navigate(test_html) self.marionette.navigate(test_html)
el = self.marionette.execute_script("return window.document.getElementById('mozLink');") 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(HTMLElement, type(found_el))
self.assertEqual(el, 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(HTMLElement, type(found_el))
self.assertEqual(el, found_el) self.assertEqual(el, found_el)
@ -117,33 +119,34 @@ class TestElements(MarionetteTestCase):
test_html = self.marionette.absolute_url("test.html") test_html = self.marionette.absolute_url("test.html")
self.marionette.navigate(test_html) self.marionette.navigate(test_html)
self.marionette.set_search_timeout(1000) 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.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): def test_timeout(self):
test_html = self.marionette.absolute_url("test.html") test_html = self.marionette.absolute_url("test.html")
self.marionette.navigate(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.assertTrue(True, self.marionette.set_search_timeout(4000))
self.marionette.navigate(test_html) 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): def test_css_selector_scope_doesnt_start_at_rootnode(self):
test_html = self.marionette.absolute_url("test.html") test_html = self.marionette.absolute_url("test.html")
self.marionette.navigate(test_html) self.marionette.navigate(test_html)
el = self.marionette.find_element("id","mozLink") el = self.marionette.find_element(By.ID, "mozLink")
nav_el = self.marionette.find_element("id","testDiv") nav_el = self.marionette.find_element(By.ID, "testDiv")
found_els = nav_el.find_elements("css selector", "a") found_els = nav_el.find_elements(By.CSS_SELECTOR, "a")
self.assertFalse(el.id in [found_el.id for found_el in found_els]) self.assertFalse(el.id in [found_el.id for found_el in found_els])
def test_finding_active_element_returns_element(self): def test_finding_active_element_returns_element(self):
test_html = self.marionette.absolute_url("test.html") test_html = self.marionette.absolute_url("test.html")
self.marionette.navigate(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() abody = self.marionette.get_active_element()
self.assertEqual(fbody, abody) self.assertEqual(fbody, abody)
class TestElementsChrome(MarionetteTestCase): class TestElementsChrome(MarionetteTestCase):
def setUp(self): def setUp(self):
MarionetteTestCase.setUp(self) MarionetteTestCase.setUp(self)
@ -161,51 +164,51 @@ class TestElementsChrome(MarionetteTestCase):
def test_id(self): def test_id(self):
el = self.marionette.execute_script("return window.document.getElementById('textInput');") 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(HTMLElement, type(found_el))
self.assertEqual(el, found_el) self.assertEqual(el, found_el)
def test_child_element(self): def test_child_element(self):
el = self.marionette.find_element("id", "textInput") el = self.marionette.find_element(By.ID, "textInput")
parent = self.marionette.find_element("id", "things") parent = self.marionette.find_element(By.ID, "things")
found_el = parent.find_element("tag name", "textbox") found_el = parent.find_element(By.TAG_NAME, "textbox")
self.assertEqual(HTMLElement, type(found_el)) self.assertEqual(HTMLElement, type(found_el))
self.assertEqual(el, found_el) self.assertEqual(el, found_el)
def test_child_elements(self): def test_child_elements(self):
el = self.marionette.find_element("id", "textInput3") el = self.marionette.find_element(By.ID, "textInput3")
parent = self.marionette.find_element("id", "things") parent = self.marionette.find_element(By.ID, "things")
found_els = parent.find_elements("tag name", "textbox") found_els = parent.find_elements(By.TAG_NAME, "textbox")
self.assertTrue(el.id in [found_el.id for found_el in found_els]) self.assertTrue(el.id in [found_el.id for found_el in found_els])
def test_tag_name(self): def test_tag_name(self):
el = self.marionette.execute_script("return window.document.getElementsByTagName('vbox')[0];") 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.assertEquals('vbox', found_el.tag_name)
self.assertEqual(HTMLElement, type(found_el)) self.assertEqual(HTMLElement, type(found_el))
self.assertEqual(el, found_el) self.assertEqual(el, found_el)
def test_class_name(self): def test_class_name(self):
el = self.marionette.execute_script("return window.document.getElementsByClassName('asdf')[0];") el = self.marionette.execute_script("return window.document.getElementsByClassName('asdf')[0];")
found_el = self.marionette.find_element("class name", "asdf") found_el = self.marionette.find_element(By.CLASS_NAME, "asdf")
self.assertEqual(HTMLElement, type(found_el)); self.assertEqual(HTMLElement, type(found_el))
self.assertEqual(el, found_el) self.assertEqual(el, found_el)
def test_xpath(self): def test_xpath(self):
el = self.marionette.execute_script("return window.document.getElementById('testBox');") el = self.marionette.execute_script("return window.document.getElementById('testBox');")
found_el = self.marionette.find_element("xpath", "id('testBox')") found_el = self.marionette.find_element(By.XPATH, "id('testBox')")
self.assertEqual(HTMLElement, type(found_el)); self.assertEqual(HTMLElement, type(found_el))
self.assertEqual(el, found_el) self.assertEqual(el, found_el)
def test_not_found(self): def test_not_found(self):
self.marionette.set_search_timeout(1000) 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.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): 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.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.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'));") self.marionette.execute_script("window.document.getElementById('things').removeChild(window.document.getElementById('myid'));")