Reputation: 27455
I have an Angular web app that continuously listens for notifications from the backend via long-poll:
scope.notification = $resource('/notification').get();
This request never completes in the test environment. This is a problem for Protractor, because it wants to wait for all outstanding HTTP requests to finish.
I see a number of potential solutions, but have some issues with all of them.
I do not see a way to tell Protractor to ignore this request. Issuing it from $interval
(which itself is ignored) is not a solution. Protractor will not wait for the request to be sent, but once it's sent, it will still wait for it to complete.
So I'm trying not to send this request when running the tests. But how do I know I'm in a test?
?protractor=true
). But my web app modifies the URL all the time, so it would get cleared quickly.browser.executeScript('window.runningProtractorTests = true;');
But it seems Protractor will wait for the page to sync first before running executeScript
. And I'm sending the request right away when the page loads.Protractor
, for example) out of fear that my libraries may rely on interpreting the user agent string, so I'd have to figure out the right string.Is there no straightforward way to do this?
Upvotes: 3
Views: 918
Reputation: 12108
Can you put the calls to $resource
into a service which you mock out using Protractor's browser.addMockModule()
? That way, Protractor will always override your original service before it has a chance to run.
This could look something like
// in your application
myModule.service('myNotificationService', function($resource) {
this.notification = $resource('/notification').get();
});
// In your Protractor test
browser.addMockModule('notificatonOverride', function() {
angular.module('notificationOverride').service('myNotificationService', function() {
this.notification = {}; // or whatever you need here.
});
});
Upvotes: 4
Reputation: 8900
Try to load the view with browser.driver.get('http://myapp')
. Calling the webdriver api directly will not wait for angular to be ready.
Then you can run the script.
browser.driver.executeScript('window.runningProtractorTests = true;');
And then continue using the protractor api.
You may have to add a browser.waitForAngular() to make sure that angular is ready before proceeding with your test.
Upvotes: 1