Skip to content

Commit 031c90b

Browse files
author
arovitn
committed
1. Converting 'empty_arrows_keys.js' into selenium based test
2. Moved "delete_cell" method to utils.py and modified references to use it from there 3. added a generalized method "trigger_keystrokes" to send keystrokes to browser
1 parent 55be52b commit 031c90b

File tree

4 files changed

+46
-34
lines changed

4 files changed

+46
-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: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,6 @@ 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')
10-
116
def test_delete_cells(notebook):
127
a = 'print("a")'
138
b = 'print("b")'
@@ -30,22 +25,22 @@ def test_delete_cells(notebook):
3025
assert cell_is_deletable(notebook, 2)
3126

3227
# Try to delete cell a (should not be deleted)
33-
delete_cell(notebook, 0)
28+
notebook.delete_cell(0)
3429
assert notebook.get_cells_contents() == [a, b, c]
3530

3631
# Try to delete cell b (should succeed)
37-
delete_cell(notebook, 1)
32+
notebook.delete_cell(1)
3833
assert notebook.get_cells_contents() == [a, c]
3934

4035
# Try to delete cell c (should succeed)
41-
delete_cell(notebook, 1)
36+
notebook.delete_cell(1)
4237
assert notebook.get_cells_contents() == [a]
4338

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

4742
# Try to delete cell a (should succeed)
48-
delete_cell(notebook, 0)
43+
notebook.delete_cell(0)
4944
assert len(notebook.cells) == 1 # it contains an empty cell
5045

5146
# Make sure copied cells are deletable
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# selenium test version for 'empty_arrow_keys.js'
2+
3+
4+
def remove_cells(notebook):
5+
for i in notebook.cells:
6+
notebook.delete_cell(notebook.index(i))
7+
return True
8+
9+
def test_empty_arrows_keys(notebook):
10+
# delete all the cells
11+
notebook.trigger_keydown('up')
12+
notebook.trigger_keydown('down')
13+
assert remove_cells(notebook);

notebook/tests/selenium/utils.py

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,11 @@ def add_cell(self, index=-1, cell_type="code", content=""):
184184
if cell_type != 'code':
185185
self.convert_cell_type(index=new_index, cell_type=cell_type)
186186

187+
def delete_cell(self, index):
188+
self.focus_cell(index)
189+
self.to_command_mode
190+
self.current_cell.send_keys('dd')
191+
187192
def add_markdown_cell(self, index=-1, content="", render=True):
188193
self.add_cell(index, cell_type="markdown")
189194
self.edit_cell(index=index, content=content, render=render)
@@ -203,6 +208,9 @@ def run_all(self):
203208
for cell in self:
204209
self.execute_cell(cell)
205210

211+
def trigger_keydown(self, keys):
212+
trigger_keystrokes(self.body, keys)
213+
206214
@classmethod
207215
def new_notebook(cls, browser, kernel_name='kernel-python3'):
208216
with new_window(browser, selector=".cell"):
@@ -251,10 +259,27 @@ def new_window(browser, selector=None):
251259

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

257264
def ctrl(browser, k):
258265
"""Send key combination Ctrl+(k)"""
259-
ActionChains(browser)\
260-
.key_down(Keys.CONTROL).send_keys(k).key_up(Keys.CONTROL).perform()
266+
trigger_keystrokes(browser, "control-%s"%k)
267+
268+
def trigger_keystrokes(browser, *keys):
269+
""" Send the keys in sequence to the browser.
270+
Handles following key combinations
271+
1. with modifiers eg. 'control-alt-a', 'shift-c'
272+
2. just modifiers eg. 'alt', 'esc'
273+
3. non-modifiers eg. 'abc'
274+
Modifiers : http://seleniumhq.github.io/selenium/docs/api/py/webdriver/selenium.webdriver.common.keys.html
275+
"""
276+
for each_key_combination in keys:
277+
keys = each_key_combination.split('-')
278+
if len(keys) > 1: # key has modifiers eg. control, alt, shift
279+
modifiers_keys = list(map(lambda x:getattr(Keys, x.upper()), keys[:-1]))
280+
ac = ActionChains(browser)
281+
for i in modifiers_keys: ac = ac.key_down(i)
282+
ac.send_keys(keys[-1])
283+
for i in modifiers_keys[::-1]: ac = ac.key_up(i)
284+
else: # single key stroke. Check if modifier eg. "up"
285+
browser.send_keys(getattr(Keys, keys[0].upper(), keys[0]))

0 commit comments

Comments
 (0)