Page tree
Skip to end of metadata
Go to start of metadata

Agenda

Table of contents

Spec overview

Time to have a look at the first spec file. A spec file is written with the same notation as Jasmine JavaScript tests. Every test starts with a describe block and has an it block for each test case (the blocks can be nested). The idea is, to create a speaking sentence for every test. 

A simple spec file may look like this:

bird.spec.js
var Bird = require("Bird")              # import the Bird object
describe("Bird", function () {          # test case root for Bird object
	it('flies way', function () {       # the actual test case
        var bird = new Bird();          # given: create an instance of Bird
		bird.flyAway();                 # when:  do something  
		expect(bird.gone()).toBe(true); # then:  check if the expected behaviour is true
	});
});

The output would be: Bird flies way (tick)

(question) For further simple examples, have a look at this Simple Protractor Example


Real world example

After a simple example here's on from the Random Page plugin (source adminSection.spec.js). It follows the same structure as the bird example.

imports

There are some more import than one object, the import include external resources (e.g. Confluence Protractor Base) and internal ones (e.g. RandomPageAdministration)

adminSection.spec.js#imports
#  external imports
## import the Confluence Protractor Base
var confluenceProtractorBase = require('confluence-protractor-base').confluenceProtractorBase; 
## import a hleper method from the CPB
var asyncElement = confluenceProtractorBase.utils.pageObjectUtils.asyncElement;

#  internal imports
## import the RandomPageAdministration page object
var RandomPageAdministration = require("../page-objects/RandomPageAdministration");
## import the shared page object (test data)
var sharedPageObjects = require("./common/sharedPageObjects");

describe blocks

'describe blocks' always declare the objects (or sometimes the methods) to be tested, as you see they can be nested. In this case it tests the PagesLimit property inside the Random Page administration page.

adminSection.spec.js#describe
describe("RandomPageAdministration", function () { # root describe block
	...
	describe("PagesLimit", function () { # nested describe block
		...
	});
});

it blocks

'it blocks' label the actual test cases. The 'it' refers to the surrounding object from the describe block. As you know from other tes

adminSection.spec.js#it
it('opens administration action', function () { # it block defining a test case
	...
});

The actual test

The test case should follow the structure 'given - when - then'. 'expect' is similiar to 'assert' in junit. 'toBe' defines a jasmine matcher that compares the result and expected values.

(question) Have a look at the build in jasmine matchers.

adminSection.spec.js#testcase
# given: Random Page Adminstration page object
var administration = sharedPageObjects.randomPageAdministration; 

	...
		# when: open the admin action
        administration.randomPageAdminAction.open();

        # then: test if the headline is "Random Page Configuration"
		expect(element(by.css(".admin-heading")).getText()).toBe("Random Page Configuration");

Before and after

For functions that should be executed right before all tests of the surrounding describe block you can define "beforeAll" (similar to @setup in junit). The same thing can be done after all tests with 'afterAll'. If there are thing that should be done before or after every test case (beforeEach and afterEach).

In this example we first authenticate as admin (login and administrator access confirmation). 

adminSection.spec.js#testcase
describe("RandomPageAdministration", function () { 

    beforeAll(function () { # executed before it('opens...
	    administration.authenticateAsAdmin();
    });


    it('opens administration action', function () {


Take away

You now know the structure of a spec file and the elements for writing your own test. We recommend to start with something simple (an admin sections is such a thing). Write the structure and simply check if an element exists or has the expected value. For an easier start, have a look at already written specs in the Random Page app or the Confluence Protractor Base.

Navigation

< Part 3: Common Spec FilesPart 5: Page Objects >

  • No labels