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

[3335] Convert JS tests to Selenium #3601

Merged
merged 6 commits into from
Jun 10, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 0 additions & 21 deletions notebook/tests/notebook/empty_arrow_keys.js

This file was deleted.

19 changes: 11 additions & 8 deletions notebook/tests/selenium/test_deletecell.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@ def cell_is_deletable(nb, index):
JS = 'return Jupyter.notebook.get_cell({}).is_deletable();'.format(index)
return nb.browser.execute_script(JS)

def delete_cell(notebook, index):
notebook.focus_cell(index)
notebook.to_command_mode
notebook.current_cell.send_keys('dd')
def remove_all_cells(notebook):
for i in range(len(notebook.cells)):
notebook.delete_cell(0)

def test_delete_cells(notebook):
a = 'print("a")'
Expand All @@ -30,22 +29,22 @@ def test_delete_cells(notebook):
assert cell_is_deletable(notebook, 2)

# Try to delete cell a (should not be deleted)
delete_cell(notebook, 0)
notebook.delete_cell(0)
assert notebook.get_cells_contents() == [a, b, c]

# Try to delete cell b (should succeed)
delete_cell(notebook, 1)
notebook.delete_cell(1)
assert notebook.get_cells_contents() == [a, c]

# Try to delete cell c (should succeed)
delete_cell(notebook, 1)
notebook.delete_cell(1)
assert notebook.get_cells_contents() == [a]

# Change the deletable state of cell a
notebook.set_cell_metadata(0, 'deletable', 'true')

# Try to delete cell a (should succeed)
delete_cell(notebook, 0)
notebook.delete_cell(0)
assert len(notebook.cells) == 1 # it contains an empty cell

# Make sure copied cells are deletable
Expand All @@ -56,3 +55,7 @@ def test_delete_cells(notebook):
notebook.current_cell.send_keys('cv')
assert len(notebook.cells) == 2
assert cell_is_deletable(notebook, 1)

notebook.set_cell_metadata(0, 'deletable', 'true') # to perform below test, remove all the cells
remove_all_cells(notebook)
assert len(notebook.cells) == 1 # notebook should create one automatically on empty notebook
35 changes: 30 additions & 5 deletions notebook/tests/selenium/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,11 @@ def add_cell(self, index=-1, cell_type="code", content=""):
if cell_type != 'code':
self.convert_cell_type(index=new_index, cell_type=cell_type)

def delete_cell(self, index):
self.focus_cell(index)
self.to_command_mode()
self.current_cell.send_keys('dd')

def add_markdown_cell(self, index=-1, content="", render=True):
self.add_cell(index, cell_type="markdown")
self.edit_cell(index=index, content=content, render=render)
Expand All @@ -213,6 +218,9 @@ def run_all(self):
for cell in self:
self.execute_cell(cell)

def trigger_keydown(self, keys):
trigger_keystrokes(self.body, keys)

@classmethod
def new_notebook(cls, browser, kernel_name='kernel-python3'):
with new_window(browser, selector=".cell"):
Expand Down Expand Up @@ -261,11 +269,28 @@ def new_window(browser, selector=None):

def shift(browser, k):
"""Send key combination Shift+(k)"""
ActionChains(browser)\
.key_down(Keys.SHIFT).send_keys(k).key_up(Keys.SHIFT).perform()
trigger_keystrokes(browser, "shift-%s"%k)

def ctrl(browser, k):
"""Send key combination Ctrl+(k)"""
ActionChains(browser)\
.key_down(Keys.CONTROL).send_keys(k).key_up(Keys.CONTROL).perform()

trigger_keystrokes(browser, "control-%s"%k)

def trigger_keystrokes(browser, *keys):
""" Send the keys in sequence to the browser.
Handles following key combinations
1. with modifiers eg. 'control-alt-a', 'shift-c'
2. just modifiers eg. 'alt', 'esc'
3. non-modifiers eg. 'abc'
Modifiers : http://seleniumhq.github.io/selenium/docs/api/py/webdriver/selenium.webdriver.common.keys.html
"""
for each_key_combination in keys:
keys = each_key_combination.split('-')
if len(keys) > 1: # key has modifiers eg. control, alt, shift
modifiers_keys = [getattr(Keys, x.upper()) for x in keys[:-1]]
ac = ActionChains(browser)
for i in modifiers_keys: ac = ac.key_down(i)
ac.send_keys(keys[-1])
for i in modifiers_keys[::-1]: ac = ac.key_up(i)
ac.perform()
else: # single key stroke. Check if modifier eg. "up"
browser.send_keys(getattr(Keys, keys[0].upper(), keys[0]))