-
Notifications
You must be signed in to change notification settings - Fork 20
/
conftest.py
150 lines (106 loc) · 4.41 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
import os
import json
import shutil
import tempfile
import time
import pytest
import typing
import platform
from webdriver_manager.core.driver_cache import DriverCacheManager
from botcity.web import WebBot, Browser, By, browsers
from webdriver_manager.chrome import ChromeDriverManager
from webdriver_manager.firefox import GeckoDriverManager
from webdriver_manager.microsoft import EdgeChromiumDriverManager
OS_NAME = platform.system()
PROJECT_DIR = os.path.abspath('tests')
TEST_PAGE = "https://lf2a.github.io/webpage-test/test.html"
INDEX_PAGE = "https://lf2a.github.io/webpage-test/"
platforms = {
"Linux": "linux",
"Darwin": "mac",
"Windows": "windows"
}
def get_fake_bin_path(web: WebBot) -> str:
return os.path.join(web.download_folder_path, 'fake.bin')
def setup_chrome(headless: bool, tmp_folder: str, download_driver: str) -> WebBot:
web = WebBot(headless)
web.browser = Browser.CHROME
web.driver_path = download_driver
web.download_folder_path = tmp_folder
return web
def setup_undetected_chrome(headless: bool, tmp_folder: str, download_driver: str) -> WebBot:
web = WebBot(headless)
web.browser = Browser.UNDETECTED_CHROME
web.driver_path = download_driver
web.download_folder_path = tmp_folder
return web
def setup_firefox(headless: bool, tmp_folder: str, download_driver: str) -> WebBot:
web = WebBot(headless)
web.browser = Browser.FIREFOX
web.driver_path = download_driver
web.download_folder_path = tmp_folder
return web
def setup_edge(headless: bool, tmp_folder: str, download_driver: str) -> WebBot:
web = WebBot(headless)
web.browser = Browser.EDGE
web.driver_path = download_driver
web.download_folder_path = tmp_folder
opt = browsers.edge.default_options(headless=headless, download_folder_path=tmp_folder)
platform_name = platforms.get(platform.system())
opt.platform_name = platform_name
web.options = opt
return web
def factory_setup_browser(browser: str, is_headless: bool, tmp_folder: str, download_driver: str) -> WebBot:
dict_browsers = {
'chrome': setup_chrome,
'firefox': setup_firefox,
'edge': setup_edge,
'undetected_chrome': setup_undetected_chrome
}
setup_browser = dict_browsers.get(browser, None)
if setup_browser is None:
raise ValueError(f'Browser [{browser}] not supported.')
return setup_browser(headless=is_headless, tmp_folder=tmp_folder, download_driver=download_driver)
def factory_driver_manager(browser: str):
dict_driver_manager = {
'chrome': ChromeDriverManager,
'firefox': GeckoDriverManager,
'edge': EdgeChromiumDriverManager,
'undetected_chrome': ChromeDriverManager
}
driver_manager = dict_driver_manager.get(browser, None)
if dict_driver_manager is None:
raise ValueError(f'Driver to [{browser}] not supported.')
return driver_manager
@pytest.fixture
def tmp_folder() -> str:
folder = tempfile.mkdtemp()
yield folder
shutil.rmtree(folder)
@pytest.fixture(autouse=True, scope="session")
def download_driver(request):
folder_driver = tempfile.mkdtemp()
browser = request.config.getoption("--browser") or Browser.CHROME
manager = factory_driver_manager(browser=browser)
cache_manager = DriverCacheManager(root_dir=folder_driver)
installed_driver = manager(cache_manager=cache_manager).install()
yield installed_driver
# Issue: https://github.com/ultrafunkamsterdam/undetected-chromedriver/issues/551
if platforms.get(platform.system()) == "windows" and browser == Browser.UNDETECTED_CHROME:
time.sleep(3)
shutil.rmtree(folder_driver)
@pytest.fixture
def web(request, tmp_folder: str, download_driver: str):
browser = request.config.getoption("--browser") or Browser.CHROME
is_headless = request.config.getoption("--headless") or "false"
is_headless = True if is_headless.lower() == "true" else False
web = factory_setup_browser(browser=browser, is_headless=is_headless, tmp_folder=tmp_folder,
download_driver=download_driver)
yield web
web.stop_browser()
def get_event_result(id_event: str, web: WebBot) -> typing.Dict:
event_result = web.find_element(id_event, By.ID)
return json.loads(event_result.text)
def pytest_addoption(parser):
parser.addoption('--headless', action='store', default="true")
parser.addoption('--browser', action='store', default='chrome')