diff --git a/tests/conftest.py b/tests/conftest.py index 82feb9b..59331e8 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -4,6 +4,7 @@ from urllib.parse import urljoin import pytest +from packaging.version import Version from selenium.common.exceptions import StaleElementReferenceException from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions @@ -17,7 +18,7 @@ JUPYTER_VERSION = None -def get_jupyter_version() -> str: +def get_jupyter_version() -> Version: """ Function so we can update the jupyter version during initialization and use it in other files @@ -45,7 +46,7 @@ def notebook_service(): ["jupyter", f"{JUPYTER_TYPE}", "--version"] ) # convert to string - JUPYTER_VERSION = jupyter_version.decode().replace("\n", "") + JUPYTER_VERSION = Version(jupyter_version.decode().replace("\n", "")) jupyter_process = subprocess.Popen( [ @@ -106,7 +107,7 @@ def _selenium_driver(nb_path): # jupyter lab < 4 if JUPYTER_TYPE == "lab": - if get_jupyter_version() < "4.0.0": + if get_jupyter_version() < Version("4.0.0"): restart_kernel_button_class_name = ( "bp3-button.bp3-minimal.jp-ToolbarButtonComponent.minimal.jp-Button" ) @@ -116,7 +117,7 @@ def _selenium_driver(nb_path): else: raise ValueError("jupyter lab > 4.0.0 is not supported.") elif JUPYTER_TYPE == "notebook": - if get_jupyter_version() < "7.0.0": + if get_jupyter_version() < Version("7.0.0"): restart_kernel_button_class_name = "btn.btn-default" restart_kernel_button_title_attribute = ( "restart the kernel, then re-run the whole notebook (with dialog)" @@ -169,7 +170,7 @@ def _selenium_driver(nb_path): # ------------------------------- if JUPYTER_TYPE == "lab": - if get_jupyter_version() < "4.0.0": + if get_jupyter_version() < Version("4.0.0"): restart_button_class_name = ( "jp-Dialog-button.jp-mod-accept.jp-mod-warn.jp-mod-styled" ) @@ -177,7 +178,7 @@ def _selenium_driver(nb_path): else: raise ValueError("jupyter lab > 4.0.0 is not supported.") elif JUPYTER_TYPE == "notebook": - if get_jupyter_version() < "7.0.0": + if get_jupyter_version() < Version("7.0.0"): restart_button_class_name = "btn.btn-default.btn-sm.btn-danger" restart_button_text = "Restart and Run All Cells" else: diff --git a/tests/test_widgets.py b/tests/test_widgets.py index 5131345..8dc702d 100644 --- a/tests/test_widgets.py +++ b/tests/test_widgets.py @@ -8,6 +8,7 @@ import pytest import requests from imageio.v3 import imread +from packaging.version import Version from selenium.webdriver.common.by import By from selenium.webdriver.common.keys import Keys from selenium.webdriver.remote.webelement import WebElement @@ -41,45 +42,6 @@ def crop_const_color_borders(image: np.ndarray, const_color: int = 255): return image[i1:i2, j1:j2, :] -if JUPYTER_TYPE == "notebook": - BUTTON_CLASS_NAME = "lm-Widget.jupyter-widgets.jupyter-button.widget-button" - OUTPUT_CLASS_NAME = "lm-Widget.jp-RenderedText.jp-mod-trusted.jp-OutputArea-output" - TEXT_INPUT_CLASS_NAME = "widget-input" - CODE_MIRROR_CLASS_NAME = "CodeMirror-code" - MATPLOTLIB_CANVAS_CLASS_NAME = "jupyter-widgets.jupyter-matplotlib-canvas-container" - CUE_BOX_CLASS_NAME = ( - "lm-Widget.lm-Panel.jupyter-widgets.widget-container" - ".widget-box.widget-vbox.scwidget-cue-box" - ) -elif JUPYTER_TYPE == "lab": - BUTTON_CLASS_NAME = ( - "lm-Widget.p-Widget.jupyter-widgets.jupyter-button.widget-button" - ) - OUTPUT_CLASS_NAME = ( - "lm-Widget.p-Widget.jp-RenderedText.jp-mod-trusted.jp-OutputArea-output" - ) - TEXT_INPUT_CLASS_NAME = "widget-input" - CODE_MIRROR_CLASS_NAME = "CodeMirror-code" - - MATPLOTLIB_CANVAS_CLASS_NAME = "jupyter-widgets.jupyter-matplotlib-canvas-container" - CUE_BOX_CLASS_NAME = ( - "lm-Widget.p-Widget.lm-Panel.p-Panel.jupyter-widgets." - "widget-container.widget-box.widget-vbox.scwidget-cue-box" - ) -else: - raise ValueError( - f"Tests do not support jupyter type {JUPYTER_TYPE!r}. Please use 'notebook' or" - " 'lab'." - ) - -CUED_CUE_BOX_CLASS_NAME = f"{CUE_BOX_CLASS_NAME}.scwidget-cue-box--cue" - -RESET_CUE_BUTTON_CLASS_NAME = f"{BUTTON_CLASS_NAME}.scwidget-reset-cue-button" -CUED_RESET_CUE_BUTTON_CLASS_NAME = ( - f"{RESET_CUE_BUTTON_CLASS_NAME}.scwidget-reset-cue-button--cue" -) - - def cue_box_class_name(cue_type: str, cued: bool): class_name = CUED_CUE_BOX_CLASS_NAME if cued else CUE_BOX_CLASS_NAME if cue_type is None: @@ -156,6 +118,62 @@ def test_notebook_running(notebook_service): assert response.status_code == 200 +def test_setup_globals(): + from .conftest import JUPYTER_VERSION + + # black formats this into one line which causes an error in the linter. + # fmt: off + global BUTTON_CLASS_NAME, OUTPUT_CLASS_NAME, TEXT_INPUT_CLASS_NAME, \ + CODE_MIRROR_CLASS_NAME, MATPLOTLIB_CANVAS_CLASS_NAME, CUE_BOX_CLASS_NAME + global CUED_CUE_BOX_CLASS_NAME, RESET_CUE_BUTTON_CLASS_NAME, \ + CUED_RESET_CUE_BUTTON_CLASS_NAME + # fmt: on + + if JUPYTER_TYPE == "notebook" and JUPYTER_VERSION >= Version("7.0.0"): + BUTTON_CLASS_NAME = "lm-Widget.jupyter-widgets.jupyter-button.widget-button" + OUTPUT_CLASS_NAME = ( + "lm-Widget.jp-RenderedText.jp-mod-trusted.jp-OutputArea-output" + ) + TEXT_INPUT_CLASS_NAME = "widget-input" + CODE_MIRROR_CLASS_NAME = "cm-content" + MATPLOTLIB_CANVAS_CLASS_NAME = ( + "jupyter-widgets.jupyter-matplotlib-canvas-container" + ) + CUE_BOX_CLASS_NAME = ( + "lm-Widget.lm-Panel.jupyter-widgets.widget-container" + ".widget-box.widget-vbox.scwidget-cue-box" + ) + elif JUPYTER_TYPE == "lab" and JUPYTER_VERSION < Version("4.0.0"): + BUTTON_CLASS_NAME = ( + "lm-Widget.p-Widget.jupyter-widgets.jupyter-button.widget-button" + ) + OUTPUT_CLASS_NAME = ( + "lm-Widget.p-Widget.jp-RenderedText.jp-mod-trusted.jp-OutputArea-output" + ) + TEXT_INPUT_CLASS_NAME = "widget-input" + CODE_MIRROR_CLASS_NAME = "cm-content" + + MATPLOTLIB_CANVAS_CLASS_NAME = ( + "jupyter-widgets.jupyter-matplotlib-canvas-container" + ) + CUE_BOX_CLASS_NAME = ( + "lm-Widget.p-Widget.lm-Panel.p-Panel.jupyter-widgets." + "widget-container.widget-box.widget-vbox.scwidget-cue-box" + ) + else: + raise ValueError( + f"Tests do not support jupyter type {JUPYTER_TYPE!r} for version" + f"{JUPYTER_VERSION!r}." + ) + + CUED_CUE_BOX_CLASS_NAME = f"{CUE_BOX_CLASS_NAME}.scwidget-cue-box--cue" + + RESET_CUE_BUTTON_CLASS_NAME = f"{BUTTON_CLASS_NAME}.scwidget-reset-cue-button" + CUED_RESET_CUE_BUTTON_CLASS_NAME = ( + f"{RESET_CUE_BUTTON_CLASS_NAME}.scwidget-reset-cue-button--cue" + ) + + def test_privacy_policy(selenium_driver): """ The first time jupyter lab is started on a fresh installation a privacy popup