Protractor for Beginners

Protractor for Beginners


Protractor is an end to end test framework for Angular and AngularJS applications. It runs tests against your application in a browser such as Chrome and Firefox


Setting up

Setting up Protractor is straight forward. Pre-requisite

  • Install Chrome (recommended) or Firefox
  • Install Node.js
  • Install Protractor
npm i -g protractor
  • Install webdriver-manager
npm i -g webdriver-manager



Create a project folder. In the project folder input this in cmd or terminal. Enter all the way.

npm init


Setup Jasmine (Default reporter for Protractor)

npm i allure-commandline --save-dev
npm i jasmine-reporters --save-dev
npm i jasmine-allure-reporter --save-dev


Create this 2 folders in the project folders

allure-results

allure-report


Config.js and Main.js

Protractor needs two files to run : the test or spec file and the configuration files.

Create 2 files in your project folder - config.js and main.js


In the config.js input this

exports.config = {
  framework: 'jasmine',
    
  seleniumAddress: 'http://localhost:4444/wd/hub',
  specs: ['main.js'],


   
    onPrepare: function () {
        var AllureReporter = require('jasmine-allure-reporter');
        jasmine.getEnv().addReporter(new AllureReporter({
            
            resultsDir: 'allure-results'
        }));
        jasmine.getEnv().afterEach(function(done){
          browser.takeScreenshot().then(function (png) {
            allure.createAttachment('Screenshot', function () {
              return new Buffer.from(png, 'base64')
            }, 'image/png')();
            done();
          })
    });
  },
    
    capabilities: {
    'browserName': 'chrome',
    'chromeOptions': {
      args: ['--disable-browser-side-navigation'] 
        }   
    },
    


}


In main.js input this


describe ("Tutorial ", function (){
    
    it ("launch url and business " , async() => {
        
        browser.get("https://marketingplatform.google.com/about/enterprise/");
  
        browser.actions().mouseMove(element(by.css("a[data-g-action='for small businesses'][href='/intl/en_uk/about/small-business/']"))).perform();


        element (by.css ("a[data-g-action='for small businesses: analytics'][href='/intl/en_uk/about/analytics/']:nth-child(1)")).click()
         
    });


});


Execution

You need to setup the webdriver-manager once only.

webdriver-manager update

Before running the script , you need to start the webdriver-manager first

webdriver-manager start


To run the script

protractor config.js



Report

To view the report in Jasmine Allure , input this

allure serve allure-results



Page Object Model

You can convert the existing script to Page Object Model (POM). POM helps to reduce maintenance of scripts and improve readability.

Create a page object file - let's name it homePage.js. We will use launching the url as an example , instead of calling it directly.


var url = "https://marketingplatform.google.com/about/enterprise/";




var home = function () {




         this.launch = function() {

                browser.get (url);

        };

}




module.exports = home


In the main.js we can change the script to

let home = require ('./homePage.js');

  

var homePage = new home();




describe ("Tutorial ", function (){




        it ("launch url and business " , async() => {




                homePage.launch();




        });





});


We have not change much here - except to shift the browser.get to another file and convert it to a method. Then we call it by "home = require ('./homePage.js')




Let me know if you have any questions . I will be glad to answer you - Ivan


Created : 25 October 2019

To view or add a comment, sign in

More articles by Ivan T.

  • Short hiatus (1 month) from social media and its impact

    I am a huge fan of LinkedIn, Instagram, TikTok, YouTube, Reddit, a few e-commerce sites and other popular platforms. I…

    15 Comments
  • Solution Focused - Not Knowing Position

    Solution Focused Brief Therapy (SFBT) is a form of therapy that has gained popularity in recent decades. It is a brief…

    9 Comments
  • Text Counselling

    Why is counselling important for our mental health Counselling is important for mental wellness because it provides a…

    11 Comments
  • Loneliness

    We feel lonely from time to time. The feelings of loneliness are personal, and everyone's experience will differ.

    9 Comments
  • Struggling over popular festive season (Lunar New Year)

    Lunar New Year is this week. It is meant to be a time of joy with celebrations, red envelopes, gifts and qualify family…

    9 Comments
  • Passing data in React between Parent and Child in Functional Components

    For beginners who started out in ReactJS, passing data between components may be confusing. I struggle this when I…

    6 Comments
  • Getting your React project ready for Heroku

    If you are new to React JS and want to deploy to Heroku, these are few steps to ensure that you can deploy…

    3 Comments
  • Console Tricks

    Console is is a favourite feature for many web developers. If you have been using it, you know there are lots of tricks.

    12 Comments
  • Why And How We Should Learn To Say No

    Are you a people pleaser ? Do you find it difficult to say "No" or reject an invitation, task or someone's request ?…

    9 Comments
  • HTTP Status Code

    When we visit a (web) site, it usually send some request to the server. There will be a returned code to indicate the…

    24 Comments

Others also viewed

Explore content categories