Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Selenium utils + markdown rendering tests #3458

Merged
merged 40 commits into from Mar 28, 2018
Merged

Conversation

@mpacer
Copy link
Member

@mpacer mpacer commented Mar 22, 2018

This PR introduces a Notebook class as a helper for running selenium functions. It still needs work to document it more completely, but… it works and makes it a lot easier to create the relevant tests inside notebooks.

I'm interested to hear people's reviews.

I think @takluyver @Carreau @minrk and @rgbkrk would be particularly interested in this.

@mpacer mpacer force-pushed the selenium_utils branch from 51c2d27 to 1889e56 Mar 22, 2018
rgbkrk
rgbkrk approved these changes Mar 22, 2018
Copy link
Member

@rgbkrk rgbkrk left a comment

Awesome

Loading

@rgbkrk
Copy link
Member

@rgbkrk rgbkrk commented Mar 22, 2018

I'm happy these at least make it easy for Python people to write tests for the JS. 😅

Loading

@mpacer mpacer force-pushed the selenium_utils branch from 1889e56 to 715d9e3 Mar 22, 2018
@mpacer mpacer force-pushed the selenium_utils branch from 715d9e3 to ebef7ba Mar 23, 2018
@mpacer
Copy link
Member Author

@mpacer mpacer commented Mar 23, 2018

This is now rebased on #3412 to take into account the issues that arise when you create a new notebook in the first place.

Loading

except IndexError as e:
e.message = 'You need a server running before you can run this command'
driver = Firefox()
auth_url = f'{server["url"]}?token={server["token"]}'
Copy link
Member

@takluyver takluyver Mar 23, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

f strings are great, but we're still supporting Python 3.4 and 3.5 for now, so we'll need to stick with the explicit .format() method for now.

Loading

Copy link
Member Author

@mpacer mpacer Mar 23, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Loading

Copy link
Member

@takluyver takluyver left a comment

Looks good, just a few points about clarity.

Loading

"""Gets all cells once they are visible.
"""
wait_for_selector(self.browser, ".cell")
Copy link
Member

@takluyver takluyver Mar 23, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't like having a wait inside a property - the rule of thumb for properties is that anything they need to do should be near-instant. If it needs to do something that's not, let's make it a regular method - the call gives people reading the code a hint that there's something going on underneath.

Loading

Copy link
Member Author

@mpacer mpacer Mar 23, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was wrong in my belief that it was needed here. One of the reasons I really didn't like it was that it couldn't handle notebooks with no cells; so this always seemed wrong. So it's gone!

Loading


