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

Commit a47df25

Browse files
committed
Refactor: Move element_has_css_class to unittest_base.
1 parent 5428e08 commit a47df25

2 files changed

Lines changed: 35 additions & 22 deletions

File tree

runestone/mchoice/test/test_assess.py

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,32 +7,15 @@
77
from unittest import TestCase
88
from selenium.webdriver.common.by import By
99
from selenium.webdriver.support import expected_conditions as EC
10-
from runestone.unittest_base import module_fixture_maker, RunestoneTestCase
10+
from runestone.unittest_base import (
11+
module_fixture_maker,
12+
RunestoneTestCase,
13+
element_has_css_class,
14+
)
1115

1216
mf, setUpModule, tearDownModule = module_fixture_maker(__file__, True)
1317

1418

15-
class element_has_css_class:
16-
"""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.
17-
18-
locator - used to find the element
19-
20-
returns the WebElement once it has the particular css class.
21-
"""
22-
23-
def __init__(self, locator, css_class):
24-
self.locator = locator
25-
self.css_class = css_class
26-
27-
def __call__(self, driver):
28-
# Find the referenced element.
29-
element = driver.find_element(*self.locator)
30-
if self.css_class in element.get_attribute("class"):
31-
return element
32-
else:
33-
return False
34-
35-
3619
# Look for errors producted by invalid questions.
3720
class MultipleChoiceQuestion_Error_Tests(TestCase):
3821
def test_1(self):

runestone/unittest_base.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@
3535
# -------------
3636
# None
3737

38+
# Globals
39+
# =======
3840
# Select an unused port for serving web pages to the test suite.
3941
PORT = "8081"
4042
# Use the localhost for testing.
@@ -50,6 +52,8 @@
5052
mf = None
5153

5254

55+
# Code
56+
# ====
5357
# Define `module fixtures <https://docs.python.org/2/library/unittest.html#setupmodule-and-teardownmodule>`_ to build the test Runestone project, run the server, then shut it down when the tests complete.
5458
class ModuleFixture(unittest.TestCase):
5559
def __init__(
@@ -224,3 +228,29 @@ def tearDown(self):
224228
self.driver.execute_script("window.localStorage.clear();")
225229
self.driver.execute_script("window.sessionStorage.clear();")
226230
self.driver.delete_all_cookies()
231+
232+
233+
# An expectation for Selenium, used 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.
234+
#
235+
# locator - used to find the element
236+
#
237+
# returns the WebElement once it has the particular css class.
238+
class element_has_css_class:
239+
def __init__(
240+
self,
241+
# The element to find; this is passed directly to `driver.find_element <https://selenium-python.readthedocs.io/api.html#selenium.webdriver.remote.webdriver.WebDriver.find_element>`_. See the `Selenium docs`_.
242+
locator,
243+
# The CSS class to look for.
244+
css_class,
245+
):
246+
247+
self.locator = locator
248+
self.css_class = css_class
249+
250+
def __call__(self, driver):
251+
# Find the referenced element.
252+
element = driver.find_element(*self.locator)
253+
if self.css_class in element.get_attribute("class"):
254+
return element
255+
else:
256+
return False

0 commit comments

Comments
 (0)