Front End Angularjs is a popular framework for learning E2E test reports and e2e JS coverage reports based on the Angularjs Nodejs platform. The use of the existing tools, but some configuration of the place to be aware.
Environmental Prerequisites:
1. Nodejs Installation (https://nodejs.org/en/download/)
Steps:
1. NPM Init creates a Nodejs project.
2. Download node modules using the following NPM Install command, and then add the Start command to the Scripts node of Package.json as follows:
NPM Install angular-dnpm Install angular-mocks-dnpm install grunt-contrib-clean-dnpm install GRUNT-CONTRIB-COPY-DNPM I Nstall grunt-protractor-coverage-dnpm Install http-server-dnpm install protractor-jasmine2-html-reporter-d "Scripts" : { "start": "Http-server-a localhost-p 8000-c-1",}
After downloading the grunt-protractor-coverage,
2.1 Execute NPM Install download dependent modules under project directory node_modules\grunt-protractor-coverage, otherwise protractor modules error will be reported when executing the test
2.2 Perform Webdriver-manager update command under project directory node_modules\grunt-protractor-coverage\node_modules\.bin\ download Seleniu server Jar and chrome driver, or you will not be able to find the server jar error when executing the test, if you cannot download it, try adding the agent as follows
Webdriver-manager Update--ignore_ssl--proxy http://username:[email protected]:p ORT
2.3 Since Grunt-protractor-coverage has not been updated for a long time, the current protractor is 3.0.0 version, the built-in Chromedriver version is 2.21, if you need to support the latest chrome, Need to change the Config.json file under the Node_modules\grunt-protractor-coverage\node_modules\protractor directory, currently the latest version of Chromedriver is 2.30, After the change is complete, update chromedriver in the Execute webdriver-manager Update command.
--Note: Since the selenium server jar is 2.52.0, there is a problem with the new version of Firefox support, but changing the server jar to the latest introduces a new issue because protractor The 3.0.0 version comes with a selenium-webdriver in support of the new version of the server jar, so it is expected to be able to directly change the protractor that Grunt-protractor-coverage relies on as the latest version ( Pending experiment)
3. Create a Angularjs-based demo as a test site, just for testing purposes, not too complicated.
New Js/index.js
varAngular =Window.angularvarApp = Angular.module (' app ', []); App.controller (' Indexctrl ',function($scope) {$scope. Add=function(A, b) {if(a&&b) {returnNumber (a) +Number (b)}return0; }, $scope. Minus=function(c, D) {if(c&&d) {returnNumber (c)-Number (d)}return0; }});
New html/index.html
<!DOCTYPE HTML><HTMLLang= "en"Ng-app= "App"><Head> <MetaCharSet= "UTF-8"> <title>Index</title></Head><Body><DivNg-controller= "Indexctrl"> <Div> <inputtype= "text"Ng-model= "a"value= "0"> + <inputtype= "text"Ng-model= "B"value= "0"> =<spanID= ' result '>{{Add (b)}}</span> </Div> <Div> <inputtype= "text"Ng-model= "C"value= "0"> - <inputtype= "text"Ng-model= "D"value= "0"> =<spanID= ' RESULT1 '>{{minus (c,d)}}</span> </Div></Div></Body></HTML><Scriptsrc=".. /node_modules/angular/angular.min.js "></Script><Scriptsrc=".. /node_modules/angular-mocks/angular-mocks.js "></Script><Scriptsrc=".. /js/index.js "></Script>
New E2E test code e2etest/index-e2etest.js. Note that the site path that the test fills in must be the project path using the injected JS, otherwise the obtained coverage report is empty.
Describe (' index.html ',function() {Beforeeach (function() {Browser.get (' Http://localhost:8000/instrumented/html/index.html '); }); It (' Test add ',function() { varA = Element (By.model (' a '))); varb = Element (By.model (' B '))); A.sendkeys (1); B.sendkeys (2); varresult = Element (by.id (' result '))); Expect (Result.gettext ()). Toequal (' 3 '); }); It (' Test minus ',function() { varA = Element (By.model (' C '))); varb = Element (By.model (' d ')); A.sendkeys (4); B.sendkeys (2); varresult = Element (By.id (' RESULT1 '))); Expect (Result.gettext ()). Toequal (' 1 '); });});
4. Configure the Protractor configuration file and the Gurnt configuration file as follows:
Create a new protractor.conf.js in the root directory, add protractor-jasmine2-html-reporter for the e2e test to produce an HTML report
var jasmine2htmlreporter = require (' Protractor-jasmine2-html-reporter ');
Exports.config ={allscriptstimeout:11000, BaseUrl:' Http://localhost:8000/html/', //Capabilities to is passed to the webdriver instance.capabilities: {' Browsername ': ' Chrome '}, Framework:' Jasmine ', //Spec Patterns is relative to the configuration file location passed //To protractor (on this example conf.js). //They may include glob patterns.Specs: [' e2etest/*.js '], //Options to is passed to Jasmine-node.jasminenodeopts: {showcolors:true,//Use colors with the command line report .}, Defaulttimeoutinterval:30000, Onprepare:function() {jasmine.getenv (). Addreporter (NewJasmine2htmlreporter ({savepath:' E2EREPORT/E2E ', Screenshotsfolder:' Images ', Takescreenshots:false }) ); }};
The most important is the Gruntfile.js file, created in the root directory, as follows:
Note: The copy node only needs to copy the files that the project itself does not need to be injected such as: html/css/... Files, depending on the JS library files, etc., here only need to copy the HTML directory files, dependent on the Angularjs and angularjs mock files.
The instrument node needs to configure the files to be injected, that is, the JS file to be tested, and inject the copy into the BasePath directory.
The protractor_coverage node configuration coverage information file holds the location Coveragedir, and the configuration file configfile that executes the e2e.
The source location of the Makereport node configuration coverage information file is SRC, and the resulting HTML includes the storage location dir
Module.exports =function(grunt) {//Project configuration.Grunt.initconfig ({Pkg:grunt.file.readJSON (' Package.json '), clean: {options: {force:true}, tests: [' Instrumented '],}, copy: {instrument: {files: [{src: [' html/**/* ', ' node_modules/angular/angular.min.js ', ' node_modules/angular-mocks/angular-mocks.js '], dest:' Instrumented/'}]}, the Connect: {options: {port:9000, hostname:' localhost '}, Runtime: {options: {base:' Instrumented '}}} , instrument: {files: [' Js/**/*.js '], Options: {lazy:true, BasePath:"Instrumented"}}, Protractor_coverage: {options: {keepAlive :true, Nocolor:false, Coveragedir:' Instrumented/coverage ', args: {baseUrl:' http://localhost:8000 '}}, Local: {options: {configfile:' Protractor.conf.js '}}} , Makereport: {src:' Instrumented/coverage/*.json ', Options: {type:' Lcov ', dir:' Instrumented/coverage ', Print:' Detail ' } } }); Grunt.loadnpmtasks (' Grunt-protractor-coverage '); Grunt.loadnpmtasks (' Grunt-contrib-connect '); Grunt.loadnpmtasks (' Grunt-contrib-clean '); Grunt.loadnpmtasks (' Grunt-contrib-copy '); Grunt.loadnpmtasks (' Grunt-istanbul '); Grunt.registertask (' E2ecoverage ', ' Run E2E coverage report ', [' Clean ', ' copy ', ' instrument ', ' connect:runtime ', ' protractor_coverage:local ', ' Makereport ']);};
The above configuration is complete, the overall directory structure is as follows:
E2edemo // for storing reports | |- e2etest| index-e2etest.js| | HTML|- index.html // used to store the project code after being injected |- JS |- index.js|- Node_ ModulesGurntfile.jspackage.jsonprotractor.conf.js
The final result
The \e2edemo\instrumented\coverage\lcov-report directory has a index.html report: You can modify the index-e2etest.js to see if coverage is insufficient.
The test report for E2E itself is in \e2edemo\e2ereport\e2e, as follows: The test report here intentionally fail in the test script
Note: How to persist JS file is injected successfully, look at the JS file source code will find a lot more strange words such as __COV_KBVE4U4YLX8FHTZHDGWWXA is injected successfully.
Angularjs e2e Test Report/coveragereport