test_shadowroot_findelement.py (4074B)
1 from urllib.parse import quote 2 3 from marionette_driver.by import By 4 from marionette_driver.errors import ( 5 DetachedShadowRootException, 6 NoSuchElementException, 7 NoSuchShadowRootException, 8 ) 9 from marionette_driver.marionette import WebElement, ShadowRoot 10 11 from marionette_harness import MarionetteTestCase 12 13 14 def inline(doc): 15 return "data:text/html;charset=utf-8,{}".format(quote(doc)) 16 17 18 page_shadow_dom = inline( 19 """ 20 <custom-element></custom-element> 21 <script> 22 customElements.define('custom-element', 23 class extends HTMLElement { 24 constructor() { 25 super(); 26 this.attachShadow({mode: 'open'}).innerHTML = ` 27 <div><a href=# id=foo>full link text</a><a href=# id=bar>another link text</a></div> 28 `; 29 } 30 }); 31 </script>""" 32 ) 33 34 35 class TestShadowDOMFindElement(MarionetteTestCase): 36 def setUp(self): 37 MarionetteTestCase.setUp(self) 38 self.marionette.timeout.implicit = 0 39 40 def test_find_element_from_shadow_root(self): 41 self.marionette.navigate(page_shadow_dom) 42 custom_element = self.marionette.find_element(By.CSS_SELECTOR, "custom-element") 43 shadow_root = custom_element.shadow_root 44 found = shadow_root.find_element(By.CSS_SELECTOR, "a") 45 self.assertIsInstance(found, WebElement) 46 47 el = self.marionette.execute_script( 48 """ 49 return arguments[0].shadowRoot.querySelector('a') 50 """, 51 [custom_element], 52 ) 53 self.assertEqual(found, el) 54 55 def test_unknown_element_from_shadow_root(self): 56 self.marionette.navigate(page_shadow_dom) 57 shadow_root = self.marionette.find_element( 58 By.CSS_SELECTOR, "custom-element" 59 ).shadow_root 60 with self.assertRaises(NoSuchElementException): 61 shadow_root.find_element(By.CSS_SELECTOR, "does not exist") 62 63 def test_detached_shadow_root(self): 64 self.marionette.navigate(page_shadow_dom) 65 shadow_root = self.marionette.find_element( 66 By.CSS_SELECTOR, "custom-element" 67 ).shadow_root 68 self.marionette.refresh() 69 with self.assertRaises(DetachedShadowRootException): 70 shadow_root.find_element(By.CSS_SELECTOR, "a") 71 72 def test_no_such_shadow_root(self): 73 not_existing_shadow_root = ShadowRoot(self.marionette, "foo") 74 with self.assertRaises(NoSuchShadowRootException): 75 not_existing_shadow_root.find_element(By.CSS_SELECTOR, "a") 76 77 78 class TestShadowDOMFindElements(MarionetteTestCase): 79 def setUp(self): 80 MarionetteTestCase.setUp(self) 81 self.marionette.timeout.implicit = 0 82 83 def test_find_elements_from_shadow_root(self): 84 self.marionette.navigate(page_shadow_dom) 85 custom_element = self.marionette.find_element(By.CSS_SELECTOR, "custom-element") 86 shadow_root = custom_element.shadow_root 87 found = shadow_root.find_elements(By.CSS_SELECTOR, "a") 88 self.assertEqual(len(found), 2) 89 90 els = self.marionette.execute_script( 91 """ 92 return arguments[0].shadowRoot.querySelectorAll('a') 93 """, 94 [custom_element], 95 ) 96 97 for i in range(len(found)): 98 self.assertIsInstance(found[i], WebElement) 99 self.assertEqual(found[i], els[i]) 100 101 def test_detached_shadow_root(self): 102 self.marionette.navigate(page_shadow_dom) 103 shadow_root = self.marionette.find_element( 104 By.CSS_SELECTOR, "custom-element" 105 ).shadow_root 106 self.marionette.refresh() 107 with self.assertRaises(DetachedShadowRootException): 108 shadow_root.find_elements(By.CSS_SELECTOR, "a") 109 110 def test_no_such_shadow_root(self): 111 not_existing_shadow_root = ShadowRoot(self.marionette, "foo") 112 with self.assertRaises(NoSuchShadowRootException): 113 not_existing_shadow_root.find_elements(By.CSS_SELECTOR, "a")