Jasmine

Jasmine is a Unit testing frameworks tool which support behavior-driven development

Jasmine could be used for testing other frameworks like protractor using JavaScript/Typescript code. It does not depend on any other JavaScript frameworks.

This is independent of DOM, or any other javascript frameworks, this tutorial is more on using Jasmine feature along with Protractor.

As it is unit testing frameworks, it provides features to keep out test file like tests and suites(user stories)

it blocks (Specs / Tests) in Jasmine

Jasmine provides a way for user to create test cases in the Jasmine test logic file. All the tests in the Jasmine are formed using it blocks

Unlike functions, You cannot call a it block from some other it block
Every it block will contains two parts:
  • Description : this is description of the it block, it could be, why you are writing this it bloc or your test name, or the functionality that you are testing
  • return call : this block contains function(), it may can also have variable in it, basically this variable will have some set time for to set the total time for the it block to get executed, sometime you can use it without time as well.
it block without any variable in function

									it('Some verifications', function() {
										console.log("this is 'it block' test");
									});			
									


it block with variable in function, when you have variable you must end the variable with giving some time or without time.


									it('Some verifications', function(end) {
										console.log("this is 'it block' test");
										end(); // denotes that this is end of test cases
									});			
									

Limiting execution time :

We can specify the maximum time a it block can take if you are testing something with time.

In below code, we have introduced sleep of 30 seconds but we are giving maximum time of 5 seconds to complete the execution.


									import { browser, element, by} from 'protractor'
									import { protractor } from 'protractor/built/ptor';
									describe('Protractor Typescript Demo', function() {
										browser.ignoreSynchronization = true;
										it('click operation', function(end) {
											setTimeout(function(){
												end();
											}, 5000)

											browser.sleep(30000)
											console.log("*************************");
										});
									});			
									


Everything in above code gets executes but result is faild because of the time taken to complete the execution.

Error: Timeout - Async callback was not invoked within timeout specified by jasmine.DEFAULT_TIMEOUT_INTERVAL.


Now modify the above program like below, as I mentioned earlier it is not mandatory


									import { browser, element, by} from 'protractor'
									import { protractor } from 'protractor/built/ptor';
									describe('Protractor Typescript Demo', function() {
										browser.ignoreSynchronization = true;
										it('click operation', function(end) {
											setTimeout(function(){
												end();
											}, 5000)

											console.log("*************************");
										});
									});			
									


You can also write Protractor Automation inside a it block


									import { browser, element, by} from 'protractor'
										import { protractor } from 'protractor/built/ptor';
										describe('Protractor Typescript Demo', function() {
											browser.ignoreSynchronization = true;
											it('click operation', function() {
												setTimeout(function(){
													end();
												}, 50000); // 50 seconds
												
												browser.get('https://google.com/');
												browser.sleep(1000)
												element(by.partialButtonText('out')).getTagName().then(function(textValue){
													expect(textValue).toBe("About", "Actual text is not match with expected text")
												})
											});
										});			
									

The negative part of using the setTimeout is, it will wait till given time even if the test gets executed before time itself, so please donot use this way for normal scenarios.

How to skip a test in Protractor :

When we run our test cases sometimes we might need to skip the tests present in the spec file.

Use x prefix to a test to skip a test in the jasmine

									import { browser, element, by} from 'protractor'
									import { protractor } from 'protractor/built/ptor';
									describe('Protractor Typescript Demo', function() {
										browser.ignoreSynchronization = true;
										// below test will be skipped as it has prefix as 'x'
										xit('click operation', function(end) {
											setTimeout(function(){
												end();
											}, 5000)
											console.log("*************************");
										});
									});			
									

How to run a particular test in Jasmine :

Sometimes you might have 20 it blocks in your spec file, but if you want to only a particular it block. Do you think it is feasible for prefixing all the 19 it blocks with x ?? Very Big NO.

Run any particular 'it block' by prefixing it with f

									describe('Protractor Typescript Demo', function() {
										
										fit('click operation', function() {
											console.log("Iam the only one who will run");
										});
										it('ignore test 1', function() {
											console.log("they have not prefixed me with f");
										});
										it('ignore test 2', function() {
											console.log("they have not prefixed me with f");
										});
									});			
									

Handle Hidden division Pop Up / Calender Pop up using Protractor

beforeEach function in Jasmine Protractor

beforeEach method will get executed before executing an it block

beforeEach function is applicable for all the it blocks present inside a describe block.


									describe('Protractor Typescript Demo', function() {
										beforeEach(function(){
											console.log("Before Each block")
										});
										it('aaa', function() {
											console.log("test One");
										});
										it('bbb', function() {
											console.log("test two");
										});
										it('ccc', function() {
											console.log("test three");
										});
									});			
									

before-each-jasmine-protractor BeforeEach block will contains all the steps which are pre-requisite and common for all the test cases like opening a browser and navigating to the URL.

afterEach in jasmine

afterEach block will be executed after completing a it block inside a describe block.

afterEach block is applicable for all the it blocks present in a describe block

									describe('Protractor Typescript Demo', function() {
										afterEach(function(){
											console.log("After Each block")
										});
										it('aaa', function() {
											console.log("test One");
										});
										it('bbb', function() {
											console.log("test two");
										});
										it('ccc', function() {
											console.log("test three");
										});
									});			
									

after-each-jasmine-protractor afterEach block will contain steps to clean up the mess that we have created in the it block, sometimes we may write code like close browser, or logout from application.

Scroll an element Into View using JavaScript Executor in Protractor

describe Block in Jasmine Protractor

describe block is nothing but the test Suite, a describe block can contains multiple it blocks.

