Protractor Tutorials

Finding Elements in Protractor

To write end to end tests in any test automation, we first identify elements and then interact with them using locators.

We have discussed locators in Protractor using examples in previous article. Let us now understand ElementFinder and ElementArrayFinder with examples.

ElementFinder - element(locator)
The ElementFinder simply represents a single element of an ElementArrayFinder.

Capabilities and Options in Protractor

In your Protractor conf.js file, we define capabilities object and add information on which browser against Protractor should run the tests.

To run tests against the chrome browser we specify capabilities parameter 'browserName' with Chrome, To use other than Chrome, we simply set a different browser name like Firefox. Know more on DesiredCapabilities

How to check if an element is present with protractor?

Before performing an action with an element, we just need to make sure of the presence of the element on the web page. When we try to interact with an element, if it is not present, it will throw an error/exception, so it is always important checking for the presence of the element.

Example : - When we click a Submit button, we know that we have to wait a second or two for something to happen. Here before trying to test the response, we can always check for element/s that are expected.

Run protractor tests on multiple browsers in parallel

In order to run protractor tests on multiple browsers, Protractor offers multiCapabilities configuration option. These options should be defined as an array of objects.

As we know we can run protractor specs on different browsers by setting the browser name property of the capabilities in the conf.js file. We can execute on chrome browser by specifying browserName as 'chrome' and 'firefox' to execute on Firefox browser or whichever browser we need like below :-

Using Locators in Protractor

To write end to end tests in Protractor, we use locators to find elements on web page. Locators in protractor are similar to selenium webdriver. As we all know Protractor works on top of Selenium WebDriver, so it supports all the Selenium locators along with protractor locators. We will see multiple ways of finding elements in Protractor with Angular specific element locating strategies by.model(), by.binding(), by.repeater() etc in detail below.

Run multiple protractor specs in parallel

When you want to run protractor scripts by opening a browser instance for each test, you should add two capabilities shardTestFiles and maxInstances in the Capabilities block of the conf.js file. This will help to execute your scripts on same browser with multiple instances. If you want to execute on multiple browser, we have to use multipleCapabilities which we discussed in detail.

Protractor basic example program

Let us now create a very basic test and execute using protractor. As we know protractor needs to files spec file (test file) and conf file (configuration file).

In the below example, we are describing a test suite using the function describe() which is a global function in Jasmine which takes two parameters: a string and a function.

The string is a name or title of the spec suite which is being tested and the function contains all the code that will implement the suite.

Protractor Introduction

Protractor is a Node.js (Node.js is an open source, cross-platform runtime environment for server-side and networking applications) program that supports the Jasmine, Mocha, and Cucumber test frameworks.

Protractor is an open source functional automation framework (also known as End to End testing framework) especially designed to verify the health of AngularJS web applications. Protractor uses Selenium WebDriver to drive browsers and simulate user’s interaction with an AngularJs application running in a browser.