summaryrefslogtreecommitdiffstats
path: root/testing/marionette/harness/marionette_harness/tests/unit/test_window_handles_chrome.py
blob: 7260d6324fed7c9f6c233ec9c576037090f236da (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.

from marionette_driver import By, Wait

from marionette_harness import MarionetteTestCase, WindowManagerMixin


class TestWindowHandles(WindowManagerMixin, MarionetteTestCase):

    def setUp(self):
        super(TestWindowHandles, self).setUp()

        self.empty_page = self.marionette.absolute_url("empty.html")
        self.test_page = self.marionette.absolute_url("windowHandles.html")
        self.marionette.navigate(self.test_page)

        self.marionette.set_context("chrome")

    def tearDown(self):
        self.close_all_windows()
        self.close_all_tabs()

        super(TestWindowHandles, self).tearDown()

    def test_chrome_window_handles_with_scopes(self):
        # Open a browser and a non-browser (about window) chrome window
        self.open_window(
            trigger=lambda: self.marionette.execute_script("window.open();"))
        self.assertEqual(len(self.marionette.chrome_window_handles), len(self.start_windows) + 1)
        self.assertEqual(self.marionette.current_chrome_window_handle, self.start_window)

        self.open_window(
            trigger=lambda: self.marionette.find_element(By.ID, "aboutName").click())
        self.assertEqual(len(self.marionette.chrome_window_handles), len(self.start_windows) + 2)
        self.assertEqual(self.marionette.current_chrome_window_handle, self.start_window)

        chrome_window_handles_in_chrome_scope = self.marionette.chrome_window_handles
        window_handles_in_chrome_scope = self.marionette.window_handles

        with self.marionette.using_context("content"):
            self.assertEqual(self.marionette.chrome_window_handles,
                             chrome_window_handles_in_chrome_scope)
            self.assertEqual(self.marionette.window_handles,
                             window_handles_in_chrome_scope)

    def test_chrome_window_handles_after_opening_new_window(self):
        def open_with_link():
            with self.marionette.using_context("content"):
                link = self.marionette.find_element(By.ID, "new-window")
                link.click()

        # We open a new window but are actually interested in the new tab
        new_win = self.open_window(trigger=open_with_link)
        self.assertEqual(len(self.marionette.chrome_window_handles), len(self.start_windows) + 1)
        self.assertEqual(self.marionette.current_chrome_window_handle, self.start_window)

        # Check that the new tab has the correct page loaded
        self.marionette.switch_to_window(new_win)
        self.assertEqual(self.marionette.current_chrome_window_handle, new_win)
        with self.marionette.using_context("content"):
            Wait(self.marionette, timeout=self.marionette.timeout.page_load).until(
                lambda mn: mn.get_url() == self.empty_page,
                message="{} did not load after opening a new tab".format(self.empty_page))

        # Ensure navigate works in our current window
        other_page = self.marionette.absolute_url("test.html")
        with self.marionette.using_context("content"):
            self.marionette.navigate(other_page)
            self.assertEqual(self.marionette.get_url(), other_page)

        # Close the opened window and carry on in our original tab.
        self.marionette.close()
        self.assertEqual(len(self.marionette.chrome_window_handles), len(self.start_windows))

        self.marionette.switch_to_window(self.start_window)
        self.assertEqual(self.marionette.current_chrome_window_handle, self.start_window)
        with self.marionette.using_context("content"):
            self.assertEqual(self.marionette.get_url(), self.test_page)

    def test_window_handles_after_opening_new_tab(self):
        def open_with_link():
            with self.marionette.using_context("content"):
                link = self.marionette.find_element(By.ID, "new-tab")
                link.click()

        new_tab = self.open_tab(trigger=open_with_link)
        self.assertEqual(len(self.marionette.window_handles), len(self.start_tabs) + 1)
        self.assertEqual(self.marionette.current_window_handle, self.start_tab)

        self.marionette.switch_to_window(new_tab)
        self.assertEqual(self.marionette.current_window_handle, new_tab)
        with self.marionette.using_context("content"):
            Wait(self.marionette, timeout=self.marionette.timeout.page_load).until(
                lambda mn: mn.get_url() == self.empty_page,
                message="{} did not load after opening a new tab".format(self.empty_page))

        # Ensure navigate works in our current tab
        other_page = self.marionette.absolute_url("test.html")
        with self.marionette.using_context("content"):
            self.marionette.navigate(other_page)
            self.assertEqual(self.marionette.get_url(), other_page)

        self.marionette.switch_to_window(self.start_tab)
        self.assertEqual(self.marionette.current_window_handle, self.start_tab)
        with self.marionette.using_context("content"):
            self.assertEqual(self.marionette.get_url(), self.test_page)

        self.marionette.switch_to_window(new_tab)
        self.marionette.close()
        self.assertEqual(len(self.marionette.window_handles), len(self.start_tabs))

        self.marionette.switch_to_window(self.start_tab)
        self.assertEqual(self.marionette.current_window_handle, self.start_tab)

    def test_window_handles_after_opening_new_window(self):
        def open_with_link():
            with self.marionette.using_context("content"):
                link = self.marionette.find_element(By.ID, "new-window")
                link.click()

        # We open a new window but are actually interested in the new tab
        new_tab = self.open_tab(trigger=open_with_link)
        self.assertEqual(len(self.marionette.window_handles), len(self.start_tabs) + 1)
        self.assertEqual(self.marionette.current_window_handle, self.start_tab)

        # Check that the new tab has the correct page loaded
        self.marionette.switch_to_window(new_tab)
        self.assertEqual(self.marionette.current_window_handle, new_tab)
        with self.marionette.using_context("content"):
            Wait(self.marionette, timeout=self.marionette.timeout.page_load).until(
                lambda mn: mn.get_url() == self.empty_page,
                message="{} did not load after opening a new tab".format(self.empty_page))

        # Ensure navigate works in our current window
        other_page = self.marionette.absolute_url("test.html")
        with self.marionette.using_context("content"):
            self.marionette.navigate(other_page)
            self.assertEqual(self.marionette.get_url(), other_page)

        # Close the opened window and carry on in our original tab.
        self.marionette.close()
        self.assertEqual(len(self.marionette.window_handles), len(self.start_tabs))

        self.marionette.switch_to_window(self.start_tab)
        self.assertEqual(self.marionette.current_window_handle, self.start_tab)
        with self.marionette.using_context("content"):
            self.assertEqual(self.marionette.get_url(), self.test_page)

    def test_window_handles_after_closing_original_tab(self):
        def open_with_link():
            with self.marionette.using_context("content"):
                link = self.marionette.find_element(By.ID, "new-tab")
                link.click()

        new_tab = self.open_tab(trigger=open_with_link)
        self.assertEqual(len(self.marionette.window_handles), len(self.start_tabs) + 1)
        self.assertEqual(self.marionette.current_window_handle, self.start_tab)

        self.marionette.close()
        self.assertEqual(len(self.marionette.window_handles), len(self.start_tabs))

        self.marionette.switch_to_window(new_tab)
        self.assertEqual(self.marionette.current_window_handle, new_tab)
        with self.marionette.using_context("content"):
            Wait(self.marionette, timeout=self.marionette.timeout.page_load).until(
                lambda mn: mn.get_url() == self.empty_page,
                message="{} did not load after opening a new tab".format(self.empty_page))

    def test_window_handles_no_switch(self):
        """Regression test for bug 1294456.
        This test is testing the case where Marionette attempts to send a
        command to a window handle when the browser has opened and selected
        a new tab. Before bug 1294456 landed, the Marionette driver was getting
        confused about which window handle the client cared about, and assumed
        it was the window handle for the newly opened and selected tab.

        This caused Marionette to think that the browser needed to do a remoteness
        flip in the e10s case, since the tab opened by menu_newNavigatorTab is
        about:newtab (which is currently non-remote). This meant that commands
        sent to what should have been the original window handle would be
        queued and never sent, since the remoteness flip in the new tab was
        never going to happen.
        """
        def open_with_menu():
            menu_new_tab = self.marionette.find_element(By.ID, 'menu_newNavigatorTab')
            menu_new_tab.click()

        new_tab = self.open_tab(trigger=open_with_menu)

        # We still have the default tab set as our window handle. This
        # get_url command should be sent immediately, and not be forever-queued.
        with self.marionette.using_context("content"):
            self.assertEqual(self.marionette.get_url(), self.test_page)

        self.assertEqual(len(self.marionette.window_handles), len(self.start_tabs) + 1)
        self.assertEqual(self.marionette.current_window_handle, self.start_tab)

        self.marionette.switch_to_window(new_tab)
        self.assertEqual(self.marionette.current_window_handle, new_tab)

        self.marionette.close()
        self.assertEqual(len(self.marionette.window_handles), len(self.start_tabs))

        self.marionette.switch_to_window(self.start_tab)
        self.assertEqual(self.marionette.current_window_handle, self.start_tab)