Skip to content

Commit

Permalink
Initial WIP playwright test skeleton.
Browse files Browse the repository at this point in the history
  • Loading branch information
ericsnekbytes committed Aug 9, 2022
1 parent 5943cc9 commit 455d5a8
Show file tree
Hide file tree
Showing 3 changed files with 222 additions and 189 deletions.
154 changes: 86 additions & 68 deletions nbclassic/tests/end_to_end/conftest.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
import json
import nbformat
from nbformat.v4 import new_notebook, new_code_cell
import os
import pytest
import requests
from subprocess import Popen
import json
import sys
from tempfile import mkstemp
from testpath.tempdir import TemporaryDirectory
import time
from os.path import join as pjoin
from subprocess import Popen
from tempfile import mkstemp
from urllib.parse import urljoin

from selenium.webdriver import Firefox, Remote, Chrome
from .utils import Notebook
import pytest
import requests
from testpath.tempdir import TemporaryDirectory
# from selenium.webdriver import Firefox, Remote, Chrome

pjoin = os.path.join
import nbformat
from nbformat.v4 import new_notebook, new_code_cell
from .utils import NotebookFrontend


def _wait_for_server(proc, info_file_path):
Expand Down Expand Up @@ -71,73 +71,91 @@ def notebook_server():
headers={'Authorization': 'token '+info['token']})


def make_sauce_driver():
"""This function helps travis create a driver on Sauce Labs.
This function will err if used without specifying the variables expected
in that context.
"""

username = os.environ["SAUCE_USERNAME"]
access_key = os.environ["SAUCE_ACCESS_KEY"]
capabilities = {
"tunnel-identifier": os.environ["TRAVIS_JOB_NUMBER"],
"build": os.environ["TRAVIS_BUILD_NUMBER"],
"tags": [os.environ['TRAVIS_PYTHON_VERSION'], 'CI'],
"platform": "Windows 10",
"browserName": os.environ['JUPYTER_TEST_BROWSER'],
"version": "latest",
}
if capabilities['browserName'] == 'firefox':
# Attempt to work around issue where browser loses authentication
capabilities['version'] = '57.0'
hub_url = f"{username}:{access_key}@localhost:4445"
print("Connecting remote driver on Sauce Labs")
driver = Remote(desired_capabilities=capabilities,
command_executor=f"http://{hub_url}/wd/hub")
return driver
# def make_sauce_driver():
# """This function helps travis create a driver on Sauce Labs.
#
# This function will err if used without specifying the variables expected
# in that context.
# """
#
# username = os.environ["SAUCE_USERNAME"]
# access_key = os.environ["SAUCE_ACCESS_KEY"]
# capabilities = {
# "tunnel-identifier": os.environ["TRAVIS_JOB_NUMBER"],
# "build": os.environ["TRAVIS_BUILD_NUMBER"],
# "tags": [os.environ['TRAVIS_PYTHON_VERSION'], 'CI'],
# "platform": "Windows 10",
# "browserName": os.environ['JUPYTER_TEST_BROWSER'],
# "version": "latest",
# }
# if capabilities['browserName'] == 'firefox':
# # Attempt to work around issue where browser loses authentication
# capabilities['version'] = '57.0'
# hub_url = f"{username}:{access_key}@localhost:4445"
# print("Connecting remote driver on Sauce Labs")
# driver = Remote(desired_capabilities=capabilities,
# command_executor=f"http://{hub_url}/wd/hub")
# return driver


@pytest.fixture(scope='session')
def selenium_driver():
if os.environ.get('SAUCE_USERNAME'):
driver = make_sauce_driver()
elif os.environ.get('JUPYTER_TEST_BROWSER') == 'chrome':
driver = Chrome()
def playwright_driver(playwright):
# TODO: Fix
# if os.environ.get('SAUCE_USERNAME'):
# driver = make_sauce_driver()
if os.environ.get('JUPYTER_TEST_BROWSER') == 'chrome':
driver = playwright.chromium.launch()
else:
driver = Firefox()
driver = playwright.firefox.launch()
driver = driver.new_context().new_page()

yield driver

# Teardown
driver.quit()
# # Teardown
# driver.quit()


# @pytest.fixture(scope='module')
# def authenticated_browser(selenium_driver, notebook_server):
# selenium_driver.jupyter_server_info = notebook_server
# selenium_driver.get("{url}?token={token}".format(**notebook_server))
# return selenium_driver
#
#
# @pytest.fixture
# def notebook(authenticated_browser):
# tree_wh = authenticated_browser.current_window_handle
# yield Notebook.new_notebook(authenticated_browser)
# authenticated_browser.switch_to.window(tree_wh)


@pytest.fixture(scope='module')
def authenticated_browser(selenium_driver, notebook_server):
selenium_driver.jupyter_server_info = notebook_server
selenium_driver.get("{url}?token={token}".format(**notebook_server))
return selenium_driver
def authenticated_browser(playwright_driver, notebook_server):
playwright_driver.jupyter_server_info = notebook_server
playwright_driver.goto("{url}?token={token}".format(**notebook_server))
return playwright_driver

@pytest.fixture
def notebook(authenticated_browser):
tree_wh = authenticated_browser.current_window_handle
yield Notebook.new_notebook(authenticated_browser)
authenticated_browser.switch_to.window(tree_wh)

