AngularJS E2E Testing with Protractor
In continuation to AngularJS series, today we'll discuss e2e or end-to-end testing of AngularJS applications. If you've been following the blog for a while, you must have noticed my numerous stressing the importance of unit testing using Jasmine and Karma and automating JavaScript testing with Grunt.js. The only thing left behind was e2e testing, of which we would talk today using Protractor for AngularJS applications.
What is E2E Testing?
End-to-end testing is a methodology used to test, whether the flow of an application is performing as designed from start to finish. The purpose of carrying out end-to-end tests is to identify system dependencies and to ensure that the right information is passed between various system components and systems.
In contrast to unit testing, which verifies the correct behaviour of various components separately, end-to-end testing verifies the entire flow of the application. From front end development perspective, it will be checking, whether JavaScript logic is reflected in the UI components. Good thing about Protractor is that we can write our end-to-end specs using Jasmine, so no knowledge of additional framework is needed.
angular-seed
Unit testing with Karma
End-to-end testing doesn't replace the good old unit testing. It merely completes it to provide a comprehensive testing tookit. Let's take a look at Karma configuration file, karma.conf.js:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | module.exports = function (config){ config.set({ basePath : './' , files : [ 'app/bower_components/angular/angular.js' , 'app/bower_components/angular-route/angular-route.js' , 'app/bower_components/angular-mocks/angular-mocks.js' , 'app/components/**/*.js' , 'app/view*/**/*.js' ], autoWatch : true , frameworks: [ 'jasmine' ], browsers : [ 'Chrome' ], plugins : [ 'karma-chrome-launcher' , 'karma-firefox-launcher' , 'karma-jasmine' , 'karma-junit-reporter' ], junitReporter : { outputFile: 'test_out/unit.xml' , suite: 'unit' } }); }; |
1 | reporters: [ 'progress' , 'junit' ] |
1 2 3 4 5 6 7 8 9 10 11 | 'use strict' ; describe( 'myApp.version module' , function () { beforeEach(module( 'myApp.version' )); describe( 'version service' , function () { it( 'should return current version' , inject( function (version) { expect(version).toEqual( '0.1' ); })); }); }); |
karma start karma.conf.js
End-to-end testing with Protractor
Protractor is a Node.js program built on top of WebDriverJS, which is Node.js runner, similar to node-jasmine, of which we've talked in Jasmine and Node.js article. Installing the driver is easy using the npm:
npm install -g selenium-webdriverThen we need to set-up the selenium environment by running the following command:
npm run webdriver-managerThe interesting thing about this command is that it is run through the npm. The executed commands can be found in package.json file under scripts section.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | "scripts" : { "postinstall" : "bower install" , "prestart" : "npm install" , "start" : "http-server -a localhost -p 8000 -c-1" , "pretest" : "npm install" , "test" : "karma start karma.conf.js" , "test-single-run" : "karma start karma.conf.js --single-run" , "preupdate-webdriver" : "npm install" , "update-webdriver" : "webdriver-manager update" , "preprotractor" : "npm run update-webdriver" , "protractor" : "protractor e2e-tests/protractor.conf.js" } |
Once everything is in place, let's start our e2e testing by typing the following command:
npm run protractorAnddddd, it doesn't work - of course it won't :) So what is the problem:
.... protractor e2e-tests/protractor.conf.js Starting selenium standalone server... Selenium standalone server started at http://10.0.0.5:36333/wd/hub /home/victor/git/angular-seed/node_modules/protractor/node_modules/selenium- webdriver/lib/webdriver/promise.js:1640 var result = fn(); ^ Error: Angular could not be found on the page http://localhost:8000/app/index.html : retries looking for angular exceededFrom looking at the log we see that the webdriver is up and running on port 36333 and Protractor tries to fetch the page from port 8000. Is this the problem? As we can see Protractor runs according to configuration file e2e-tests/protractor.conf.js. Let's have a look at it:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | exports.config = { allScriptsTimeout: 11000, specs: [ '*.js' ], capabilities: { 'browserName' : 'chrome' }, framework: 'jasmine' , jasmineNodeOpts: { defaultTimeoutInterval: 30000 } }; |
1 2 3 4 5 6 7 8 9 10 11 12 13 | describe( 'view1' , function () { beforeEach( function () { browser.get( 'index.html#/view1' ); }); it( 'should render view1 when user navigates to /view1' , function () { expect(element.all(by.css( '[ng-view] p' )).first() .getText()).toMatch(/partial for view 1/); }); }) |
Hope you found this article useful and would try to Protractor in your own projects. Next time we'll discuss Protractor usage with non AngularJS sites and also compare it to additional utility called CasperJS.
Comments
Post a Comment