|
1 | | -""" |
2 | | -Test Multiple Choice question directive |
3 | | -""" |
| 1 | +# **************************************************** |
| 2 | +# |docname| - Test Multiple Choice question directive |
| 3 | +# **************************************************** |
4 | 4 |
|
5 | 5 | __author__ = "yasinovskyy" |
6 | 6 |
|
7 | 7 | from unittest import TestCase |
8 | | -from selenium.webdriver.support import expected_conditions as EC |
9 | 8 | from selenium.webdriver.common.by import By |
10 | 9 | from runestone.unittest_base import module_fixture_maker, RunestoneTestCase |
11 | 10 |
|
12 | 11 | mf, setUpModule, tearDownModule = module_fixture_maker(__file__, True) |
13 | 12 |
|
| 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 | + |
14 | 35 | # Look for errors producted by invalid questions. |
15 | 36 | class MultipleChoiceQuestion_Error_Tests(TestCase): |
16 | 37 | def test_1(self): |
@@ -132,10 +153,9 @@ def test_mc1(self): |
132 | 153 | t1 = self.driver.find_element_by_id("question2") |
133 | 154 | btn_check = t1.find_element_by_tag_name("button") |
134 | 155 | 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 | + ) |
139 | 159 |
|
140 | 160 | def test_mc2(self): |
141 | 161 | """Multiple Choice: Correct answer selected""" |
|
0 commit comments