Star

Created With

linkTypes of Hooks

Any hooks below can be written using the 3 supported ways of writing JS:



linkSuite-Level Hooks

These hooks are specified at the suite-level.



linkbefore_all

This runs once, before all of the specs will run.

This is a good place to set up test data or other data pre-requisites.

this keyword provides access to:

1linkbefore_all:

2link

3linkmeta:

4link name: Star Wars suite

5linkconfiguration:

6link scheme: https

7link host: swapi.dev

8link base_path: /api

9linkbefore_all:

10link run_type: module

11link module:

12link module_path: 'lib/faker.js'

13link method_name: generateRandomUser

14link

15linkspecs:

16link # ...



linkafter_all

This runs once, after all of the specs have run.

this keyword provides access to:

1linkmeta:

2link name: Star Wars suite

3linkconfiguration:

4link scheme: https

5link host: swapi.dev

6link base_path: /api

7link

8linkafter_all:

9link run_type: module

10link module:

11link module_path: 'lib/faker.js'

12link method_name: generateRandomUser

13link

14linkspecs:

15link # ...



linkbefore_each

This runs {n} times, before each of the specs.

💡 You can save add previously saved authentication info to this.test, such as a bearer auth token or cookies for use in subsequent specs.

this keyword provides access to:

1linkmeta:

2link name: Star Wars suite

3linkconfiguration:

4link scheme: https

5link host: swapi.dev

6link base_path: /api

7link

8linkafter_each:

9link run_type: inline

10link inline:

11link function: !!js/function >

12link function () {

13link this.test.headers = { 'Authorization': `Bearer ${this.suite.token}` };

14link }

15linkspecs:

16link # ...



linkafter_each

This runs {n} times, after each of the specs.

💡 You can save returned authentication info to this.suite, such as a bearer auth token or cookies for use in subsequent tests.

this keyword provides access to:

1linkmeta:

2link name: Star Wars suite

3linkconfiguration:

4link scheme: https

5link host: swapi.dev

6link base_path: /api

7link

8linkafter_each:

9link run_type: inline

10link inline:

11link function: !!js/function >

12link function () {

13link if (this.response.headers['Authorization']) {

14link this.suite.token = this.response.headers['Authorization'];

15link }

16link }

17linkspecs:

18link # ...



linkSpec-Level Hooks



linkbefore_test

This runs once, before the spec which it is specified.

this keyword provides access to:

💡 You can modified any aspect of the spec via this.test.


1linkmeta:

2link name: Star Wars suite

3linkconfiguration:

4link scheme: https

5link host: swapi.dev

6link base_path: /api

7link

8linkspecs:

9link - name: Get some user

10link request:

11link path: /people/{userId}/ # <--- runs random {n} times with random ID between 1 to 100

12link method: get

13link before_test:

14link run_type: inline

15link inline:

16link function: !!js/function >

17link function () {

18link this.path_params = { userId: 24 };

19link }

20link response:

21link status_code: 200

22link # GET request to URL: https://swapi.dev/api/people/42/


⚠️ NOTE: the above format for headers, cookies, path_prams, & query_params using a simple: {key: 'value'}

This is unlike normal spec definition uses: [{ name: 'name', value: 'value'}]

This will be fixed in future versions for consistency.



linkafter_test

This runs once, after the spec which it is specified.

this keyword provides access to:

💡 You can save any aspect of the spec response to this.suite to access in subsequent specs.


1linkmeta:

2link name: Star Wars suite

3linkconfiguration:

4link scheme: https

5link host: swapi.dev

6link base_path: /api

7link

8linkspecs:

9link - name: Get some user

10link request:

11link path: /people/1/ # <--- runs random {n} times with random ID between 1 to 100

12link method: get

13link after_test:

14link run_type: inline

15link inline:

16link function: !!js/function >

17link function () {

18link this.suite.person = JSON.parse(this.response.body);

19link }

20link response:

21link status_code: 200


⚠️ NOTE: as above, specifying this.test for headers, cookies, path_prams, & query_params uses a plain key/value set as an object, and DOES NOT use the same [{ name: 'name', value: 'value'}] format used when specifying in yaml. This will be fixed in future versions for consistency.



Types of HooksSuite-Level Hooksbefore_allafter_allbefore_eachafter_eachSpec-Level Hooksbefore_testafter_test

Home

Getting Startedchevron_right
Basicschevron_right
Requestchevron_right

Request Basics Headers Cookies Query Parameters Path Parameters

Payload Bodychevron_right

File Uploads Auto-Retry Additional Options

Response Validationchevron_right

Overview

Assertionschevron_right

Status Code Headers & Cookies

Bodychevron_right
Hooks & JSchevron_right

Overview

Function Typeschevron_right
Suite-Level Hookschevron_right
Spec-Level Hookschevron_right
Contextchevron_right
DRY & Reusing Specschevron_right
Command Line Interface (CLI)chevron_right