summaryrefslogtreecommitdiffstats
path: root/testing/marionette/harness/marionette_harness/tests/unit/test_modal_dialogs.py
blob: f7108bdfff463882d6e3fe1c9bd7a7401a9904d3 (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
# 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.by import By
from marionette_driver.errors import NoAlertPresentException, ElementNotInteractableException
from marionette_driver.marionette import Alert
from marionette_driver.wait import Wait

from marionette_harness import MarionetteTestCase, skip_if_e10s


class TestTabModals(MarionetteTestCase):

    def setUp(self):
        super(TestTabModals, self).setUp()
        self.marionette.set_pref("prompts.tab_modal.enabled", True)
        self.marionette.navigate(self.marionette.absolute_url('modal_dialogs.html'))

    def tearDown(self):
        # Ensure an alert is absent before proceeding past this test.
        Wait(self.marionette).until(lambda _: not self.alert_present())
        self.marionette.execute_script("window.onbeforeunload = null;")
        self.marionette.clear_pref("prompts.tab_modal.enabled")
        super(TestTabModals, self).tearDown()

    def alert_present(self):
        try:
            Alert(self.marionette).text
            return True
        except NoAlertPresentException:
            return False

    def wait_for_alert(self):
        Wait(self.marionette).until(lambda _: self.alert_present())

    def test_no_alert_raises(self):
        self.assertRaises(NoAlertPresentException, Alert(self.marionette).accept)
        self.assertRaises(NoAlertPresentException, Alert(self.marionette).dismiss)

    def test_alert_accept(self):
        self.marionette.find_element(By.ID, 'modal-alert').click()
        self.wait_for_alert()
        alert = self.marionette.switch_to_alert()
        alert.accept()

    def test_alert_dismiss(self):
        self.marionette.find_element(By.ID, 'modal-alert').click()
        self.wait_for_alert()
        alert = self.marionette.switch_to_alert()
        alert.dismiss()

    def test_confirm_accept(self):
        self.marionette.find_element(By.ID, 'modal-confirm').click()
        self.wait_for_alert()
        alert = self.marionette.switch_to_alert()
        alert.accept()
        self.wait_for_condition(lambda mn: mn.find_element(By.ID, 'confirm-result').text == 'true')

    def test_confirm_dismiss(self):
        self.marionette.find_element(By.ID, 'modal-confirm').click()
        self.wait_for_alert()
        alert = self.marionette.switch_to_alert()
        alert.dismiss()
        self.wait_for_condition(lambda mn: mn.find_element(By.ID, 'confirm-result').text == 'false')

    def test_prompt_accept(self):
        self.marionette.find_element(By.ID, 'modal-prompt').click()
        self.wait_for_alert()
        alert = self.marionette.switch_to_alert()
        alert.accept()
        self.wait_for_condition(lambda mn: mn.find_element(By.ID, 'prompt-result').text == '')

    def test_prompt_dismiss(self):
        self.marionette.find_element(By.ID, 'modal-prompt').click()
        self.wait_for_alert()
        alert = self.marionette.switch_to_alert()
        alert.dismiss()
        self.wait_for_condition(lambda mn: mn.find_element(By.ID, 'prompt-result').text == 'null')

    def test_alert_text(self):
        with self.assertRaises(NoAlertPresentException):
            alert = self.marionette.switch_to_alert()
            alert.text
        self.marionette.find_element(By.ID, 'modal-alert').click()
        self.wait_for_alert()
        alert = self.marionette.switch_to_alert()
        self.assertEqual(alert.text, 'Marionette alert')
        alert.accept()

    def test_prompt_text(self):
        with self.assertRaises(NoAlertPresentException):
            alert = self.marionette.switch_to_alert()
            alert.text
        self.marionette.find_element(By.ID, 'modal-prompt').click()
        self.wait_for_alert()
        alert = self.marionette.switch_to_alert()
        self.assertEqual(alert.text, 'Marionette prompt')
        alert.accept()

    def test_confirm_text(self):
        with self.assertRaises(NoAlertPresentException):
            alert = self.marionette.switch_to_alert()
            alert.text
        self.marionette.find_element(By.ID, 'modal-confirm').click()
        self.wait_for_alert()
        alert = self.marionette.switch_to_alert()
        self.assertEqual(alert.text, 'Marionette confirm')
        alert.accept()

    def test_set_text_throws(self):
        self.assertRaises(NoAlertPresentException, Alert(self.marionette).send_keys, "Foo")
        self.marionette.find_element(By.ID, 'modal-alert').click()
        self.wait_for_alert()
        alert = self.marionette.switch_to_alert()
        self.assertRaises(ElementNotInteractableException, alert.send_keys, "Foo")
        alert.accept()

    def test_set_text_accept(self):
        self.marionette.find_element(By.ID, 'modal-prompt').click()
        self.wait_for_alert()
        alert = self.marionette.switch_to_alert()
        alert.send_keys("Some text!");
        alert.accept()
        self.wait_for_condition(lambda mn: mn.find_element(By.ID, 'prompt-result').text == 'Some text!')

    def test_set_text_dismiss(self):
        self.marionette.find_element(By.ID, 'modal-prompt').click()
        self.wait_for_alert()
        alert = self.marionette.switch_to_alert()
        alert.send_keys("Some text!");
        alert.dismiss()
        self.wait_for_condition(lambda mn: mn.find_element(By.ID, 'prompt-result').text == 'null')

    def test_onbeforeunload_dismiss(self):
        start_url = self.marionette.get_url()
        self.marionette.find_element(By.ID, 'onbeforeunload-handler').click()
        self.wait_for_condition(
            lambda mn: mn.execute_script("""
              return window.onbeforeunload !== null;
            """))
        self.marionette.navigate("about:blank")
        self.wait_for_alert()
        alert = self.marionette.switch_to_alert()
        self.assertTrue(alert.text.startswith("This page is asking you to confirm"))
        alert.dismiss()
        self.assertTrue(self.marionette.get_url().startswith(start_url))

    def test_onbeforeunload_accept(self):
        self.marionette.find_element(By.ID, 'onbeforeunload-handler').click()
        self.wait_for_condition(
            lambda mn: mn.execute_script("""
              return window.onbeforeunload !== null;
            """))
        self.marionette.navigate("about:blank")
        self.wait_for_alert()
        alert = self.marionette.switch_to_alert()
        self.assertTrue(alert.text.startswith("This page is asking you to confirm"))
        alert.accept()
        self.wait_for_condition(lambda mn: mn.get_url() == "about:blank")

    @skip_if_e10s("Bug 1325044")
    def test_unrelated_command_when_alert_present(self):
        click_handler = self.marionette.find_element(By.ID, 'click-handler')
        text = self.marionette.find_element(By.ID, 'click-result').text
        self.assertEqual(text, '')

        self.marionette.find_element(By.ID, 'modal-alert').click()
        self.wait_for_alert()

        # Commands succeed, but because the dialog blocks the event loop,
        # our actions aren't reflected on the page.
        text = self.marionette.find_element(By.ID, 'click-result').text
        self.assertEqual(text, '')
        click_handler.click()
        text = self.marionette.find_element(By.ID, 'click-result').text
        self.assertEqual(text, '')

        alert = self.marionette.switch_to_alert()
        alert.accept()

        Wait(self.marionette).until(lambda _: not self.alert_present())

        click_handler.click()
        text = self.marionette.find_element(By.ID, 'click-result').text
        self.assertEqual(text, 'result')


class TestGlobalModals(TestTabModals):

    def setUp(self):
        super(TestGlobalModals, self).setUp()
        self.marionette.set_pref("prompts.tab_modal.enabled", False)

    def test_unrelated_command_when_alert_present(self):
        # The assumptions in this test do not hold on certain platforms, and not when
        # e10s is enabled.
        pass