Skip to content

Commit ebe0176

Browse files
authored
Merge pull request #3601 from arovit/test_empty_arrows
[WIP] [3335] Convert JS tests to Selenium
2 parents 1437dfe + 2941158 commit ebe0176

File tree

3 files changed

+41
-34
lines changed

3 files changed

+41
-34
lines changed

notebook/tests/notebook/empty_arrow_keys.js

Lines changed: 0 additions & 21 deletions
This file was deleted.

notebook/tests/selenium/test_deletecell.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,9 @@ def cell_is_deletable(nb, index):
33
JS = 'return Jupyter.notebook.get_cell({}).is_deletable();'.format(index)
44
return nb.browser.execute_script(JS)
55

6-
def delete_cell(notebook, index):
7-
notebook.focus_cell(index)
8-
notebook.to_command_mode
9-
notebook.current_cell.send_keys('dd')
6+
def remove_all_cells(notebook):
7+
for i in range(len(notebook.cells)):
8+
notebook.delete_cell(0)
109

1110
def test_delete_cells(notebook):
1211
a = 'print("a")'
@@ -30,22 +29,22 @@ def test_delete_cells(notebook):
3029
assert cell_is_deletable(notebook, 2)
3130

3231
# Try to delete cell a (should not be deleted)
33-
delete_cell(notebook, 0)
32+
notebook.delete_cell(0)
3433
assert notebook.get_cells_contents() == [a, b, c]
3534

3635
# Try to delete cell b (should succeed)
37-
delete_cell(notebook, 1)
36+
notebook.delete_cell(1)
3837
assert notebook.get_cells_contents() == [a, c]
3938

4039
# Try to delete cell c (should succeed)
41-
delete_cell(notebook, 1)
40+
notebook.delete_cell(1)
4241
assert notebook.get_cells_contents() == [a]
4342

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

4746
# Try to delete cell a (should succeed)
48-
delete_cell(notebook, 0)
47+
notebook.delete_cell(0)
4948
assert len(notebook.cells) == 1 # it contains an empty cell
5049

5150
# Make sure copied cells are deletable
@@ -56,3 +55,7 @@ def test_delete_cells(notebook):
5655
notebook.current_cell.send_keys('cv')
5756
assert len(notebook.cells) == 2
5857
assert cell_is_deletable(notebook, 1)
58+
59+
notebook.set_cell_metadata(0, 'deletable', 'true') # to perform below test, remove all the cells
60+
remove_all_cells(notebook)
61+
assert len(notebook.cells) == 1 # notebook should create one automatically on empty notebook

notebook/tests/selenium/utils.py

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,11 @@ def add_cell(self, index=-1, cell_type="code", content=""):
194194
if cell_type != 'code':
195195
self.convert_cell_type(index=new_index, cell_type=cell_type)
196196

197+
def delete_cell(self, index):
198+
self.focus_cell(index)
199+
self.to_command_mode()
200+
self.current_cell.send_keys('dd')
201+
197202
def add_markdown_cell(self, index=-1, content="", render=True):
198203
self.add_cell(index, cell_type="markdown")
199204
self.edit_cell(index=index, content=content, render=render)
@@ -213,6 +218,9 @@ def run_all(self):
213218
for cell in self:
214219
self.execute_cell(cell)
215220

221+
def trigger_keydown(self, keys):
222+
trigger_keystrokes(self.body, keys)
223+
216224
@classmethod
217225
def new_notebook(cls, browser, kernel_name='kernel-python3'):
218226
with new_window(browser, selector=".cell"):
@@ -261,11 +269,28 @@ def new_window(browser, selector=None):
261269

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

267274
def ctrl(browser, k):
268275
"""Send key combination Ctrl+(k)"""
269-
ActionChains(browser)\
270-
.key_down(Keys.CONTROL).send_keys(k).key_up(Keys.CONTROL).perform()
271-
276+
trigger_keystrokes(browser, "control-%s"%k)
277+
278+
def trigger_keystrokes(browser, *keys):
279+
""" Send the keys in sequence to the browser.
280+
Handles following key combinations
281+
1. with modifiers eg. 'control-alt-a', 'shift-c'
282+
2. just modifiers eg. 'alt', 'esc'
283+
3. non-modifiers eg. 'abc'
284+
Modifiers : http://seleniumhq.github.io/selenium/docs/api/py/webdriver/selenium.webdriver.common.keys.html
285+
"""
286+
for each_key_combination in keys:
287+
keys = each_key_combination.split('-')
288+
if len(keys) > 1: # key has modifiers eg. control, alt, shift
289+
modifiers_keys = [getattr(Keys, x.upper()) for x in keys[:-1]]
290+
ac = ActionChains(browser)
291+
for i in modifiers_keys: ac = ac.key_down(i)
292+
ac.send_keys(keys[-1])
293+
for i in modifiers_keys[::-1]: ac = ac.key_up(i)
294+
ac.perform()
295+
else: # single key stroke. Check if modifier eg. "up"
296+
browser.send_keys(getattr(Keys, keys[0].upper(), keys[0]))

0 commit comments

Comments
 (0)