@@ -194,6 +194,11 @@ def add_cell(self, index=-1, cell_type="code", content=""):
194
194
if cell_type != 'code' :
195
195
self .convert_cell_type (index = new_index , cell_type = cell_type )
196
196
197
+ def delete_cell (self , index ):
198
+ self .focus_cell (index )
199
+ self .to_command_mode ()
200
+ self .current_cell .send_keys ('dd' )
201
+
197
202
def add_markdown_cell (self , index = - 1 , content = "" , render = True ):
198
203
self .add_cell (index , cell_type = "markdown" )
199
204
self .edit_cell (index = index , content = content , render = render )
@@ -213,6 +218,9 @@ def run_all(self):
213
218
for cell in self :
214
219
self .execute_cell (cell )
215
220
221
+ def trigger_keydown (self , keys ):
222
+ trigger_keystrokes (self .body , keys )
223
+
216
224
@classmethod
217
225
def new_notebook (cls , browser , kernel_name = 'kernel-python3' ):
218
226
with new_window (browser , selector = ".cell" ):
@@ -261,11 +269,28 @@ def new_window(browser, selector=None):
261
269
262
270
def shift (browser , k ):
263
271
"""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 )
266
273
267
274
def ctrl (browser , k ):
268
275
"""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