-
Notifications
You must be signed in to change notification settings - Fork 5k
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
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Awesome
I'm happy these at least make it easy for Python people to write tests for the JS. 😅 |
This is now rebased on #3412 to take into account the issues that arise when you create a new notebook in the first place. |
__getitem__ also accepts slices also introduces __len__
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"]}' |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
✓
There was a problem hiding this 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.
notebook/tests/selenium/utils.py
Outdated
"""Gets all cells once they are visible. | ||
|
||
""" | ||
wait_for_selector(self.browser, ".cell") |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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!
notebook/tests/selenium/utils.py
Outdated
|
||
@contextmanager | ||
def new_window(browser, selector=None): | ||
"""Creates new window, switches you to that window, waits for selector if set. |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
notebook/tests/selenium/utils.py
Outdated
# 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)") |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
notebook/tests/selenium/utils.py
Outdated
|
||
|
||
@classmethod | ||
def new_notebook(cls, browser, kernel_name='kernel-python3'): |
There was a problem hiding this comment.
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.
notebook/tests/selenium/utils.py
Outdated
def current_cell_index(self): | ||
return self.cells.index(self.current_cell) | ||
|
||
def remove_safety_check(self): |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
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. |
notebook/tests/selenium/utils.py
Outdated
"""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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
abs
?
notebook/tests/selenium/utils.py
Outdated
def index(self, cell): | ||
return self.cells.index(cell) | ||
|
||
def remove_safety_check(self): |
There was a problem hiding this comment.
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") |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
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. |
@mpacer sorry to take over the PR, but I wanted to get it merged in quickly. |
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"): |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
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.