AngularJS Protractor E2E - mock httpBackend with absolute URLs -
i have started using protractor e2e ui testing angularjs app.
the base url app looks -
http://localhost:8181/web-module/?username=admin
this url redirects security-module in actual application given username. security-module running on different port - 9191.
the redirected request looks -
http://localhost:9191/security-module/user/?username=admin
when try mock request using $httpbackend using -
describe('home page', function () { var ptor = protractor.getinstance(); var httpbackendmock = function () { var app = angular.module('httpbackendmock', ['ngmocke2e']); app.run(function ($httpbackend) { var current_user = {"username":"admin","password":null,"fullname":"system admin","email":"admin@example.com"}; $httpbackend.whenget('http://localhost:9191/security-module/user/?username=admin').respond(function(method, url, data, headers) { return [200, current_user, {}]; }); $httpbackend.whenget(/.*/).passthrough(); }) }; ptor.addmockmodule('httpbackendmock', httpbackendmock); it('home page', function () { ptor.get('http://localhost:8181/web-module/?username=admin'); var element = ptor.findelement(protractor.by.id('username')); expect(element.gettext()).toequal('system admin'); }); });
the call still tries hit actual http service/url rather mocked one.
i not sure doing wrong here. great.
i had same issue before. use regex instead in whenget() method make sure other parameter there , don't see it.
try:
$httpbackend .whenget(/^http:\/\/localhost:9191\/security-module\/user.*$/) .respond(function(method, url, data, headers) { return [200, current_user, {}]; });
you match urls start regex path. then, can update regex specify when there username=admin parameter. it's pain use regex $httpbackend service check parameters string.
to avoid writing each time:
/^http:\/\/localhost:9191
you can specify baseurl in protractor.conf.js:
baseurl: 'http://localhost:9191',
Comments
Post a Comment