Pages

beforeAll in protractor

beforeAll() in protractor?
  • beforeAll() block will be executed only once, before executing the all it() blocks in a describe() block
  • We can write multiple it() blocks in a describe() block.
  • beforeAll is normally used to write the code(steps) for launch browser URL, maximize the window, open DB connections, etc.,. depends upon your requirements.
Demo on beforeAll():
describe('First describe block', function () {
    beforeAll(function () {
        console.log("beforeAll 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 () {
    beforeAll(function () {
        console.log("beforeAll 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:
Output:
protractor beforeAll
beforeAll() in protractor
  • Here,till now beforeAll() block scope is defined to specific describe() block only.
  • We can also declare beforeAll() block to before the all describe() blocks in the spec file.
beforeAll(function () { console.log("beforeAll block - before describe block(s)"); }); describe('First describe block', function () { beforeAll(function () { console.log("beforeAll 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 () { beforeAll(function () { console.log("beforeAll 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 beforeAll() block
beforeAll() block in protractor
  • Here, we observe that beforeAll() blocks executed once before the all it() blocks.
Please comment below to feedback or ask questions.

No comments:

Post a Comment

Please comment below to feedback or ask questions.