Hooks in playwright are functions that are executed before or after each test, test file, or test group. They can help us set up and tear down the environment for our tests, such as launching browsers, creating pages, logging in, cleaning up, etc.
test.beforeAll(hookFunction)
When called in the scope of a test file, it runs before all tests in the file. When called inside a test.describe() group, it runs before all tests in the group. We can use it to perform some global setup, such as launching a browser or creating a context.
test.afterAll(hookFunction)
When called in the scope of a test file, it runs after all tests in the file. When called inside a test.describe() group, it runs after all tests in the group. We can use it to perform some global teardown, such as closing the browser or deleting the context.
test.beforeEach(hookFunction)
When called in the scope of a test file, it runs before each test in the file. When called inside a test.describe() group, it runs before each test in the group. We can use it to perform some local setup, such as creating a page, navigating to a URL, or logging in.
test.afterEach(hookFunction)
When called in the scope of a test file, it runs after each test in the file. When called inside a test.describe() group, it runs after each test in the group. We can use it to perform some local teardown, such as reverting the actions performed during a test, taking screenshots, clearing cookies, or logging out.
Alternatively, the above hooks can also accept hook title as its first parameter. The following example demonstrates this approach.
This is a theoretical chapter. There is nothing to commit in here. If you have made some changes to the project, clean them up by executing the
following command.