@@ -184,6 +184,11 @@ def add_cell(self, index=-1, cell_type="code", content=""):
184
184
if cell_type != 'code' :
185
185
self .convert_cell_type (index = new_index , cell_type = cell_type )
186
186
187
+ def delete_cell (self , index ):
188
+ self .focus_cell (index )
189
+ self .to_command_mode
190
+ self .current_cell .send_keys ('dd' )
191
+
187
192
def add_markdown_cell (self , index = - 1 , content = "" , render = True ):
188
193
self .add_cell (index , cell_type = "markdown" )
189
194
self .edit_cell (index = index , content = content , render = render )
@@ -203,6 +208,9 @@ def run_all(self):
203
208
for cell in self :
204
209
self .execute_cell (cell )
205
210
211
+ def trigger_keydown (self , keys ):
212
+ trigger_keystrokes (self .body , keys )
213
+
206
214
@classmethod
207
215
def new_notebook (cls , browser , kernel_name = 'kernel-python3' ):
208
216
with new_window (browser , selector = ".cell" ):
@@ -251,10 +259,27 @@ def new_window(browser, selector=None):
251
259
252
260
def shift (browser , k ):
253
261
"""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 )
256
263
257
264
def ctrl (browser , k ):
258
265
"""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