Javascript Automated Testing Private Cheetsheet
Javascript Automated Testing Private Cheetsheet
JS Testing Cheatsheet
Protactor (E2E)
- Configuration:
To test if installation work, protractor provide us with default config [conf.js] file that can run test without have to run selenium
server. it is in ./node_modules/protractor/example. To run it go to example directory then run:
../bin/protractor.js conf.js
protractor need server to run (can be express, apache etc.). For testing or learning purpose we can use node http-server, to
install it:
npm install http-server -g
to run it:
http-server -p 8080
Important config: (for starter you only need specs [it will run just ok]), but it nice to have baseUrl, capabilities, framework
- Syntax:
Pointing browser to an url: browser.get(/)
Get current url of browser: (will return a promise) browser.getCurrentUrl().then(function(url) {});
Tell Browser to Sleep: browser.driver.sleep(500)
Element finder (single): element(locator)
Element finder (multiple/array): element.all(locator)
Selecting by CSS selector: element(by.css(.abc)), also have a short version like this $(.abc)
Selecting by CSS selector (Multiple): element.all(by.css(.abc)),
also have a short version like this $$(.abc)
by Antoni
Selecting by binding:
element(by.binding(comment.like))
browser.ignoreSynchronization = true;
careful when using this command, use browser.sleep(), to manage the timming.
Karma (Unit)
- Install locally:
npm install karma --save-dev
npm install karma-jasmine karma-chrome-launcher --save-dev
- To run it:
./node_module/karma/bin/karma start
- For karma configuration, need to include angular and angular-mocks path to the files:
2
by Antoni
files: [
'bower_components/angular/angular.js',
'bower_components/angular-mocks/angular-mocks.js',
'spec/unit/**/*.js'
],
If we are using the popular Controller As syntax, we are not testing the scope we test the controller it self:
describe("description", function () {
var ctrl = {};
beforeEach(function () {
module('anyModule');
inject(function($controller) {
ctrl = $controller(ControllerName);
});
});
});
Mocking a service locally: (let say we have service named ItemService that need to be mock)
describe("description", function () {
var ctrl = {};
beforeEach(function () {
by Antoni
});
Mocking a service globaly: we need to make new angular module that will overwrite current service on testing.
mock file ( also live is testing folder):
angular.module('notesApp1Mocks', [])
.factory('ItemService', [function() {
return {
list: function() {return [{id: 1, label: 'Mock'}];}
};
}]);
than we do this on test spec:
describe('ItemCtrl With global mock', function() {
var ctrl;
beforeEach(module('notesApp1'));
beforeEach(module('notesApp1Mocks'));
beforeEach(inject(function($controller) {
ctrl = $controller('ItemCtrl');
}));
Use Jasmine Spy, if we didnt want to implement an entire mocked-out service, if we just wanted to know in the case of
ItemService whether or not the list function was called, and not worry about the actual value from it.
describe('ItemCtrl with SpyReturn', function() {
beforeEach(module('notesApp1'));
var ctrl, itemService;
beforeEach(inject(function($controller, ItemService) {
spyOn(ItemService, 'list').and.returnValue([{id: 1, label: 'Mock'}]);
itemService = ItemService;
4
by Antoni
afterEach(function() {
// Ensure that all expects set on the $httpBackend
// were actually called
5
by Antoni
});
- aaa
Nb: Karma conf file not taking capital letter very well (use down case for first letter when naming the file).
Subject
- xx