Over a million developers have joined DZone.

Setting Up Protractor, for Angular and Web Application Testing

DZone 's Guide to

Setting Up Protractor, for Angular and Web Application Testing

In this article, written in Q&A format, we'll go over how to create a Protractor automation tool, allowing you to automated AngularJS and Web application testing.

· Web Dev Zone ·
Free Resource

For the last two weeks, I have been busy studying the Protractor automation tool. During the process, several questions came to mind and as I researched and studied to uncover the mysteries of this framework, I decided to put the results together as a Q&A. This blog is the result! Consider this your well-informed Siri or Alexa guide to Protractor for beginners.

What is Protractor?

Answer: Protractor is an open source E2E testing automation framework, designed specifically for AngularJS web applications. The Protractor automation tool is a Node.js program built on top of WebDriverJS.       

Protractor works as a Solution integrator, combining powerful technologies like Node.js, Jasmine, Selenium, Mocha, Cucumber, and Web driver. Protractor is intended not only to test AngularJS applications but also for writing automation tests for any web applications. 

Why Do We Need Yet Another Framework? Why Protractor? 

Answer: AngularJS applications are Web Applications, which use the extended HTML's syntax to express web application components. It is mainly used for dynamic web applications. These applications use less, but more flexible code as compared to normal Web Applications.

Sometimes it is difficult to capture the web elements in AngularJS applications using JUnit or Selenium Web driver. Protractor supports Angular-specific locator strategies, which allows you to test Angular-specific elements without any setup effort on your part. 

Really? So What Can Protractor do That Selenium Can’t?

Answer: Well, AngularJS applications 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 those web elements using Selenium API. So, Protractor on top of Selenium can handle and controls those attributes in Web Applications.  

Protractor also speeds up your testing as it avoids the need for a lot of “sleeps” and “waits” in your tests, as it optimizes sleep and wait times.

So What Framework Does Protractor Use? 

Answer: Protractor supports two behavior-driven development (BDD) test frameworks out of the box: Jasmine and Mocha. These frameworks are based on JavaScript and Node.js and provide the syntax, scaffolding, and reporting tools you will use to write and manage your tests.                            

Cucumber is no longer included by default. You can integrate Cucumber with Protractor with the custom framework option. 

Okay, Can You Help Me Visualize the Workflow? 

Answer: Sure. Protractor needs two files to run, the test or spec file, and the configuration file. The spec file (test) is written using the syntax of the test framework you choose, such as Jasmine or Mocha along with Protractor API. The configuration file tells Protractor how to configure the testing environment - a Selenium server, which tests to run, browsers, and more.

 Image title


Thank You. Is it Easy to Setup/Install Protractor?

 Answer: Easy peasy. You may need 2-3 minutes.

Download Node.jssudo npm install –g protractorwebdriver-manager update.

Use npm to install protractor globally with:

              npm install –g protractor

This will install two command line tools: protractor and webdriver-manager. Try running it to make sure.

              protractor --version  

The webdriver-manager is a helper tool to easily get an instance of a Selenium Server running. Use it to download the necessary binaries with:

              webdriver-manager update

This will start up a Selenium Server and will output a bunch of info logs. Your Protractor test will send requests to this server to control a local browser. Leave this server running throughout the tutorial. You can see information about the status of the server at http://localhost:4444/wd/hu.

That was Easy. I am Excited to Write my First Protractor Test. Can You Help Me? 

Answer: Sure. Let's follow the steps below: 

Setup a config.js file .

Image title

2. Create a file example-spec.js using Jasmine and WebdriverJS.

Image title

3. Let’s run it.

First things first, open the terminal and start the webdriver server:

                                                  webdriver-manager start 

After that, you can run Protractor in another terminal by typing:

                                                    protractor config.js 

You should see a Chrome browser window open up and navigate to the AngularJS page enter Julie, then close itself (this should be very fast!). You should see “1 spec, 0 failures” in the terminal.  Your first test is successfully completed.

I Didn't Declare “browser” or “element” - Where Will it be Initialized?

Answer: That's right. Protractor exports these global variables to your spec file:                           

browser - A wrapper around an instance of WebDriver, used for navigation and page-wide information. The browser.get method loads a page. Protractor expects Angular to be present on a page, so it will throw an error if the page it is attempting to load does not contain the Angular library. (If you need to interact with a non-Angular page, you may access the wrapped webdriver instance directly with browser.driver).

element - A helper function for finding and interacting with DOM elements on the page you are testing. The element function searches for an element on the page. It requires one parameter, a locator strategy for locating the element. 

by - A collection of element locator strategies. For example, elements can be found by CSS selector, by ID, or by the attribute they are bound to with ng-model.

What are the Browsers Supported by Protractor? 

Answer: Protractor supports the two latest major versions of Chrome, Firefox, Safari, and IE.  In your Protractor config file (see config.js), all browser setup is done within the capabilities object. This object is passed directly to the WebDriver builder (builder.js).To use a browser other than Chrome, simply set a different browser name in the capabilities object.                             

capabilities: {
 'browserName': 'firefox'

Can I Run Parallel Tests Using Protractor?

Answer: Of course! If you would like to test against multiple browsers, use the multiCapabilities configuration option.

Again, you have to tweak the config.js file:     

multiCapabilities: [{
 'browserName': 'firefox'
}, {
 'browserName': 'chrome'

Protractor will run tests in parallel against each set of capabilities. Please note that if multiCapabilities is defined, the runner will ignore the capabilities configuration.

That concludes my brief Q&A on Protractor, based on my research. I hope it was helpful! Questions? Leave them in the comments!

protractor ,selenium ,angularjs

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}