SAP

Must Learn!

Big Data

Web

Protractor plays an important role in the Testing of AngularJS applications and works as a Solution integrator combining powerful technologies like Selenium, Jasmine, Web driver, etc. It is intended not only to test AngularJS application but also for writing automating regression tests for normal Web Applications as well.







In this tutorial, you will learn-

Why Do We Need Protractor Framework

JavaScript is used in only almost all web applications. As the applications grows, JavaScript also increases in size and complexity. In such case, it becomes difficult task for Testers to test the web application for various scenarios.

Sometimes it is difficult to capture the web elements in AngularJS applications using Junit or Selenium WebDriver.

Protractor is a NodeJS program which is written in JavaScript and runs with Node to identinfy the web elements in AngularJS applications, and it also uses WebDriver to control the browser with user actions.

Ok, fine now let's discuss what exactly is an AngularJS application?

AngularJS applications are Web Applications which uses extended HTML's syntax to express web application components. It is mainly used for dynamic web applications. These applications use less and flexible code compared with normal Web Applications.

Why can't we find Angular JS web elements using Normal Selenium Web driver?

Angular JS 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 code. So, Protractor on the top of Selenium can handle and controls those attributes in Web Applications.

The protractor is an end to end testing framework for Angular JS based applications. While most frameworks focus on conducting unit tests for Angular JS applications, Protractor focuses on testing the actual functionality of an application.

Before we start Protractor, we need to install the following:

  1. Selenium

    You can find the Selenium Installation steps in the following links, (http://www.guru99.com/installing-selenium-webdriver.html )

  2. NPM (Node.js)

    NodeJS Installation, we need to install NodeJS to install Protractor. You can find this installation steps in the following link. ( http://www.guru99.com/download-install-node-js.html )

Protractor Installation

Step 1) Open command prompt and type "npm install –g protractor" and hit Enter.

The above command will download the necessary files and install Protractor on the client system

Protractor Testing

Step 2) Check the installation and version using "Protractor --version." If successful it will show the version as like in below screenshot. If not, perform the step 1 again.

Protractor Testing

(Steps 3 and 4 are Optional but recommended for better practice)

Step 3) Update the Web driver manager. The web driver manager is used for running the tests against the angular web application in a specific browser. After Protractor is installed, the web driver manager needs to be updated to the latest version. This can be done by running the following command in the command prompt

webdriver-manager update

Protractor Testing

Step 4) Start the web driver manager. This step will run the web driver manager in the background and will listen to any tests which runs via protractor.

Once Protractor is used to run any test, the web driver will automatically load and run the test in the relevant browser. To start the web driver manager, the following command needs to be executed from the command prompt.

webdriver-manager start

Protractor Testing

Now, if you go to the following url (http://localhost:4444/wd/hub/static/resource/hub.html) in your browser, you will actually see the Web driver manager running in the background.

Protractor Testing

Sample AngularJS application testing using Protractor

Protractor needs 2 files to run, a spec file and configuration file.

  1. Configuration file: This File helps protractor to where the test files are placed (specs.js) and to talk with Selenium server (Selenium Address). Chrome is the default browser for Protractor.
  1. Spec file: This File contains the logic and locators to interact with the application.

Step 1) We have to login https://angularjs.org and enter the text as "GURU99" in "Enter a name here" textbox.

Protractor Testing

Step 2) In this step,

  1. Entered the name "Guru99"
  2. In output text " Hello Guru99" is seen.

Protractor Testing

Step 3) Now we have to capture the text from the webpage after entering the name and need to verify with the expected text.

Code:

We have to prepare configuration file (conf.js) and spec file (spec.js) as mentioned above.

Logic of spec.js :

describe('Enter GURU99 Name', function() {
 it('should add a Name as GURU99', function() {
 browser.get('https://angularjs.org');
 element(by.model('yourName')).sendKeys('GURU99');
  var guru= element(by.xpath('html/body/div[2]/div[1]/div[2]/div[2]/div/h3'));
expect(guru.getText()).toEqual('Hello GURU99!');
  });
});

