Skip to content
This repository was archived by the owner on Jun 7, 2023. It is now read-only.

Commit 95f4c0d

Browse files
committed
Fix: Update the short answer component for better testability.
1 parent 50a9025 commit 95f4c0d

3 files changed

Lines changed: 64 additions & 63 deletions

File tree

runestone/shortanswer/js/shortanswer.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@ export default class ShortAnswer extends RunestoneBase {
3535
this.mathjax = true;
3636
}
3737
this.renderHTML();
38-
this.checkServer("shortanswer");
3938
this.caption = "shortanswer";
4039
this.addCaption("runestone");
40+
this.checkServer("shortanswer", true);
4141
}
4242
}
4343

runestone/shortanswer/test/_sources/index.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@ Testing: Short Answer Questions
1717
Short Answer
1818
------------
1919

20-
.. shortanswer:: question1
20+
.. shortanswer:: test_short_answer_1
2121

2222
What are the colors in the rainbow?
2323

2424

25-
.. shortanswer:: question2
25+
.. shortanswer:: test_short_answer_2
2626
:optional:
2727
:mathjax:
2828

runestone/shortanswer/test/test_shortanswer.py

Lines changed: 61 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -2,65 +2,66 @@
22
Test Short Answer question directive
33
"""
44

5+
import pytest
6+
57
__author__ = "yasinovskyy"
68

7-
from time import sleep
8-
from runestone.unittest_base import module_fixture_maker, RunestoneTestCase
9-
10-
setUpModule, tearDownModule = module_fixture_maker(__file__)
11-
12-
13-
class ShortAnswerQuestion_Tests(RunestoneTestCase):
14-
def test_sa1(self):
15-
"""No input. Button not clicked"""
16-
self.driver.get(self.host + "/index.html")
17-
t1 = self.driver.find_element_by_id("question1")
18-
19-
fb = t1.find_element_by_id("question1_feedback")
20-
self.assertIsNotNone(fb)
21-
cnamestr = fb.get_attribute("class")
22-
self.assertIn("alert-danger", cnamestr)
23-
24-
def test_sa2(self):
25-
"""No input. Button clicked"""
26-
self.driver.get(self.host + "/index.html")
27-
sleep(2)
28-
t1 = self.driver.find_element_by_id("question1")
29-
30-
btn_check = t1.find_element_by_tag_name("button")
31-
btn_check.click()
32-
33-
fb = t1.find_element_by_id("question1_feedback")
34-
self.assertIsNotNone(fb)
35-
cnamestr = fb.get_attribute("class")
36-
self.assertIn("alert-success", cnamestr)
37-
38-
def test_sa3(self):
39-
"""Answer entered"""
40-
self.driver.get(self.host + "/index.html")
41-
t1 = self.driver.find_element_by_id("question1")
42-
ta = t1.find_element_by_id("question1_solution")
43-
ta.send_keys("My answer")
44-
45-
btn_check = t1.find_element_by_tag_name("button")
46-
btn_check.click()
47-
48-
fb = t1.find_element_by_id("question1_feedback")
49-
self.assertIsNotNone(fb)
50-
cnamestr = fb.get_attribute("class")
51-
self.assertIn("alert-success", cnamestr)
52-
53-
def test_sa4(self):
54-
"""Answer entered and cleared"""
55-
self.driver.get(self.host + "/index.html")
56-
t1 = self.driver.find_element_by_id("question1")
57-
ta = t1.find_element_by_id("question1_solution")
58-
ta.send_keys("My answer")
59-
60-
btn_check = t1.find_element_by_tag_name("button")
61-
btn_check.click()
62-
63-
fb = t1.find_element_by_id("question1_feedback")
64-
self.assertIsNotNone(fb)
65-
cnamestr = fb.get_attribute("class")
66-
self.assertIn("alert-success", cnamestr)
9+
DIV_ID = "test_short_answer_1"
10+
11+
12+
@pytest.fixture
13+
def selenium_utils_1(selenium_utils):
14+
selenium_utils.get("index.html")
15+
selenium_utils.wait_until_ready(DIV_ID)
16+
return selenium_utils
17+
18+
19+
def test_sa1(selenium_utils_1):
20+
"""No input. Button not clicked"""
21+
self = selenium_utils_1
22+
t1 = self.driver.find_element_by_id(DIV_ID)
23+
24+
fb = t1.find_element_by_id(f"{DIV_ID}_feedback")
25+
assert "alert-danger" in fb.get_attribute("class")
26+
27+
28+
def test_sa2(selenium_utils_1):
29+
"""No input. Button clicked"""
30+
t1 = selenium_utils_1.driver.find_element_by_id(DIV_ID)
31+
32+
btn_check = t1.find_element_by_tag_name("button")
33+
btn_check.click()
34+
35+
fb = t1.find_element_by_id(f"{DIV_ID}_feedback")
36+
assert "alert-success" in fb.get_attribute("class")
37+
38+
39+
def test_sa3(selenium_utils_1):
40+
"""Answer entered"""
41+
t1 = selenium_utils_1.driver.find_element_by_id(DIV_ID)
42+
ta = t1.find_element_by_id(f"{DIV_ID}_solution")
43+
ta.clear()
44+
ta.send_keys("My answer")
45+
46+
btn_check = t1.find_element_by_tag_name("button")
47+
btn_check.click()
48+
49+
fb = t1.find_element_by_id(f"{DIV_ID}_feedback")
50+
assert fb is not None
51+
assert "alert-success" in fb.get_attribute("class")
52+
53+
54+
# TODO: this is the same as ``_test_sa3``.
55+
def test_sa4(selenium_utils_1):
56+
"""Answer entered and cleared"""
57+
t1 = selenium_utils_1.driver.find_element_by_id(DIV_ID)
58+
ta = t1.find_element_by_id(f"{DIV_ID}_solution")
59+
ta.clear()
60+
ta.send_keys("My answer")
61+
62+
btn_check = t1.find_element_by_tag_name("button")
63+
btn_check.click()
64+
65+
fb = t1.find_element_by_id(f"{DIV_ID}_feedback")
66+
assert fb is not None
67+
assert "alert-success" in fb.get_attribute("class")

0 commit comments

Comments
 (0)