Sunday 22 November 2015

Protractor - Jasmine

Prerequisites:

Jasmine is installed by default when 'protractor' is installed(protractor can be installed by using the command 'npm install -g protractor').

Jasmine is a BDD(Behavior Driven Development) framework for testing the JavaScript code.

Suites: 'describe' your tests,
'describe' jasmine function has two parameters 
- string : Describes the spec suite 'name' or 'title' 
- function : Function contains code for implementing the suite.

Specs: Specs are defined by using the jasmine function 'it', 
'it' jasmine function has two parameters
- string : specifies the 'title' of the spec.
- function : Function is the actual test.

'describe' and 'it' both contains the necessary code block to execute and implement the test,
Ex:
describe("Suite Name/Suite Description", function() {
  it("Spec Title/Spec Description", function() {
    expect(true).toBe(true);
  });
});

Variables declared in 'describe' block are available to any 'it' block.
Ex:
describe("Suite Name/Suite Description", function() {
  var flag;

  it("Spec Title/Spec Description", function() {
    flag = true;

    expect(flag).toBe(true);
  });
});


Expectations:
'expect' function takes the 'actual' value, chained with 'matcher' function(matcher function takes 'expected' value).

Matchers:
Performs the boolean comparison between 'actual' and 'expected' values, jasmine will 'pass' or 'fail' spec based on the matcher comparison.

Below are some of the Jasmine Matchers:
- not                    - toBeTruly
- toBe                  - toBeFalsy
- toEqual              - toContain
- toMatch               - toBeLessThan
- toBeDefined       - toBeGreaterThan
- toBeUndefined   - toBeCloseTo
- toBeNull              - toThrow

Below are some of the Jasmine functions:
- describe          - it
- beforeEach     - afterEach
- beforeAll         - afterAll
- xdescribe        - xit
- spyOn

No comments:

Post a Comment