Writing tests Synchronous style with Protractor

Protractor is an end-to-end testing framework which can be used in Angular applications. It’s build on top of the NodeJS and WebDirverJS. It’s simple you can just write tests in a synchronous style without waiting on each operation to get executed. Protractor has that capability within itself. Let’s look at how it works.

Usual way

This is how code will look like if operations are written in an asynchronous style. This requires promise chaining since there are sequence of asynchronous operations to be done one after another.

it( 'verify the search time' , () => {
browser.driver.manage().window().maximize()
.then(() => browser.driver.get( ‘http://creately.com’ ))
.then(() => browser.driver.getDeviceTime())
.then( time => {
console.log( time );
});
});

How Protractor Works

Protractor simplifies this since it has built-in support for Angular page load and actions. When an asynchronous operation is invoked it’s added to a queue where it waits until the previous asynchronous operation gets executed. So that it brings the capability of writing asynchronous operations in synchronous way.

it( ‘verify the search on google’, () => {
browser.driver.manage().window().maximize();
browser.driver.get( ‘http://creately.com’ );
browser.driver.getDeviceTime()
.then(time => {
console.log( time );
});
});

So you simply forget about waiting on asynchronous operations and Protractor takes care of it for you!