Introduction
Protractor is a Node.js framework that sits on top of the Selenium / WebDriver APIs. It acts as an abstraction layer to make it easier to write and run end to end tests against AngularJS web applications.
It contains every feature that is available in the Selenium WebDriver. Additionally, Protractor provides some new locator strategies and functions which are very helpful to automate the AngularJS application. Examples include things like: waitForAngular, By.binding, By.repeater, By.textarea, By.model, WebElement.all, WebElement.evaluate, etc.
It is perfectly possible, but painful, to write end to end tests for an AngularJS site using only Selenium tools. The challenge lies in determining when AngularJS is actually done with a given action, such as a change of view – this is somewhat more difficult than is the case for straightforward old-school AJAX operations. So Selenium test scripts for AngularJS tend to bloat with wait commands and checks.
Salient features of the Protractor Automation tool:
· Allows running tests targeting remote addresses
· Can take advantage of Selenium grid to run multiple browsers at once
· Protractor works as a solution integrator – combining powerful tools and technologies such as NodeJS, Selenium, webDriver, Jasmine, Cucumber and Mocha.
· 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.
· Protractor allows tests to be organized based on Jasmine, thus allowing you to write both unit and functional tests on Jasmine.
· It runs on real browsers and headless browsers.
What is an End to End Test?
An end to end test runs against the front-end of a fully functional application. In the case of a web application, this implies driving a browser that loads pages, runs Javascript, interacts with the DOM, fills and submits forms, and so forth. The web application is served from a machine with a fully functional backend and suitably populated database. The setup is intended to mimic as closely as possible the live application, environment, and use cases.
You might compare this with unit testing, wherein a unit test runs against a small piece of functionality in isolation. Any data that might come from the rest of the application is mocked for the purposes of a unit test.
Installation
To install Protractor, node should be installed, if not please install from here.
npm install -g protractor
To verify your installation, please type in the command,
Protractor –version
If Protractor is installed successfully then the system will display the installed version. Otherwise you will have to recheck the installation
Configurations
Next step is to create a configuration file (conf.js) with some parameters which will be passed to Protractor to execute our spec files.
Following are contents,
SeleniumAddress: This allows you provide a URL to the Selenium Server that Protractor will use to execute tests. In this case Selenium Server must be previously started to be able to run tests on Protractor.
SeleniumServerJar: This allows you provide the file path of the SeleniumServer.jarfile. This parameter will be used by Protractor to control the Selenium life cycle. That way it is not necessary to start and stop the Selenium Server to run Protractor.
Specs: An array of test files can be sent through the specs parameter for Protractor to execute. The path of the test files must be relative to the config file.
seleniumArgs: This allows you to pass a parameter to Selenium if SeleniumServerJar is being used.
capabilities: Parameters also can be passed to the WebDriver using the capabilities parameter. It should contain the browser against which Protractor should run the tests.
baseURL: A default URL may be passed to Protractor through the baseURL parameter. That way all calls by Protractor to the browser will use that URL.
framework: This can be used to set the test framework and assertions that should be used by Protractor. There are 3 options currently for this parameter: Jasmine, Cucumber and Mocha.
allScriptsTimeout: To set up a timeout for each test executed on Protractor use this parameter with a number value in milliseconds.
All of the parameters are encapsulated in a node object which should be named “config”. That way Protractor will identify those parameters inside that object.
Below is an example of a Protractor configuration file named config.js
exports.config = {
// The address of a running selenium server.
seleniumAddress: ‘http://localhost:4444/wd/hub’,
specs: [‘homepage_test.js’, ‘project_test.js’],
capabilities: {
browserName: ‘chrome’,
version: ”,
platform: ‘ANY’
},
allScriptsTimeout: 22000,
// Options to be passed to Jasmine-node.
jasmineNodeOpts: {
showColors: true,
defaultTimeoutInterval: 30000,
isVerbose : true,
includeStackTrace : true
}
};
Execute Test case
Open the terminal and execute following command,
protractor /path/to/protractor.conf.js