You are on page 1of 12

Automation By Jasmine and

Protractor
Jasmine
It is a behavior driven development framework for testing javascript.

It does not depend upon any other javascript frameworks and DOM

It has clean syntax to write unit tests easily

It is a stand-alone distribution which can be downloaded and integrate in the


application

It consists of Suites and Specs


Jasmine - Suites
Jasmine Suite is used to describe the test case.

It can be initiated by calling global Jasmine function ‘describe’.

Syntax:
describe(‘name of your test suite’, function(){
//Collection of Specs
})

Suites also contain beforeEach and afterEach

Suites can be nestable and can be disabled

Example:
xdescribe(‘name of disabled suite’, function() {})
Jasmine - Specs
Jasmine Spec is used to test a specific block using assertions

It can be initiated by calling global Jasmine function ‘it’.

Syntax:

it(‘name of spec’, function(){


expect().toBe();
})

String: which denotes the name of your test spec


Function: which consists of assertions

Specs are not nestable but can be disabled


Example:
xit(‘name of disabled spec’, function() {})
Jasmine - Assertions
Jasmine assertions are expectations.

Assertions are implemented by keyword ‘expect’

Syntax:

expect(true).toBe(true);

Negetive assertions can be implemented by using ‘not’

Example:

Expect(true).not.tobe(false);
Jasmine – Assertions Types
toBe()

toBeDefined()

toEqual()

toBeTruthy()

toContain()

toMatch()

toBeLessThan()

toThrow()
Protractor
It is an end-to-end testing framework for angular apps. It focuses on testing the
actual functionality of the application

It tests the flow of application by running in real browser

It has to be integrated with Jasmine Framework to implement test cases

It has integrated mechanism to detect the pending tasks which relives the developer
to write wait/sleep

It has webdriver-manager which is used to start the instance of the selenium server to
initiate the end-to-end testing
Protractor Vs Selenium
Selenium cannot detect the extra html attributes of angular like ng-repeater, ng-
controller ng-model etc., using selenium code

Protractor is built using javascript on top of nodejs. Hence it can easily detect the
angular attributes which makes it perfect e2e testing of angular applications

In Angular application, web elements are created, controlled and destroyed


dynamically.
Selenium cannot detect the dynamic web elements which makes hard for the tester to
write e2e test cases. This makes protractor a viable platform for angular applications
Protractor - Setup
Protractor is available as npm package.

Prerequisites:
1. nodejs (latest)
2. Jasmine framework

Npm command:
npm i –g protractor

Updating webdriver
Webdriver-manager update

Starting webdriver
Webdriver-manager start
Protractor – Writing Tests
Protractor needs spec file and config file

Spec file contains the test cases which needs to be validate by protractor

The test cases will be written using jasmine testframework

Config file contains the setup required by protractor to start executing test cases

Config file consists of the location of selenium webdriver address and spec file
Protractor – Config file
Sample config file:
exports.config = {
seleniumAddress: ‘http://localhost:4444/wd/hub’,
specs: [‘spec.js’],
capabilities: {
’browserName’: ‘chrome’
}
beforeLaunch: function(){
//Delete logs
},
onPrepare: function(){
//Set browser dimensions, jasmine reporters
}
}

To run protractor: protractor conf.js


Protractor – Spec file
Sample spec file:

Describe(‘sample suite’, function(){


It(‘sample testcase’, function(){
//opens a new browser instance targeting the application
Browser.get(‘’);

//access angular ng-model and set the value


Element(by.Model(‘model_name’)).sendKeys(‘value’);

//access DOM to get the value


Var value = Element(byxpath(‘html/body/div[1]’)).getText();
Expect(value).toBe(‘value’);
})
})

You might also like