Skip to content

Commit

Permalink
Created initial selenium testing framework with pylenium. Added test …
Browse files Browse the repository at this point in the history
…for user login (#2216)

* Created initial selenium testing framework with pylenium. Added test for user login

* Restoring precommit hook
  • Loading branch information
joshuastegmaier authored Dec 19, 2023
1 parent ca3e457 commit e921af0
Show file tree
Hide file tree
Showing 9 changed files with 2,120 additions and 450 deletions.
6 changes: 6 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,14 @@ jobs:
touch ./logs/concordia-celery.log
npm install
npx gulp build
chromepath=$(npx @puppeteer/browsers install chrome@stable)
chromepath=${chromepath#* }
chromepath=${chromepath%/chrome}
OLDPATH=$PATH
PATH=$PATH:$chromepath
pipenv run ./manage.py collectstatic --no-input
pipenv run coverage run ./manage.py test
PATH=$OLDPATH
env:
PGPASSWORD: postgres
# The hostname used to communicate with the PostgreSQL service container
Expand Down
1 change: 1 addition & 0 deletions Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ locust = "*"
twisted = {extras = ["http2", "tls"], version = "*"}
hiredis = "*"
pyasn1 = "*"
pyleniumio = "*"

[dev-packages]
invoke = "*"
Expand Down
1,196 changes: 782 additions & 414 deletions Pipfile.lock

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions concordia/settings_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -402,3 +402,5 @@
TINYMCE_JS_URL = "https://cdn.tiny.cloud/1/rf486i5f1ww9m8191oolczn7f0ry61mzdtfwbu7maiiiv2kv/tinymce/6/tinymce.min.js"

PYTESSERACT_ALLOWED_LANGUAGES = ["eng"]

PYLENIUM_CONFIG = os.path.join(SITE_ROOT_DIR, "pylenium.json")
4 changes: 2 additions & 2 deletions concordia/templates/registration/login.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<div class="col-8 col-md-6 col-lg-4 mx-auto my-3">
<h2 class="text-center">Welcome back!</h2>

<form method="post" action="{% url 'login' %}" class="col-10 my-3 mx-auto">
<form id="login-form" method="post" action="{% url 'login' %}" class="col-10 my-3 mx-auto">
{% csrf_token %}

{% if next %}
Expand All @@ -29,7 +29,7 @@ <h2 class="text-center">Welcome back!</h2>
security requirements</a>
</p>
{% buttons %}
<button type="submit" class="btn btn-primary">
<button id="login" type="submit" class="btn btn-primary">
Login
</button>
{% endbuttons %}
Expand Down
60 changes: 60 additions & 0 deletions concordia/tests/test_selenium.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import json
from logging import getLogger
from secrets import token_hex

from django.conf import settings
from django.contrib.staticfiles.testing import StaticLiveServerTestCase
from django.urls import reverse
from pylenium.config import PyleniumConfig
from pylenium.driver import Pylenium

from .utils import CreateTestUsers

logger = getLogger(__name__)


class SeleniumTests(CreateTestUsers, StaticLiveServerTestCase):
@classmethod
def setUpClass(cls):
super().setUpClass()
try:
with open(settings.PYLENIUM_CONFIG) as file:
_json = json.load(file)
config = PyleniumConfig(**_json)
except FileNotFoundError:
logger.warning(
"settings.PYLENIUM_CONFIG (%s) was not found; using defaults.",
settings.PYLENIUM_CONFIG,
)
config = PyleniumConfig()

cls.py = Pylenium(config)

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

def reverse(self, name):
return f"{self.live_server_url}{reverse(name)}"

def test_login(self):
self.py.visit(self.reverse("registration_login"))
self.py.get("[name='username']").type(token_hex(8))
self.py.get("[name='password']").type(token_hex(24))
self.py.get("button#login").click()
self.assertTrue(
self.py.should().have_url(f"{self.live_server_url}/account/login/")
)
self.assertTrue(
self.py.get("form#login-form")
.should()
.contain_text("Please enter a correct username and password")
)

user = self.create_user("login-test")
self.py.visit(self.reverse("registration_login"))
self.py.get("[name='username']").type(user.username)
self.py.get("[name='password']").type(user._password)
self.py.get("button#login").click()
self.assertTrue(self.py.should().have_url(f"{self.live_server_url}/"))
Loading

0 comments on commit e921af0

Please sign in to comment.