|
1 | | -import contextlib |
2 | | -import time |
3 | | -from collections import namedtuple |
4 | | -from typing import TYPE_CHECKING |
| 1 | +from typing import Generator |
5 | 2 |
|
| 3 | +from django.conf import settings |
| 4 | +from seleniumbase import config as sb_config |
| 5 | +from seleniumbase.core import session_helper |
6 | 6 | import pytest |
7 | | -from selenium.webdriver import Keys |
8 | | -from selenium.webdriver.common.by import By |
9 | 7 |
|
10 | | -if TYPE_CHECKING: |
11 | | - from selenium.webdriver.common.timeouts import Timeouts |
12 | | - |
13 | | -Proxy = namedtuple("Proxy", "host,port") |
14 | | - |
15 | | - |
16 | | -def pytest_configure(config): |
17 | | - if not config.option.driver: |
18 | | - config.option.driver = "chrome" |
19 | | - |
20 | | - |
21 | | -@contextlib.contextmanager |
22 | | -def timeouts(driver, wait=None, page=None, script=None): |
23 | | - _current: Timeouts = driver.timeouts |
24 | | - if wait: |
25 | | - driver.implicitly_wait(wait) |
26 | | - if page: |
27 | | - driver.set_page_load_timeout(page) |
28 | | - if script: |
29 | | - driver.set_script_timeout(script) |
30 | | - yield |
31 | | - driver.timeouts = _current |
32 | | - |
33 | | - |
34 | | -def set_input_value(driver, *args): |
35 | | - rules = args[:-1] |
36 | | - el = driver.find_element(*rules) |
37 | | - el.clear() |
38 | | - el.send_keys(args[-1]) |
39 | | - |
40 | | - |
41 | | -def find_by_css(selenium, *args): |
42 | | - from testutils.selenium import wait_for |
43 | | - |
44 | | - return wait_for(selenium, By.CSS_SELECTOR, *args) |
45 | | - |
46 | | - |
47 | | -def select2(driver, by, selector, value): |
48 | | - el = driver.find_element(by, selector) |
49 | | - el.click() |
50 | | - time.sleep(1) |
51 | | - driver.switch_to.active_element.send_keys(value) |
52 | | - driver.find_element(By.XPATH, f"//div[contains(text(),'{value}')]").click() |
53 | | - time.sleep(1) |
54 | | - driver.switch_to.active_element.send_keys(Keys.TAB) |
55 | | - time.sleep(1) |
56 | | - driver.switch_to.active_element.send_keys(Keys.TAB) |
| 8 | +from testutils.selenium import CountryWorkspaceSeleniumTC |
57 | 9 |
|
58 | 10 |
|
59 | 11 | @pytest.fixture |
60 | | -def chrome_options(request, chrome_options): |
61 | | - if not request.config.getvalue("show_browser"): |
62 | | - chrome_options.add_argument("--headless") |
63 | | - chrome_options.add_argument("--allow-insecure-localhost") |
64 | | - chrome_options.add_argument("--disable-browser-side-navigation") |
65 | | - chrome_options.add_argument("--disable-dev-shm-usage") |
66 | | - chrome_options.add_argument("--disable-gpu") |
67 | | - chrome_options.add_argument("--disable-translate") |
68 | | - chrome_options.add_argument("--ignore-certificate-errors") |
69 | | - chrome_options.add_argument("--lang=en-GB") |
70 | | - chrome_options.add_argument("--no-sandbox") |
71 | | - chrome_options.add_argument("--proxy-bypass-list=*") |
72 | | - chrome_options.add_argument("--proxy-server='direct://'") |
73 | | - chrome_options.add_argument("--start-maximized") |
74 | | - |
75 | | - prefs = {"profile.default_content_setting_values.notifications": 1} # explicitly allow notifications |
76 | | - chrome_options.add_experimental_option("prefs", prefs) |
77 | | - |
78 | | - return chrome_options |
79 | | - |
80 | | - |
81 | | -SELENIUM_DEFAULT_PAGE_LOAD_TIMEOUT = 5 |
82 | | -SELENIUM_DEFAULT_IMPLICITLY_WAIT = 1 |
83 | | -SELENIUM_DEFAULT_SCRIPT_TIMEOUT = 1 |
84 | | - |
85 | | - |
86 | | -@pytest.fixture |
87 | | -def selenium(monkeypatch, live_server, settings, driver): |
88 | | - from testutils.selenium import wait_for, wait_for_url |
89 | | - |
| 12 | +def browser(live_server, request) -> Generator[CountryWorkspaceSeleniumTC, None, None]: |
| 13 | + """SeleniumBase as a pytest fixture. |
| 14 | + Usage example: "def test_one(sb):" |
| 15 | + You may need to use this for tests that use other pytest fixtures.""" |
90 | 16 | settings.FLAGS = {"LOCAL_LOGIN": [("boolean", True)]} |
91 | | - |
92 | | - driver.with_timeouts = timeouts.__get__(driver) |
93 | | - driver.set_input_value = set_input_value.__get__(driver) |
94 | | - driver.live_server = live_server |
95 | | - driver.wait_for = wait_for.__get__(driver) |
96 | | - driver.wait_for_url = wait_for_url.__get__(driver) |
97 | | - driver.find_by_css = find_by_css.__get__(driver) |
98 | | - driver.select2 = select2.__get__(driver) |
99 | | - |
100 | | - return driver |
| 17 | + if request.cls: |
| 18 | + if sb_config.reuse_class_session: |
| 19 | + the_class = str(request.cls).split(".")[-1].split("'")[0] |
| 20 | + if the_class != sb_config._sb_class: |
| 21 | + session_helper.end_reused_class_session_as_needed() |
| 22 | + sb_config._sb_class = the_class |
| 23 | + request.cls.sb = CountryWorkspaceSeleniumTC("base_method") |
| 24 | + request.cls.sb.live_server_url = str(live_server) |
| 25 | + request.cls.sb.setUp() |
| 26 | + request.cls.sb._needs_tearDown = True |
| 27 | + request.cls.sb._using_sb_fixture = True |
| 28 | + request.cls.sb._using_sb_fixture_class = True |
| 29 | + sb_config._sb_node[request.node.nodeid] = request.cls.sb |
| 30 | + yield request.cls.sb |
| 31 | + if request.cls.sb._needs_tearDown: |
| 32 | + request.cls.sb.tearDown() |
| 33 | + request.cls.sb._needs_tearDown = False |
| 34 | + else: |
| 35 | + sb = CountryWorkspaceSeleniumTC("base_method") |
| 36 | + sb.live_server_url = str(live_server) |
| 37 | + sb.setUp() |
| 38 | + sb._needs_tearDown = True |
| 39 | + sb._using_sb_fixture = True |
| 40 | + sb._using_sb_fixture_no_class = True |
| 41 | + sb_config._sb_node[request.node.nodeid] = sb |
| 42 | + sb.maximize_window() |
| 43 | + yield sb |
| 44 | + if sb._needs_tearDown: |
| 45 | + sb.tearDown() |
| 46 | + sb._needs_tearDown = False |
0 commit comments