|
9 | 9 | import requests
|
10 | 10 | from imageio.v3 import imread
|
11 | 11 | from packaging.version import Version
|
| 12 | +from selenium.webdriver.common.action_chains import ActionChains |
12 | 13 | from selenium.webdriver.common.by import By
|
13 | 14 | from selenium.webdriver.common.keys import Keys
|
14 | 15 | from selenium.webdriver.remote.webelement import WebElement
|
@@ -77,8 +78,48 @@ def scwidget_reset_cue_button_class_name(cue_type: str, cued: bool):
|
77 | 78 | return class_name
|
78 | 79 | return class_name.replace("reset-cue-button", f"{cue_type}-reset-cue-button")
|
79 | 80 |
|
| 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 |
80 | 121 |
|
81 |
| -def get_nb_cells(driver) -> List[WebElement]: |
| 122 | +def NotebookCellList(driver) -> List[WebElement]: |
82 | 123 | """
|
83 | 124 | Filters out empty cells
|
84 | 125 |
|
@@ -247,7 +288,7 @@ def test_widget_answer(self, selenium_driver):
|
247 | 288 |
|
248 | 289 | driver = selenium_driver("tests/notebooks/widget_answers.ipynb")
|
249 | 290 |
|
250 |
| - nb_cells = get_nb_cells(driver) |
| 291 | + nb_cells = NotebookCellList(driver) |
251 | 292 |
|
252 | 293 | # Test 1:
|
253 | 294 | # -------
|
@@ -554,7 +595,7 @@ def test_widget_figure(selenium_driver, nb_filename, mpl_backend):
|
554 | 595 | # TODO for inline i need to get the image directly from the panel
|
555 | 596 | driver = selenium_driver(nb_filename)
|
556 | 597 |
|
557 |
| - nb_cells = get_nb_cells(driver) |
| 598 | + nb_cells = NotebookCellList(driver) |
558 | 599 |
|
559 | 600 | if "inline" == mpl_backend:
|
560 | 601 | by_type = By.TAG_NAME
|
@@ -678,7 +719,7 @@ def test_widgets_cue(selenium_driver):
|
678 | 719 | """
|
679 | 720 | driver = selenium_driver("tests/notebooks/widgets_cue.ipynb")
|
680 | 721 |
|
681 |
| - nb_cells = get_nb_cells(driver) |
| 722 | + nb_cells = NotebookCellList(driver) |
682 | 723 | # Test 1:
|
683 | 724 | # -------
|
684 | 725 | # Check if CueBox shows cue when changed
|
@@ -895,7 +936,7 @@ def test_widget_check_registry(selenium_driver):
|
895 | 936 | """
|
896 | 937 | driver = selenium_driver("tests/notebooks/widget_check_registry.ipynb")
|
897 | 938 |
|
898 |
| - nb_cells = get_nb_cells(driver) |
| 939 | + nb_cells = NotebookCellList(driver) |
899 | 940 |
|
900 | 941 | # Test 1:
|
901 | 942 | # -------
|
@@ -1023,7 +1064,7 @@ def test_widgets_code(selenium_driver):
|
1023 | 1064 | """
|
1024 | 1065 | driver = selenium_driver("tests/notebooks/widget_code_exercise.ipynb")
|
1025 | 1066 |
|
1026 |
| - nb_cells = get_nb_cells(driver) |
| 1067 | + nb_cells = NotebookCellList(driver) |
1027 | 1068 | # Test 1:
|
1028 | 1069 | # -------
|
1029 | 1070 | WebDriverWait(driver, 5).until(
|
|
0 commit comments