Skip to content

Commit d87da7c

Browse files
committed
Tests: Add NotebookCell class putting cell into view on access (Fix #50)
In lab 4 jupyter cells load their content only when in view, so we implement a scrolling behavior into the class NotebookCell that jumps to the beginning of the ipynb and then scrolls down the list to the target.
1 parent 66e74cb commit d87da7c

File tree

1 file changed

+42
-13
lines changed

1 file changed

+42
-13
lines changed

tests/test_widgets.py

+42-13
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import requests
1010
from imageio.v3 import imread
1111
from packaging.version import Version
12+
from selenium.webdriver.common.action_chains import ActionChains
1213
from selenium.webdriver.common.by import By
1314
from selenium.webdriver.common.keys import Keys
1415
from selenium.webdriver.remote.webelement import WebElement
@@ -78,18 +79,46 @@ def scwidget_reset_cue_button_class_name(cue_type: str, cued: bool):
7879
return class_name.replace("reset-cue-button", f"{cue_type}-reset-cue-button")
7980

8081

81-
def get_nb_cells(driver) -> List[WebElement]:
82+
class NotebookCellList(list):
8283
"""
83-
Filters out empty cells
84+
List of notebook cells that scrolls them into the view when accessing it. When a
85+
cell is accessed it always goes to the top to scroll down cell by cell. We can only
86+
scroll to an element if it is partially visible, so this method works as long as a
87+
cell is not larger than the view. We need to put the cells into the view because the
88+
content of the cells in lab 4 is not loaded otherwise.
8489
8590
:param driver: see conftest.py selenium_driver function
8691
"""
87-
# Each cell of the notebook, the cell number can be retrieved from the
88-
# attribute "data-windowed-list-index"
89-
nb_cells = driver.find_elements(
90-
By.CLASS_NAME, "lm-Widget.jp-Cell.jp-CodeCell.jp-Notebook-cell"
91-
)
92-
return [nb_cell for nb_cell in nb_cells if nb_cell.text != ""]
92+
93+
def __init__(self, driver):
94+
self._driver = driver
95+
96+
nb_cells = driver.find_elements(
97+
By.CLASS_NAME, "lm-Widget.jp-Cell.jp-CodeCell.jp-Notebook-cell"
98+
)
99+
# we scroll through the notebook and remove the cells that are empty
100+
ActionChains(driver).send_keys(Keys.HOME).perform()
101+
time.sleep(0.1)
102+
nb_cells_non_empty = []
103+
for nb_cell in nb_cells:
104+
driver.execute_script("arguments[0].scrollIntoView();", nb_cell)
105+
if nb_cell.text != "":
106+
nb_cells_non_empty.append(nb_cell)
107+
108+
super().__init__(nb_cells_non_empty)
109+
110+
def __getitem__(self, key):
111+
# have to retrieve from scratch as positions may have changed
112+
ActionChains(self._driver).send_keys(Keys.HOME).perform()
113+
time.sleep(0.1)
114+
for i in range(key):
115+
self._driver.execute_script(
116+
"arguments[0].scrollIntoView();", super().__getitem__(i)
117+
)
118+
119+
nb_cell = super().__getitem__(key)
120+
self._driver.execute_script("arguments[0].scrollIntoView();", nb_cell)
121+
return nb_cell
93122

94123

95124
#########
@@ -245,7 +274,7 @@ def test_widget_answer(self, selenium_driver):
245274

246275
driver = selenium_driver("tests/notebooks/widget_answers.ipynb")
247276

248-
nb_cells = get_nb_cells(driver)
277+
nb_cells = NotebookCellList(driver)
249278

250279
# Test 1:
251280
# -------
@@ -552,7 +581,7 @@ def test_widget_figure(selenium_driver, nb_filename, mpl_backend):
552581
# TODO for inline i need to get the image directly from the panel
553582
driver = selenium_driver(nb_filename)
554583

555-
nb_cells = get_nb_cells(driver)
584+
nb_cells = NotebookCellList(driver)
556585

557586
if "inline" == mpl_backend:
558587
by_type = By.TAG_NAME
@@ -676,7 +705,7 @@ def test_widgets_cue(selenium_driver):
676705
"""
677706
driver = selenium_driver("tests/notebooks/widgets_cue.ipynb")
678707

679-
nb_cells = get_nb_cells(driver)
708+
nb_cells = NotebookCellList(driver)
680709
# Test 1:
681710
# -------
682711
# Check if CueBox shows cue when changed
@@ -893,7 +922,7 @@ def test_widget_check_registry(selenium_driver):
893922
"""
894923
driver = selenium_driver("tests/notebooks/widget_check_registry.ipynb")
895924

896-
nb_cells = get_nb_cells(driver)
925+
nb_cells = NotebookCellList(driver)
897926

898927
# Test 1:
899928
# -------
@@ -1021,7 +1050,7 @@ def test_widgets_code(selenium_driver):
10211050
"""
10221051
driver = selenium_driver("tests/notebooks/widget_code_exercise.ipynb")
10231052

1024-
nb_cells = get_nb_cells(driver)
1053+
nb_cells = NotebookCellList(driver)
10251054
# Test 1:
10261055
# -------
10271056
WebDriverWait(driver, 5).until(

0 commit comments

Comments
 (0)