diff --git a/test/test2/casper/add.items.coffee b/test/test2/casper/add.items.coffee new file mode 100644 index 0000000000..9094b67c77 --- /dev/null +++ b/test/test2/casper/add.items.coffee @@ -0,0 +1,40 @@ +helpers = casper.helpers +uid = helpers.uid +# test creation of Todos, Rewards, Habits, etc + +casper.start helpers.playUrl, -> + casper.test.assert(true,'true==true') +# helpers.getModel (err, model) -> +# tasksCount=0 +# for own key,value of model.user.tasks +# tasksCount++ +# utils.dump model.user.tasks +# casper.fill "form[data-task-type='habit']", { +# 'new-task': 'Habit' + uid +# }, true +# helpers.getModel (err, model) -> +# newTasksCount=0 +# for own key,value of model.user.tasks +# newTasksCount++ +# casper.echo "Tasks new: " + tasksCount +# casper.test.assert(newTasksCount>tasksCount,"Task list increases in length after new habit form submitted") +# +# +##casper.then -> +## helpers.getModel (err, model) -> +## casper.test.assertEquals(typeof model.user.stats.exp, "number", 'XP is number') +## casper.test.assertEquals(model.user.stats.exp, 0, 'XP == 0') +## casper.click '.habits a[data-direction="up"]' +## helpers.getModel (err, newModel) -> +## casper.test.assert(newModel.user.stats.exp > model.user.stats.exp, 'XP has increased after clicking habits "+"') +## casper.test.assert(newModel.user.stats.gp > model.user.stats.gp, 'GP has increased after clicking habits "+"') + + +# ---------- finish tests ------------ +casper.then -> + casper.test.done() + + +# ---------- Run ------------ +casper.run -> + casper.test.renderResults true diff --git a/test/test2/casper/general.casper.coffee b/test/test2/casper/general.casper.coffee deleted file mode 100644 index 26f10f2c46..0000000000 --- a/test/test2/casper/general.casper.coffee +++ /dev/null @@ -1,25 +0,0 @@ -helpers = new require('./lib/helpers')() -casper = helpers.casper -utils = helpers.utils -url = helpers.playUrl - -casper.on('remote.message' - (msg) -> - this.echo '[Console] : ' + msg - ) - -# ---------- Basic Test ------------ -casper.start url, -> - helpers.getModel (model) -> - casper.test.assertEquals(typeof model.user.stats.exp, "number", 'exp is number') - casper.test.assertEquals(model.user.stats.exp, 0, 'exp == 0') - casper.echo 'Clicking...' - casper.click '.habits a[data-direction="up"]' - casper.echo '...clicked' - helpers.getModel (model) -> - casper.test.assertEquals(model.user.stats.exp, 7.5, 'exp == 7.5') - - -# ---------- Run ------------ -casper.run -> - casper.test.renderResults true \ No newline at end of file diff --git a/test/test2/casper/stats.coffee b/test/test2/casper/stats.coffee new file mode 100644 index 0000000000..d134f518ee --- /dev/null +++ b/test/test2/casper/stats.coffee @@ -0,0 +1,103 @@ +#helpers = new require('./lib/helpers')() +#casper = helpers.casper +helpers = casper.helpers +eTest = helpers.evalTest +getModel = helpers.getModel +# ---------- Checks if clicking on the buttons changes stats and in right direction ------------ + + +casper.start helpers.playUrl, -> + test = 'EXP and GP increasing after clicking habits "+"' + getModel (err, model) -> + casper.test.assertEquals(typeof model.user.stats.exp, "number", 'XP is number') + casper.test.assertEquals(model.user.stats.exp, 0, 'XP == 0') + casper.click '.habits a[data-direction="up"]' + eTest( + (oldStats)-> + stats = window.DERBY.app.model.get '_user.stats' + console.log "Was:" + oldStats.exp + "Is: " + stats.exp + console.log "Was:" + oldStats.gp + "Is: " + stats.gp + (stats.exp > oldStats.exp && stats.gp > oldStats.gp) + test + model.user.stats + ) + + +casper.then -> + test = 'HP decreasing after clicking habits "-"' + getModel (err, model) -> + casper.click '.habits a[data-direction="down"]' + eTest( + (oldStats)-> + stats = window.DERBY.app.model.get '_user.stats' + console.log "Was:" + oldStats.hp + "Is: " + stats.hp + (stats.hp < oldStats.hp) + test + model.user.stats + ) + +casper.then -> + test = 'EXP and GP increasing after clicking .todo.uncompleted' + getModel (err, model) -> + casper.click '.task.todo.uncompleted input[type=checkbox]' + eTest( + (oldStats)-> + stats = window.DERBY.app.model.get '_user.stats' + console.log "Was:" + oldStats.exp + "Is: " + stats.exp + console.log "Was:" + oldStats.gp + "Is: " + stats.gp + (stats.exp > oldStats.exp && stats.gp > oldStats.gp) + test + model.user.stats + ) + +casper.then -> + test = 'EXP and GP decreasing after clicking .todo.completed' + getModel (err, model) -> + casper.click '.task.todo.completed input[type=checkbox]' + eTest( + (oldStats)-> + stats = window.DERBY.app.model.get '_user.stats' + console.log "Was:" + oldStats.exp + "Is: " + stats.exp + console.log "Was:" + oldStats.gp + "Is: " + stats.gp + (stats.exp < oldStats.exp && stats.gp < oldStats.gp) + test + model.user.stats + ) + +casper.then -> + test = 'EXP and GP increasing after clicking .daily.uncompleted' + getModel (err, model) -> + casper.click '.task.daily.uncompleted input[type=checkbox]' + eTest( + (oldStats)-> + stats = window.DERBY.app.model.get '_user.stats' + console.log "Was:" + oldStats.exp + "Is: " + stats.exp + console.log "Was:" + oldStats.gp + "Is: " + stats.gp + (stats.exp > oldStats.exp && stats.gp > oldStats.gp) + test + model.user.stats + ) + +casper.then -> + test = 'EXP and GP decreasing after clicking .daily.completed' + getModel (err, model) -> + casper.click '.task.daily.completed input[type=checkbox]' + eTest( + (oldStats)-> + stats = window.DERBY.app.model.get '_user.stats' + console.log "Was:" + oldStats.exp + "Is: " + stats.exp + console.log "Was:" + oldStats.gp + "Is: " + stats.gp + (stats.exp < oldStats.exp && stats.gp < oldStats.gp) + test + model.user.stats + ) + + +# ---------- finish tests ------------ +casper.then -> + casper.test.done() + + +# ---------- Run ------------ +casper.run -> + casper.test.renderResults true \ No newline at end of file diff --git a/test/test2/lib/helpers.coffee b/test/test2/lib/helpers.coffee index 4a2c0173cb..16d7103fc4 100644 --- a/test/test2/lib/helpers.coffee +++ b/test/test2/lib/helpers.coffee @@ -1,35 +1,45 @@ utils = require('utils') -module.exports = -> +#enable this to get remote console output, useful for debug. +casper.on "remote.message", (msg)-> + casper.echo "Remote console: " + msg + +casper.helpers = (-> baseUrl = 'http://localhost:3000' - casper = require("casper").create - clientScripts: 'lib/lodash.min.js' getModel = (cb)-> - casper.echo 'Loading model...' casper.waitFor( -> #check function casper.evaluate -> user = window.DERBY.app.model.get('_user') #wait till all fields get ready - check = (typeof user == "object" && typeof user.stats == "object" && typeof user.stats.exp == 'number') + check = (user?.stats?.exp?) #assign to the window so we can access it later - window.userCopy = userCopy = {} + window.userCopy = userCopy = + {} #dirty hack to get all fields in the object for k of user userCopy[k] = user[k] check -> #run this if check passed model = casper.evaluate -> - {user: window.userCopy} - casper.echo '...model loaded' - utils.dump model.user.stats - cb model + {user: window.userCopy} + cb null, model + ) + evalTest = (check, text, variables) -> + casper.waitFor( + -> #check function + casper.evaluate check, variables + -> #run this if check passed + casper.test.assert(true, text) + -> #run this if timeout + casper.test.assert(false, text) ) { casper: casper baseUrl: baseUrl playUrl: baseUrl + '/?play=1' - utils: utils getModel: getModel - } + uid: Math.random().toString(36).substring(2) + Math.random().toString(36).substring(2) + evalTest: evalTest + })() diff --git a/test/test2/readme.md b/test/test2/readme.md new file mode 100644 index 0000000000..a0567235cb --- /dev/null +++ b/test/test2/readme.md @@ -0,0 +1,9 @@ +1) It is a bit verbose +2) It needs to be refactored using "async" library. Somehow casper.then() is introducing bugs so I'm not going to use it until I figure out why exactly. Leave it as it is, I'll refactor as it grows. +3) It does not use any timeouts along the way, which is a good thing. +4) It runs consistently on my side (i.e. always passes). +5) So far phantomJS is given new userID every time so app.reset,app.revive is not necessary. I'll add it later. + +To run the tests run from terminal: +cd test/test2 +casperjs test ./casper/ --includes=./lib/helpers.coffee \ No newline at end of file