Hooks in Testcafe

What are hooks in automation?

The test hooks are the customized interface that enables automated testing of test objects. In the test hooks test run before the test starts and after the test is completed. If the test runs in a separate browser then the test hooks are also run on each separate browser.

While building tests you will often find some initial setup before the test run, so they execute successfully. You need to run a few tasks
after the test, finish ensuring future test runs start from clear.

Write functionality to complete these tasks before and after each test. Instead of writing to manage lots of code use hooks to keep your tests readable and manageable.

You specify each test in a fixture with a fixture .beforeEach and fixture. afterEach methods.

How to use hooks in Testcafe?

In basic tests.js you define a test hook either when initializing a fixture object or test object. Both object methods expose different to allow the editor to set up the hooks.

fixture `Getting started with Testcafe`
.page `https://devexpress.github.io/testcafe/example/`
.before(async t =>{
    //test setup goes here
    //await runDatabaseReset()
    //await seedTestData()
})
.after(async t => {
    //cleaning test data 
    //loging and sending data to monitoring systems
})

test
.before(async t =>{
    //test setup goes here
    //await runDatabaseReset()
    //await seedTestData()
})
.after(async t => {
    //cleaning test data 
    //loging and sending data to monitoring systems
})

Fixture hooks in Testcafe

Fixture hooks are the other type of hooks function. As its name, the functions are only defined in fixture objects. These
fixture hooks run only once before any test in fixture begins and after all tests in fixture ended.

How to use fixture hooks in Testcafe?

Define fixture hooks when initializing a fixture object by using the before method and it’s similar to beforeEach method.

fixture `Getting started with Testcafe`
.before(async t =>{
    //test setup goes here
    //await runDatabaseReset()
    //await seedTestData()
})
.beforeEach(async t =>{
    //Run before each test

})
.after(async t => {
    //cleaning test data 
    //loging and sending data to monitoring systems
})
.afterEach(async t =>{
    //run after each test
})

Test hooks in Testcafe

Use test hooks for defining functions that will run before and after test execution. In test code you can write any setup or cleanup code it will keep code organized.

How to use test hooks in Testcafe?

Use test hooks either initializing a fixture object or test object .

fixture("Fixture with test hooks")
  .beforeEach(async t => {
    // The code inside this function runs
    // before every test in this fixture
    // except where overridden.
  })
  .afterEach(async t => {
    // The code inside this function runs
    // before every test in this fixture
    // except where overridden.
  });
  
test
  .before(async t => {
    // The code inside this function runs
    // before this test, ignoring the code
    // defined in the beforeEach function
    // in the fixture.
  })
  .after(async t => {
    // The code inside this function runs
    // after this test, ignoring the code