Skip to content

Commit 8a41551

Browse files
reppuli92reppuli92
authored andcommitted
Added new Playwright tests to replace the old Selenium tests
1 parent 470fc84 commit 8a41551

10 files changed

Lines changed: 490 additions & 1 deletion
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import re
2+
from playwright.sync_api import Page, expect
3+
from e2e_tests.helpers import navigate_to_default_course, login
4+
5+
def test_should_not_count_empty_submit(page: Page):
6+
page.goto("http://localhost:8000/?hl=en")
7+
login(page, "student", "student")
8+
navigate_to_default_course(page)
9+
page.get_by_role("link", name="Course materials").click()
10+
first_link = page.get_by_role("link", name="AJAX exercises: grading in browser JavaScript").first
11+
first_link.click()
12+
page.get_by_role("button", name="Submit").click()
13+
14+
submissions = page.get_by_role("button", name=re.compile("My submissions"))
15+
expect(submissions).to_contain_text("0 / 20")
16+
#empty submit should not count as submission
17+
18+
points = page.locator('#chapter-exercise-1').get_by_role("button", name=re.compile("Points"))
19+
expect(points).to_contain_text("0 / 10")
20+
21+
22+
def test_should_give_full_points_on_correct_answer(page: Page):
23+
page.goto("http://localhost:8000/?hl=en")
24+
login(page, "student", "student")
25+
navigate_to_default_course(page)
26+
page.get_by_role("link", name="Course materials").click()
27+
first_link = page.get_by_role("link", name="AJAX exercises: grading in browser JavaScript").first
28+
first_link.click()
29+
30+
page.get_by_role("textbox").fill("abc")
31+
page.get_by_role("button", name="Submit").click()
32+
33+
submissions = page.get_by_role("button", name=re.compile("My submissions"))
34+
expect(submissions).to_contain_text("1 / 20")
35+
36+
points = page.locator('#chapter-exercise-1').get_by_role("button", name=re.compile("Points"))
37+
expect(points).to_contain_text("10 / 10")

e2e_tests/test_compare_submissions.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@ def test_compare_submissions(page: Page) -> None:
3535
first_link.click()
3636
page.get_by_label("6.3.6 Wallet").get_by_role(
3737
"button", name="View all submissions").click()
38-
page.locator("#submission-2").get_by_role("link", name="Inspect").click()
38+
39+
latest_submission = page.locator('[id^="submission-"]').first
40+
latest_submission.get_by_role("link", name="Inspect").click()
3941

4042
def assert_line(filename: str, line_number: int, text: str, color: str):
4143
line = page.get_by_test_id(f"{filename}-line-{line_number}")
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import re
2+
from playwright.sync_api import Page, expect
3+
from e2e_tests.helpers import navigate_to_default_course, login
4+
5+
6+
def test_should_not_submit_without_files(page: Page):
7+
page.goto("http://localhost:8000/?hl=en")
8+
login(page, "student", "student")
9+
navigate_to_default_course(page)
10+
page.get_by_role("link", name="Course materials").click()
11+
first_link = page.get_by_role("link", name="Exercises with Python grader utils").first
12+
first_link.click()
13+
14+
exercise = page.locator('#chapter-exercise-1')
15+
16+
exercise.get_by_role("button", name="Submit").click()
17+
18+
submissions = exercise.get_by_role("button", name=re.compile("My submissions"))
19+
points = exercise.get_by_role("button", name=re.compile("Points"))
20+
21+
expect(submissions).to_contain_text("0 / 10")
22+
#clicking submit without files will not result in a submission
23+
expect(points).to_contain_text("0 / 10")
24+
25+
#TODO more tests ?

e2e_tests/test_homepage.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import re
2+
from playwright.sync_api import Page, expect
3+
from e2e_tests.helpers import navigate_to_default_course, login
4+
5+
6+
def test_homepage(page: Page):
7+
page.goto("http://localhost:8000/?hl=en")
8+
expect(page).to_have_title(re.compile("A+"))
9+
10+
login(page, "student", "student")
11+
navigate_to_default_course(page)
12+
expect(page.get_by_role("heading", name="A+ Manual")).to_be_visible()
13+
#TODO figure out more testing for the homepage

e2e_tests/test_login.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import re
2+
from playwright.sync_api import Page, expect
3+
from e2e_tests.helpers import login
4+
5+
6+
def test_login(page: Page):
7+
page.goto("http://localhost:8000/?hl=en")
8+
login(page, "student", "student")
9+
expect(page).to_have_title(re.compile("A+"))
10+
expect(page.get_by_label("Main")).to_contain_text("Stacy Student")
11+
12+
def test_false_login(page: Page):
13+
page.goto("http://localhost:8000/?hl=en")
14+
login(page, "fake", "fake")
15+
expect(page).to_have_title(re.compile("Log in to A+ | A+"))
16+
17+
error_message = "Please enter a correct username and password. Note that both fields may be case-sensitive."
18+
expect(
19+
page.locator("div.alert.alert-danger.alert-dismissible")
20+
).to_contain_text(error_message)

e2e_tests/test_main_navigation.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import re
2+
from playwright.sync_api import Page, expect
3+
from e2e_tests.helpers import navigate_to_default_course, login
4+
5+
6+
def test_main_navigation(page: Page):
7+
page.goto("http://localhost:8000/?hl=en")
8+
login(page, "teacher", "teacher")
9+
navigate_to_default_course(page)
10+
11+
page.get_by_role("link", name="Your points").click()
12+
expect(page).to_have_url(re.compile("/user/results/"))
13+
navigate_to_default_course(page)
14+
15+
page.get_by_role("link", name="All results").click()
16+
expect(page).to_have_url(re.compile("/teachers/results/"))
17+
18+
#TODO test rest of links? Also student view?
19+

