Merge pull request #2112 from djuretic/e2e

End-to-end testing
This commit is contained in:
djuretic 2014-01-01 09:10:16 -08:00
commit 2f4112e2f1
6 changed files with 111 additions and 58 deletions

View file

@ -1,8 +1,10 @@
language: node_js
node_js:
- '0.10'
services:
- mongodb
before_script:
- 'npm install -g bower grunt-cli'
- 'bower install'
- 'npm install -g grunt-cli'
- export DISPLAY=:99.0
- sh -e /etc/init.d/xvfb start
- cp config.json.example config.json

View file

@ -1,54 +0,0 @@
// Karma configuration
// http://karma-runner.github.io/0.10/config/configuration-file.html
module.exports = function(config) {
config.set({
// base path, that will be used to resolve files and exclude
basePath: '',
// testing framework to use (jasmine/mocha/qunit/...)
frameworks: ['ng-scenario'],
// list of files / patterns to load in the browser
files: [
'test/e2e/**/*.js'
],
// list of files / patterns to exclude
exclude: [],
// web server port
port: 8080,
// level of logging
// possible values: LOG_DISABLE || LOG_ERROR || LOG_WARN || LOG_INFO || LOG_DEBUG
logLevel: config.LOG_INFO,
// enable / disable watching file and executing tests whenever any file changes
autoWatch: false,
// Start these browsers, currently available:
// - Chrome
// - ChromeCanary
// - Firefox
// - Opera
// - Safari (only Mac)
// - PhantomJS
// - IE (only Windows)
browsers: ['Chrome'],
// Continuous Integration mode
// if true, it capture browsers, run tests and exit
singleRun: false
// Uncomment the following lines if you are using grunt's server to run the tests
// proxies: {
// '/': 'http://localhost:9000/'
// },
// URL root prevent conflicts with the site root
// urlRoot: '_karma_'
});
};

View file

@ -58,12 +58,12 @@
"npm": "1.2.x"
},
"scripts": {
"test": "grunt karma:continuous",
"test": "./test/run_tests.sh",
"start": "grunt run:dev",
"postinstall": "./node_modules/bower/bin/bower install -f"
},
"devDependencies": {
"karma-ng-scenario": "~0.1.0",
"protractor": "~0.14.0",
"grunt-karma": "~0.6.2",
"karma-script-launcher": "~0.1.0",
"karma-chrome-launcher": "~0.1.0",

24
protractor.conf.js Normal file
View file

@ -0,0 +1,24 @@
// An example configuration file.
exports.config = {
// The address of a running selenium server.
seleniumAddress: 'http://localhost:4444/wd/hub',
// Capabilities to be passed to the webdriver instance.
capabilities: {
'browserName': 'firefox'
},
// Spec patterns are relative to the current working directly when
// protractor is called.
specs: ['test/e2e/e2e.js'],
// A base URL for your application under test. Calls to protractor.get()
// with relative paths will be prepended with this.
baseUrl: 'http://localhost:3001',
// Options to be passed to Jasmine-node.
jasmineNodeOpts: {
showColors: true,
defaultTimeoutInterval: 30000
}
};

61
test/e2e/e2e.js Normal file
View file

@ -0,0 +1,61 @@
'use strict';
describe('front page', function() {
beforeEach(function(){
browser.ignoreSynchronization = true;
browser.get('/');
browser.sleep(1000);
});
// based on https://github.com/angular/protractor/issues/114#issuecomment-29046939
afterEach(function(){
var currentSpec = jasmine.getEnv().currentSpec;
var passed = currentSpec.results().passed();
if(!passed){
var filename = 'exception_' + currentSpec.description + '.png';
browser.takeScreenshot().then(function(png){
var fs = require('fs');
var buffer = new Buffer(png, 'base64');
var stream = fs.createWriteStream(filename);
stream.write(buffer);
stream.end();
});
}
});
it('shows the front page', function(){
var button = element(by.className('btn'));
expect(button.getText()).toEqual('Play');
});
it("don't login when using wrong credentials", function(){
var button = element(by.className('btn'));
button.click();
browser.sleep(1000);
element(by.model('loginUsername')).sendKeys('username');
element(by.model('loginPassword')).sendKeys('pass');
var login = element(by.css("#login-tab input[value='Login']"));
login.click();
var alertDialog = browser.switchTo().alert();
expect(alertDialog.getText()).toMatch(/Username 'username' not found/);
alertDialog.accept();
});
it('registers a new user', function(){
var button = element(by.className('btn'));
button.click();
browser.sleep(1000);
var registerTab = element(by.linkText('Register'));
registerTab.click();
element(by.model('registerVals.username')).sendKeys('user');
element(by.model('registerVals.email')).sendKeys('user@example.com');
element(by.model('registerVals.password')).sendKeys('pass');
element(by.model('registerVals.confirmPassword')).sendKeys('pass');
var register = element(by.css("#register-tab input[value='Register']"));
register.click();
browser.sleep(1000);
browser.getCurrentUrl().then(function(url){
expect(url).not.toMatch(/static\/front/);
});
});
});

20
test/run_tests.sh Executable file
View file

@ -0,0 +1,20 @@
#!/bin/bash
# Configuration
TEST_DB=habitrpg_test
TEST_DB_URI="mongodb://localhost/$TEST_DB"
TEST_SERVER_PORT=3001
# Build assets
grunt build:dev
# Launch Node server and Selenium
echo "Recreating test database"
mongo "$TEST_DB" --eval "db.dropDatabase()"
./node_modules/protractor/bin/webdriver-manager update
./node_modules/protractor/bin/webdriver-manager start > /dev/null &
NODE_DB_URI="$TEST_DB_URI" PORT=$TEST_SERVER_PORT node ./src/server.js > /dev/null &
NODE_PID=$!
trap "kill $NODE_PID && curl http://localhost:4444/selenium-server/driver/?cmd=shutDownSeleniumServer" EXIT
sleep 3 # Wait for Selenium
grunt karma:continuous && ./node_modules/protractor/bin/protractor protractor.conf.js