Skip to content

Commit 8e8100b

Browse files
committed
Setup infra / Implement basic frontend test
1 parent a2c6cea commit 8e8100b

File tree

5 files changed

+79
-0
lines changed

5 files changed

+79
-0
lines changed

compose/local/django/Dockerfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ RUN apt-get update && apt-get install --no-install-recommends -y \
5252
&& wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | apt-key add - \
5353
&& apt-get update \
5454
&& apt-get install -y postgresql-15 postgresql-client-15 \
55+
&& apt-get install -y chromium chromium-driver \
5556
# cleaning up unused files
5657
&& apt-get purge -y --auto-remove -o APT::AutoRemove::RecommendsImportant=false \
5758
&& rm -rf /var/lib/apt/lists/*

requirements/local.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ pytest==8.0.0 # https://github.com/pytest-dev/pytest
1313
pytest-sugar==1.0.0 # https://github.com/Frozenball/pytest-sugar
1414
types-requests # maybe instead, we should add `mypy --install-types` to the dockerfile?
1515
types-xmltodict
16+
pytest-xdist>=3.3.1
17+
pytest-cov>=4.1.0
18+
selenium>=4.15.2 # Selenium (Frontend Testing)
1619

1720
# Documentation
1821
# ------------------------------------------------------------------------------

sde_collections/tests/frontend/__init__.py

Whitespace-only changes.
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import shutil
2+
import subprocess
3+
4+
import pytest
5+
from django.contrib.staticfiles.testing import StaticLiveServerTestCase
6+
from selenium import webdriver
7+
from selenium.webdriver.chrome.options import Options
8+
from selenium.webdriver.chrome.service import Service
9+
from selenium.webdriver.support.ui import WebDriverWait
10+
11+
12+
class BaseTestCase(StaticLiveServerTestCase):
13+
"""Base class for all frontend tests using Selenium."""
14+
15+
@classmethod
16+
def setUpClass(cls):
17+
super().setUpClass()
18+
19+
# Verify ChromeDriver is available
20+
chromedriver_path = shutil.which("chromedriver")
21+
if not chromedriver_path:
22+
pytest.fail("ChromeDriver not found. Please ensure chromium-driver is installed.")
23+
24+
# # Set up Chrome options
25+
chrome_options = Options()
26+
chrome_options.add_argument("--headless")
27+
chrome_options.add_argument("--no-sandbox")
28+
chrome_options.add_argument("--disable-dev-shm-usage")
29+
chrome_options.binary_location = "/usr/bin/chromium"
30+
31+
try:
32+
# Create service with explicit path
33+
service = Service(executable_path=chromedriver_path, log_path="/tmp/chromedriver.log")
34+
35+
# Initialize WebDriver with service and options
36+
cls.driver = webdriver.Chrome(service=service, options=chrome_options)
37+
38+
cls.driver.set_window_size(1920, 1080)
39+
cls.driver.implicitly_wait(10)
40+
cls.wait = WebDriverWait(cls.driver, 10)
41+
42+
except Exception as e:
43+
# Print debugging information
44+
subprocess.run(["which", "chromium"])
45+
subprocess.run(["which", "chromedriver"])
46+
subprocess.run(["chromium", "--version"])
47+
subprocess.run(["chromedriver", "--version"])
48+
pytest.fail(f"Failed to initialize ChromeDriver: {str(e)}")
49+
50+
@classmethod
51+
def tearDownClass(cls):
52+
if hasattr(cls, "driver"):
53+
cls.driver.quit()
54+
super().tearDownClass()
55+
56+
def setUp(self):
57+
"""Set up test case."""
58+
super().setUp()
59+
# Add any additional setup here
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
from .base import BaseTestCase
2+
3+
4+
class TestSetup(BaseTestCase):
5+
"""Verify Selenium setup is working correctly."""
6+
7+
def test_basic_page_load(self):
8+
"""Test that we can load a page."""
9+
# Print the live server URL
10+
print(f"\nTest server running at: {self.live_server_url}")
11+
12+
self.driver.get(self.live_server_url)
13+
print(f"Current URL: {self.driver.current_url}")
14+
print(f"Page Title: {self.driver.title}")
15+
16+
assert self.driver.title == "Sign In | COSMOS"

0 commit comments

Comments
 (0)