diff --git a/pyproject.toml b/pyproject.toml index 4d6d508..a6ffaf2 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -60,6 +60,7 @@ tests = [ 'pytest>=8', 'selenium', 'pytest-selenium', + 'pytest-docker', 'black', 'nox', ] diff --git a/tests/conftest.py b/tests/conftest.py index 0ca9b66..bc025aa 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -18,6 +18,44 @@ def chrome_driver(): _driver.quit() +@pytest.fixture(scope="session") +def docker_compose_command() -> str: + return "podman-compose" + + +def is_responsive(port): + import socket + try: + sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + result = sock.connect_ex(('127.0.0.1',port)) + if result == 0: + return True + else: + return False + sock.close() + except Exception: + return False + + +@pytest.fixture(scope="session") +def selenium_service(docker_ip, docker_services): + # `port_for` takes a container port and returns the corresponding host port + port = docker_services.port_for("firefox", 5900) + docker_services.wait_until_responsive( + timeout=30.0, pause=0.1, check=lambda: is_responsive(port) + ) + + options = webdriver.FirefoxOptions() + driver = webdriver.Remote( + command_executor='http://localhost:4444/wd/hub', + options=options, + ) + + yield driver + + driver.quit() + + @pytest.fixture(autouse=True) def run_make_commands(request): # Get the directory of the current test file diff --git a/tests/docker-compose.yml b/tests/docker-compose.yml new file mode 100644 index 0000000..12e6302 --- /dev/null +++ b/tests/docker-compose.yml @@ -0,0 +1,50 @@ +version: '2' +services: + chrome: + image: docker.io/selenium/node-chrome:4.19.1-20240402 + shm_size: 2gb + depends_on: + - selenium-hub + environment: + - SE_EVENT_BUS_HOST=selenium-hub + - SE_EVENT_BUS_PUBLISH_PORT=4442 + - SE_EVENT_BUS_SUBSCRIBE_PORT=4443 + ports: + - "6900:5900" + volumes: + - .:/mnt + + edge: + image: docker.io/selenium/node-edge:4.19.1-20240402 + shm_size: 2gb + depends_on: + - selenium-hub + environment: + - SE_EVENT_BUS_HOST=selenium-hub + - SE_EVENT_BUS_PUBLISH_PORT=4442 + - SE_EVENT_BUS_SUBSCRIBE_PORT=4443 + ports: + - "6901:5900" + volumes: + - .:/mnt + + firefox: + image: docker.io/selenium/node-firefox:4.19.1-20240402 + shm_size: 2gb + depends_on: + - selenium-hub + environment: + - SE_EVENT_BUS_HOST=selenium-hub + - SE_EVENT_BUS_PUBLISH_PORT=4442 + - SE_EVENT_BUS_SUBSCRIBE_PORT=4443 + ports: + - "6902:5900" + volumes: + - .:/mnt + + selenium-hub: + image: docker.io/selenium/hub:4.19.1-20240402 + ports: + - "4442:4442" + - "4443:4443" + - "4444:4444" diff --git a/tests/test_zundler.py b/tests/test_zundler.py index af3c55f..cd5f203 100644 --- a/tests/test_zundler.py +++ b/tests/test_zundler.py @@ -8,17 +8,17 @@ from selenium.webdriver.common.keys import Keys TEST_DIR = Path(__file__).resolve().parent -def test_copy_button(selenium): - path = TEST_DIR / "copy-button//_build//zundler//index.html" - selenium.get(path.as_uri()) +def test_copy_button(selenium_service): + path = Path("//mnt//copy-button//_build//zundler//index.html") + selenium_service.get(path.as_uri()) time.sleep(1) - assert "copy-button documentation" in selenium.title + assert "copy-button documentation" in selenium_service.title - selenium.switch_to.frame("zundler-iframe") + selenium_service.switch_to.frame("zundler-iframe") - button = selenium.find_element(By.CSS_SELECTOR, "button.copybtn") + button = selenium_service.find_element(By.CSS_SELECTOR, "button.copybtn") assert button.get_attribute("data-tooltip") == "Copy" assert "success" not in button.get_attribute("class")