Example #1
0
def test_cancel_restart_or_shutdown(notebook):
    """Click each of the restart options, then cancel the confirmation dialog"""
    browser = notebook.browser
    kernel_menu = browser.find_element_by_id('kernellink')

    for menu_item in restart_selectors + [shutdown_selector]:
        kernel_menu.click()
        wait_for_selector(browser, menu_item, visible=True, single=True).click()
        wait_for_selector(browser, cancel_selector, visible=True, single=True).click()
        WebDriverWait(browser, 3).until(
            EC.invisibility_of_element((By.CSS_SELECTOR, '.modal-backdrop'))
        )
        assert notebook.is_kernel_running()
def test_save_notebook_as(notebook):
    # Set a name for comparison later
    set_notebook_name(notebook, name="nb1.ipynb")
    wait_for_rename(notebook.browser, "nb1")
    assert get_notebook_name(notebook) == "nb1.ipynb"
    # Wait for Save As modal, save
    save_as(notebook)
    wait_for_selector(notebook.browser, '.save-message')
    inp = notebook.browser.find_element_by_xpath('//input[@data-testid="save-as"]')
    inp.send_keys('new_notebook.ipynb')
    inp.send_keys(Keys.RETURN)
    wait_for_rename(notebook.browser, "new_notebook")
    # Test that the name changed
    assert get_notebook_name(notebook) == "new_notebook.ipynb"
    # Test that address bar was updated (TODO: get the base url)
    assert "new_notebook.ipynb" in notebook.browser.current_url
def test_save_readonly_notebook_as(notebook):
    # Make notebook read-only
    notebook.edit_cell(
        index=0,
        content='import os\nimport stat\nos.chmod("' +
        notebook.browser.current_url.split('?')[0].split('/')[-1] +
        '", stat.S_IREAD)\nprint(0)')
    notebook.browser.execute_script("Jupyter.notebook.get_cell(0).execute();")
    notebook.wait_for_cell_output(0)
    refresh_notebook(notebook)
    # Test that the notebook is read-only
    assert notebook.browser.execute_script(
        'return Jupyter.notebook.writable') == False

    # Add some content
    test_content_0 = "print('a simple')\nprint('test script')"
    notebook.edit_cell(index=0, content=test_content_0)

    # Wait for Save As modal, save
    save_as(notebook)
    wait_for_selector(notebook.browser, '.save-message')
    inp = notebook.browser.find_element_by_xpath(
        '//input[@data-testid="save-as"]')
    inp.send_keys('writable_notebook.ipynb')
    inp.send_keys(Keys.RETURN)
    wait_for_rename(notebook.browser, "writable_notebook")
    # Test that the name changed
    assert get_notebook_name(notebook) == "writable_notebook.ipynb"
    # Test that address bar was updated (TODO: get the base url)
    assert "writable_notebook.ipynb" in notebook.browser.current_url
    # Test that it is no longer read-only
    assert notebook.browser.execute_script(
        'return Jupyter.notebook.writable') == True

    # Add some more content
    test_content_1 = "print('a second simple')\nprint('script to test save feature')"
    notebook.add_and_execute_cell(content=test_content_1)
    # and save the notebook
    execute_promise("Jupyter.notebook.save_notebook()", notebook.browser)

    # Test that it still contains the content
    assert notebook.get_cell_contents(index=0) == test_content_0
    assert notebook.get_cell_contents(index=1) == test_content_1
    # even after a refresh
    refresh_notebook(notebook)
    assert notebook.get_cell_contents(index=0) == test_content_0
    assert notebook.get_cell_contents(index=1) == test_content_1
