Pages

beforeEach in protractor

beforeEach in protractor?
  • beforeEach block will be executed before executing the each it() blocks in a describe() block
  • We can write multiple it() blocks in a describe() block.
  • beforeEach is normally used to write the code(steps) which is repeated in each it() block.
Demo on beforeEach():
describe('First describe block', function () {
    beforeEach(function () {
        console.log("before each block in First describe block");
    });
    it('First Test case - First describe block ', function () {
        console.log("First Test case in First describe block");
    });
    it('Second Test case - First describe block ', function () {
        console.log("Second Test case in First describe block");
    });
});
Run above spec file:
beforeEach in protractor
Protractor beforeEach
  • We will see how the beforeEach block will execute if we have multiple describe blocks.
describe('First describe block', function () {
    beforeEach(function () {
        console.log("before each block in First describe block");
    });
    it('First Test case - First describe block ', function () {
        console.log("First Test case in First describe block");
    });
    it('Second Test case - First describe block ', function () {
        console.log("Second Test case in First describe block");
    });
});
describe('Second describe block', function () {
    beforeEach(function () {
        console.log("before each block in Second describe block");
    });
    it('First Test case - second describe block ', function () {
        console.log("First Test case in second describe block");
    });
    it('Second Test case - second describe block ', function () {
        console.log("Second Test case in second describe block");
    });
});
Run above spec file:
Protractor beforeEach
Protractor beforeEach()
  • Here,till now beforeEach() block scope is defined to specific describe() block only.
  • We can also declare beforeEach() block to before the all describe() blocks in the spec file.
beforeEach(function () {
    console.log("before each block - before describe block(s)");
});
describe('First describe block', function () {
    beforeEach(function () {
        console.log("before each block in First describe block");
    });
    it('First Test case - First describe block ', function () {
        console.log("First Test case in First describe block");
    });
    it('Second Test case - First describe block ', function () {
        console.log("Second Test case in First describe block");
    });
});
describe('Second describe block', function () {
    beforeEach(function () {
        console.log("before each block in Second describe block");
    });
    it('First Test case - second describe block ', function () {
        console.log("First Test case in second describe block");
    });
    it('Second Test case - second describe block ', function () {
        console.log("Second Test case in second describe block");
    });
});
Output:
protractor beforeEach() block
protractor beforeEach() block
  • Here, we observe that beforeEach() blocks executed before each it() block.
Please comment below to feedback or ask questions.

No comments:

Post a Comment

Please comment below to feedback or ask questions.