Like it block, describe block also will have two parameters in the syntax, 1. Description of the suite, 2. function.


									describe('I hold multiple it blocks, because I'm describe block', function() {
										beforeEach(function(){
											console.log("Before Each block")
										});
										it('aaa', function() {
											console.log("test One");
										});
										it('bbb', function() {
											console.log("test two");
										});
										it('ccc', function() {
											console.log("test three");
										});
										afterEach(function(){
											console.log("After Each block")
										});
									});			
									


describe-block-jasmine-protractor

Nested describe block in jasmine Protractor

We can write one describe block inside another describe block, this structure is called as nested describe block.


									describe('Outer Describe block', function() {
										afterEach(function(){
											console.log("After Each block of top describe block")
										})
										it('aaa', function() {
											console.log("test One in top describe block");
										});

										describe('inner describe block one', function() {
											afterEach(function(){
												console.log("After Each block in inner describe block one")
											})
											it('aaa', function() {
												console.log("test One in inner describe block one");
											});
										});
									});			
									


nested-describe-block-jasmine-protractor Are you able to see the strange out line in above image, the afterEach block in the Top describe block executed twice but we have only one it block in top describe block ?

Yes, Your guess is right.

before/afterEach will be executed not only for it block present in the describe block but also for describe block present inside that describe block.

beforeAll block in Jasmine Protractor

beforeAll block will be executed before executing any it or describe block inside a describe block.

beforeAll block will be executed only once per respective describe block.


									describe('Outer Describe block', function() {
										beforeAll(function(){
											console.log("Before All in top describe block")
										})
										it('it block in top describe block', function() {
											console.log("test One in top describe block");
										});

										describe('inner describe block one', function() {
											beforeAll(function(){
												console.log("Before All block in inner describe block one")
											})
											it('inner it block', function() {
												console.log("test One in inner describe block one");
											});
										});
									});			
									


before-all-describe-block-jasmine-protractor

afterAll block in Jasmine Protractor

afterAll block will be executed after executing all the it & describe blocks present in the describe block.

afterAll block will be executed only once per respective describe block.


									describe('Outer Describe block', function() {
										afterAll(function(){
											console.log("After All in top describe block")
										})
										it('first it block in top describe block', function() {
											console.log("test One in top describe block");
										});
										it('second it block in top describe block', function() {
											console.log("test two in top describe block");
										});

										describe('inner describe block one', function() {
											beforeAll(function(){
												console.log("Before All block in inner describe block one")
											})
											it('inner it block', function() {
												console.log("test One in inner describe block one");
											});
										});
									});			
									


afterAll-block-jasmine-protractor

f / x describe block in Jasmine

We can focus(run only), or x the describe block for skipping the describe in similar way to a it block


									xdescribe('First Describe block', function() {
										it('test in First describe', function() {
											console.log("test 1");
										});
									});

									fdescribe('Second Describe block', function() {
										it('test in Second describe', function() {
											console.log("test 2");
										});
									});			
									


x-f-describe-block-jasmine-protractor

Assertions in Protractor

I hope you might have seen few expect statements in script, which are nothing but the assertions in protractor.

expect statement will contain actual value and the second part of the expect statement should be the expected values.

  • toBe() : passed if the actual value is of the same type and value as that of the expected value. It compares with === operator
  • toEqual() : works for simple literals and variables; should work for objects too
  • toMatch() : to check whether a value matches a string or a regular expression
  • toBeDefined() : to ensure that a property or a value is defined
  • toBeUndefined() : to ensure that a property or a value is undefined
  • toBeNull() : to ensure that a property or a value is null.
  • toBeTruthy() : to ensure that a property or a value is true
  • toBeFalsy() : to ensure that a property or a value is false
  • toContain() : to check whether a string or array contains a substring or an item.
  • toBeLessThan() : for mathematical comparisons of less than
  • toBeGreaterThan() : for mathematical comparisons of greater than
  • toBeCloseTo() : for precision math comparison
  • toThrow() : for testing if a function throws an exception
  • toThrowError() : for testing a specific thrown exception
  • not : we can invert above expectations using not keyword

										import { browser, element, by} from 'protractor'
										import { protractor } from 'protractor/built/ptor';
										describe('Protractor Typescript Demo', function() {
											browser.ignoreSynchronization = true;
											it('click operation', function() {
												browser.get('https://google.com/');
												browser.sleep(1000)
												element(by.partialButtonText('out')).getTagName().then(function(textValue){
													expect(textValue).toBe("About", "Actual text is not match with expected text")
													expect(textValue).not.toBe("Bingo", "we are using mot keyword")
												})
											});
										});			
										


Expectation of a unit testing Framework

Any given unit testing framework should have below properties:

  • Good framework should not be tied to any browser, framework, platform, or host language.
  • should have idiomatic and unsurprising syntax.
  • Framework should work anywhere JavaScript can run, including browsers, servers, phones, etc.
  • shouldn't intrude in your application's territory
  • should play well with IDEs (e.g. test code should pass static analysis).

Feature of Javascript Framework

  • Behavior-driven development framework for testing JavaScript and Typescript code.
  • Jasmine does not require a DOM
  • Jasmine runs on any JavaScript-enabled platform
  • Jasmine is influenced by other unit testing frameworks, such as ScrewUnit, JSSpec, JSpec, and RSpec.
  • A Suite represents a bunch of tests that are related. Each suite in turn contains a set of Expectations that compare the results of the test - called the actual - with the expected value
  • A Suite is defined by calling the describe() function. It takes two parameters: the name of the Suite, and the function which contains the calls to the expectation methods called Specs

Protractor Interview Questions

About Author

Myself KarthiQ, I am the author of this blog, I know ways to write a good article but some how I donot have the skills to make it to reach people, would you like help me to reach more people By sharing this Article in the social media.

Share this Article Facebook
Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions
Copyright © CherCher Tech