Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert jstests to selenium: deletecell.js #3465

Merged
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 74 additions & 0 deletions notebook/tests/selenium/test_deletecell.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import os
import pytest
from .utils import Notebook

@pytest.fixture
def notebook(authenticated_browser):
return Notebook.new_notebook(authenticated_browser)

def get_cells_contents(nb):
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@takluyver Thanks for taking the time to review my PR. I made some changes to address your comments. I have only one more idea that I'm not sure about and I would like your input. I was thinking of moving this function to the Notebook class since I think it might be needed/repeated in several tests. I'm not sure about other helper functions like delete_cell, cell_is_deletable and set_cell_metadata, do you think they also should be moved to the Notebook class too?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd say that the get_cells_contents and set_cell_metadata can move to the notebook class, but let's leave delete_cell and cell_is_deletable here for now. They can always be moved in another PR if we change our minds. :-)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I made the changes. I believe the PR is ready to be merged if you don't have any more comments :)

JS = 'return Jupyter.notebook.get_cells().map(function(c) {return c.get_text();})'
return nb.browser.execute_script(JS)

def set_cell_metadata(nb, index, key, value):
JS = 'Jupyter.notebook.get_cell({}).metadata.{} = {}'.format(index, key, value)
return nb.browser.execute_script(JS)

def cell_is_deletable(nb, index):
JS = 'return Jupyter.notebook.get_cell({}).is_deletable();'.format(index)
return nb.browser.execute_script(JS)

def delete_cell(notebook, index):
notebook.focus_cell(index)
notebook.to_command_mode
notebook.current_cell.send_keys('dd')

def test_delete_cells(notebook):
print('testing deleteable cells')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We shouldn't need this print() - pytest should tell us which test is which when it runs them.

a = 'print("a")'
b = 'print("b")'
c = 'print("c")'

notebook.edit_cell(index=0, content=a)
notebook.append(b, c)
notebook.to_command_mode()

# Validate initial state
assert get_cells_contents(notebook) == [a, b, c]
for cell in range(0, 3):
assert cell_is_deletable(notebook, cell)

set_cell_metadata(notebook, 0, 'deletable', 'false')
set_cell_metadata(notebook, 1, 'deletable', 0
)
assert not cell_is_deletable(notebook, 0)
assert cell_is_deletable(notebook, 1)
assert cell_is_deletable(notebook, 2)

# Try to delete cell a (should not be deleted)
delete_cell(notebook, 0)
assert get_cells_contents(notebook) == [a, b, c]

# Try to delete cell b (should succeed)
delete_cell(notebook, 1)
assert get_cells_contents(notebook) == [a, c]

# Try to delete cell c (should succeed)
delete_cell(notebook, 1)
assert get_cells_contents(notebook) == [a]

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

# Try to delete cell a (should succeed)
delete_cell(notebook, 0)
assert len(notebook.cells) == 1 # it contains an empty cell

# Make sure copied cells are deletable
notebook.edit_cell(index=0, content=a)
set_cell_metadata(notebook, 0, 'deletable', 'false')
assert not cell_is_deletable(notebook, 0)
notebook.to_command_mode()
notebook.current_cell.send_keys('cv')
assert len(notebook.cells) == 2
assert cell_is_deletable(notebook, 1)
3 changes: 2 additions & 1 deletion notebook/tests/selenium/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,8 @@ def add_cell(self, index=-1, cell_type="code", content=""):
new_index = index + 1 if index >= 0 else index
if content:
self.edit_cell(index=index, content=content)
self.convert_cell_type(index=new_index, cell_type=cell_type)
if cell_type != 'code':
self.convert_cell_type(index=new_index, cell_type=cell_type)

def add_markdown_cell(self, index=-1, content="", render=True):
self.add_cell(index, cell_type="markdown")
Expand Down