-
Notifications
You must be signed in to change notification settings - Fork 120
/
Copy pathtest_driver.py
301 lines (252 loc) · 7.66 KB
/
test_driver.py
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
# 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 functools import partial
import pytest_selenium
import pytest
from pytest_selenium.utils import CaseInsensitiveDict
pytestmark = pytest.mark.nondestructive
@pytest.fixture
def testfile(testdir):
return testdir.makepyfile(
"""
import pytest
@pytest.mark.nondestructive
def test_pass(selenium): pass
"""
)
def failure_with_output(testdir, *args, **kwargs):
reprec = testdir.inline_run(*args, **kwargs)
passed, skipped, failed = reprec.listoutcomes()
assert len(failed) == 1
out = failed[0].longrepr.reprcrash.message
return out
@pytest.fixture
def failure(testdir, testfile, httpserver_base_url):
return partial(failure_with_output, testdir, testfile, httpserver_base_url)
def test_driver_case_insensitive(testdir):
file_test = testdir.makepyfile(
"""
import pytest
@pytest.mark.nondestructive
def test_pass(request):
assert request.config.getoption('driver') == 'SaUcELaBs'
"""
)
testdir.quick_qa("--driver", "SaUcELaBs", file_test, passed=1)
def test_missing_driver(failure):
out = failure()
assert "UsageError: --driver must be specified" in out
def test_invalid_driver(testdir):
testdir.makepyfile(
"""
import pytest
@pytest.mark.nondestructive
def test_pass(): pass
"""
)
invalid_driver = "noop"
result = testdir.runpytest("--driver", invalid_driver)
message = "--driver: invalid choice: '{}'".format(invalid_driver)
assert message in result.errlines[1]
def test_driver_quit(testdir):
testdir.makepyfile(
"""
import pytest
@pytest.mark.nondestructive
def test_driver_quit(selenium):
selenium.quit()
selenium.title
"""
)
result = testdir.runpytestqa()
result.stdout.fnmatch_lines_random(
[
"WARNING: Failed to gather URL: *",
"WARNING: Failed to gather screenshot: *",
"WARNING: Failed to gather HTML: *",
"WARNING: Failed to gather log types: *",
]
)
outcomes = result.parseoutcomes()
assert outcomes.get("failed") == 1
def test_default_host_port(testdir):
host = "localhost"
port = "4444"
file_test = testdir.makepyfile(
"""
import pytest
@pytest.mark.nondestructive
def test_pass(driver_kwargs):
assert driver_kwargs['command_executor'] == 'http://{}:{}/wd/hub'
""".format(
host, port
)
)
testdir.quick_qa("--driver", "Remote", file_test, passed=1)
@pytest.mark.parametrize(
("host", "expected"),
[
("https://some.host.com", "https://some.host.com"),
("http://some.host.com", "http://some.host.com"),
("some.host.com", "http://some.host.com"),
],
)
def test_host_protocol(host, expected, testdir):
filetest = testdir.makepyfile(
"""
import pytest
@pytest.mark.nondestructive
def test_pass(driver_kwargs):
assert driver_kwargs['command_executor'] == '{}:4444/wd/hub'
""".format(
expected
)
)
testdir.quick_qa(
"--driver",
"Remote",
"--selenium-host",
host,
"--selenium-port",
"4444",
filetest,
passed=1,
)
def test_arguments_order(testdir):
host = "notlocalhost"
port = "4441"
file_test = testdir.makepyfile(
"""
import pytest
@pytest.mark.nondestructive
def test_pass(driver_kwargs):
assert driver_kwargs['command_executor'] == 'http://{}:{}/wd/hub'
""".format(
host, port
)
)
testdir.quick_qa(
"--driver",
"Remote",
"--selenium-host",
host,
"--selenium-port",
port,
file_test,
passed=1,
)
def test_arguments_order_random(testdir):
host = "notlocalhost"
port = "4441"
file_test = testdir.makepyfile(
"""
import pytest
@pytest.mark.nondestructive
def test_pass(driver_kwargs):
assert driver_kwargs['command_executor'] == 'http://{}:{}/wd/hub'
""".format(
host, port
)
)
testdir.quick_qa(
"--selenium-host",
host,
"--driver",
"Remote",
"--selenium-port",
port,
file_test,
passed=1,
)
@pytest.mark.parametrize(
"name", ["SauceLabs", "TestingBot", "CrossBrowserTesting", "BrowserStack"]
)
def test_provider_naming(name):
import importlib
driver = name
module = importlib.import_module(
"pytest_selenium.drivers.{}".format(driver.lower())
)
provider = getattr(module, driver)()
assert provider.uses_driver(driver)
assert provider.name == name
@pytest.mark.xfail(reason="Remote driver currently doesn't support logs")
def test_service_log_path(testdir):
file_test = testdir.makepyfile(
"""
import pytest
@pytest.mark.nondestructive
def test_pass(driver_kwargs):
assert driver_kwargs['service_log_path'] is not None
"""
)
testdir.quick_qa(file_test, passed=1)
@pytest.mark.xfail(reason="Remote driver currently doesn't support logs")
def test_no_service_log_path(testdir):
file_test = testdir.makepyfile(
"""
import pytest
@pytest.fixture
def driver_log():
return None
@pytest.mark.nondestructive
def test_pass(driver_kwargs):
assert driver_kwargs['service_log_path'] is None
"""
)
testdir.quick_qa(file_test, passed=1)
def test_driver_retry_pass(testdir, mocker):
mocker.patch.dict(
pytest_selenium.SUPPORTED_DRIVERS,
CaseInsensitiveDict({"Firefox": mocker.MagicMock()}),
)
mock_retrying = mocker.spy(pytest_selenium.pytest_selenium, "Retrying")
file_test = testdir.makepyfile(
"""
import pytest
@pytest.mark.nondestructive
def test_pass(driver):
assert driver
"""
)
testdir.quick_qa(file_test, passed=1)
assert mock_retrying.spy_return.statistics["attempt_number"] == 1
@pytest.mark.parametrize("max_num_attempts", [None, 2])
def test_driver_retry_fail(testdir, mocker, max_num_attempts):
mocker.patch(
"pytest_selenium.webdriver.firefox.webdriver.FirefoxRemoteConnection",
side_effect=Exception("Connection Error"),
)
mocker.patch("pytest_selenium.webdriver.firefox.webdriver.Service")
mocker.patch("pytest_selenium.pytest_selenium.wait_exponential", return_value=0)
mock_retrying = mocker.spy(pytest_selenium.pytest_selenium, "Retrying")
default_attempts = 3
if max_num_attempts is not None:
testdir.makeini(
f"""
[pytest]
max_driver_init_attempts = {max_num_attempts}
"""
)
file_test = testdir.makepyfile(
"""
import pytest
@pytest.mark.nondestructive
def test_pass(driver):
assert True # catch if this starts to pass
"""
)
testdir.quick_qa("--driver", "Firefox", file_test, failed=1)
expected_attempts = max_num_attempts or default_attempts
assert mock_retrying.spy_return.statistics["attempt_number"] == expected_attempts
def test_xdist(testdir):
file_test = testdir.makepyfile(
"""
import pytest
@pytest.mark.nondestructive
def test_xdist(driver):
pass
"""
)
testdir.quick_qa("-n", "2", file_test, passed=1)