Protractor - waiting for async to finish - angularjs

In our application we are calling every minute to our feature flags service using angular therefore browser.waitForAngular() always fail.
is there a way to use browser.waitForAngular() and make it ignore this specific feature flag call (API call that is being called every 60 secs) so we can still use the wait for angular and not implement waits on the code?

There is an option to enable/disable waitForAngular which may be used to disable for pages where polling is happening and enabled again later for other pages..Check the below link
browser.waitForAngularEnabled

Related

Angular JS $http promise object -- How does it work?

All,
I'm a beginner to Angular framework and have been reading $http service. I thought I understood Promise object in Angular JS until the below questions popped up my mind. Can you please help me understand?
– When I make a REST call from Angular, Angular work not make the rest call before it executes the other steps in the js from where the REST API is invoked. This is because it’s single threaded. Okay I get this. So when does it execute the REST call? May be after all the instructions in the current JS are done? If yes,
Why do we even call it asynchronous?
Secondly, during the time it executes the REST API (let’s say the REST API results in an output not before 2 seconds), right after it calls REST API would Angular just wait for the 2 seconds without doing anything?
If $http service executes the REST API asynchronously as soon as it sees it,
Who spawns the second thread to execute the REST API? The framework? If so then are they making Javascript no longer be uni–threaded?
Secondly when the service returns while the main thread hasn’t even completed executing all the lines in my js, does Angular/jQuery goes and executes the instructions coded in the .then function, leaving the instructions in the main thread waiting?
Thanks much for your help!
Prem
When I make a REST call from Angular, Angular work not make the rest call before it executes the other steps in the js from where the REST API is invoked
No. That is incorrect. It sends the request immediately. But it doesn't block until the response comes back, because that could take a whole lot of time and completely freezes the application. So instead, it registers a callback, and this callback will be called, later, when the response is available. That's the principle of asynchronism.
Who spawns the second thread to execute the REST API
There is no second thread.
The easiest way to look at it is to consider that an HTTP response is an event, just like a click or a keypress. All the unique thread does is to wait for the next event and react to it, in a loop.
First of all, it wont block the execution, it sends the request immediately. Basically it follow the promise pattern which uses the concept of callback function. So whenever API returns the response it calls the callback function and resume the process.
There is no concept like second thread. When HTTP return the response the execution doesn't stop. it just call its callback method and after that excute the remaining line of code

Force protractor to wait for seed data to load

I have to load some seed data for my tests. I'm having a really hard time making sure the seed data has loaded completely before the tests begin running.
In the beforeAll block I'm calling an adapter I wrote for my API that clears out any data, loads a specified file of seed data and then runs a callback passed in from the protractor test file.
I can't include the test cases in a callback (this seems like it would be a similar blocking issue to refactoring to promises) or protractor doesn't recognize them.
Can anyone suggest a way I can make sure my API has been successfully seeded before the tests begin?
Thanks!
If you are using Jasmine 2.1 or higher with Protractor you can make use of the done() function in your beforeAll.
So if you have a function called seedMyDataAsync() that takes a callback function as a parameter, you could do something as simple as this:
beforeAll( function(done) {
seedMyDataAsync(done);
});
The done() function was introduced with Jasmine 2.0, but wasn't available for beforeAll() until Jasmine 2.1.
From the documentation:
Calls to beforeAll, afterAll, beforeEach, afterEach, and it can take an optional single argument that should be called when the async work is complete.
By default jasmine will wait for 5 seconds for an asynchronous spec to finish before causing a timeout failure. If the timeout expires before done is called, the current spec will be marked as failed and suite execution will continue as if done was called.

Error Timed out waiting for Protractor to synchronize with the page after 11 seconds

I have a problem with my Protractor tests. My angular app uses $timeout to continuously poll something from a server. Unfortunately this lead to the following error message when I'm running my tests:
There was a webdriver error: Error Timed out waiting for Protractor to synchronize with the page after 11 seconds. Please see https://github.com/angular/protrac
tor/blob/master/docs/faq.md
How can I handle such continuously polling timeouts? Setting the browser.ignoreSynchronization to false is not a good solution in my case (when I do that I have to insert a lot of browser.sleep())
Switching from $timeout to $interval as suggested here is currently not possible in my application. Increasing the timeout is also not possible (as I said the service is continuously polling something from the server)
Is there a possibility to change the waitForAngular routine so that my test won't timeout?
Any help will be highly appreciated.
From protractor's documentation:
Before performing any action, Protractor asks Angular to wait until
the page is synchronized. This means that all timeouts and http
requests are finished. If your application continuously polls $timeout
or $http, it will never be registered as completely loaded. You should
use the $interval service (interval.js) for anything that polls
continuously (introduced in Angular 1.2rc3).
Same boat with you.
We've seen a lot of posts discussing sleep(), waitForAngular()and browser.ignoreSynchronization.
I agree with you that it is not a good idea to set: ignoreSynchronization = true, since it forces us to wait on each async process manually.
Some suggested using sleep() and then waitForAngular() together like this:
browser.sleep(10000);
browser.waitForAngular();
I do not really understand why sometimes waitForAngular() fails to wait for the page synchronization to complete, and this "double waiting" code did work.
Please see if it works in your case.
add allScriptsTimeout: 50000 in protractor configuration file
exports.config = {
...
allScriptsTimeout: 50000,
...
};
or at least this solved my issue
Changing allScriptsTimeout to a greater value than 11000 thousand fixed my problem
Timed out waiting for Protractor ...
In node_modules/protractor/lib/configParser.js, changed:
allScriptsTimeout: 25000

With Protractor 1.0.0 rc2 + is the browser.waitForAngular() still required?

I was watching a youtube video and in that there was an example:
loginPage.userName.sendKeys ...
loginPage.password.sendKeys ...
loginPage.loginButton.click();
browser.waitForAngular();
expect( ...
I thought Protractor was coded in such a way that it would do the wait without the need for a browser.waitForAngular().
Can someone confirm if I need the waitForAngular() in this example?
I think that whether you need browser.waitForAngular() depends on the application you're testing.
Not 100% sure on what the conditions are, but I have found that I required this method when my test is performing a task that requires a response from a server, such as changing a user's password in a web app.
On another note, in my experience, browser.waitForAngular() is not robust enough to handle every type of wait condition I've needed to perform.
For example, I'm writing tests for a web application where the user logs in through a non-Angular page and is directed to an Angular page. If I want to make assertions to test the elements present on the login landing page, I can't use browser.waitForAngular() because non-angular pages do not have access to this method. I've found that a more reliable way to tell the driver "wait for the page to fully load, then check the text present on the page" is to use browser.wait to explicitly wait for the presence or visibility of an element on the next page that you know will take the longest to load.

Get the ExtJS Active Ajax call count

I am working in selenium to test the ExtJs application. My problem is, I need to ensure the page is completely rendered. I cant use selenium.waitForPageLoad. In normal Ajax application i can use "Ajax.activeRequestCount", which will give the Ajax call count. If it is '0' We can ensure the page is completly loaded. Is there any similar function available in ExtJs? Can any one pls help me on this
There is no such method in ExtJs. You can however create handlers for global Ext.Ajax events beforerequest and requestcomplete http://docs.sencha.com/ext-js/4-0/#!/api/Ext.Ajax-event-beforerequest and count active connections yourself.
Or, if the Selenium tests are to be run in modern browsers, you could try Progress Listeners (with these).
For Firefox, you could create a simple addon that would wait for async requests to complete, and ship that addon with your Selenium tests.

Resources