-
Notifications
You must be signed in to change notification settings - Fork 119
Expand file tree
/
Copy pathtest_browserstack.py
More file actions
185 lines (155 loc) · 6.41 KB
/
test_browserstack.py
File metadata and controls
185 lines (155 loc) · 6.41 KB
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
# 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 os
import json
import pytest
from pytest_selenium.drivers.cloud import Provider
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,
"--driver",
"BrowserStack",
)
def test_missing_username(failure, monkeypatch, tmpdir):
monkeypatch.setattr(os.path, "expanduser", lambda p: str(tmpdir))
assert "BrowserStack username must be set" in failure()
def test_missing_access_key_env(failure, monkeypatch, tmpdir):
monkeypatch.setattr(os.path, "expanduser", lambda p: str(tmpdir))
monkeypatch.setenv("BROWSERSTACK_USERNAME", "foo")
assert "BrowserStack key must be set" in failure()
def test_missing_access_key_file(failure, monkeypatch, tmpdir):
monkeypatch.setattr(os.path, "expanduser", lambda p: str(tmpdir))
tmpdir.join(".browserstack").write("[credentials]\nusername=foo")
assert "BrowserStack key must be set" in failure()
@pytest.mark.parametrize(
("username", "key"),
[
("BROWSERSTACK_USERNAME", "BROWSERSTACK_ACCESS_KEY"),
("BROWSERSTACK_USR", "BROWSERSTACK_PSW"),
],
)
def test_invalid_credentials_env(failure, monkeypatch, tmpdir, username, key):
monkeypatch.setenv(username, "foo")
monkeypatch.setenv(key, "bar")
out = failure()
messages = [
"Invalid username or password",
"basic auth failed",
"Authorization required",
]
assert any(message in out for message in messages)
def test_invalid_credentials_file(failure, monkeypatch, tmpdir):
cfg_file = tmpdir.join(".browserstack")
cfg_file.write("[credentials]\nusername=foo\nkey=bar")
monkeypatch.setattr(Provider, "config_file_path", str(cfg_file))
out = failure()
messages = [
"Invalid username or password",
"basic auth failed",
"Authorization required",
]
assert any(message in out for message in messages)
def test_invalid_job_access_value(failure, monkeypatch, tmpdir):
monkeypatch.setattr(os.path, "expanduser", lambda p: str(tmpdir))
tmpdir.join(".browserstack").write("[report]\njob_access=foo")
assert "BrowserStack job_access invalid value `foo`" in failure()
def test_default_caps_in_jsonwp(monkeypatch, testdir):
capabilities = {"browserName": "chrome"}
test_name = "test_default_caps_in_jsonwp.test_bstack_capabilities"
monkeypatch.setenv("BROWSERSTACK_USERNAME", "foo")
monkeypatch.setenv("BROWSERSTACK_ACCESS_KEY", "bar")
variables = testdir.makefile(
".json", '{{"capabilities": {}}}'.format(json.dumps(capabilities))
)
file_test = testdir.makepyfile("""
import pytest
@pytest.mark.nondestructive
def test_bstack_capabilities(driver_kwargs):
assert driver_kwargs['options'].capabilities['browserstack.user'] == 'foo'
assert driver_kwargs['options'].capabilities['browserstack.key'] == 'bar'
assert driver_kwargs['options'].capabilities['name'] == '{0}'
""".format(test_name))
testdir.quick_qa(
"--driver", "BrowserStack", "--variables", variables, file_test, passed=1
)
def test_default_caps_in_jsonwp_with_conflict(monkeypatch, testdir):
capabilities = {"browserName": "chrome", "name": "conflicting_name"}
monkeypatch.setenv("BROWSERSTACK_USERNAME", "foo")
monkeypatch.setenv("BROWSERSTACK_ACCESS_KEY", "bar")
variables = testdir.makefile(
".json", '{{"capabilities": {}}}'.format(json.dumps(capabilities))
)
file_test = testdir.makepyfile("""
import pytest
@pytest.mark.nondestructive
def test_bstack_capabilities(driver_kwargs):
assert driver_kwargs['options'].capabilities['browserstack.user'] == 'foo'
assert driver_kwargs['options'].capabilities['browserstack.key'] == 'bar'
assert driver_kwargs['options'].capabilities['name'] == 'conflicting_name'
""")
testdir.quick_qa(
"--driver", "BrowserStack", "--variables", variables, file_test, passed=1
)
def test_default_caps_in_W3C(monkeypatch, testdir):
capabilities = {"browserName": "chrome", "bstack:options": {}}
monkeypatch.setenv("BROWSERSTACK_USERNAME", "foo")
monkeypatch.setenv("BROWSERSTACK_ACCESS_KEY", "bar")
variables = testdir.makefile(
".json", '{{"capabilities": {}}}'.format(json.dumps(capabilities))
)
file_test = testdir.makepyfile("""
import pytest
@pytest.mark.nondestructive
def test_bstack_capabilities(driver_kwargs):
assert driver_kwargs['options'].capabilities['bstack:options'] == {
'userName': 'foo',
'accessKey': 'bar',
'sessionName': 'test_default_caps_in_W3C.test_bstack_capabilities'
}
""")
testdir.quick_qa(
"--driver", "BrowserStack", "--variables", variables, file_test, passed=1
)
def test_default_caps_in_W3C_with_conflict(monkeypatch, testdir):
capabilities = {
"browserName": "chrome",
"bstack:options": {"sessionName": "conflicting_name"},
}
monkeypatch.setenv("BROWSERSTACK_USERNAME", "foo")
monkeypatch.setenv("BROWSERSTACK_ACCESS_KEY", "bar")
variables = testdir.makefile(
".json", '{{"capabilities": {}}}'.format(json.dumps(capabilities))
)
file_test = testdir.makepyfile("""
import pytest
@pytest.mark.nondestructive
def test_bstack_capabilities(driver_kwargs):
assert driver_kwargs['options'].capabilities['bstack:options'] == {
'userName': 'foo',
'accessKey': 'bar',
'sessionName': 'conflicting_name'
}
""")
testdir.quick_qa(
"--driver", "BrowserStack", "--variables", variables, file_test, passed=1
)