e2e_tests/test_submissions.py

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import re
2+
from playwright.sync_api import Page, expect
3+
from e2e_tests.helpers import navigate_to_default_course, login
4+
#this is a test done to represent the 'my-first-exercise-test.py' in the old Selenium tests
5+
6+
7+
8+
def test_submission_count(page: Page):
9+
page.goto("http://localhost:8000/?hl=en")
10+
login(page, "student", "student")
11+
navigate_to_default_course(page)
12+
page.get_by_role("link", name="5.2 Question groups").click()
13+
14+
#fill in an answer 11 times for exercise 5.2.1
15+
exercise = page.locator("#chapter-exercise-1")
16+
17+
sub_count = 0
18+
19+
while (sub_count <= 10):
20+
21+
submissions = exercise.get_by_role("button", name=re.compile("My submissions"))
22+
expect(submissions).to_contain_text(f"{sub_count} / 10")
23+
24+
points = exercise.get_by_role("button", name=re.compile("Points"))
25+
26+
if (sub_count == 0):
27+
expect(points).to_contain_text("0 / 15")
28+
else:
29+
expect(points).to_contain_text("10 / 15")
30+
31+
exercise.get_by_role("radio", name="2").check() #question 1
32+
33+
q2 = exercise.get_by_label(re.compile(r"2.*7.*?"))
34+
q2.select_option("14")
35+
36+
q3 = exercise.locator(".question-area", has_text=re.compile(r"2.*x.*y"))
37+
q3.locator("input[type='checkbox'][value='option_0']").check()
38+
q3.locator("input[type='checkbox'][value='option_2']").check()
39+
40+
exercise.get_by_role("textbox").fill("15") #question 4
41+
42+
exercise.get_by_role("radio", name="8").check() #question 5
43+
44+
q5 = exercise.get_by_label(re.compile(r"4.*8.*?"))
45+
q5.select_option("28")
46+
47+
exercise.get_by_role("button", name="Submit").click()
48+
49+
identical_warning = page.get_by_role("dialog", name = "Identical submission")
50+
if identical_warning.is_visible():
51+
identical_warning.get_by_role("button", name="Submit").click()
52+
53+
sub_count += 1
54+
55+
#the 11th time should result in a failure
56+
57+
submissions = exercise.get_by_role("button", name=re.compile("My submissions"))
58+
expect(submissions).to_contain_text("10 / 10")
59+
60+
points = exercise.get_by_role("button", name=re.compile("Points"))
61+
expect(points).to_contain_text("10 / 15")

e2e_tests/test_teacher_list.py

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
from playwright.sync_api import Page, expect
2+
from e2e_tests.helpers import login, logout
3+
4+
5+
def test_teacher_list_access_rights(page: Page):
6+
7+
page.goto("http://localhost:8000/?hl=en")
8+
if page.get_by_test_id('user-menu').is_visible():
9+
logout(page)
10+
page.goto("http://localhost:8000/accounts/teachers")
11+
12+
alert = page.get_by_role("alert")
13+
message = "Unfortunately, you are not permitted to view this content"
14+
15+
expect(alert).to_be_visible()
16+
expect(alert).to_contain_text(message)
17+
18+
login(page, "student", "student")
19+
page.goto("http://localhost:8000/accounts/teachers")
20+
expect(alert).to_be_visible()
21+
expect(alert).to_contain_text(message)
22+
23+
logout(page)
24+
login(page, "teacher", "teacher")
25+
page.goto("http://localhost:8000/accounts/teachers")
26+
expect(alert).to_be_visible()
27+
expect(alert).to_contain_text(message)
28+
29+
logout(page)
30+
login(page, "admin", "admin")
31+
page.goto("http://localhost:8000/accounts/teachers")
32+
expect(alert).not_to_be_visible()
33+
expect(page).to_have_title("Teacher list | A+")
34+
35+
36+
def test_teacher_list_content(page: Page):
37+
38+
page.goto("http://localhost:8000/?hl=en")
39+
login(page, "admin", "admin")
40+
page.goto("http://localhost:8000/accounts/teachers")
41+
42+
table = page.get_by_role("table")
43+
expect(table).to_be_visible()
44+
45+
rows = table.locator("tbody tr")
46+
expect(rows).to_have_count(3)
47+
48+
expected_first_three_cols = [ #there are 5 columns in total, validate first 3
49+
[
50+
"Terry Teacher",
51+
"teacher@localhost.invalid",
52+
"aplus-manual Aplus Manual: Main",
53+
],
54+
[
55+
"Terry Teacher",
56+
"teacher@localhost.invalid",
57+
"test-course Test Course: Master",
58+
],
59+
[
60+
"Terry Teacher",
61+
"teacher@localhost.invalid",
62+
"DEF000 Def. Course: Current",
63+
],
64+
]
65+
66+
for i, expected_cells in enumerate(expected_first_three_cols):
67+
row_cells = rows.nth(i).locator("td")
68+
expect(row_cells).to_have_count(5)
69+
70+
for j, expected_text in enumerate(expected_cells):
71+
expect(row_cells.nth(j)).to_contain_text(expected_text)
72+
expect(row_cells.nth(j)).to_contain_text(expected_text)

0 commit comments

Comments
 (0)