Skip to content

Commit 3992647

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 3992647

File tree

1 file changed

+45
-13
lines changed

1 file changed

+45
-13
lines changed

tests/test_widgets.py

Lines changed: 45 additions & 13 deletions
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,49 @@ 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+
time.sleep(0.05)
106+
if nb_cell.text != "":
107+
nb_cells_non_empty.append(nb_cell)
108+
109+
super().__init__(nb_cells_non_empty)
110+
111+
def __getitem__(self, key):
112+
# have to retrieve from scratch as positions may have changed
113+
ActionChains(self._driver).send_keys(Keys.HOME).perform()
114+
time.sleep(0.1)
115+
for i in range(key):
116+
self._driver.execute_script(
117+
"arguments[0].scrollIntoView();", super().__getitem__(i)
118+
)
119+
time.sleep(0.05)
120+
121+
nb_cell = super().__getitem__(key)
122+
self._driver.execute_script("arguments[0].scrollIntoView();", nb_cell)
123+
time.sleep(0.05)
124+
return nb_cell
93125

94126

95127
#########
@@ -245,7 +277,7 @@ def test_widget_answer(self, selenium_driver):
245277

246278
driver = selenium_driver("tests/notebooks/widget_answers.ipynb")
247279

248-
nb_cells = get_nb_cells(driver)
280+
nb_cells = NotebookCellList(driver)
249281

250282
# Test 1:
251283
# -------
@@ -552,7 +584,7 @@ def test_widget_figure(selenium_driver, nb_filename, mpl_backend):
552584
# TODO for inline i need to get the image directly from the panel
553585
driver = selenium_driver(nb_filename)
554586

555-
nb_cells = get_nb_cells(driver)
587+
nb_cells = NotebookCellList(driver)
556588

557589
if "inline" == mpl_backend:
558590
by_type = By.TAG_NAME
@@ -676,7 +708,7 @@ def test_widgets_cue(selenium_driver):
676708
"""
677709
driver = selenium_driver("tests/notebooks/widgets_cue.ipynb")
678710

679-
nb_cells = get_nb_cells(driver)
711+
nb_cells = NotebookCellList(driver)
680712
# Test 1:
681713
# -------
682714
# Check if CueBox shows cue when changed
@@ -893,7 +925,7 @@ def test_widget_check_registry(selenium_driver):
893925
"""
894926
driver = selenium_driver("tests/notebooks/widget_check_registry.ipynb")
895927

896-
nb_cells = get_nb_cells(driver)
928+
nb_cells = NotebookCellList(driver)
897929

898930
# Test 1:
899931
# -------
@@ -1021,7 +1053,7 @@ def test_widgets_code(selenium_driver):
10211053
"""
10221054
driver = selenium_driver("tests/notebooks/widget_code_exercise.ipynb")
10231055

1024-
nb_cells = get_nb_cells(driver)
1056+
nb_cells = NotebookCellList(driver)
10251057
# Test 1:
10261058
# -------
10271059
WebDriverWait(driver, 5).until(

0 commit comments

Comments
 (0)