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

Commit 54c3d11

Browse files
committed
Fix: Add wait for CSS class.
1 parent 969ac58 commit 54c3d11

1 file changed

Lines changed: 28 additions & 8 deletions

File tree

runestone/mchoice/test/test_assess.py

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,37 @@
1-
"""
2-
Test Multiple Choice question directive
3-
"""
1+
# ****************************************************
2+
# |docname| - Test Multiple Choice question directive
3+
# ****************************************************
44

55
__author__ = "yasinovskyy"
66

77
from unittest import TestCase
8-
from selenium.webdriver.support import expected_conditions as EC
98
from selenium.webdriver.common.by import By
109
from runestone.unittest_base import module_fixture_maker, RunestoneTestCase
1110

1211
mf, setUpModule, tearDownModule = module_fixture_maker(__file__, True)
1312

13+
14+
class element_has_css_class:
15+
"""An expectation for checking that an element has a particular css class. From the `Selenium docs <https://selenium-python.readthedocs.io/waits.html#explicit-waits>`_, under the "Custom wait conditions" subheading.
16+
17+
locator - used to find the element
18+
19+
returns the WebElement once it has the particular css class.
20+
"""
21+
22+
def __init__(self, locator, css_class):
23+
self.locator = locator
24+
self.css_class = css_class
25+
26+
def __call__(self, driver):
27+
# Find the referenced element.
28+
element = driver.find_element(*self.locator)
29+
if self.css_class in element.get_attribute("class"):
30+
return element
31+
else:
32+
return False
33+
34+
1435
# Look for errors producted by invalid questions.
1536
class MultipleChoiceQuestion_Error_Tests(TestCase):
1637
def test_1(self):
@@ -132,10 +153,9 @@ def test_mc1(self):
132153
t1 = self.driver.find_element_by_id("question2")
133154
btn_check = t1.find_element_by_tag_name("button")
134155
btn_check.click()
135-
fb = t1.find_element_by_id("question2_feedback")
136-
self.assertIsNotNone(fb)
137-
cnamestr = fb.get_attribute("class")
138-
self.assertIn("alert-danger", cnamestr)
156+
self.wait.until(
157+
element_has_css_class((By.ID, "question2_feedback"), "alert-danger")
158+
)
139159

140160
def test_mc2(self):
141161
"""Multiple Choice: Correct answer selected"""

0 commit comments

Comments
 (0)