Promises in Protractor

A Promises object represents a value that may not be available yet, but will be resolved at some point in the future

A promise is an object returned by a module, library, function, that will be resolved or rejected some time in the future.

That's the point: an object that will be resolved in the future. You can then use additional functions that will be executed once that promise is resolved (so, not now with your current code).

You can think of promises as helpful abstraction on top of some operation that cannot be performed synchronously, or would be more performant to be executed asynchronously.

Promises are, as their name implies, a promise that you will have a return value, though it might not be immediate.

It gives you a .then method, to which you can pass your callbacks, or functions that will be executed once the promise is resolved/rejected.


										import { browser, element, by} from 'protractor'
										import { protractor } from 'protractor/built/ptor';
										describe('Protractor Typescript Demo', function() {
											browser.ignoreSynchronization = true;
											it('click operation', function() {
												browser.get('https://google.com/');
												browser.sleep(1000)
												element(by.name('q')).getAttribute("title").then(function(tooltip){
													expect(tooltip).toBe("Search", "tooltip is not 'Search'")
												})
											});
										});			
										

You can pass many callbacks (other functions) to it's .then() method. Each of those will be executed when Google responds to that query.


promise.then(function(response) {
        console.log(response.body);
    });			


Repeat that (or pass another functions) as many times as you need. You can even pass the `promise` object to some other function or module which will also be able to attach listeners.

Once the promise is resolved (ie. Google responds to that query), all those callbacks get executed, and they get the resolved value as a parameter.

There's also a chance that the promise will be rejected, usually when there's an error or the input to your module is wrong. You can pass a callback for that case too, as a second parameter to the .then() method.


promise.then(null, function(error) {
        console.log('There has been an error in the request: ', err);
    });			


Or pass both success and error handlers together:


promise.then(someFunction, someOtherFunction);		


There's a chance that your promise is never going to be resolved or rejected. In that case, your handlers will never be called. You might want to cover this case with some timeout or a similar function. Some modules, like request, have a built-in timeouts.


promise.then(someFunction, someOtherFunction);		


The core idea behind promises is that a promise represents the result of an asynchronous operation. A promise is in one of three different states:

  • pending - The initial state of a promise.
  • fulfilled - The state of a promise representing a successful operation.
  • rejected - The state of a promise representing a failed operation.
  • Using promises is more of a design pattern choice.
  • For simple projects it is not needed (unless the project intends to be used in other large-codebase projects.)
  • Mixing promise pattern and callbacks within same project could end up being confusing for other developers to read or contribute to the code

Protractor Interview Questions

About Author

Myself KarthiQ, I am the author of this blog, I know ways to write a good article but some how I donot have the skills to make it to reach people, would you like help me to reach more people By sharing this Article in the social media.

Share this Article Facebook
Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions
Copyright © CherCher Tech