test_element_state.py (6020B)
1 # This Source Code Form is subject to the terms of the Mozilla Public 2 # License, v. 2.0. If a copy of the MPL was not distributed with this 3 # file, You can obtain one at http://mozilla.org/MPL/2.0/. 4 5 from urllib.parse import quote 6 7 from marionette_driver.by import By 8 from marionette_harness import MarionetteTestCase 9 10 11 boolean_attributes = { 12 "audio": ["autoplay", "controls", "loop", "muted"], 13 "button": ["autofocus", "disabled", "formnovalidate"], 14 "details": ["open"], 15 "dialog": ["open"], 16 "fieldset": ["disabled"], 17 "form": ["novalidate"], 18 "iframe": ["allowfullscreen"], 19 "img": ["ismap"], 20 "input": [ 21 "autofocus", 22 "checked", 23 "disabled", 24 "formnovalidate", 25 "multiple", 26 "readonly", 27 "required", 28 ], 29 "menuitem": ["checked", "default", "disabled"], 30 "ol": ["reversed"], 31 "optgroup": ["disabled"], 32 "option": ["disabled", "selected"], 33 "script": ["async", "defer"], 34 "select": ["autofocus", "disabled", "multiple", "required"], 35 "textarea": ["autofocus", "disabled", "readonly", "required"], 36 "track": ["default"], 37 "video": ["autoplay", "controls", "loop", "muted"], 38 } 39 40 41 def inline(doc, doctype="html"): 42 if doctype == "html": 43 return "data:text/html;charset=utf-8,{}".format(quote(doc)) 44 elif doctype == "xhtml": 45 return "data:application/xhtml+xml,{}".format( 46 quote( 47 r"""<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 48 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 49 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> 50 <head> 51 <title>XHTML might be the future</title> 52 </head> 53 54 <body> 55 {} 56 </body> 57 </html>""".format( 58 doc 59 ) 60 ) 61 ) 62 63 64 attribute = inline("<input foo=bar>") 65 input = inline("<input>") 66 disabled = inline("<input disabled=baz>") 67 check = inline("<input type=checkbox>") 68 69 70 class TestIsElementEnabled(MarionetteTestCase): 71 def test_is_enabled(self): 72 test_html = self.marionette.absolute_url("test.html") 73 self.marionette.navigate(test_html) 74 l = self.marionette.find_element(By.NAME, "myCheckBox") 75 self.assertTrue(l.is_enabled()) 76 self.marionette.execute_script("arguments[0].disabled = true;", [l]) 77 self.assertFalse(l.is_enabled()) 78 79 80 class TestIsElementDisplayed(MarionetteTestCase): 81 def test_is_displayed(self): 82 test_html = self.marionette.absolute_url("test.html") 83 self.marionette.navigate(test_html) 84 l = self.marionette.find_element(By.NAME, "myCheckBox") 85 self.assertTrue(l.is_displayed()) 86 self.marionette.execute_script("arguments[0].hidden = true;", [l]) 87 self.assertFalse(l.is_displayed()) 88 89 90 class TestGetElementAttribute(MarionetteTestCase): 91 def test_normal_attribute(self): 92 self.marionette.navigate(inline("<p style=foo>")) 93 el = self.marionette.find_element(By.TAG_NAME, "p") 94 attr = el.get_attribute("style") 95 self.assertIsInstance(attr, str) 96 self.assertEqual("foo", attr) 97 98 def test_boolean_attributes(self): 99 for tag, attrs in boolean_attributes.items(): 100 for attr in attrs: 101 print("testing boolean attribute <{0} {1}>".format(tag, attr)) 102 doc = inline("<{0} {1}>".format(tag, attr)) 103 self.marionette.navigate(doc) 104 el = self.marionette.find_element(By.TAG_NAME, tag) 105 res = el.get_attribute(attr) 106 self.assertIsInstance(res, str) 107 self.assertEqual("true", res) 108 109 def test_global_boolean_attributes(self): 110 self.marionette.navigate(inline("<p hidden>foo")) 111 el = self.marionette.find_element(By.TAG_NAME, "p") 112 attr = el.get_attribute("hidden") 113 self.assertIsInstance(attr, str) 114 self.assertEqual("true", attr) 115 116 self.marionette.navigate(inline("<p>foo")) 117 el = self.marionette.find_element(By.TAG_NAME, "p") 118 attr = el.get_attribute("hidden") 119 self.assertIsNone(attr) 120 121 self.marionette.navigate(inline("<p itemscope>foo")) 122 el = self.marionette.find_element(By.TAG_NAME, "p") 123 attr = el.get_attribute("itemscope") 124 self.assertIsInstance(attr, str) 125 self.assertEqual("true", attr) 126 127 self.marionette.navigate(inline("<p>foo")) 128 el = self.marionette.find_element(By.TAG_NAME, "p") 129 attr = el.get_attribute("itemscope") 130 self.assertIsNone(attr) 131 132 # TODO(ato): Test for custom elements 133 134 def test_xhtml(self): 135 doc = inline('<p hidden="true">foo</p>', doctype="xhtml") 136 self.marionette.navigate(doc) 137 el = self.marionette.find_element(By.TAG_NAME, "p") 138 attr = el.get_attribute("hidden") 139 self.assertIsInstance(attr, str) 140 self.assertEqual("true", attr) 141 142 143 class TestGetElementProperty(MarionetteTestCase): 144 def test_get(self): 145 self.marionette.navigate(disabled) 146 el = self.marionette.find_element(By.TAG_NAME, "input") 147 prop = el.get_property("disabled") 148 self.assertIsInstance(prop, bool) 149 self.assertTrue(prop) 150 151 def test_missing_property_returns_default(self): 152 self.marionette.navigate(input) 153 el = self.marionette.find_element(By.TAG_NAME, "input") 154 prop = el.get_property("checked") 155 self.assertIsInstance(prop, bool) 156 self.assertFalse(prop) 157 158 def test_attribute_not_returned(self): 159 self.marionette.navigate(attribute) 160 el = self.marionette.find_element(By.TAG_NAME, "input") 161 self.assertEqual(el.get_property("foo"), None) 162 163 def test_manipulated_element(self): 164 self.marionette.navigate(check) 165 el = self.marionette.find_element(By.TAG_NAME, "input") 166 self.assertEqual(el.get_property("checked"), False) 167 168 el.click() 169 self.assertEqual(el.get_property("checked"), True) 170 171 el.click() 172 self.assertEqual(el.get_property("checked"), False)