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

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.

Copy link
Member

@rgbkrk rgbkrk left a comment

Choose a reason for hiding this comment

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

Awesome

@rgbkrk
Copy link
Member

rgbkrk commented Mar 22, 2018

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

@mpacer
Copy link
Member Author

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.

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

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.

Copy link
Member Author

Choose a reason for hiding this comment

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

Copy link
Member

@takluyver takluyver left a comment

Choose a reason for hiding this comment

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

Looks good, just a few points about clarity.

"""Gets all cells once they are visible.

"""
wait_for_selector(self.browser, ".cell")
Copy link
Member

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.

Copy link
Member Author

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!


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

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.

Copy link
Member Author

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.

# 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

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.

Copy link
Member Author

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.



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

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.

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

def remove_safety_check(self):
Copy link
Member

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.

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

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.

Copy link
Member Author

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.

@mpacer
Copy link
Member Author

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.

@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

Choose a reason for hiding this comment

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

abs?

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

def remove_safety_check(self):
Copy link
Member

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.

'<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

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.

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

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.

Copy link
Member Author

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.

@takluyver
Copy link
Member

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.

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

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

@rgbkrk
Copy link
Member

rgbkrk commented Mar 28, 2018

Thanks @mpacer and @takluyver!

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

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?

Copy link
Member

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.

Copy link
Contributor

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.

@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
Development

Successfully merging this pull request may close these issues.

4 participants