Pages

afterEach block in protractor

afterEach block in protractor?
  • afterEach block will be executed after executing the each it() blocks in a describe() block
  • We can write multiple it() blocks in a describe() block.
  • afterEach is normally used to write the code(steps) which is repeated at the end of each it() block like browser close, close DB connection, etc.,. depends upon your requirement.
Demo on afterEach():
describe('First describe block', function () {
    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");
    });
    afterEach(function () {
        console.log("after each block in First describe block");
    });
});
describe('Second describe block', function () {
    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");
    });
    afterEach(function () {
        console.log("after each block in second describe block");
    });
});
Run above spec file:
Output:
afterEach in protractor
afterEach in protractor
  • Here,till now afterEach() block scope is defined to specific describe() block only.
  • We can also declare afterEach() block to after the all describe() blocks in the spec file.
describe('First describe block', function () { 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"); }); afterEach(function () { console.log("after each block in First describe block"); }); }); describe('Second describe block', function () { 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"); }); afterEach(function () { console.log("after each block in second describe block"); }); }); afterEach(function () { console.log("after each block - after describe block(s)"); });
Output:
protractor afterEach()
protractor afterEach()
  • Here, we observe that afterEach() blocks executed after the each it() block.
Please comment below to feedback or ask questions.

No comments:

Post a Comment

Please comment below to feedback or ask questions.