From b3f624a11993651de1cf9d8f0100924439bad09a Mon Sep 17 00:00:00 2001 From: Trung Le Date: Thu, 8 Apr 2021 01:54:51 +0700 Subject: [PATCH] feat: add tox test for static format check via black/flake8, integration test --- .gitignore | 3 +++ tests/conftest.py | 33 +++++++++++++++++++++++++++++++++ tests/v1/head_test.py | 20 ++++++++++++++++++++ tox.ini | 34 ++++++++++++++++++++++++++++++++++ 4 files changed, 90 insertions(+) create mode 100644 tests/conftest.py create mode 100644 tests/v1/head_test.py create mode 100644 tox.ini diff --git a/.gitignore b/.gitignore index 25ff4b0..ab9c70d 100644 --- a/.gitignore +++ b/.gitignore @@ -127,3 +127,6 @@ dmypy.json # Pyre type checker .pyre/ + +# patched chromedriver +chromedriver \ No newline at end of file diff --git a/tests/conftest.py b/tests/conftest.py new file mode 100644 index 0000000..0d0d1eb --- /dev/null +++ b/tests/conftest.py @@ -0,0 +1,33 @@ +import pytest +from _pytest.fixtures import FixtureRequest +import undetected_chromedriver as uc + +FAILED_SCREENSHOT_NAME = "failed.png" + + +@pytest.fixture +def head_uc(request: FixtureRequest): + request.instance.driver = uc.Chrome() + + def teardown(): + request.instance.driver.save_screenshot(FAILED_SCREENSHOT_NAME) + request.instance.driver.quit() + + request.addfinalizer(teardown) + + return request.instance.driver + + +@pytest.fixture +def headless_uc(request: FixtureRequest): + options = uc.ChromeOptions() + options.headless = True + request.instance.driver = uc.Chrome(options=options) + + def teardown(): + request.instance.driver.save_screenshot(FAILED_SCREENSHOT_NAME) + request.instance.driver.quit() + + request.addfinalizer(teardown) + + return request.instance.driver diff --git a/tests/v1/head_test.py b/tests/v1/head_test.py new file mode 100644 index 0000000..bbc3713 --- /dev/null +++ b/tests/v1/head_test.py @@ -0,0 +1,20 @@ +import pytest +from selenium.webdriver.remote.webdriver import WebDriver +from selenium.webdriver.common.by import By +from selenium.webdriver.support.ui import WebDriverWait +from selenium.webdriver.support import expected_conditions as EC + + +TIMEOUT = 10 + + +@pytest.mark.usefixtures("head_uc") +class TestBotCheck: + def test_bot_protect_io(self): + driver: WebDriver = self.driver # type: ignore + driver.get("https://lhcdn.botprotect.io") + msg = WebDriverWait(driver, TIMEOUT).until( + EC.visibility_of_element_located((By.ID, "msg")) + ) + print(msg.text) + assert "You are human." in msg.text diff --git a/tox.ini b/tox.ini new file mode 100644 index 0000000..8fe6ad2 --- /dev/null +++ b/tox.ini @@ -0,0 +1,34 @@ +[tox] +envlist = + py38, + static_check, + v1, + v2 + +[testenv] +deps = + pytest + selenium + +[testenv:static_check] +deps = + black + flake8 +commands = + black --check --diff . + flake8 undetected_chromedriver \ + --max-line-length=88 \ + --exclude v2.py + +[testenv:v1] +commands = + pytest \ + -v -s \ + ./tests/v1 + +; Currently v2 is WIP +; [testenv:v2] +; commands = +; pytest \ +; -v -s \ +; ./tests/v2