-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3458 from mpacer/selenium_utils
Selenium utils + markdown rendering tests
- Loading branch information
Showing
5 changed files
with
355 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
"""Utilities for driving Selenium interactively to develop tests. | ||
These are not used in the tests themselves - rather, the developer writing tests | ||
can use them to experiment with Selenium. | ||
""" | ||
from selenium.webdriver import Firefox | ||
|
||
from notebook.tests.selenium.utils import Notebook | ||
from notebook.notebookapp import list_running_servers | ||
|
||
class NoServerError(Exception): | ||
|
||
def __init__(self, message): | ||
self.message = message | ||
|
||
def quick_driver(lab=False): | ||
"""Quickly create a selenium driver pointing at an active noteboook server. | ||
Usage example: | ||
from notebook.tests.selenium.quick_selenium import quick_driver | ||
driver = quick_driver | ||
Note: you need to manually close the driver that opens with driver.quit() | ||
""" | ||
try: | ||
server = list(list_running_servers())[0] | ||
except IndexError as e: | ||
raise NoServerError('You need a server running before you can run ' | ||
'this command') | ||
driver = Firefox() | ||
auth_url = '{url}?token={token}'.format(**server) | ||
driver.get(auth_url) | ||
|
||
# If this redirects us to a lab page and we don't want that; | ||
# then we need to redirect ourselves to the classic notebook view | ||
if driver.current_url.endswith('/lab') and not lab: | ||
driver.get(driver.current_url.rstrip('lab')+'tree') | ||
return driver | ||
|
||
|
||
def quick_notebook(): | ||
"""Quickly create a new classic notebook in a selenium driver | ||
Usage example: | ||
from notebook.tests.selenium.quick_selenium import quick_notebook | ||
nb = quick_notebook() | ||
Note: you need to manually close the driver that opens with nb.browser.quit() | ||
""" | ||
return Notebook.new_notebook(quick_driver()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import os | ||
|
||
import pytest | ||
from selenium.webdriver.common.keys import Keys | ||
|
||
from .utils import wait_for_selector, Notebook | ||
|
||
pjoin = os.path.join | ||
|
||
|
||
@pytest.fixture | ||
def notebook(authenticated_browser): | ||
return Notebook.new_notebook(authenticated_browser) | ||
|
||
|
||
def get_rendered_contents(nb): | ||
cl = ["text_cell", "render"] | ||
rendered_cells = [cell.find_element_by_class_name("text_cell_render") | ||
for cell in nb.cells | ||
if all([c in cell.get_attribute("class") for c in cl])] | ||
return [x.get_attribute('innerHTML').strip() | ||
for x in rendered_cells | ||
if x is not None] | ||
|
||
|
||
def test_markdown_cell(notebook): | ||
nb = notebook | ||
cell_text = ["# Foo", | ||
'**Bar**', | ||
'*Baz*', | ||
'```\nx = 1\n```', | ||
'```aaaa\nx = 1\n```', | ||
] | ||
expected_contents = ['<h1 id="Foo">Foo<a class="anchor-link" href="#Foo">¶</a></h1>', | ||
'<p><strong>Bar</strong></p>', | ||
'<p><em>Baz</em></p>', | ||
'<pre><code>x = 1\n</code></pre>', | ||
'<pre><code class="cm-s-ipython language-aaaa">x = 1\n</code></pre>' | ||
] | ||
nb.append(*cell_text, cell_type="markdown") | ||
nb.run_all() | ||
rendered_contents = get_rendered_contents(nb) | ||
assert rendered_contents == expected_contents |
Oops, something went wrong.