Code Explanation of spec.js:

  1. describe('Enter GURU99 Name', function()

    The describe syntax is from the Jasmine framework. Here "describe" ('Enter GURU99 Name') typically defines components of an application, which can be a class or function etc. In the code suite called as "Enter GURU99," it's just a string and not a code.

  2. it('should add a Name as GURU99', function()
  3. browser.get('https://angularjs.org')

    As like in Selenium Webdriver browser.get will open a new browser instance with mentioned URL.

  4. element(by.model('yourName')).sendKeys('GURU99')

    Here we are finding the web element using the Model name as "yourName," which is the value of "ng-model" in the webpage. Check the screenshot below-

Protractor Testing

  1. var guru= element(by.xpath('html/body/div[2]/div[1]/div[2]/div[2]/div/h3'))

    Here we are finding the web element using xpath and store its value in a variable "guru".

  2. expect(guru.getText()).toEqual('Hello GURU99!')

    Finally we are verifying the text which we have got from the webpage (using gettext() ) with expected text .

Logic of conf.js:

exports.config = {
  seleniumAddress: 'http://localhost:4444/wd/hub',
  specs: ['spec.js']
};

Code Explanation of conf.js

  1. seleniumAddress: 'http://localhost:4444/wd/hub'

    The Configuration file tells Protractor the location of Selenium Address to talk with Selenium WebDriver.

  2. specs: ['spec.js']

    This line tells Protractor the location of test files spec.js

Execution of the Code

Here first, we will change the directory path or navigate to the folder where the confi.js and spec.js are placed in our system.

Follow the following step.

Step 1) Open the command prompt

Step 2) Make sure selenium web driver manager is up and running. For that give command as "webdriver-manager start" and hit Enter.

Protractor Testing

(If selenium web driver is not up and running we cannot proceed with test as Protractor cannot find the web driver to handle the web application)

Step 3) Open a new command prompt and give the command as "protractor conf.js" to run the configuration file.

Protractor Testing

Explanation:

  • Here Protractor will execute the configuration file with given spec file in it.
  • We can see the selenium server running at "http://localhost:4444/wd/hub"which we have given in the conf.js file.
  • Also, here can see the result how many are passed and failures like in above screenshot.

Fine, we have verified the result when it is passed or as expected. Now let us look into fail result also.

Step 1) Open and change expected result in spec.js to "'Hello change GURU99" like below.

After change in spec.js :


describe('Enter GURU99 Name', function() {
 it('should add a Name as GURU99', function() {
 browser.get('https://angularjs.org');
 element(by.model('yourName')).sendKeys('GURU99');
  var guru= element(by.xpath('html/body/div[2]/div[1]/div[2]/div[2]/div/h3'));
expect(guru.getText()).toEqual('Hello change GURU99!');
  });
});

Step 2) Save the spec.js file and repeat above steps of "Execution of the Code" section

Now, execute the above steps.

Result:

Protractor Testing

We can see the result as failed indicated with 'F' in the screenshot with the reason as "Expected 'Hello GURU99!' to equal 'Hello change GURU99!'. Also, it shows how many failure is encountered when executing code.

Can we achieve the same with Selenium web driver?

Sometimes we can identify the web elements of AngularJS applications using xpath or css selector from Selenium web driver. But in AngularJS applications, the elements will be generated and changed dynamically. So Protractor is the better practice to work with AngularJS applications.

Generate Reports using Jasmine Reporters

Protractor supports Jasmine reporters to generate test reports. In this section, we will use JunitXMLReporter to generate Test execution reports automatically in XML format.

Follow the below steps to generate reports in XML format.

Installation of Jasmine Reporter

There are two way you can do this, locally or globally

  1. Open command prompt execute the following command to install locally

npm install --save-dev jasmine-reporters@^2.0.0

Above command will install jasmine reports node-modules locally means from the directory where we are running the command in command prompt.

OR

  1. Open command prompt execute the following command for global installation

    npm install –g jasmine-reporters@^2.0.0

In this tutorial, we will install the jasmine reporters locally.

Step 1) Execute the command "npm install --save-dev jasmine-reporters@^2.0.0

" from the command prompt like below.

Protractor Testing

Step 2) Check the installation folders in the directory. " Node_modules" should be available if it is successfully installed like in below snapshot.

Protractor Testing

Step 3) Add the following colored code to existed conf.js file

exports.config = {
      seleniumAddress: 'http://localhost:4444/wd/hub',
      capabilities: {
          'browserName': 'firefox'
      },
      specs: ['spec.js'],
     framework: 'jasmine2' ,
      onPrepare: function() {
          var jasmineReporters = require('C:/Users/RE041943/Desktop/guru/node_modules/jasmine-reporters');
          jasmine.getEnv().addReporter(new jasmineReporters.JUnitXmlReporter(null, true, true)
          );
     }
   };

Explanation of code:

In code, we are generating the report "JUnitXmlReporter" and giving the Path where to store the report.

Step 4) Open the command prompt and execute command protractor conf.js.

Protractor Testing

Step 5) When you execute the above code, junitresults.xml will be generated in mentioned path.

Protractor Testing

Step 6) Open the XML and verify the result. The failure message is shown in the result file as our test case is failed. The Test case is failed as because of the Expected Result from "spec.js" is not matched with the Actual result from Web page

Protractor Testing

Step 7) Use the junitresult.xml file for evidences or result files.

Summary:

Though Selenium can do some of the things what protractor does, the protractor is the industrial standard and best practice to test AngularJS applications. Protractor can also manage multiple capabilities in it and handle the dynamic changes of web elements using ng-model, ng-click.., etc.. (Which selenium cannot do).

This article is contributed by Ranjith Kumar Enishetti