Skip to content

Commit

Permalink
better js tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Nicholas Bollweg committed Apr 18, 2016
1 parent d8aab20 commit 462c38d
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 146 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Expand Up @@ -57,3 +57,5 @@ docs/_build/
target/
.ipynb_checkpoints/
node_modules/
screenshots/
*.xunit.xml
221 changes: 83 additions & 138 deletions nb_conda_kernels/tests/js/_utils.js
@@ -1,154 +1,99 @@
/* globals require casper Jupyter */
var system = require('system');
;(function(){
"use strict";

var root = casper,
_img = 0,
node_modules = system.env.NBPRESENT_TEST_MODULES || "../../../node_modules",
vendor = root.vendor = root.vendor ? root.vendor : (root.vendor = {}),
_ = vendor._ = require(node_modules + "/lodash"),
_shotDir = "_unnamed";
var system = require('system');

function nextId(){
return ("000" + (_img++)).slice(-4);
}
var root = casper,
_img = 0,
_shotDir = "unnamed";

function slug(text){
return text.replace(/[^a-z0-9]/g, "_");
}


root.screenshot = function(message){
return this.captureSelector([
"screenshots/",
_shotDir,
"/",
nextId(),
"_",
slug(message),
".png"
].join(""),
"body"
);
};


root.screenshot.init = function(ns){
_shotDir = ns;
_img = 0;
return root;
};


root.canSeeAndClick = function(message, visible, click){
var things = message;

if(arguments.length !== 1){
things = [[message, visible, click]];
function nextId(){
return ("000" + (_img++)).slice(-4);
}

var that = this;
function slug(text){
return text.replace(/[^a-z0-9]/g, "_");
}

things.map(function(thing){
var message = thing[0],
visible = thing[1],
click = thing[2];
root.screenshot = function(message){
this.captureSelector([
"screenshots/",
_shotDir,
"/",
nextId(),
"_",
slug(message),
".png",
].join(""),
"body"
);
};


root.screenshot.init = function(ns){
_shotDir = ns;
_img = 0;
};

root.canSee = function(message, visible){
return this
.waitUntilVisible(visible)
.then(function(){
this.test.assertExists(visible, "I can see " + message);
this.screenshot(message);
});
}

that = that
root.canSeeAndClick = function(message, visible, click){
return this
.waitUntilVisible(visible)
.then(function(){
this.test.assertExists(click || visible, "I can see and click " + message);
this.screenshot(message);
this.click(click || visible);
});
});

return that;
}

root.hasMeta = function(path, tests){
var meta;

this.thenEvaluate(function () {
require(['base/js/events'], function(events) {
Jupyter._save_success = Jupyter._save_failed = false;
events.on('notebook_saved.Notebook', function () {
Jupyter._save_success = true;
});
events.on('notebook_save_failed.Notebook',
function (event, error) {
Jupyter._save_failed = "save failed with " + error;
});
Jupyter.notebook.save_notebook();
});
});

this.waitFor(function () {
return this.evaluate(function(){
return Jupyter._save_failed || Jupyter._save_success;
});
});
}

return this
.then(function(){
meta = this.evaluate(function(){
return Jupyter.notebook.metadata;
root.dragRelease = function(message, selector, opts){
var it, x, y, x1, y1;
return this.then(function(){
it = this.getElementBounds(selector);
x = it.left + it.width / 2;
y = it.top + it.height / 2;
x1 = x + (opts.right || -opts.left || 0);
y1 = y + (opts.down || -opts.up || 0);
})
.then(function(){
this.mouse.down(x, y);
})
.then(function(){
this.screenshot("click " + message);
this.mouse.move(x1, y1);
})
.then(function(){
this.screenshot("drag " + message);
this.mouse.up(x1, y1);
})
.then(function(){
this.screenshot("release " + message);
});
})
.then(function(){
_.map(tests, function(test, message){
var results = test(_.get(meta, path));
this.test.assertEquals(results[0], results[1],
"I remember " + message + " in " + path);
}, this);
});
};

root.dragRelease = function(message, selector, opts){
var it, x, y, x1, y1;
return this.then(function(){
it = this.getElementBounds(selector);
x = it.left + it.width / 2;
y = it.top + it.height / 2;
x1 = x + (opts.right || -opts.left || 0);
y1 = y + (opts.down || -opts.up || 0);
})
.then(function(){
this.mouse.down(x, y);
})
.then(function(){
this.screenshot("click " + message);
this.mouse.move(x1, y1);
})
.then(function(){
this.screenshot("drag " + message);
this.mouse.up(x1, y1);
})
.then(function(){
this.screenshot("release " + message);
});
};

root.baseline_notebook = function(){
// the actual test

this.append_cell();
this.set_cell_text(1, [
'from IPython.display import Markdown',
'Markdown("# Hello World!")'
].join("\n"));
this.execute_cell_then(1);

this.append_cell();
this.set_cell_text(2, [
'from ipywidgets import FloatSlider',
'x = FloatSlider()',
'x'
].join("\n"));
this.execute_cell_then(2);
}
};

root.baseline_notebook = function(){
// the actual test
this.set_cell_text(0, [
'from IPython.display import Markdown',
'Markdown("# Hello World!")'
].join("\n"));
this.execute_cell_then(0);

this.append_cell();
};

root.runCell = function(idx, lines){
// the actual test
this.set_cell_text(idx, lines.join("\n"));
this.execute_cell_then(idx);
}

root.saveNbpresent = function (){
return this.thenEvaluate(function(){
window.nbpresent.mode.metadata(true);
});
}
}).call(this);
14 changes: 9 additions & 5 deletions nb_conda_kernels/tests/js/test_notebook_basic.js
@@ -1,14 +1,18 @@
/* global casper */
casper.notebook_test(function(){
casper.dashboard_test(function(){
casper.screenshot.init("basic");
casper.viewport(1440, 900)
.then(basic_test);
});

function basic_test(){
this.baseline_notebook();
var pysel = '#new-menu li[id*=Python]',
rsel = '#new-menu li[id*=R ]';

return this.then(function(){
this.canSeeAndClick("the body", "body");
});
return this.canSeeAndClick(
"the kernel selector", "#new-buttons > .dropdown-toggle"
)
.canSee("a python kernel", pysel)
.canSee("an r kernel", rsel)
.canSeeAndClick("fin", "body");
}
8 changes: 5 additions & 3 deletions nb_conda_kernels/tests/test_notebook.py
Expand Up @@ -11,7 +11,7 @@
from notebook import jstest

import platform
import nb_conda_kernels
import nb_conda_kernels.install

IS_WIN = "Windows" in platform.system()

Expand Down Expand Up @@ -132,9 +132,11 @@ def add_xunit(self):

# ensure the system-of-interest is installed and enabled!
with patch.dict(os.environ, self.env):
nb_conda_kernels.install(
nb_conda_kernels.install.install(
prefix=os.environ.get("CONDA_ENV_PATH",
os.environ.get("CONDA_DEFAULT_ENV")))
os.environ.get("CONDA_DEFAULT_ENV")),
enable=True,
verbose=True)

def cleanup(self):
if hasattr(self, "stream_capturer"):
Expand Down

0 comments on commit 462c38d

Please sign in to comment.