def test_save_notebook_as(notebook):
    # Set a name for comparison later
    set_notebook_name(notebook, name="nb1.ipynb")
    wait_for_rename(notebook.browser, "nb1")
    assert get_notebook_name(notebook) == "nb1.ipynb"
    # Wait for Save As modal, save
    save_as(notebook)
    wait_for_selector(notebook.browser, '.save-message')
    inp = notebook.browser.find_element_by_xpath(
        '//input[@data-testid="save-as"]')
    inp.send_keys('new_notebook.ipynb')
    inp.send_keys(Keys.RETURN)
    wait_for_rename(notebook.browser, "new_notebook")
    # Test that the name changed
    assert get_notebook_name(notebook) == "new_notebook.ipynb"
    # Test that address bar was updated (TODO: get the base url)
    assert "new_notebook.ipynb" in notebook.browser.current_url
def get_list_items(browser):
    """Gets list items from a directory listing page
    
    Raises PageError if not in directory listing page (url has tree in it)
    """
    if not url_in_tree(browser):
        raise PageError(
            "You are not in the notebook's file tree view."
            "This function can only be used the file tree context.")
    # we need to make sure that at least one item link loads
    wait_for_selector(browser, '.item_link')

    return [{
        'link': a.get_attribute('href'),
        'label': a.find_element_by_class_name('item_name').text,
        'element': a,
    } for a in browser.find_elements_by_class_name('item_link')]
Example #6
0
def test_menu_items(notebook):
    browser = notebook.browser
    kernel_menu = browser.find_element_by_id('kernellink')

    for menu_item in restart_selectors:
        # Shutdown
        kernel_menu.click()
        wait_for_selector(browser, shutdown_selector, visible=True, single=True).click()

        # Confirm shutdown
        wait_for_selector(browser, confirm_selector, visible=True, single=True).click()

        WebDriverWait(browser, 3).until(
            lambda b: not notebook.is_kernel_running(),
            message="Kernel did not shut down as expected"
        )

        # Restart
        # Selenium can't click the menu while a modal dialog is fading out
        WebDriverWait(browser, 3).until(
            EC.invisibility_of_element((By.CSS_SELECTOR, '.modal-backdrop'))
        )
        kernel_menu.click()

        wait_for_selector(browser, menu_item, visible=True, single=True).click()
        WebDriverWait(browser, 10).until(
            lambda b: notebook.is_kernel_running(),
            message="Restart (%r) after shutdown did not start kernel" % menu_item
        )
def test_items(authenticated_browser):
    visited_dict = {}
    # Going down the tree to collect links
    while True:
        wait_for_selector(authenticated_browser, '.item_link')
        current_url = authenticated_browser.current_url
        items = visited_dict[current_url] = only_dir_links(
            authenticated_browser)
        try:
            item = items[0]
            item["element"].click()
            assert authenticated_browser.current_url == item['link']
        except IndexError:
            break
    # Going back up the tree while we still have unvisited links
    while visited_dict:
        current_items = only_dir_links(authenticated_browser)
        current_items_links = [item["link"] for item in current_items]
        stored_items = visited_dict.pop(authenticated_browser.current_url)
        stored_items_links = [item["link"] for item in stored_items]
        assert stored_items_links == current_items_links
        authenticated_browser.back()
def click_menu_item(notebook, menu_id, item_id):
    notebook.browser.find_element_by_id(menu_id).find_element_by_xpath(
        '..').click()
    wait_for_selector(notebook.browser, '#{}'.format(item_id), visible=True)
    notebook.browser.find_element_by_id(item_id).click()
Example #9
0
def test_execute_from_menu(prefill_notebook):
    notebook = prefill_notebook(['print("hello world")'])

    wait_for_selector(notebook.browser, '#editlink', single=True).click()
    wait_for_selector(notebook.browser, '#copy_cell', single=True).click()
    wait_for_selector(notebook.browser, '#editlink', single=True).click()
    wait_for_selector(notebook.browser, '#paste_cell_below',
                      single=True).click()

    wait_for_selector(notebook.browser, '#celllink', single=True).click()
    wait_for_selector(notebook.browser, '#run_all_cells', single=True).click()

    outputs = notebook.wait_for_cell_output(1)
    assert outputs[0].text == 'hello world'
Example #10
0
 def wait_for_debug_button():
     wait_for_selector(nb.browser, debug_button_selector, single=True)