Skip to content

Commit

Permalink
Merge pull request #144 from SADiLaR/test/multiple-browser-selenium
Browse files Browse the repository at this point in the history
test: run Selenium tests on more browsers/JS configurations
  • Loading branch information
Restioson authored Nov 7, 2024
2 parents c0fc10f + ce0bdf4 commit 4410af2
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 9 deletions.
45 changes: 44 additions & 1 deletion .github/workflows/testing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,57 @@ jobs:
cp .env.testing app/.env
cd app/
mkdir -p static_files
python manage.py test
python manage.py test --exclude-tag=selenium
env:
DJANGO_SETTINGS_MODULE: app.settings
DATABASE_URL: postgres://sadilar:sadilar@localhost:5432/test_db
- name: Manager Check
run: |
cd app/
python manage.py check
selenium:
strategy:
matrix:
browser-and-os:
- [firefox, ubuntu-latest]
- [chrome, ubuntu-latest]
js: [js-enabled, js-disabled]
runs-on: ${{ matrix.browser-and-os[1] }}
services:
postgres:
image: postgres:16
env:
POSTGRES_USER: sadilar
POSTGRES_PASSWORD: sadilar
POSTGRES_DB: test_db_1
ports:
- 5432:5432
options: --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5
steps:
- uses: actions/checkout@v4
with:
persist-credentials: false
- uses: actions/setup-python@v5
with:
python-version: 3.12
- name: Install Dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements-test.txt
sudo apt-get install -y gettext
- name: Create logging folder
run: |
sudo mkdir -p /logging
sudo chown runner:runner /logging
- name: Run Selenium Tests
run: |
cp .env.testing app/.env
cd app/
mkdir -p static_files
BROWSER=${{ matrix.browser-and-os[0] }} ENABLE_JS=${{ matrix.js }} python manage.py test --tag=selenium
env:
DJANGO_SETTINGS_MODULE: app.settings
DATABASE_URL: postgres://sadilar:sadilar@localhost:5432/test_db
lighthouse:
runs-on: ubuntu-latest # operating system your code will run on
services:
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ create-schema:
@docker compose run --rm web python manage.py graph_models -a -o schema/schema.png

test:
@docker compose run --rm web python manage.py test $(module)
@docker compose run --rm -e BROWSER -e JS_ENABLED web python manage.py test $(module)

ruff-check:
@docker compose run --rm web ruff check .
Expand Down
9 changes: 9 additions & 0 deletions app/general/tests/files/test_js.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<!DOCTYPE html>
<html lang="en">

<body>
<script>
document.querySelector("body").innerHTML = "<p id='js-enabled'>Hello world!</p>";
</script>
</body>
</html>
63 changes: 56 additions & 7 deletions app/general/tests/test_frontend.py
Original file line number Diff line number Diff line change
@@ -1,30 +1,79 @@
import os

from django.contrib.staticfiles.testing import StaticLiveServerTestCase
from django.test import tag
from selenium.common import TimeoutException
from selenium.webdriver.chrome.webdriver import Options, WebDriver
from selenium.webdriver.chrome.webdriver import (
Options as ChromeOptions,
)
from selenium.webdriver.chrome.webdriver import (
WebDriver as ChromeWebDriver,
)
from selenium.webdriver.common.by import By
from selenium.webdriver.firefox.webdriver import (
Options as FirefoxOptions,
)
from selenium.webdriver.firefox.webdriver import (
WebDriver as FirefoxWebDriver,
)
from selenium.webdriver.support.wait import WebDriverWait

# Wait timeout in seconds
WAIT_TIMEOUT = 5


@tag("selenium")
class TestFrontend(StaticLiveServerTestCase):
@classmethod
def setUpClass(cls):
opts = Options()
opts.add_argument("--headless")
opts.add_argument("--no-sandbox")
opts.add_argument("--disable-dev-shm-usage")
opts.add_argument("--window-size=1920,1080")
cls.driver = WebDriver(opts)
browser = os.environ.get("BROWSER", "chrome").lower()
cls.js_enabled = os.environ.get("JS_ENABLED", "js-enabled") == "js-enabled"

print(
f"Running Selenium tests on {browser}. JS is {'enabled' if cls.js_enabled else 'disabled'}."
)

if browser == "chrome":
opts = ChromeOptions()
opts.add_argument("--headless=new")
opts.add_argument("--no-sandbox")
opts.add_argument("--disable-dev-shm-usage")
opts.add_argument("--window-size=1920,1080")

if not cls.js_enabled:
# Taken from https://stackoverflow.com/a/73961818
prefs = dict()
prefs["webkit.webprefs.javascript_enabled"] = False
prefs["profile.content_settings.exceptions.javascript.*.setting"] = 2
prefs["profile.default_content_setting_values.javascript"] = 2
prefs["profile.managed_default_content_settings.javascript"] = 2
opts.add_experimental_option("prefs", prefs)
opts.add_argument("--disable-javascript")

cls.driver = ChromeWebDriver(opts)
elif browser == "firefox":
options = FirefoxOptions()
options.add_argument("-headless")
options.set_preference("javascript.enabled", cls.js_enabled)
cls.driver = FirefoxWebDriver(options=options)

cls.driver.implicitly_wait(WAIT_TIMEOUT)

super().setUpClass()

@classmethod
def tearDownClass(cls):
cls.driver.quit()
super().tearDownClass()

# Checks that JS is properly disabled if passed through env var, else check if enabled
def test_js_enabled_or_disabled(self):
test_dir = os.getenv("TESTING_DIR", "/app/general/tests/files")
self.driver.get(f"file://{test_dir}/test_js.html")
self.assertEqual(
len(self.driver.find_elements(By.ID, "js-enabled")), 1 if self.js_enabled else 0
)

def test_no_404s(self):
# Sanity check in case we ever change the 404 title
self.driver.get(f"{self.live_server_url}/blabla404")
Expand Down

0 comments on commit 4410af2

Please sign in to comment.