|
| 1 | +import os |
| 2 | +import json |
| 3 | +import pytest |
| 4 | +import typing |
| 5 | +import platform |
| 6 | + |
| 7 | +from botcity.web import WebBot, Browser, By, browsers |
| 8 | + |
| 9 | +OS_NAME = platform.system() |
| 10 | + |
| 11 | +PROJECT_DIR = os.path.abspath('') |
| 12 | +FAKE_BIN_PATH = os.path.join(PROJECT_DIR, 'fake.bin') |
| 13 | +TEST_PAGE = "https://lf2a.github.io/webpage-test/test.html" |
| 14 | +INDEX_PAGE = "https://lf2a.github.io/webpage-test/" |
| 15 | + |
| 16 | + |
| 17 | +def setup_chrome(headless: bool) -> WebBot: |
| 18 | + web = WebBot(headless) |
| 19 | + web.browser = Browser.CHROME |
| 20 | + |
| 21 | + if OS_NAME == 'Windows': |
| 22 | + web.driver_path = os.path.join(PROJECT_DIR, 'web-drivers', 'windows', 'chromedriver.exe') |
| 23 | + elif OS_NAME == 'Linux': |
| 24 | + web.driver_path = os.path.join(PROJECT_DIR, 'web-drivers', 'linux', 'chromedriver') |
| 25 | + elif OS_NAME == 'Darwin': |
| 26 | + web.driver_path = os.path.join(PROJECT_DIR, 'web-drivers', 'macos', 'chromedriver') |
| 27 | + else: |
| 28 | + raise ValueError(f'OS [{OS_NAME}] not supported.') |
| 29 | + return web |
| 30 | + |
| 31 | + |
| 32 | +def setup_firefox(headless: bool) -> WebBot: |
| 33 | + web = WebBot(headless) |
| 34 | + web.browser = Browser.FIREFOX |
| 35 | + |
| 36 | + if OS_NAME == 'Windows': |
| 37 | + web.driver_path = os.path.join(PROJECT_DIR, 'web-drivers', 'windows', 'geckodriver.exe') |
| 38 | + elif OS_NAME == 'Linux': |
| 39 | + web.driver_path = os.path.join(PROJECT_DIR, 'web-drivers', 'linux', 'geckodriver') |
| 40 | + elif OS_NAME == 'Darwin': |
| 41 | + web.driver_path = os.path.join(PROJECT_DIR, 'web-drivers', 'macos', 'geckodriver') |
| 42 | + else: |
| 43 | + raise ValueError(f'OS [{OS_NAME}] not supported.') |
| 44 | + return web |
| 45 | + |
| 46 | + |
| 47 | +def setup_edge(headless: bool) -> WebBot: |
| 48 | + web = WebBot(headless) |
| 49 | + web.browser = Browser.EDGE |
| 50 | + |
| 51 | + opt = browsers.edge.default_options(headless=headless, download_folder_path=web.download_folder_path) |
| 52 | + opt.set_capability('platform', 'ANY') # WINDOWS is default value: |
| 53 | + |
| 54 | + if OS_NAME == 'Windows': |
| 55 | + web.driver_path = os.path.join(PROJECT_DIR, 'web-drivers', 'windows', 'msedgedriver.exe') |
| 56 | + elif OS_NAME == 'Linux': |
| 57 | + web.driver_path = os.path.join(PROJECT_DIR, 'web-drivers', 'linux', 'msedgedriver') |
| 58 | + elif OS_NAME == 'Darwin': |
| 59 | + web.driver_path = os.path.join(PROJECT_DIR, 'web-drivers', 'macos', 'msedgedriver') |
| 60 | + else: |
| 61 | + raise ValueError(f'OS [{OS_NAME}] not supported.') |
| 62 | + web.options = opt |
| 63 | + return web |
| 64 | + |
| 65 | + |
| 66 | +@pytest.fixture |
| 67 | +def web(request): |
| 68 | + |
| 69 | + browser = request.config.getoption("--browser") or Browser.CHROME |
| 70 | + is_headless = request.config.getoption("--headless") |
| 71 | + |
| 72 | + if browser == 'chrome': |
| 73 | + web = setup_chrome(is_headless) |
| 74 | + elif browser == 'firefox': |
| 75 | + web = setup_firefox(is_headless) |
| 76 | + elif browser == 'edge': |
| 77 | + web = setup_edge(is_headless) |
| 78 | + else: |
| 79 | + raise ValueError(f'Browser [{browser}] not supported.') |
| 80 | + yield web |
| 81 | + web.stop_browser() |
| 82 | + |
| 83 | + |
| 84 | +def get_event_result(id_event: str, web: WebBot) -> typing.Dict: |
| 85 | + event_result = web.find_element(id_event, By.ID) |
| 86 | + return json.loads(event_result.text) |
| 87 | + |
| 88 | + |
| 89 | +def pytest_addoption(parser): |
| 90 | + parser.addoption('--headless', action='store_const', const=True) |
| 91 | + parser.addoption('--browser', action='store', default='chrome') |
| 92 | + |
0 commit comments