todo page object casperjs
function TodoPage() { | |
'use strict'; | |
var idNew = '#new-todo'; | |
var title = '#header > h1'; | |
var children = '#todo-list li'; | |
var nthChild = function(n) { | |
return children+':nth-child('+n+')'; | |
}; | |
var nthChildAndSelector = function(selector,n) { | |
return nthChild(n) + selector; | |
}; | |
var nthLabel = _.partial(nthChildAndSelector,' label'); | |
var nthInput = _.partial(nthChildAndSelector,' input.edit'); | |
var nthDestroyBtn = _.partial(nthChildAndSelector,' button.destroy'); | |
var nthCheckbox = _.partial(nthChildAndSelector,' input.toggle'); | |
var nthHidden = _.partial(nthChildAndSelector,'.hidden'); | |
var nthNotHidden = _.partial(nthChildAndSelector,':not(.hidden)'); | |
this.before = function() { | |
casper.start(casper.cli.get("urlstart"), function() { | |
return casper.evaluate(function() { | |
window.localStorage.clear(); | |
}); | |
}); | |
}; | |
this.test = function(fn) { | |
casper.then(fn); | |
} | |
this.titleList = function() { | |
return casper.evaluate(function(selector) { | |
return __utils__.findOne(selector).textContent; | |
},title); | |
}; | |
this.typeNew = function(newTodo) { | |
casper.sendKeys(idNew, newTodo); | |
return this; | |
}; | |
this.enterNew = function() { | |
casper.sendKeys(idNew, casper.page.event.key.Enter); | |
}; | |
this.nbVisible = function() { | |
return casper.evaluate(function(selector) { | |
return _.chain(_.toArray(__utils__.findAll(selector))) | |
.filter(function(element){return window.getComputedStyle(element).display !== 'none';}) | |
.reduce(function(count) { return ++count;},0) | |
.value(); | |
},children); | |
}; | |
this.nthText = function(nth) { | |
return casper.evaluate(function(selector) { | |
return __utils__.findOne(selector).textContent; | |
},nthLabel(nth)); | |
}; | |
this.mouseOverNth = function(nth) { | |
mouse.move(nthChild(nth)); | |
return this; | |
}; | |
this.deleteNth = function(nth) { | |
casper.click(nthDestroyBtn(nth)); | |
}; | |
this.done = function(nth) { | |
casper.click(nthCheckbox(nth)); | |
}; | |
this.nthCompleted = function(nth) { | |
return casper.evaluate(function(selector) { | |
return __utils__.findOne(selector).checked; | |
},nthCheckbox(nth)); | |
}; | |
this.doubleClickNth = function(nth) { | |
mouse.doubleclick(nthLabel(nth)); | |
}; | |
this.editNth = function(nth,todo) { | |
casper.sendKeys(nthInput(nth),todo); | |
return this; | |
}; | |
this.enterNth = function(nth) { | |
casper.sendKeys(nthInput(nth), casper.page.event.key.Enter); | |
}; | |
this.first = _.partial(this.nthText,1); | |
this.mouseOverFirst = _.partial(this.mouseOverNth,1); | |
this.deleteFirst = _.partial(this.deleteNth,1); | |
this.doubleClickFirst = _.partial(this.doubleClickNth,1); | |
this.editFirst = _.partial(this.editNth,1); | |
this.enterFirst = _.partial(this.enterNth,1); | |
} | |
module.exports = new TodoPage(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment