summaryrefslogtreecommitdiffstats
path: root/testing/marionette/harness/marionette_harness/tests/unit/test_capabilities.py
blob: d3386316d2b2f07d4748df2c6bca036d48bbc966 (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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
# 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.errors import SessionNotCreatedException

from marionette_harness import MarionetteTestCase


class TestCapabilities(MarionetteTestCase):

    def setUp(self):
        super(TestCapabilities, self).setUp()
        self.caps = self.marionette.session_capabilities
        with self.marionette.using_context("chrome"):
            self.appinfo = self.marionette.execute_script(
                "return Services.appinfo")
            self.os_name = self.marionette.execute_script(
                "return Services.sysinfo.getProperty('name')").lower()
            self.os_version = self.marionette.execute_script(
                "return Services.sysinfo.getProperty('version')")

    def test_mandated_capabilities(self):
        self.assertIn("browserName", self.caps)
        self.assertIn("browserVersion", self.caps)
        self.assertIn("platformName", self.caps)
        self.assertIn("platformVersion", self.caps)
        self.assertIn("acceptInsecureCerts", self.caps)
        self.assertIn("timeouts", self.caps)

        self.assertEqual(self.caps["browserName"], self.appinfo["name"].lower())
        self.assertEqual(self.caps["browserVersion"], self.appinfo["version"])
        self.assertEqual(self.caps["platformName"], self.os_name)
        self.assertEqual(self.caps["platformVersion"], self.os_version)
        self.assertFalse(self.caps["acceptInsecureCerts"])
        self.assertDictEqual(self.caps["timeouts"],
                             {"implicit": 0,
                              "page load": 300000,
                              "script": 30000})

    def test_supported_features(self):
        self.assertIn("rotatable", self.caps)

    def test_additional_capabilities(self):
        self.assertIn("moz:processID", self.caps)
        self.assertEqual(self.caps["moz:processID"], self.appinfo["processID"])
        self.assertEqual(self.marionette.process_id, self.appinfo["processID"])

        self.assertIn("moz:profile", self.caps)
        if self.marionette.instance is not None:
            if self.caps["browserName"] == "fennec":
                current_profile = self.marionette.instance.runner.device.app_ctx.remote_profile
            else:
                current_profile = self.marionette.instance.runner.profile.profile
            self.assertEqual(self.caps["moz:profile"], current_profile)
            self.assertEqual(self.marionette.profile, current_profile)

        self.assertIn("moz:accessibilityChecks", self.caps)
        self.assertFalse(self.caps["moz:accessibilityChecks"])
        self.assertIn("specificationLevel", self.caps)
        self.assertEqual(self.caps["specificationLevel"], 0)

    def test_set_specification_level(self):
        self.marionette.delete_session()
        self.marionette.start_session({"desiredCapabilities": {"specificationLevel": 2}})
        caps = self.marionette.session_capabilities
        self.assertEqual(2, caps["specificationLevel"])

        self.marionette.delete_session()
        self.marionette.start_session({"requiredCapabilities": {"specificationLevel": 3}})
        caps = self.marionette.session_capabilities
        self.assertEqual(3, caps["specificationLevel"])

    def test_we_can_pass_in_required_capabilities_on_session_start(self):
        self.marionette.delete_session()
        capabilities = {"requiredCapabilities": {"browserName": self.appinfo["name"].lower()}}
        self.marionette.start_session(capabilities)
        caps = self.marionette.session_capabilities
        self.assertIn("browserName", caps)

        # Start a new session just to make sure we leave the browser in the
        # same state it was before it started the test
        self.marionette.start_session()

    def test_capability_types(self):
        for value in ["", "invalid", True, 42, []]:
            print("testing value {}".format(value))
            with self.assertRaises(SessionNotCreatedException):
                print("  with desiredCapabilities")
                self.marionette.delete_session()
                self.marionette.start_session({"desiredCapabilities": value})
            with self.assertRaises(SessionNotCreatedException):
                print("  with requiredCapabilities")
                self.marionette.delete_session()
                self.marionette.start_session({"requiredCapabilities": value})

    def test_we_get_valid_uuid4_when_creating_a_session(self):
        self.assertNotIn("{", self.marionette.session_id,
                         "Session ID has {{}} in it: {}".format(
                             self.marionette.session_id))


class TestCapabilityMatching(MarionetteTestCase):
    allowed = [None, "*"]
    disallowed = ["", 42, True, {}, []]

    def setUp(self):
        MarionetteTestCase.setUp(self)
        self.browser_name = self.marionette.session_capabilities["browserName"]
        self.platform_name = self.marionette.session_capabilities["platformName"]
        self.delete_session()

    def delete_session(self):
        if self.marionette.session is not None:
            self.marionette.delete_session()

    def test_browser_name_desired(self):
        self.marionette.start_session({"desiredCapabilities": {"browserName": self.browser_name}})
        self.assertEqual(self.marionette.session_capabilities["browserName"], self.browser_name)

    def test_browser_name_required(self):
        self.marionette.start_session({"requiredCapabilities": {"browserName": self.browser_name}})
        self.assertEqual(self.marionette.session_capabilities["browserName"], self.browser_name)

    def test_browser_name_desired_allowed_types(self):
        for typ in self.allowed:
            self.delete_session()
            self.marionette.start_session({"desiredCapabilities": {"browserName": typ}})
            self.assertEqual(self.marionette.session_capabilities["browserName"], self.browser_name)

    def test_browser_name_desired_disallowed_types(self):
        for typ in self.disallowed:
            with self.assertRaises(SessionNotCreatedException):
                self.marionette.start_session({"desiredCapabilities": {"browserName": typ}})

    def test_browser_name_required_allowed_types(self):
        for typ in self.allowed:
            self.delete_session()
            self.marionette.start_session({"requiredCapabilities": {"browserName": typ}})
            self.assertEqual(self.marionette.session_capabilities["browserName"], self.browser_name)

    def test_browser_name_requried_disallowed_types(self):
        for typ in self.disallowed:
            with self.assertRaises(SessionNotCreatedException):
                self.marionette.start_session({"requiredCapabilities": {"browserName": typ}})

    def test_browser_name_prefers_required(self):
        caps = {"desiredCapabilities": {"browserName": "invalid"},
                    "requiredCapabilities": {"browserName": "*"}}
        self.marionette.start_session(caps)

    def test_browser_name_error_on_invalid_required(self):
        with self.assertRaises(SessionNotCreatedException):
            caps = {"desiredCapabilities": {"browserName": "*"},
                        "requiredCapabilities": {"browserName": "invalid"}}
            self.marionette.start_session(caps)

    # TODO(ato): browser version comparison not implemented yet

    def test_platform_name_desired(self):
        self.marionette.start_session({"desiredCapabilities": {"platformName": self.platform_name}})
        self.assertEqual(self.marionette.session_capabilities["platformName"], self.platform_name)

    def test_platform_name_required(self):
        self.marionette.start_session({"requiredCapabilities": {"platformName": self.platform_name}})
        self.assertEqual(self.marionette.session_capabilities["platformName"], self.platform_name)

    def test_platform_name_desired_allowed_types(self):
        for typ in self.allowed:
            self.delete_session()
            self.marionette.start_session({"desiredCapabilities": {"platformName": typ}})
            self.assertEqual(self.marionette.session_capabilities["platformName"], self.platform_name)

    def test_platform_name_desired_disallowed_types(self):
        for typ in self.disallowed:
            with self.assertRaises(SessionNotCreatedException):
                self.marionette.start_session({"desiredCapabilities": {"platformName": typ}})

    def test_platform_name_required_allowed_types(self):
        for typ in self.allowed:
            self.delete_session()
            self.marionette.start_session({"requiredCapabilities": {"platformName": typ}})
            self.assertEqual(self.marionette.session_capabilities["platformName"], self.platform_name)

    def test_platform_name_requried_disallowed_types(self):
        for typ in self.disallowed:
            with self.assertRaises(SessionNotCreatedException):
                self.marionette.start_session({"requiredCapabilities": {"platformName": typ}})

    def test_platform_name_prefers_required(self):
        caps = {"desiredCapabilities": {"platformName": "invalid"},
                    "requiredCapabilities": {"platformName": "*"}}
        self.marionette.start_session(caps)

    def test_platform_name_error_on_invalid_required(self):
        with self.assertRaises(SessionNotCreatedException):
            caps = {"desiredCapabilities": {"platformName": "*"},
                        "requiredCapabilities": {"platformName": "invalid"}}
            self.marionette.start_session(caps)

    # TODO(ato): platform version comparison not imlpemented yet

    def test_accept_insecure_certs(self):
        for capability_type in ["desiredCapabilities", "requiredCapabilities"]:
            print("testing {}".format(capability_type))
            for value in ["", 42, {}, []]:
                print("  type {}".format(type(value)))
                with self.assertRaises(SessionNotCreatedException):
                    self.marionette.start_session({capability_type: {"acceptInsecureCerts": value}})

        self.delete_session()
        self.marionette.start_session({"desiredCapabilities": {"acceptInsecureCerts": True}})
        self.assertTrue(self.marionette.session_capabilities["acceptInsecureCerts"])
        self.delete_session()
        self.marionette.start_session({"requiredCapabilities": {"acceptInsecureCerts": True}})

        self.assertTrue(self.marionette.session_capabilities["acceptInsecureCerts"])

    def test_page_load_strategy(self):
        for strategy in ["none", "eager", "normal"]:
            print("valid strategy {}".format(strategy))
            self.delete_session()
            self.marionette.start_session({"desiredCapabilities": {"pageLoadStrategy": strategy}})
            self.assertEqual(self.marionette.session_capabilities["pageLoadStrategy"], strategy)

        for value in ["", "EAGER", True, 42, {}, []]:
            print("invalid strategy {}".format(value))
            with self.assertRaises(SessionNotCreatedException):
                self.marionette.start_session({"desiredCapabilities": {"pageLoadStrategy": value}})

    def test_proxy_default(self):
        self.marionette.start_session()
        self.assertNotIn("proxy", self.marionette.session_capabilities)

    def test_proxy_desired(self):
        self.marionette.start_session({"desiredCapabilities": {"proxy": {"proxyType": "manual"}}})
        self.assertIn("proxy", self.marionette.session_capabilities)
        self.assertEqual(self.marionette.session_capabilities["proxy"]["proxyType"], "manual")
        self.assertEqual(self.marionette.get_pref("network.proxy.type"), 1)

    def test_proxy_required(self):
        self.marionette.start_session({"requiredCapabilities": {"proxy": {"proxyType": "manual"}}})
        self.assertIn("proxy", self.marionette.session_capabilities)
        self.assertEqual(self.marionette.session_capabilities["proxy"]["proxyType"], "manual")
        self.assertEqual(self.marionette.get_pref("network.proxy.type"), 1)

    def test_timeouts(self):
        timeouts = {u"implicit": 123, u"page load": 456, u"script": 789}
        caps = {"desiredCapabilities": {"timeouts": timeouts}}
        self.marionette.start_session(caps)
        self.assertIn("timeouts", self.marionette.session_capabilities)
        self.assertDictEqual(self.marionette.session_capabilities["timeouts"], timeouts)
        self.assertDictEqual(self.marionette._send_message("getTimeouts"), timeouts)