@pytest.fixture
def prefill_notebook(selenium_driver, notebook_server):
def inner(cells):
cells = [new_code_cell(c) if isinstance(c, str) else c
for c in cells]
nb = new_notebook(cells=cells)
fd, path = mkstemp(dir=notebook_server['nbdir'], suffix='.ipynb')
with open(fd, 'w', encoding='utf-8') as f:
nbformat.write(nb, f)
fname = os.path.basename(path)
selenium_driver.get(
"{url}notebooks/{}?token={token}".format(fname, **notebook_server)
)
return Notebook(selenium_driver)

return inner
def notebook(authenticated_browser):
# tree_wh = authenticated_browser.current_window_handle
yield NotebookFrontend.new_notebook(authenticated_browser)
# authenticated_browser.switch_to.window(tree_wh)


# @pytest.fixture
# def prefill_notebook(selenium_driver, notebook_server):
# def inner(cells):
# cells = [new_code_cell(c) if isinstance(c, str) else c
# for c in cells]
# nb = new_notebook(cells=cells)
# fd, path = mkstemp(dir=notebook_server['nbdir'], suffix='.ipynb')
# with open(fd, 'w', encoding='utf-8') as f:
# nbformat.write(nb, f)
# fname = os.path.basename(path)
# selenium_driver.get(
# "{url}notebooks/{}?token={token}".format(fname, **notebook_server)
# )
# return Notebook(selenium_driver)
#
# return inner
103 changes: 51 additions & 52 deletions nbclassic/tests/end_to_end/test_playwright.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,64 +9,63 @@


def test_execute_code(notebook):
browser = notebook.browser
# browser = notebook.browser

def clear_outputs():
return notebook.browser.execute_script(
return notebook.evaluate(
"Jupyter.notebook.clear_all_output();")

# Execute cell with Javascript API
notebook.edit_cell(index=0, content='a=10; print(a)')
browser.execute_script("Jupyter.notebook.get_cell(0).execute();")
notebook.evaluate("Jupyter.notebook.get_cell(0).execute();")
outputs = notebook.wait_for_cell_output(0)
assert outputs[0].text == '10'

# Execute cell with Shift-Enter
notebook.edit_cell(index=0, content='a=11; print(a)')
clear_outputs()
shift(notebook.browser, Keys.ENTER)
outputs = notebook.wait_for_cell_output(0)
assert outputs[0].text == '11'
notebook.delete_cell(index=1)

# Execute cell with Ctrl-Enter
notebook.edit_cell(index=0, content='a=12; print(a)')
clear_outputs()
cmdtrl(notebook.browser, Keys.ENTER)
outputs = notebook.wait_for_cell_output(0)
assert outputs[0].text == '12'

# Execute cell with toolbar button
notebook.edit_cell(index=0, content='a=13; print(a)')
clear_outputs()
notebook.browser.find_element_by_css_selector(
"button[data-jupyter-action='jupyter-notebook:run-cell-and-select-next']").click()
outputs = notebook.wait_for_cell_output(0)
assert outputs[0].text == '13'

# Set up two cells to test stopping on error
notebook.edit_cell(index=0, content='raise IOError')
notebook.edit_cell(index=1, content='a=14; print(a)')

# Default behaviour: stop on error
clear_outputs()
browser.execute_script("""
var cell0 = Jupyter.notebook.get_cell(0);
var cell1 = Jupyter.notebook.get_cell(1);
cell0.execute();
cell1.execute();
""")
outputs = notebook.wait_for_cell_output(0)
assert notebook.get_cell_output(1) == []

# Execute a cell with stop_on_error=false
clear_outputs()
browser.execute_script("""
var cell0 = Jupyter.notebook.get_cell(0);
var cell1 = Jupyter.notebook.get_cell(1);
cell0.execute(false);
cell1.execute();
""")
outputs = notebook.wait_for_cell_output(1)
assert outputs[0].text == '14'

# # Execute cell with Shift-Enter
# notebook.edit_cell(index=0, content='a=11; print(a)')
# clear_outputs()
# shift(notebook.browser, Keys.ENTER)
# outputs = notebook.wait_for_cell_output(0)
# assert outputs[0].text == '11'
# notebook.delete_cell(index=1)
#
# # Execute cell with Ctrl-Enter
# notebook.edit_cell(index=0, content='a=12; print(a)')
# clear_outputs()
# cmdtrl(notebook.browser, Keys.ENTER)
# outputs = notebook.wait_for_cell_output(0)
# assert outputs[0].text == '12'
#
# # Execute cell with toolbar button
# notebook.edit_cell(index=0, content='a=13; print(a)')
# clear_outputs()
# notebook.browser.find_element_by_css_selector(
# "button[data-jupyter-action='jupyter-notebook:run-cell-and-select-next']").click()
# outputs = notebook.wait_for_cell_output(0)
# assert outputs[0].text == '13'
#
# # Set up two cells to test stopping on error
# notebook.edit_cell(index=0, content='raise IOError')
# notebook.edit_cell(index=1, content='a=14; print(a)')
#
# # Default behaviour: stop on error
# clear_outputs()
# browser.execute_script("""
# var cell0 = Jupyter.notebook.get_cell(0);
# var cell1 = Jupyter.notebook.get_cell(1);
# cell0.execute();
# cell1.execute();
# """)
# outputs = notebook.wait_for_cell_output(0)
# assert notebook.get_cell_output(1) == []
#
# # Execute a cell with stop_on_error=false
# clear_outputs()
# browser.execute_script("""
# var cell0 = Jupyter.notebook.get_cell(0);
# var cell1 = Jupyter.notebook.get_cell(1);
# cell0.execute(false);
# cell1.execute();
# """)
# outputs = notebook.wait_for_cell_output(1)
# assert outputs[0].text == '14'
Loading

0 comments on commit 455d5a8

Please sign in to comment.