@contextmanager
def new_window(browser, selector=None):
"""Creates new window, switches you to that window, waits for selector if set.
Copy link
Member

@takluyver takluyver Mar 23, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Neat! The docstring needs some work, though - this doesn't create a new window, rather it expects that code called in its context will create a new window, and then it switches to that on leaving the context.

This is worth explaining clearly, because most context managers do the interesting stuff when you create/enter them, and exiting just cleans something up. Here the interesting stuff is on exit.

Loading

Copy link
Member Author

@mpacer mpacer Mar 23, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I took care of this one.

Loading

# initial_window_handles = browser.window_handles
with new_window(browser, selector=".cell"):
select_kernel(browser, kernel_name=kernel_name)
browser.execute_script("Jupyter.notebook.set_autosave_interval(0)")
Copy link
Member

@takluyver takluyver Mar 23, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it OK that this is in new_notebook() but remove_safety_check() is called from __init__? I'd expect they'd go together.

Loading

Copy link
Member Author

@mpacer mpacer Mar 23, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yea… I think you're right on that account.

Loading



@classmethod
def new_notebook(cls, browser, kernel_name='kernel-python3'):
Copy link
Member

@takluyver takluyver Mar 23, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Docstring! It should mention that this expects to be called with the browser on the dashboard page. You could use your other utilities to verify that and throw an error if it's not.

Loading

def current_cell_index(self):
return self.cells.index(self.current_cell)

def remove_safety_check(self):
Copy link
Member

@takluyver takluyver Mar 23, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'safety' is rather vague. remove_unsaved_check, maybe? Or base it on the hook name: remove_onbeforeunload.

Loading

if visible:
conditional = EC.visibility_of_all_elements_located
else:
conditional = EC.presence_of_all_elements_located
Copy link
Member

@takluyver takluyver Mar 23, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is steadily heading towards replicating the complexity of the underlying Selenium API. It's fine for now, but we should keep in mind that it may be better to use the underlying APIs directly in some cases.

Loading

Copy link
Member Author

@mpacer mpacer Mar 23, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed — however, I think this covers all the cases where we wanted it and it hides a tonne of the selenium boilerplate so I think it's still ok.

Loading

@mpacer
Copy link
Member Author

@mpacer mpacer commented Mar 23, 2018

I think this now stands on its own. It's not fully handling everything that the markdown tests are doing, but it handles all the basic tests of our markdown rendering.

The mathjax rendering is a different story — however, I think we might be able to test that even more directly in pure javascript code as it's not really about what's rendered on the page but is entirely about what is parsed on the javascript side.

Loading

@mpacer mpacer force-pushed the selenium_utils branch from 64a2619 to 74af79c Mar 23, 2018
@mpacer mpacer changed the title Selenium utils + 1 markdown test Selenium utils + markdown tests Mar 23, 2018
@mpacer mpacer changed the title Selenium utils + markdown tests Selenium utils + markdown rendering tests Mar 23, 2018
"""Contextmanager for switching to & waiting for a window created.
This context manager gives you the ability to create a new window inside
the created context and it will switch you to that new window.abs
Copy link
Member

@takluyver takluyver Mar 25, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

abs?

Loading

def index(self, cell):
return self.cells.index(cell)

def remove_safety_check(self):
Copy link
Member

@takluyver takluyver Mar 25, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's find a more representative name for this.

Loading

'<pre><code class="cm-s-ipython language-aaaa">x = 1\n</code></pre>'
]
for i, cell in enumerate(nb):
nb.append(*cell_text, cell_type="markdown")
Copy link
Member

@takluyver takluyver Mar 25, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this needs to be in a loop now - the .append() call adds all the cells in one go.

Loading

@@ -0,0 +1,43 @@
from selenium.webdriver import Firefox
Copy link
Member

@takluyver takluyver Mar 25, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's have a module level docstring to explain that these utilities are not used by the tests, but they're intended for interactive exploration while writing tests.

Loading

Copy link
Member Author

@mpacer mpacer Mar 30, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had intended to add this.

Loading

@takluyver
Copy link
Member

@takluyver takluyver commented Mar 27, 2018

The last few things on this are all docstring and naming points. If you've got other things to do, let me know and I can tidy them up.

Loading

@takluyver takluyver merged commit faa0cab into jupyter:master Mar 28, 2018
4 checks passed
Loading
@takluyver takluyver added this to the 5.5 milestone Mar 28, 2018
@takluyver
Copy link
Member

@takluyver takluyver commented Mar 28, 2018

@mpacer sorry to take over the PR, but I wanted to get it merged in quickly.

Loading

@rgbkrk
Copy link
Member

@rgbkrk rgbkrk commented Mar 28, 2018

Thanks @mpacer and @takluyver!

Loading

self.add_cell(index, cell_type="markdown")
self.edit_cell(index=index, content=content, render=render)

def append(self, *values, cell_type="code"):
Copy link
Contributor

@hroncok hroncok May 11, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is invalid syntax on Python 2. Are those files intended to be supported on all the Pythons as notebook itself?

Loading

Copy link
Member

@rgbkrk rgbkrk May 11, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Notebook development is Python3 only now. Your runtime / kernel will always be allowed to be in Python2.

Loading

Copy link
Contributor

@hroncok hroncok May 11, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm planning to get python2-notebook out of Fedora, however I was wondering what's the upstream status on this. The docs say Python 2.7 is supported. And except the selenium tests it seems to be.

Loading

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 1, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Linked issues

Successfully merging this pull request may close these issues.

None yet

4 participants