-
Notifications
You must be signed in to change notification settings - Fork 6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add framework for UI testing via bs4 #62
Closed
Closed
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
88c852d
Add framework for UI testing via bs4
JasonGrace2282 ead01ec
Add a test for the submit button on viewing assignments
JasonGrace2282 5ca62b6
Fix failing test
JasonGrace2282 d37c687
Add final test for assignment
JasonGrace2282 c1c6bcc
clean up has_button check
JasonGrace2282 f4749ee
make case-sensitive option kw-only
JasonGrace2282 5f45ba3
Improve code quality
JasonGrace2282 e2b6e6b
Relock after rebasing with main
JasonGrace2282 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,3 +10,4 @@ Tests | |
~tests.utils | ||
~tests.assertions | ||
~tests.fixtures | ||
~tests.ui_testing |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
from __future__ import annotations | ||
|
||
import pytest | ||
from django.urls import reverse | ||
|
||
from tin.tests import Html, login | ||
|
||
from .. import views | ||
|
||
|
||
@login("student") | ||
@pytest.mark.parametrize( | ||
("course_perm", "is_archived", "is_visible"), | ||
( | ||
("-", False, True), | ||
("r", False, True), | ||
("w", False, True), | ||
("-", True, False), | ||
("r", True, False), | ||
("w", True, True), | ||
), | ||
) | ||
def test_can_submit_assignment( | ||
client, | ||
course, | ||
assignment, | ||
course_perm: str, | ||
is_archived: bool, | ||
is_visible: bool, | ||
): | ||
course.archived = is_archived | ||
course.permission = course_perm | ||
course.save() | ||
response = client.get( | ||
reverse("assignments:show", args=[assignment.id]), | ||
) | ||
html = Html.from_response(response) | ||
assert html.has_button("Submit") is is_visible | ||
|
||
|
||
@pytest.mark.parametrize( | ||
("user", "is_visible"), | ||
( | ||
("student", False), | ||
("teacher", True), | ||
), | ||
) | ||
def test_can_create_assignment(rf, course, student, teacher, user, is_visible): | ||
user_map = { | ||
"student": student, | ||
"teacher": teacher, | ||
} | ||
request = rf.get( | ||
reverse("assignments:add", args=[course.id]), | ||
) | ||
request.user = user_map[user] | ||
response = views.create_view(request, course.id) | ||
html = Html.from_response(response) | ||
assert html.has_button("Create") is is_visible | ||
|
||
|
||
@login("student") | ||
def test_can_submit_assignment_from_form(client, assignment): | ||
response = client.get( | ||
reverse("assignments:submit", args=[assignment.id]), | ||
) | ||
html = Html.from_response(response) | ||
# no grader has been added yet | ||
assert not html.has_button("Submit") | ||
assignment.save_grader_file("print('Instanced Rendering OP')") | ||
|
||
response = client.get( | ||
reverse("assignments:submit", args=[assignment.id]), | ||
) | ||
html = Html.from_response(response) | ||
assert html.has_button("Submit") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
from __future__ import annotations | ||
|
||
from .assertions import * | ||
from .ui_testing import * | ||
from .utils import * |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
from __future__ import annotations | ||
|
||
import pytest | ||
|
||
from .ui_testing import Html | ||
|
||
|
||
def test_html_has_button_no_href(): | ||
raw_html = """ | ||
<button>Hi!</button> | ||
<input type="submit" value="Blame the compiler"> | ||
""" | ||
html = Html(raw_html) | ||
# with no arguments it should raise a ValueError | ||
with pytest.raises(ValueError, match="At least one of text or href must be provided"): | ||
html.has_button() | ||
assert html.has_button("Hi!") | ||
# has button is case insensitive | ||
assert html.has_button("hi!") | ||
assert html.has_button("Blame the compiler") | ||
|
||
raw_html = """ | ||
<buttn></buttn> | ||
<input type="list" value="Blame the compiler"> | ||
""" | ||
html = Html(raw_html) | ||
assert not html.has_button("Blame the compiler") | ||
|
||
|
||
def test_html_has_button_with_href(): | ||
raw_html = """ | ||
<button><a href="https://example.com">Hi!</a></button> | ||
<input type="submit" value="Blame the compiler"> | ||
<a href='https://google.com'><button>Google</button></a> | ||
<a class="bob tin-btn" href="https://example.com">Bob</a> | ||
""" | ||
html = Html(raw_html) | ||
assert html.has_button(href="https://example.com") | ||
assert not html.has_button(href="https://example.org") | ||
assert html.has_button("Google", href="https://google.com") | ||
# should also search for <a class="tin-btn" | ||
assert html.has_button("Bob", href="https://example.com") | ||
raw_html = """ | ||
<button>Hi!</button> | ||
<input type="submit" value="Blame the compiler"> | ||
""" | ||
html = Html(raw_html) | ||
assert not html.has_button(href="https://example.com") | ||
|
||
|
||
def test_has_text(): | ||
raw_html = '<p class="insideTag">This is a really long sentence</p>' | ||
html = Html(raw_html) | ||
|
||
# it should be case insensitive by default | ||
assert html.has_text("REALLY long sentence") | ||
|
||
assert not html.has_text("REALLY long sentence", case_sensitive=True) | ||
assert html.has_text("really long sentence", case_sensitive=True) | ||
|
||
assert not html.has_text("insideTag") |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Due to
pytest
's bad naming, this is a bit confusing, but theerror
means to transform all warnings into exceptions. I'm not sure if this is wanted (or needed) but it should help during migrations between e.g. Django 4.2 - Django 5.2