Skip to content

Commit 9f618c6

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 99e9d38 commit 9f618c6

File tree

1 file changed

+47
-6
lines changed

1 file changed

+47
-6
lines changed

tests/test_widgets.py

+47-6
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
@@ -77,8 +78,48 @@ def scwidget_reset_cue_button_class_name(cue_type: str, cued: bool):
7778
return class_name
7879
return class_name.replace("reset-cue-button", f"{cue_type}-reset-cue-button")
7980

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

81-
def get_nb_cells(driver) -> List[WebElement]:
122+
def NotebookCellList(driver) -> List[WebElement]:
82123
"""
83124
Filters out empty cells
84125
@@ -247,7 +288,7 @@ def test_widget_answer(self, selenium_driver):
247288

248289
driver = selenium_driver("tests/notebooks/widget_answers.ipynb")
249290

250-
nb_cells = get_nb_cells(driver)
291+
nb_cells = NotebookCellList(driver)
251292

252293
# Test 1:
253294
# -------
@@ -554,7 +595,7 @@ def test_widget_figure(selenium_driver, nb_filename, mpl_backend):
554595
# TODO for inline i need to get the image directly from the panel
555596
driver = selenium_driver(nb_filename)
556597

557-
nb_cells = get_nb_cells(driver)
598+
nb_cells = NotebookCellList(driver)
558599

559600
if "inline" == mpl_backend:
560601
by_type = By.TAG_NAME
@@ -678,7 +719,7 @@ def test_widgets_cue(selenium_driver):
678719
"""
679720
driver = selenium_driver("tests/notebooks/widgets_cue.ipynb")
680721

681-
nb_cells = get_nb_cells(driver)
722+
nb_cells = NotebookCellList(driver)
682723
# Test 1:
683724
# -------
684725
# Check if CueBox shows cue when changed
@@ -895,7 +936,7 @@ def test_widget_check_registry(selenium_driver):
895936
"""
896937
driver = selenium_driver("tests/notebooks/widget_check_registry.ipynb")
897938

898-
nb_cells = get_nb_cells(driver)
939+
nb_cells = NotebookCellList(driver)
899940

900941
# Test 1:
901942
# -------
@@ -1023,7 +1064,7 @@ def test_widgets_code(selenium_driver):
10231064
"""
10241065
driver = selenium_driver("tests/notebooks/widget_code_exercise.ipynb")
10251066

1026-
nb_cells = get_nb_cells(driver)
1067+
nb_cells = NotebookCellList(driver)
10271068
# Test 1:
10281069
# -------
10291070
WebDriverWait(driver, 5).until(

0 commit comments

Comments
 (0)