From 462bc416deba81a544c022b8cbf5425e71df2045 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Du=C5=A1an=20Jureti=C4=87?= Date: Wed, 25 Dec 2013 16:17:05 -0300 Subject: [PATCH 1/9] Starting e2e testing --- .travis.yml | 6 ++++++ karma-e2e.conf.js | 54 ---------------------------------------------- package.json | 4 ++-- protractor.conf.js | 24 +++++++++++++++++++++ test/e2e/e2e.js | 22 +++++++++++++++++++ 5 files changed, 54 insertions(+), 56 deletions(-) delete mode 100644 karma-e2e.conf.js create mode 100644 protractor.conf.js create mode 100644 test/e2e/e2e.js diff --git a/.travis.yml b/.travis.yml index 9f977e2505..5828184608 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,8 +1,14 @@ language: node_js node_js: - '0.10' +services: + - mongodb before_script: - 'npm install -g bower grunt-cli' - 'bower install' - export DISPLAY=:99.0 - sh -e /etc/init.d/xvfb start + - cp config.json.example config.json + - ./node_modules/protractor/bin/webdriver-manager update + - ./node_modules/protractor/bin/webdriver-manager start & + - grunt run:dev > /dev/null & diff --git a/karma-e2e.conf.js b/karma-e2e.conf.js deleted file mode 100644 index fa01484a0a..0000000000 --- a/karma-e2e.conf.js +++ /dev/null @@ -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_' - }); -}; diff --git a/package.json b/package.json index 9e2ac50493..87f7604b40 100644 --- a/package.json +++ b/package.json @@ -58,12 +58,12 @@ "npm": "1.2.x" }, "scripts": { - "test": "grunt karma:continuous", + "test": "grunt karma:continuous && ./node_modules/protractor/bin/protractor protractor.conf.js", "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", diff --git a/protractor.conf.js b/protractor.conf.js new file mode 100644 index 0000000000..290322646f --- /dev/null +++ b/protractor.conf.js @@ -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:3000', + + // Options to be passed to Jasmine-node. + jasmineNodeOpts: { + showColors: true, + defaultTimeoutInterval: 30000 + } +}; \ No newline at end of file diff --git a/test/e2e/e2e.js b/test/e2e/e2e.js new file mode 100644 index 0000000000..6e57d30121 --- /dev/null +++ b/test/e2e/e2e.js @@ -0,0 +1,22 @@ +'use strict'; + +describe('index page', function() { + beforeEach(function(){ + browser.ignoreSynchronization = true; + browser.get('http://localhost:3000/'); + }); + + it('shows the front page', function(){ + // browser.sleep(3000); + var button = element(by.className('btn')); + expect(button.getText()).toEqual('Play'); + }); + + it('shows the login form', function(){ + var button = element(by.className('btn')); + button.click(); + + }); + + +}); \ No newline at end of file From 91b0f3fb9607d940161f3113777763b8d88b9030 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Du=C5=A1an=20Jureti=C4=87?= Date: Wed, 25 Dec 2013 20:09:18 -0300 Subject: [PATCH 2/9] E2e testing: added tests for failed login and register new user --- test/e2e/e2e.js | 33 +++++++++++++++++++++++++++------ 1 file changed, 27 insertions(+), 6 deletions(-) diff --git a/test/e2e/e2e.js b/test/e2e/e2e.js index 6e57d30121..4d5b5903a6 100644 --- a/test/e2e/e2e.js +++ b/test/e2e/e2e.js @@ -1,22 +1,43 @@ 'use strict'; -describe('index page', function() { +describe('front page', function() { beforeEach(function(){ browser.ignoreSynchronization = true; - browser.get('http://localhost:3000/'); + browser.get('/'); }); it('shows the front page', function(){ - // browser.sleep(3000); var button = element(by.className('btn')); expect(button.getText()).toEqual('Play'); }); - it('shows the login form', function(){ + it("don't login when using wrong credentials", function(){ var button = element(by.className('btn')); button.click(); - + 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.getCurrentUrl().then(function(url){ + browser.sleep(1000); + expect(url).toMatch(/#\/tasks$/); + }); + }); }); \ No newline at end of file From a6443bceb8f1efe38723f78a3ce82110e12f6418 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Du=C5=A1an=20Jureti=C4=87?= Date: Fri, 27 Dec 2013 23:03:16 -0300 Subject: [PATCH 3/9] Fix build --- test/e2e/e2e.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/e2e/e2e.js b/test/e2e/e2e.js index 4d5b5903a6..40f8c64dc2 100644 --- a/test/e2e/e2e.js +++ b/test/e2e/e2e.js @@ -4,6 +4,7 @@ describe('front page', function() { beforeEach(function(){ browser.ignoreSynchronization = true; browser.get('/'); + browser.sleep(1000); }); it('shows the front page', function(){ @@ -35,8 +36,8 @@ describe('front page', function() { 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){ - browser.sleep(1000); expect(url).toMatch(/#\/tasks$/); }); }); From 87e7b2ead6785c686e004ad3bffba72f528146ff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Du=C5=A1an=20Jureti=C4=87?= Date: Fri, 27 Dec 2013 23:41:22 -0300 Subject: [PATCH 4/9] Fix build by giving more time to Protractor --- test/e2e/e2e.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/e2e/e2e.js b/test/e2e/e2e.js index 40f8c64dc2..f346ad0e78 100644 --- a/test/e2e/e2e.js +++ b/test/e2e/e2e.js @@ -36,7 +36,7 @@ describe('front page', function() { element(by.model('registerVals.confirmPassword')).sendKeys('pass'); var register = element(by.css("#register-tab input[value='Register']")); register.click(); - browser.sleep(1000); + browser.sleep(2000); browser.getCurrentUrl().then(function(url){ expect(url).toMatch(/#\/tasks$/); }); From 3c5f9d7f8bfaaee7bc22f0d87d2c13400ec60dd8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Du=C5=A1an=20Jureti=C4=87?= Date: Sat, 28 Dec 2013 23:16:02 -0300 Subject: [PATCH 5/9] Mute Selenium on Travis --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 5828184608..08a66baefe 100644 --- a/.travis.yml +++ b/.travis.yml @@ -10,5 +10,5 @@ before_script: - sh -e /etc/init.d/xvfb start - cp config.json.example config.json - ./node_modules/protractor/bin/webdriver-manager update - - ./node_modules/protractor/bin/webdriver-manager start & + - ./node_modules/protractor/bin/webdriver-manager start > /dev/null & - grunt run:dev > /dev/null & From c572a195aa1de69348f24deb08fcd720c6960781 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Du=C5=A1an=20Jureti=C4=87?= Date: Sat, 28 Dec 2013 23:44:41 -0300 Subject: [PATCH 6/9] Take screenshot of failed e2e tests --- test/e2e/e2e.js | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/test/e2e/e2e.js b/test/e2e/e2e.js index f346ad0e78..26e76640d6 100644 --- a/test/e2e/e2e.js +++ b/test/e2e/e2e.js @@ -7,6 +7,22 @@ describe('front page', function() { 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'); From 69c4977511fed80e9cbe2a6a679347be62b38e7b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Du=C5=A1an=20Jureti=C4=87?= Date: Sun, 29 Dec 2013 16:56:51 -0300 Subject: [PATCH 7/9] Fix again e2e test --- test/e2e/e2e.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/e2e/e2e.js b/test/e2e/e2e.js index 26e76640d6..c62d57b639 100644 --- a/test/e2e/e2e.js +++ b/test/e2e/e2e.js @@ -52,9 +52,9 @@ describe('front page', function() { element(by.model('registerVals.confirmPassword')).sendKeys('pass'); var register = element(by.css("#register-tab input[value='Register']")); register.click(); - browser.sleep(2000); + browser.sleep(1000); browser.getCurrentUrl().then(function(url){ - expect(url).toMatch(/#\/tasks$/); + expect(url).not.toMatch(/static\/front/); }); }); }); \ No newline at end of file From 431622ce1492c908bebe44a7123c4d73d032ca51 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Du=C5=A1an=20Jureti=C4=87?= Date: Tue, 31 Dec 2013 12:55:23 -0300 Subject: [PATCH 8/9] E2e tests: launch Selenium and node server and kill them after finishing the tests, using a test database --- .travis.yml | 6 +----- package.json | 2 +- protractor.conf.js | 2 +- test/e2e/e2e.js | 1 + test/run_tests.sh | 19 +++++++++++++++++++ 5 files changed, 23 insertions(+), 7 deletions(-) create mode 100755 test/run_tests.sh diff --git a/.travis.yml b/.travis.yml index 08a66baefe..7edd7c20b0 100644 --- a/.travis.yml +++ b/.travis.yml @@ -4,11 +4,7 @@ node_js: 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 - - ./node_modules/protractor/bin/webdriver-manager update - - ./node_modules/protractor/bin/webdriver-manager start > /dev/null & - - grunt run:dev > /dev/null & diff --git a/package.json b/package.json index 87f7604b40..a81bbd57d2 100644 --- a/package.json +++ b/package.json @@ -58,7 +58,7 @@ "npm": "1.2.x" }, "scripts": { - "test": "grunt karma:continuous && ./node_modules/protractor/bin/protractor protractor.conf.js", + "test": "./test/run_tests.sh", "start": "grunt run:dev", "postinstall": "./node_modules/bower/bin/bower install -f" }, diff --git a/protractor.conf.js b/protractor.conf.js index 290322646f..305bb2cd5e 100644 --- a/protractor.conf.js +++ b/protractor.conf.js @@ -21,4 +21,4 @@ exports.config = { showColors: true, defaultTimeoutInterval: 30000 } -}; \ No newline at end of file +}; diff --git a/test/e2e/e2e.js b/test/e2e/e2e.js index c62d57b639..8bbdbe8a62 100644 --- a/test/e2e/e2e.js +++ b/test/e2e/e2e.js @@ -31,6 +31,7 @@ describe('front page', function() { 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']")); diff --git a/test/run_tests.sh b/test/run_tests.sh new file mode 100755 index 0000000000..761dd2b72d --- /dev/null +++ b/test/run_tests.sh @@ -0,0 +1,19 @@ +#!/bin/bash +# Configuration +TEST_DB=habitrpg_test +TEST_DB_URI="mongodb://localhost/$TEST_DB" + +# 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" 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 From 803a45b47f2849e39c1861fc0fb9646b01abce43 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Du=C5=A1an=20Jureti=C4=87?= Date: Wed, 1 Jan 2014 13:36:02 -0300 Subject: [PATCH 9/9] Test server port changed to 3001 --- protractor.conf.js | 2 +- test/run_tests.sh | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/protractor.conf.js b/protractor.conf.js index 305bb2cd5e..32999a2233 100644 --- a/protractor.conf.js +++ b/protractor.conf.js @@ -14,7 +14,7 @@ exports.config = { // A base URL for your application under test. Calls to protractor.get() // with relative paths will be prepended with this. - baseUrl: 'http://localhost:3000', + baseUrl: 'http://localhost:3001', // Options to be passed to Jasmine-node. jasmineNodeOpts: { diff --git a/test/run_tests.sh b/test/run_tests.sh index 761dd2b72d..d9f588ea06 100755 --- a/test/run_tests.sh +++ b/test/run_tests.sh @@ -2,6 +2,7 @@ # Configuration TEST_DB=habitrpg_test TEST_DB_URI="mongodb://localhost/$TEST_DB" +TEST_SERVER_PORT=3001 # Build assets grunt build:dev @@ -11,7 +12,7 @@ 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" node ./src/server.js > /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