test_window_management.py (5607B)
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 import os 6 import sys 7 8 from marionette_driver import By 9 from marionette_driver.errors import NoSuchWindowException 10 from marionette_harness import MarionetteTestCase, WindowManagerMixin 11 12 # add this directory to the path 13 sys.path.append(os.path.dirname(__file__)) 14 15 from chrome_handler_mixin import ChromeHandlerMixin 16 17 18 class TestNoSuchWindowContent( 19 ChromeHandlerMixin, WindowManagerMixin, MarionetteTestCase 20 ): 21 def setUp(self): 22 super(TestNoSuchWindowContent, self).setUp() 23 24 def tearDown(self): 25 self.close_all_tabs() 26 super(TestNoSuchWindowContent, self).tearDown() 27 28 def test_closed_chrome_window(self): 29 with self.marionette.using_context("chrome"): 30 new_window = self.open_window() 31 self.marionette.switch_to_window(new_window) 32 self.marionette.close_chrome_window() 33 34 # When closing a browser window both handles are not available 35 for context in ("chrome", "content"): 36 with self.marionette.using_context(context): 37 with self.assertRaises(NoSuchWindowException): 38 self.marionette.current_chrome_window_handle 39 with self.assertRaises(NoSuchWindowException): 40 self.marionette.current_window_handle 41 42 self.marionette.switch_to_window(self.start_window) 43 44 with self.assertRaises(NoSuchWindowException): 45 self.marionette.switch_to_window(new_window) 46 47 def test_closed_chrome_window_while_in_frame(self): 48 new_window = self.open_chrome_window(self.chrome_base_url + "test.xhtml") 49 self.marionette.switch_to_window(new_window) 50 with self.marionette.using_context("chrome"): 51 self.marionette.switch_to_frame(0) 52 self.marionette.close_chrome_window() 53 54 with self.assertRaises(NoSuchWindowException): 55 self.marionette.current_window_handle 56 with self.assertRaises(NoSuchWindowException): 57 self.marionette.current_chrome_window_handle 58 59 self.marionette.switch_to_window(self.start_window) 60 61 with self.assertRaises(NoSuchWindowException): 62 self.marionette.switch_to_window(new_window) 63 64 def test_closed_tab(self): 65 new_tab = self.open_tab() 66 self.marionette.switch_to_window(new_tab) 67 self.marionette.close() 68 69 # Check that only the content window is not available in both contexts 70 for context in ("chrome", "content"): 71 with self.marionette.using_context(context): 72 with self.assertRaises(NoSuchWindowException): 73 self.marionette.current_window_handle 74 self.marionette.current_chrome_window_handle 75 76 self.marionette.switch_to_window(self.start_tab) 77 78 with self.assertRaises(NoSuchWindowException): 79 self.marionette.switch_to_window(new_tab) 80 81 def test_closed_tab_while_in_frame(self): 82 new_tab = self.open_tab() 83 self.marionette.switch_to_window(new_tab) 84 85 with self.marionette.using_context("content"): 86 self.marionette.navigate(self.marionette.absolute_url("test_iframe.html")) 87 frame = self.marionette.find_element(By.ID, "test_iframe") 88 self.marionette.switch_to_frame(frame) 89 self.marionette.close() 90 91 with self.assertRaises(NoSuchWindowException): 92 self.marionette.current_window_handle 93 self.marionette.current_chrome_window_handle 94 95 self.marionette.switch_to_window(self.start_tab) 96 97 with self.assertRaises(NoSuchWindowException): 98 self.marionette.switch_to_window(new_tab) 99 100 101 class TestNoSuchWindowChrome(TestNoSuchWindowContent): 102 def setUp(self): 103 super(TestNoSuchWindowChrome, self).setUp() 104 self.marionette.set_context("chrome") 105 106 def tearDown(self): 107 self.close_all_windows() 108 super(TestNoSuchWindowChrome, self).tearDown() 109 110 111 class TestSwitchWindow(WindowManagerMixin, MarionetteTestCase): 112 def setUp(self): 113 super(TestSwitchWindow, self).setUp() 114 self.marionette.set_context("chrome") 115 116 def tearDown(self): 117 self.close_all_windows() 118 super(TestSwitchWindow, self).tearDown() 119 120 def test_switch_window_after_open_and_close(self): 121 with self.marionette.using_context("chrome"): 122 new_window = self.open_window() 123 self.assertEqual( 124 len(self.marionette.chrome_window_handles), len(self.start_windows) + 1 125 ) 126 self.assertIn(new_window, self.marionette.chrome_window_handles) 127 self.assertEqual( 128 self.marionette.current_chrome_window_handle, self.start_window 129 ) 130 131 # switch to the new chrome window and close it 132 self.marionette.switch_to_window(new_window) 133 self.assertEqual(self.marionette.current_chrome_window_handle, new_window) 134 self.assertNotEqual( 135 self.marionette.current_chrome_window_handle, self.start_window 136 ) 137 138 self.marionette.close_chrome_window() 139 self.assertEqual( 140 len(self.marionette.chrome_window_handles), len(self.start_windows) 141 ) 142 self.assertNotIn(new_window, self.marionette.chrome_window_handles) 143 144 # switch back to the original chrome window 145 self.marionette.switch_to_window(self.start_window) 146 self.assertEqual( 147 self.marionette.current_chrome_window_handle, self.start_window 148 )