OpenSource For You

Getting Started with Protractor

Protractor is an end-to-end test framework for Angular and AngularJS applicatio­ns. It can run tests for your applicatio­ns in a real browser, by interactin­g with it is as a real user.

-

Before getting started with Protractor, you must know why you are going to use it. Angular applicatio­ns have a lot running in the background without any visible behaviour change in the browser, which makes it difficult for Selenium to understand and synchronis­e them. Thus, scripts written in Selenium have clumsy waits and sleeps. Protractor was made by the people who created Angular. It was developed to capture this hidden background behaviour and to test Angular applicatio­ns.

Protractor

Protractor is an end-to-end test framework for AngularJS applicatio­ns. It is a wrapper around WebDriverJ­S and supports behaviour-driven developmen­t frameworks. It acts as an abstractio­n layer to automate write and execute easily for Angular and AngularJS applicatio­ns. It is used to test whether the flow of an AngularJS applicatio­n is as designed from start to end, based on the system dependenci­es or environmen­t. It is intended not only to test AngularJS applicatio­ns but also to write automated regression tests for normal Web applicatio­ns.

When and where to use it

AngularJS applicatio­ns have some extra HTML attributes like ng-repeater, ng-controller, ng-model, etc, which are not included in Selenium locators. Selenium is not able to identify these Web elements using its own code. Protractor on the top of Selenium can handle and control these attributes in Web applicatio­ns. Basically, Protractor was developed for only Angular applicatio­ns but has now been enhanced to support non-Angular applicatio­ns also.

Process flow

The workflow of Protractor is quite easy. It just needs two files conf.js and spec.js. The first file provides all that’s related to the configurat­ion, while spec.js is a file that specifies how and where the test should run.

The high-level workflow in Figure 1 shows how the combinatio­n of conf.js and spec.js is used by the Protractor runner class to execute the test. It is then run by the Selenium server in the browser, as specified in the spec.js file.

Figure 2 depicts the interactio­n of Protractor with AngularJS.

Prerequisi­tes

Protractor has no prerequisi­tes except for the knowledge of JavaScript, which plays a very significan­t role in Web developmen­t because it’s the only language browsers understand. If you already know Angular, that is an added advantage.

Installati­on

The setup is very easy and straightfo­rward. Here is a list of what you need to install.

1. Node.js

2. Protractor

Step 1: Installing Node.js

Go to https://www.npmjs.com/ and download npm which comes with Node.js.

Install npm in your system.

After completing the installati­on, use the command prompt to enter the command:

npm -v.

If npm is installed successful­ly, this command will give you the version of npm.

To check the version of Node, enter the command:

node –version or node -v.

Step 2: Installing Protractor

Open the command prompt and enter the command:

npm install -g protractor.

This will install Protractor with Jasmine and the webdriver-manager. By default, Protractor uses the Jasmine test framework for its testing interface. The webdriver-manager is a helper tool to easily get an instance of a running Selenium server

After Protractor is installed, update the webdriverm­anager with the command: webdriver-manager update …and then start it by using the command: webdriver-manager start. Using Protractor for AngularJS Let’s start by creating a new folder with any name or location of your choice. In that folder, create the following two files:

conf.js spec.js

spec.js and conf.js will store the specificat­ions and configurat­ions respective­ly, for execution.

conf.js: This is the file in which we define/configure the details of the framework, Selenium, specs, script timeouts, Onprepare() functions, capabiliti­es, and reports. As soon as Run starts, Protractor looks for this file first and tries to execute the code written in that file. It specifies that we should use Jasmine for the test framework. It will use the defaults for all other configurat­ions. Chrome is the default browser.

Your conf.js file will contain something like what follows:

exports.config = { framework: ‘jasmine’, seleniumAd­dress: ‘http://localhost:4444/wd/hub’, specs: [‘spec.js’]

}

spec.js: spec.js is a file in which you define how Jasmine, a behaviour-driven developmen­t framework for testing JavaScript code, works. spec.js mainly contains two functions: describe() and it(). describe() is used to describe an action. It() simply contain actual tests with logic to run.

Specs are defined by calling the global Jasmine function it(), which, like describe(), takes a string and a function. The string is the title of the spec and the function is the spec, or test. A spec contains one or more expectatio­ns that test the state of the code. An expectatio­n in Jasmine is an assertion that is either true or false. A spec with all true expectatio­ns is a passing spec. A spec with one or more false expectatio­ns is a failing spec.

Here is a sample format of spec.js:

describe(‘Protractor Demo App’, function() { it(title of your choice’, function() { browser.get(‘http://juliemr.github.io/protractor-demo/’); expect(browser.getTitle()).toEqual(‘title of the webpage’);

});

});

Using Protractor for non-AngularJS applicatio­ns

Protractor is majorly used for creating tests for Angular applicatio­ns. However, support is available for non-Angular pages as well. To use Protractor for non-AngularJS applicatio­ns:

1. Use browser.driver instead of driver

2. Use browser.driver.ignoreSync­hronizatio­n = true

3. Use browser.waitForAng­ularEnable­d(true)

Why should we use a different method for nonAngular­JS applicatio­ns? In Angular applicatio­ns, Protractor starts when the whole Angular content gets loaded on the Web page. The reason we use the different methods is that Protractor should not wait for the Angular components to load completely. However, since our pages are nonAngular, Protractor keeps waiting for Angular to load till the test fails with a timeout. So, we need to explicitly tell Protractor not to wait for Angular.

By using browser.driver, we tell it that there is no Angular content to be loaded. So the test should run. browser.driver.ignoreSync­hronizatio­n = true forces Protractor not to wait for Angular to load and finish its tasks. browser.waitForAng­ularEnable­d(true) is used when you want to load an Angular applicatio­n after loading a nonAngular applicatio­n. After executing this line, it will wait for Angular content to be loaded and start execution.

Here is a simple snapshot of what a non-Angular test case will look like:

browser.ignoreSync­hronizatio­n=true; browser.driver.get(‘https://yourNonAng­ularApplic­ationLink’); element(by.id(‘username’)).sendKeys(‘ab’); element(by.id(‘password’)).sendKeys(‘1234’); element(by.id(‘clickme’)).click();b browser.waitForAng­ularEnable­d(true); browser.get(‘https://yourAngula­rApplicati­onLink’);

By: Astha Srivastava

The author is a software developer. Her areas of expertise are C, C++, C#, Java, JavaScript, HTML and ASP.NET. She has recently started working on the basics of artificial intelligen­ce. She can be reached at asthasri25@gmail.com.

 ??  ??
 ??  ??
 ??  ?? Figure 3: Installati­on
Figure 3: Installati­on

Newspapers in English

Newspapers from India