Skip to content

Instantly share code, notes, and snippets.

@giovanettid
Last active November 9, 2015 21:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save giovanettid/95746c4f8858ceb8e49c to your computer and use it in GitHub Desktop.
Save giovanettid/95746c4f8858ceb8e49c to your computer and use it in GitHub Desktop.
todo page object seleniumjs
function TodoPage() {
'use strict';
var idNew = '#new-todo';
var title = 'title';
var titleList = '#header > h1';
var children = '#todo-list li';
var childrenVisible = '#todo-list li:not(.hidden)';
var nthChild = function(n) {
return children+':nth-child('+n+')';
};
var nthChildAndSelector = function(selector,n) {
return nthChild(n) + selector;
};
var find = function(selector) {
return driver.findElement(By.css(selector));
};
var enterKey = function(selector) {
find(selector).sendKeys(webdriver.Key.ENTER);
};
var nthLabel = _.partial(nthChildAndSelector,' label');
var nthInput = _.partial(nthChildAndSelector,' input.edit');
var nthDestroyBtn = _.partial(nthChildAndSelector,' button.destroy');
var nthCheckbox = _.partial(nthChildAndSelector,' input.toggle');
var nthCompleted = _.partial(nthChildAndSelector,'.completed');
var nthHidden = _.partial(nthChildAndSelector,'.hidden');
var nthNotHidden = _.partial(nthChildAndSelector,':not(.hidden)');
var driver;
var capabilities = {
chrome: webdriver.Capabilities.chrome(),
phantomjs: webdriver.Capabilities.phantomjs()
};
this.before = function() {
driver = new webdriver.Builder()
.withCapabilities(capabilities[config.browser]).build();
driver.get(config.url);
driver.executeScript('window.localStorage.clear();');
return driver.navigate().refresh();
};
this.after = function() {
return driver.quit();
};
this.title = function() {
return driver.getTitle();
};
this.titleList = function() {
return find(titleList).getText();
};
this.typeNew = function(newTodo) {
find(idNew).sendKeys(newTodo);
return this;
};
this.enterNew = function() {
enterKey(idNew);
};
this.nbVisible = function() {
return webdriver.promise.filter(driver.findElements(By.css(childrenVisible)), function(element) {
return element.isDisplayed();
});
};
this.nthText = function(nth) {
return find(nthLabel(nth)).getText();
};
this.mouseOverNth = function(nth) {
new webdriver.ActionSequence(driver).mouseMove(find(nthChild(nth))).perform();
return this;
};
this.deleteNth = function(nth) {
find(nthDestroyBtn(nth)).click();
};
this.done = function(nth) {
find(nthCheckbox(nth)).click();
};
this.nthCompleted = function(nth) {
return driver.findElements(By.css(nthCompleted(nth))).then(function (elements) {
return _.isArray(elements) && elements.length === 1;
});
};
this.doubleClickNth = function(nth) {
new webdriver.ActionSequence(driver).mouseMove(find(nthLabel(nth))).doubleClick().perform();
};
this.editNth = function(nth,todo) {
find(nthInput(nth)).sendKeys(todo);
return this;
};
this.enterNth = function(nth) {
enterKey(nthInput(nth));
};
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