test_actions_pointer.py (4586B)
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 import By, errors, Wait 8 from marionette_driver.keys import Keys 9 10 from marionette_harness import MarionetteTestCase 11 12 13 def inline(doc): 14 return "data:text/html;charset=utf-8,{}".format(quote(doc)) 15 16 17 class BaseMouseAction(MarionetteTestCase): 18 def setUp(self): 19 super(BaseMouseAction, self).setUp() 20 self.mouse_chain = self.marionette.actions.sequence( 21 "pointer", "pointer_id", {"pointerType": "mouse"} 22 ) 23 24 if self.marionette.session_capabilities["platformName"] == "mac": 25 self.mod_key = Keys.META 26 else: 27 self.mod_key = Keys.CONTROL 28 29 def tearDown(self): 30 self.marionette.actions.release() 31 32 super(BaseMouseAction, self).tearDown() 33 34 @property 35 def click_position(self): 36 return self.marionette.execute_script( 37 """ 38 if (window.click_x && window.click_y) { 39 return {x: window.click_x, y: window.click_y}; 40 } 41 """, 42 sandbox=None, 43 ) 44 45 def get_element_center_point(self, elem): 46 # pylint --py3k W1619 47 return { 48 "x": elem.rect["x"] + elem.rect["width"] / 2, 49 "y": elem.rect["y"] + elem.rect["height"] / 2, 50 } 51 52 53 class TestPointerActions(BaseMouseAction): 54 def test_click_action(self): 55 test_html = self.marionette.absolute_url("test.html") 56 self.marionette.navigate(test_html) 57 link = self.marionette.find_element(By.ID, "mozLink") 58 self.mouse_chain.click(element=link).perform() 59 self.assertEqual( 60 "Clicked", 61 self.marionette.execute_script( 62 "return document.getElementById('mozLink').innerHTML" 63 ), 64 ) 65 66 def test_clicking_element_out_of_view(self): 67 self.marionette.navigate( 68 inline( 69 """ 70 <div style="position:relative;top:200vh;">foo</div> 71 """ 72 ) 73 ) 74 el = self.marionette.find_element(By.TAG_NAME, "div") 75 with self.assertRaises(errors.MoveTargetOutOfBoundsException): 76 self.mouse_chain.click(element=el).perform() 77 78 def test_double_click_action(self): 79 self.marionette.navigate( 80 inline( 81 """ 82 <script>window.eventCount = 0;</script> 83 <button onclick="window.eventCount++">foobar</button> 84 """ 85 ) 86 ) 87 88 el = self.marionette.find_element(By.CSS_SELECTOR, "button") 89 self.mouse_chain.click(el).pause(100).click(el).perform() 90 91 event_count = self.marionette.execute_script( 92 "return window.eventCount", sandbox=None 93 ) 94 self.assertEqual(event_count, 2) 95 96 def test_context_click_action(self): 97 test_html = self.marionette.absolute_url("clicks.html") 98 self.marionette.navigate(test_html) 99 click_el = self.marionette.find_element(By.ID, "normal") 100 101 def context_menu_state(): 102 with self.marionette.using_context("chrome"): 103 cm_el = self.marionette.find_element(By.ID, "contentAreaContextMenu") 104 return cm_el.get_property("state") 105 106 self.assertEqual("closed", context_menu_state()) 107 self.mouse_chain.click(element=click_el, button=2).perform() 108 Wait(self.marionette).until( 109 lambda _: context_menu_state() == "open", 110 message="Context menu did not open", 111 ) 112 with self.marionette.using_context("chrome"): 113 cm_el = self.marionette.find_element(By.ID, "contentAreaContextMenu") 114 self.marionette.execute_script( 115 "arguments[0].hidePopup()", script_args=(cm_el,) 116 ) 117 Wait(self.marionette).until( 118 lambda _: context_menu_state() == "closed", 119 message="Context menu did not close", 120 ) 121 122 def test_middle_click_action(self): 123 test_html = self.marionette.absolute_url("clicks.html") 124 self.marionette.navigate(test_html) 125 126 self.marionette.find_element(By.ID, "addbuttonlistener").click() 127 128 el = self.marionette.find_element(By.ID, "showbutton") 129 self.mouse_chain.click(element=el, button=1).perform() 130 131 Wait(self.marionette).until( 132 lambda _: el.get_property("innerHTML") == "1", 133 message="Middle-click hasn't been fired", 134 )