Debian Codegraph @

testing/web-platform/mozilla/tests/webdriver/support/fixtures.py

raw
1 import pytest
2 import pytest_asyncio
3 from tests.support.helpers import deep_update
4
5 from .helpers import (
6 Browser,
7 Geckodriver,
8 create_custom_profile,
9 get_pref,
10 get_profile_folder,
11 read_user_preferences,
12 set_pref,
13 )
14
15
16 def pytest_collection_modifyitems(items):
17 """Auto-apply markers for specific API usage by fixtures"""
18 for item in items:
19 if "use_pref" in getattr(item, "fixturenames", ()):
20 item.add_marker("allow_system_access")
21
22
23 def pytest_configure(config):
24 # register the allow_system_access marker
25 config.addinivalue_line(
26 "markers", "allow_system_access: Mark test to allow system access"
27 )
28
29
30 @pytest.fixture(scope="module")
31 def browser(configuration, firefox_options):
32 """Start a Firefox instance without using geckodriver.
33
34 geckodriver will automatically use the --remote-allow-hosts and
35 --remote.allow.origins command line arguments.
36
37 Starting Firefox without geckodriver allows to set those command line arguments
38 as needed. The fixture method returns the browser instance that should be used
39 to connect to a RemoteAgent supported protocol (CDP, WebDriver BiDi).
40 """
41 current_browser = None
42
43 def _browser(
44 clone_profile=True,
45 extra_args=None,
46 extra_prefs=None,
47 use_bidi=False,
48 use_cdp=False,
49 use_marionette=False,
50 ):
51 nonlocal current_browser
52
53 webdriver_args = configuration["webdriver"]["args"]
54 log_level = None
55 truncate_enabled = True
56
57 if "-vv" or "-vvv" in webdriver_args:
58 log_level = "Trace"
59
60 if "-vvv" in webdriver_args:
61 truncate_enabled = False
62 elif "-v" in webdriver_args:
63 log_level = "Debug"
64
65 # If the requested preferences and arguments match the ones for the
66 # already started firefox, we can reuse the current firefox instance,
67 # return the instance immediately.
68 if current_browser:
69 if (
70 current_browser.extra_args == extra_args
71 and current_browser.extra_prefs == extra_prefs
72 and current_browser.is_running
73 and current_browser.use_bidi == use_bidi
74 and current_browser.use_cdp == use_cdp
75 and current_browser.use_marionette == use_marionette
76 and current_browser.log_level == log_level
77 and current_browser.truncate_enabled == truncate_enabled
78 ):
79 return current_browser
80
81 # Otherwise, if firefox is already started, terminate it because we need
82 # to create a new instance for the provided preferences.
83 current_browser.quit()
84
85 binary = configuration["browser"]["binary"]
86 env = configuration["browser"]["env"]
87
88 profile_path = get_profile_folder(firefox_options)
89 default_prefs = read_user_preferences(profile_path)
90 profile = create_custom_profile(
91 profile_path, default_prefs, clone=clone_profile
92 )
93
94 current_browser = Browser(
95 binary,
96 profile,
97 extra_args=extra_args,
98 extra_prefs=extra_prefs,
99 env=env,
100 log_level=log_level,
101 truncate_enabled=truncate_enabled,
102 use_bidi=use_bidi,
103 use_cdp=use_cdp,
104 use_marionette=use_marionette,
105 )
106 current_browser.start()
107 return current_browser
108
109 yield _browser
110
111 # Stop firefox at the end of the test module.
112 if current_browser is not None:
113 current_browser.quit()
114 current_browser = None
115
116
117 @pytest.fixture
118 def default_capabilities(request):
119 """Default capabilities to use for a new WebDriver session for Mozilla specific tests."""
120
121 # Get the value from the overwritten original fixture
122 capabilities = request.getfixturevalue("default_capabilities")
123
124 allow_system_access = any(
125 marker.name == "allow_system_access" for marker in request.node.own_markers
126 )
127
128 if allow_system_access:
129 deep_update(
130 capabilities,
131 {
132 "moz:firefoxOptions": {
133 "args": [
134 "--remote-allow-system-access",
135 ]
136 }
137 },
138 )
139
140 return capabilities
141
142
143 @pytest.fixture
144 def default_preferences(profile_folder):
145 return read_user_preferences(profile_folder)
146
147
148 @pytest.fixture(name="create_custom_profile")
149 def fixture_create_custom_profile(default_preferences, profile_folder):
150 profile = None
151
152 def _create_custom_profile(clone=True):
153 profile = create_custom_profile(
154 profile_folder, default_preferences, clone=clone
155 )
156
157 return profile
158
159 yield _create_custom_profile
160
161 # if profile is not None:
162 if profile:
163 profile.cleanup()
164
165
166 @pytest.fixture(scope="session")
167 def firefox_options(configuration):
168 return configuration["capabilities"]["moz:firefoxOptions"]
169
170
171 @pytest_asyncio.fixture
172 async def geckodriver(configuration):
173 """Start a geckodriver instance directly."""
174 driver = None
175
176 def _geckodriver(config=None, hostname=None, extra_args=None, extra_env=None):
177 nonlocal driver
178
179 if config is None:
180 config = configuration
181
182 driver = Geckodriver(config, hostname, extra_args, extra_env)
183 driver.start()
184
185 return driver
186
187 yield _geckodriver
188
189 if driver is not None:
190 await driver.stop()
191
192
193 @pytest.fixture
194 def profile_folder(firefox_options):
195 return get_profile_folder(firefox_options)
196
197
198 @pytest.fixture
199 def use_pref(session):
200 """Set a specific pref value."""
201 reset_values = {}
202
203 def _use_pref(pref, value):
204 if pref not in reset_values:
205 reset_values[pref] = get_pref(session, pref)
206
207 set_pref(session, pref, value)
208
209 yield _use_pref
210
211 for pref, reset_value in reset_values.items():
212 set_pref(session, pref, reset_value)