How can I use Mock HTTP using Jasmine and Protractor?
In my test.spec.js, I declared a layout, but this layout does not work. I have no mistakes. My api always answers, not the layout.
I never see a 'mockModule!' in my console. My function is never executed:
browser.addMockModule('modName', function() { browser.executeScript(function() {console.log('mockModule!')}); angular.module('modName', []).value('foo', 'bar').run(function ($httpBackend) { $httpBackend.whenPOST('http://api.webapp.net/app_dev.php/module/search?direction=asc&page=1').respond('repsond'); browser.executeScript(function() {console.log('enter mockModule!')}); }); });
In my app.js I don't have "ngMock".
I added this to my index.html:
node_modules/angular-mocks/angular-mocks.js
I run the test from the command line using 'gulp protractor-local':
gulp.task('protractor-local', shell.task([ 'protractor protractor.conf.js --baseUrl="http://mywebapp.local.net"' ]));
All tests are OK, but not the layout.
test.spec.js
var loginPO = new(require('./models/login.model.js'))(); describe("hello", function() { it("I click on the button-search button", function() { loginPO.wait(10); //browser.ignoreSynchronization = false; browser.addMockModule('modName', function() { browser.executeScript(function() {console.log('mockModule!')}); angular.module('modName', []).value('foo', 'bar').run(function ($httpBackend) { $httpBackend.whenPOST('http://api.webapp.net/app_dev.php/module/search?direction=asc&page=1').respond('repsond'); browser.executeScript(function() {console.log('enter mockModule!')}); }); }); //browser.getRegisteredMockModules(); loginPO.clickButtonSearchButton(); loginPO.wait(10); }); it("I am on the home page", function() { loginPO.visit('#/'); }); ... });
models /login.model.js
'use strict'; var _ = require('lodash'); var LoginPageObject = function() { var signInButton = element(by.id('signIn')); _.mixin(this, require('./common/base.js').Base); this.path = '#/'; this.clickButtonSearchButton = function() { return browser.driver.findElement(by.id('button-search')).click(); }; ... }; module.exports = LoginPageObject;
generic /base.js
function visit(path, params) { return browser.get(this.path + (params || '')); } function wait(params) { params = params * 1000; return browser.sleep(params); } ... exports.Base = { visit: visit, wait: wait, ... };
protractor.config.js
exports.config = { directConnect: true, seleniumServerJar: 'node_modules/selenium-server/lib/runner/selenium-server-standalone-2.48.2.jar', specs: [ 'jasmine/*.spec.js' ], getPageTimeout: 30000, capabilities: { 'browserName': 'chrome', version: '', platform: 'ANY' }, framework: 'jasmine2' };
** karma.conf.js **
// Karma configuration module.exports = function(config) { var configuration = { basePath: './', frameworks: [ 'jasmine-jquery', 'jasmine', 'jasmine-matchers' ], files: [ ... 'assets/libs/angularjs/angular.min.js', 'node_modules/angular-mocks/angular-mocks.js', ... 'assets/libs/angularjs/sweetalert.min.js', 'assets/libs/angularjs/ui-bootstrap-tpls.min.js', 'app/app.js', 'app/*/*.js', 'app/*/*/*.js', 'app/*/*/*/*.js', { pattern: 'app/*/*/*/*/*.json', included: false } ], exclude: [], preprocessors: { 'app/**/!(*.test).js': ['coverage'] }, coverageReporter: { type: 'text', dir: 'coverage/' }, reporters: ['spec'], port: 8080, colors: true, logLevel: config.LOG_INFO, autoWatch: false, browsers: ['PhantomJS'], singleRun: true, customLaunchers: { 'PhantomJS_custom': { base: 'PhantomJS', options: { windowName: 'my-window', settings: { webSecurityEnabled: false } }, flags: ['--load-images=true'], debug: true } }, phantomjsLauncher: { exitOnResourceError: true } }; config.set(configuration); };
package.json
{ "name": "webapp", "version": "1.0.0", "description": "webapp", "dependencies": { "angular-mocks": "^1.4.5", "chromedriver": "^2.19.0", "gulp-protractor": "^1.0.0", "gulp-shell": "^0.5.0", ... "protractor": "^2.5.1", "selenium-server": "^2.48.2", "selenium-standalone": "^4.7.0", "selenium-webdriver": "^2.48.0", } }