|
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
|
@@ -78,18 +79,46 @@ def scwidget_reset_cue_button_class_name(cue_type: str, cued: bool):
|
78 | 79 | return class_name.replace("reset-cue-button", f"{cue_type}-reset-cue-button")
|
79 | 80 |
|
80 | 81 |
|
81 |
| -def get_nb_cells(driver) -> List[WebElement]: |
| 82 | +class NotebookCellList(list): |
82 | 83 | """
|
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. |
84 | 89 |
|
85 | 90 | :param driver: see conftest.py selenium_driver function
|
86 | 91 | """
|
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 |
93 | 122 |
|
94 | 123 |
|
95 | 124 | #########
|
@@ -245,7 +274,7 @@ def test_widget_answer(self, selenium_driver):
|
245 | 274 |
|
246 | 275 | driver = selenium_driver("tests/notebooks/widget_answers.ipynb")
|
247 | 276 |
|
248 |
| - nb_cells = get_nb_cells(driver) |
| 277 | + nb_cells = NotebookCellList(driver) |
249 | 278 |
|
250 | 279 | # Test 1:
|
251 | 280 | # -------
|
@@ -552,7 +581,7 @@ def test_widget_figure(selenium_driver, nb_filename, mpl_backend):
|
552 | 581 | # TODO for inline i need to get the image directly from the panel
|
553 | 582 | driver = selenium_driver(nb_filename)
|
554 | 583 |
|
555 |
| - nb_cells = get_nb_cells(driver) |
| 584 | + nb_cells = NotebookCellList(driver) |
556 | 585 |
|
557 | 586 | if "inline" == mpl_backend:
|
558 | 587 | by_type = By.TAG_NAME
|
@@ -676,7 +705,7 @@ def test_widgets_cue(selenium_driver):
|
676 | 705 | """
|
677 | 706 | driver = selenium_driver("tests/notebooks/widgets_cue.ipynb")
|
678 | 707 |
|
679 |
| - nb_cells = get_nb_cells(driver) |
| 708 | + nb_cells = NotebookCellList(driver) |
680 | 709 | # Test 1:
|
681 | 710 | # -------
|
682 | 711 | # Check if CueBox shows cue when changed
|
@@ -893,7 +922,7 @@ def test_widget_check_registry(selenium_driver):
|
893 | 922 | """
|
894 | 923 | driver = selenium_driver("tests/notebooks/widget_check_registry.ipynb")
|
895 | 924 |
|
896 |
| - nb_cells = get_nb_cells(driver) |
| 925 | + nb_cells = NotebookCellList(driver) |
897 | 926 |
|
898 | 927 | # Test 1:
|
899 | 928 | # -------
|
@@ -1021,7 +1050,7 @@ def test_widgets_code(selenium_driver):
|
1021 | 1050 | """
|
1022 | 1051 | driver = selenium_driver("tests/notebooks/widget_code_exercise.ipynb")
|
1023 | 1052 |
|
1024 |
| - nb_cells = get_nb_cells(driver) |
| 1053 | + nb_cells = NotebookCellList(driver) |
1025 | 1054 | # Test 1:
|
1026 | 1055 | # -------
|
1027 | 1056 | WebDriverWait(driver, 5).until(
|
|
0 commit comments