Use pytest-docker for selenium

This commit is contained in:
Adrian Vollmer 2024-04-22 00:07:10 +02:00
parent 2a8cbc75c8
commit 66aa150326
4 changed files with 95 additions and 6 deletions

View File

@ -60,6 +60,7 @@ tests = [
'pytest>=8', 'pytest>=8',
'selenium', 'selenium',
'pytest-selenium', 'pytest-selenium',
'pytest-docker',
'black', 'black',
'nox', 'nox',
] ]

View File

@ -18,6 +18,44 @@ def chrome_driver():
_driver.quit() _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) @pytest.fixture(autouse=True)
def run_make_commands(request): def run_make_commands(request):
# Get the directory of the current test file # Get the directory of the current test file

50
tests/docker-compose.yml Normal file
View File

@ -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"

View File

@ -8,17 +8,17 @@ from selenium.webdriver.common.keys import Keys
TEST_DIR = Path(__file__).resolve().parent TEST_DIR = Path(__file__).resolve().parent
def test_copy_button(selenium): def test_copy_button(selenium_service):
path = TEST_DIR / "copy-button//_build//zundler//index.html" path = Path("//mnt//copy-button//_build//zundler//index.html")
selenium.get(path.as_uri()) selenium_service.get(path.as_uri())
time.sleep(1) 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 button.get_attribute("data-tooltip") == "Copy"
assert "success" not in button.get_attribute("class") assert "success" not in button.get_attribute("class")