Skip to content

Commit b26221a

Browse files
authored
Merge pull request #4445 from emiliotl/convert_shutdown_js_to_py_selenium
Converted shutdown.js test to selenium python test
2 parents 9560e0c + e3c9a67 commit b26221a

File tree

3 files changed

+40
-55
lines changed

3 files changed

+40
-55
lines changed

notebook/tests/notebook/shutdown.js

-49
This file was deleted.
+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
"""Tests shutdown of the Kernel."""
2+
from .utils import wait_for_selector, wait_for_xpath
3+
4+
def test_shutdown(notebook):
5+
notebook.edit_cell(content="print(21)")
6+
wait_for_xpath(notebook.browser, '//a[text()="Kernel"]', single=True).click()
7+
wait_for_selector(notebook.browser, '#shutdown_kernel', single=True).click()
8+
wait_for_selector(notebook.browser, '.btn.btn-default.btn-sm.btn-danger', single=True).click()
9+
10+
#Wait until all shutdown modal elements dissapear before trying to execute the cell
11+
wait_for_xpath(notebook.browser, "//div[contains(@class,'modal')]", obscures=True)
12+
notebook.execute_cell(0)
13+
14+
assert not notebook.is_kernel_running()
15+
assert len(notebook.get_cell_output()) == 0

notebook/tests/selenium/utils.py

+25-6
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,28 @@
1313
pjoin = os.path.join
1414

1515

16-
def wait_for_selector(driver, selector, timeout=10, visible=False, single=False, wait_for_n=1):
16+
def wait_for_selector(driver, selector, timeout=10, visible=False, single=False, wait_for_n=1, obscures=False):
1717
if wait_for_n > 1:
1818
return _wait_for_multiple(
1919
driver, By.CSS_SELECTOR, selector, timeout, wait_for_n, visible)
20-
return _wait_for(driver, By.CSS_SELECTOR, selector, timeout, visible, single)
20+
return _wait_for(driver, By.CSS_SELECTOR, selector, timeout, visible, single, obscures)
2121

2222

23-
def wait_for_tag(driver, tag, timeout=10, visible=False, single=False, wait_for_n=1):
23+
def wait_for_tag(driver, tag, timeout=10, visible=False, single=False, wait_for_n=1, obscures=False):
2424
if wait_for_n > 1:
2525
return _wait_for_multiple(
2626
driver, By.TAG_NAME, tag, timeout, wait_for_n, visible)
27-
return _wait_for(driver, By.TAG_NAME, tag, timeout, visible, single)
27+
return _wait_for(driver, By.TAG_NAME, tag, timeout, visible, single, obscures)
2828

2929

30-
def _wait_for(driver, locator_type, locator, timeout=10, visible=False, single=False):
30+
def wait_for_xpath(driver, xpath, timeout=10, visible=False, single=False, wait_for_n=1, obscures=False):
31+
if wait_for_n > 1:
32+
return _wait_for_multiple(
33+
driver, By.XPATH, xpath, timeout, wait_for_n, visible)
34+
return _wait_for(driver, By.XPATH, xpath, timeout, visible, single, obscures)
35+
36+
37+
def _wait_for(driver, locator_type, locator, timeout=10, visible=False, single=False, obscures=False):
3138
"""Waits `timeout` seconds for the specified condition to be met. Condition is
3239
met if any matching element is found. Returns located element(s) when found.
3340
@@ -39,9 +46,12 @@ def _wait_for(driver, locator_type, locator, timeout=10, visible=False, single=F
3946
visible: if True, require that element is not only present, but visible
4047
single: if True, return a single element, otherwise return a list of matching
4148
elements
49+
osbscures: if True, waits until the element becomes invisible
4250
"""
4351
wait = WebDriverWait(driver, timeout)
44-
if single:
52+
if obscures:
53+
conditional = EC.invisibility_of_element_located
54+
elif single:
4555
if visible:
4656
conditional = EC.visibility_of_element_located
4757
else:
@@ -195,13 +205,19 @@ def wait_for_stale_cell(self, cell):
195205
wait = WebDriverWait(self.browser, 10)
196206
element = wait.until(EC.staleness_of(cell))
197207

208+
def wait_for_element_availability(self, element):
209+
_wait_for(self.browser, By.CLASS_NAME, element, visible=True)
210+
198211
def get_cells_contents(self):
199212
JS = 'return Jupyter.notebook.get_cells().map(function(c) {return c.get_text();})'
200213
return self.browser.execute_script(JS)
201214

202215
def get_cell_contents(self, index=0, selector='div .CodeMirror-code'):
203216
return self.cells[index].find_element_by_css_selector(selector).text
204217

218+
def get_cell_output(self, index=0, output='output_subarea'):
219+
return self.cells[index].find_elements_by_class_name(output)
220+
205221
def set_cell_metadata(self, index, key, value):
206222
JS = 'Jupyter.notebook.get_cell({}).metadata.{} = {}'.format(index, key, value)
207223
return self.browser.execute_script(JS)
@@ -282,6 +298,9 @@ def run_all(self):
282298

283299
def trigger_keydown(self, keys):
284300
trigger_keystrokes(self.body, keys)
301+
302+
def is_kernel_running(self):
303+
return self.browser.execute_script("return Jupyter.notebook.kernel.is_connected()")
285304

286305
@classmethod
287306
def new_notebook(cls, browser, kernel_name='kernel-python3'):

0 commit comments

Comments
 (0)