-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
conftest.py
157 lines (116 loc) · 3.7 KB
/
conftest.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
import time
import pytest
from bs4 import BeautifulSoup
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
def pytest_addoption(parser):
parser.addoption(
"--driver-path",
dest="driver_path",
action="store",
help="Path to the chrome webdriver to use",
required=True,
)
parser.addoption(
"--use-headless",
dest="use_headless",
action="store_true",
help="Use browser in headless mode",
required=False,
default=False,
)
parser.addoption(
"--site-path",
dest="site_path",
help="Base URL to load for tests",
required=False,
default="http://localhost:4000",
)
def pytest_generate_tests(metafunc):
# This is called for every test. Only get/set command line arguments
# if the argument is specified in the list of test "fixturenames".
driver_path = metafunc.config.option.driver_path
use_headless = metafunc.config.option.use_headless
site_path = metafunc.config.option.site_path
if driver_path is None:
raise Exception("Must provide --driver_path")
if "driver_path" in metafunc.fixturenames:
metafunc.parametrize(
"driver_path", [metafunc.config.getoption("driver_path")], scope="session"
)
if "use_headless" in metafunc.fixturenames:
metafunc.parametrize("use_headless", [use_headless], scope="session")
if "site_path" in metafunc.fixturenames:
metafunc.parametrize("site_path", [site_path])
@pytest.fixture(scope="session",)
def browser_backend(driver_path, use_headless):
if not browser_backend.result:
opts = Options()
if use_headless:
opts.add_argument("--headless")
opts.add_argument("--disable-gpu")
browser_backend.result = webdriver.Chrome(driver_path, options=opts)
browser_backend.result.set_page_load_timeout(10)
yield browser_backend.result
try:
browser_backend.result.close()
except:
pass
browser_backend.result = None
browser_backend.result = None
@pytest.fixture()
def browser(browser_backend, base_url):
browser_backend.delete_all_cookies()
browser_backend.set_window_size(*BrowserSizes.MEDIUM)
time.sleep(.5)
browser_backend.get(base_url)
yield browser_backend
def browser_set_cookie(browser, name, value):
browser.add_cookie(
{
"name": name,
"value": value,
# "domain": "localhost"
}
)
@pytest.fixture()
def eager_generation_browser(browser):
browser_set_cookie(browser, "feature_toggles/EAGER_GENERATION", "true")
yield browser
class BrowserSizes:
# width x height
SMALL = (360, 560)
MEDIUM = (1034, 708)
LARGE = (1600, 745)
@pytest.fixture()
def small_browser(browser):
browser.set_window_size(*BrowserSizes.SMALL)
yield browser
@pytest.fixture()
def medium_browser(browser):
browser.set_window_size(*BrowserSizes.MEDIUM)
yield browser
@pytest.fixture()
def large_browser(browser):
browser.set_window_size(*BrowserSizes.LARGE)
yield browser
@pytest.fixture()
def single_source_methods__browser(browser):
browser_set_cookie(browser, "feature_toggles/SINGLE_SOURCE", "true")
yield browser
@pytest.fixture()
def compression_enabled__browser(browser):
browser_set_cookie(browser, "feature_toggles/ENABLE_COMPRESSION", "true")
yield browser
@pytest.fixture()
def root_page(browser, base_url):
browser.get(base_url)
yield browser
@pytest.fixture()
def parser():
def _get_parser(html):
return BeautifulSoup(html, "html.parser")
yield _get_parser
@pytest.fixture()
def base_url(site_path):
return site_path