Skip to content

Instantly share code, notes, and snippets.

@b2l
Last active August 29, 2015 14:05
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 b2l/48eb382ba86d2dac2232 to your computer and use it in GitHub Desktop.
Save b2l/48eb382ba86d2dac2232 to your computer and use it in GitHub Desktop.
// --- Dependencies
var test = require('selenium-webdriver/testing');
var expect = require('chai').expect;
var seleniumServer = require('./selenium-server');
var webdriver = require('selenium-webdriver');
var By = webdriver.By;
// Start the selenium standalone server
seleniumServer.start();
// Create a client for the selenium server.
// This client will automate a chrome browser
var driver = new webdriver
.Builder()
.usingServer(seleniumServer.address())
.withCapabilities(webdriver.Capabilities.chrome()) // Other browsers are listed here http://selenium.googlecode.com/git/docs/api/javascript/class_webdriver_Capabilities.html
.build();
// Behind the hood, this is the mocha framework.
// But use the "test" prefix from selenium-webdriver as it provide support for asynchronous actions
test.describe('Todo App', function() {
// Before the first test, open the application
test.before(function() {
driver.get('http://localhost:8000/html/todo.html');
});
// After the last test, close the browser
test.after(function() {
driver.close();
driver.quit();
});
// Test case
test.it('Should add a todo on ENTER', function() {
var input = driver.findElement(By.css('#add-todo-form .input'));
input.sendKeys('a new todo', webdriver.Key.ENTER); // Act on the dom as a real user will
// Actions are asynchronous, but are chained via a promise api.
// When you want to take controle after an action, just use "then":
getTodos().then(function(todos) { // getTodos() is a helper function that is defined at the end of the file
// Assert on the DOM state
expect(todos).to.have.property('length').equal(1);
assertTodoLabel(todos[0], 'a new todo');
});
});
test.it('Should add a todo on click on the "Add" button', function() {
var input = driver.findElement(By.css('#add-todo-form .input'));
input.sendKeys('something else to do');
driver.findElement(By.css('#add-todo-form input[type="submit"]')).click();
getTodos().then(function(todos) {
expect(todos).to.have.property('length').equal(2);
assertTodoLabel(todos[1], 'something else to do');
});
});
test.it('Should remove a todo on click on the "Remove" button', function() {
var firstTodo = getFirstTodo(); // Again a helper
firstTodo.findElement(By.css('.btn.remove')).click();
getTodos().then(function(todos) {
expect(todos).to.have.property('length').equal(1);
assertTodoLabel(todos[0], 'something else to do');
});
});
/**
* All the other test cases goes here
* see https://github.com/b2l/todo-app/blob/master/tests/todo-interface.js for the whole file
*/
});
function getFirstTodo() {
return driver.findElement(By.css('.todo-item:first-child'));
}
function getTodos() {
return driver.findElements(By.css('#todo-list .todo-item'));
}
function assertTodoLabel(todo, expectedLabel) {
todo.findElement(By.css('.todo-name-label')).getText().then(function(text) {
expect(text).to.equal(expectedLabel);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment