Skip to content

Commit

Permalink
Tests: Add NotebookCell class putting cell into view on access (Fix #50)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
agoscinski committed Jul 3, 2024
1 parent 0919196 commit eee90dd
Showing 1 changed file with 47 additions and 6 deletions.
53 changes: 47 additions & 6 deletions tests/test_widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import requests
from imageio.v3 import imread
from packaging.version import Version
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.remote.webelement import WebElement
Expand Down Expand Up @@ -77,8 +78,48 @@ def scwidget_reset_cue_button_class_name(cue_type: str, cued: bool):
return class_name
return class_name.replace("reset-cue-button", f"{cue_type}-reset-cue-button")

class NotebookCellList(list):
"""
List of notebook cells that scrolls them into the view when accessing it. When a
cell is accessed it always goes to the top to scroll down cell by cell. We can only
scroll to an element if it is partially visible, so this method works as long as a
cell is not larger than the view. We need to put the cells into the view because the
content of the cells in lab 4 is not loaded otherwise.
:param driver: see conftest.py selenium_driver function
"""
def __init__(self, driver):
self._driver = driver

nb_cells = driver.find_elements(
By.CLASS_NAME, "lm-Widget.jp-Cell.jp-CodeCell.jp-Notebook-cell"
)
# we scroll through the notebook and remove the cells that are empty
ActionChains(driver).send_keys(Keys.HOME).perform()
nb_cells_non_empty = []
for nb_cell in nb_cells:
driver.execute_script("arguments[0].scrollIntoView();", nb_cell)
if nb_cell.text != "":
nb_cells_non_empty.append(nb_cell)

super.__init__(nb_cells_non_empty)


def __getitem__(self, key):
# have to retrieve from scratch as positions may have changed
time.sleep(0.1)
ActionChains(self._driver).send_keys(Keys.HOME).perform()
for i in range(key):
self._driver.execute_script(
"arguments[0].scrollIntoView();", super().__getitem__(i)
)

nb_cell = super().__getitem__(key)
self._driver.execute_script("arguments[0].scrollIntoView();", nb_cell)
time.sleep(0.1)
return nb_cell

def get_nb_cells(driver) -> List[WebElement]:
def NotebookCellList(driver) -> List[WebElement]:
"""
Filters out empty cells
Expand Down Expand Up @@ -247,7 +288,7 @@ def test_widget_answer(self, selenium_driver):

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

nb_cells = get_nb_cells(driver)
nb_cells = NotebookCellList(driver)

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

nb_cells = get_nb_cells(driver)
nb_cells = NotebookCellList(driver)

if "inline" == mpl_backend:
by_type = By.TAG_NAME
Expand Down Expand Up @@ -678,7 +719,7 @@ def test_widgets_cue(selenium_driver):
"""
driver = selenium_driver("tests/notebooks/widgets_cue.ipynb")

nb_cells = get_nb_cells(driver)
nb_cells = NotebookCellList(driver)
# Test 1:
# -------
# Check if CueBox shows cue when changed
Expand Down Expand Up @@ -895,7 +936,7 @@ def test_widget_check_registry(selenium_driver):
"""
driver = selenium_driver("tests/notebooks/widget_check_registry.ipynb")

nb_cells = get_nb_cells(driver)
nb_cells = NotebookCellList(driver)

# Test 1:
# -------
Expand Down Expand Up @@ -1023,7 +1064,7 @@ def test_widgets_code(selenium_driver):
"""
driver = selenium_driver("tests/notebooks/widget_code_exercise.ipynb")

nb_cells = get_nb_cells(driver)
nb_cells = NotebookCellList(driver)
# Test 1:
# -------
WebDriverWait(driver, 5).until(
Expand Down

0 comments on commit eee90dd

Please sign in to comment.