Reputation: 916
I have just started using Protractor to do E2E UI testing for an angularJS app.
The base url for the app looks something like -
http://localhost:8181/web-module/?username=admin
This url then redirects to the security-module in the actual application for the given username. The security-module is running on a different port - 9191.
The redirected request looks like -
http://localhost:9191/security-module/user/?username=admin
When I try to mock this 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":"[email protected]"};
$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 GET call still tries to hit the actual http service/url rather than the mocked one.
I am not sure what I am doing wrong here. Any help would be great.
Upvotes: 1
Views: 1794
Reputation: 2546
I had the same issue before. Use regex instead in your whenGET() method to make sure that any other parameter is there and you don't see it.
Try:
$httpBackend
.whenGET(/^http:\/\/localhost:9191\/security-module\/user.*$/)
.respond(function(method, url, data, headers) {
return [200, current_user, {}];
});
You will match all the urls which start with the regex path. Then, you can update your regex to specify only when there is username=admin as parameter. It's a pain to use regex with $httpBackend but this service only check parameters as a string.
To avoid writing each time:
/^http:\/\/localhost:9191
You can specify the baseUrl in your protractor.conf.js:
baseUrl: 'http://localhost:9191',
Upvotes: 2
Reputation: 783
Where the redirection is performed? I think, that it should look like:
$httpBackend.whenGET('http://localhost:8181/web-module/?username=admin')
.respond(function(method, url, data, headers) {
return [200, current_user, {}];
});
because the application doesn't know about the redirection if it is done on the server side.
Upvotes: 0