Star

Created With

linkLooping

You can use the loop construct to loop through a list of items, each item generating a test.

When the loop property is specified, that spec will be run once for each item provided with the this.loopItem available in the lifecycle hooks (before_test and after_test).

The list of items can be specified in spec beforehand or you can specify a custom JS function which returns a list of items.

linkStatic Loop

Here's how a static loop is specified, type is static and loop list is mapped to static field as list of items.

specs/star-wars-static-loop.yml
1linkmeta:

2link name: Star Wars static loop

3linkconfiguration:

4link scheme: https

5link host: swapi.dev

6linkspecs:

7link - name: fetch each character

8link loop:

9link type: static

10link static:

11link - 1 # Luke Skywalker

12link - 4 # Darth Vader

13link before_test:

14link run_type: inline

15link inline:

16link function: !!js/function >

17link function() {

18link this.test.path_params = { userId : this.loopItem };

19link }

20link request:

21link path: /api/people/{userId}/ # <--- runs twice: once as "/api/people/1/" and once as "/api/people/4"

22link method: get

23link response:

24link status_code: 200

25link json_data:

26link - path: $.gender

27link value: male

See also: Example: Suite Using Loops



linkDynamic Loop Using JS

Here's how a static loop is specified, type is static and loop list is mapped to static field as list of items.

specs/star-wars-dynamic-loop.yml
1linkmeta:

2link name: Star Wars dynamic loop

3linkconfiguration:

4link scheme: https

5link host: swapi.dev

6linkspecs:

7link - name: fetch each character

8link loop:

9link type: dynamic

10link dynamic:

11link run_type: inline

12link inline:

13link function: !!js/function >

14link function() {

15link var generatePositiveInt = (max) => {

16link return Math.floor(Math.random() * max) + 1;

17link };

18link var runTimes = generatePositiveInt(10);

19link var items = [];

20link while (items.length < runTimes) {

21link items.push(generatePositiveInt());

22link }

23link return runTimes;

24link }

25link before_test:

26link run_type: inline

27link inline:

28link function: !!js/function >

29link function() {

30link this.test.path_params = { userId : this.loopItem };

31link }

32link request:

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

34link method: get

35link response:

36link status_code: 200

See also: Example: Suite Using Loops



linkAccessing Suite

Your dynamic loop can also utilize existing data saved on the this.suite context:

star-wars-suite-w-previous-data.yml
1linkmeta:

2link name: Star Wars dynamic suite with previous data

3linkconfiguration:

4link scheme: https

5link host: swapi.dev

6linkspecs:

7link - name: fetch a movie

8link request:

9link path: /api/films/1/

10link method: get

11link after_test:

12link run_type: inline

13link inline:

14link function: !!js/function >

15link function () {

16link const film = JSON.parse(this.response.body);

17link this.suite.characters = film.characters;

18link }

19link response:

20link status_code: 200

21link - name: fetch each character for the first film

22link loop:

23link type: dynamic

24link dynamic:

25link run_type: inline

26link inline:

27link # transform the list of URLs into a list of IDs

28link function: !!js/function >

29link function() {

30link return this.suite.characters.map((url) => {

31link var matches = url.match(/\/api\/people\/(\d+)\//);

32link return matches[1];

33link });

34link }

35link before_test:

36link run_type: inline

37link inline:

38link function: !!js/function >

39link function() {

40link this.test.path_params = { userId : this.loopItem };

41link }

42link request:

43link path: /api/people/{userId}/

44link method: get

45link response:

46link status_code: 200

47link after_test:

48link run_type: inline

49link inline:

50link function: !!js/function >

51link function() {

52link var character = JSON.parse(this.response.body);

53link console.log(character);

54link }



LoopingStatic LoopDynamic Loop Using JSAccessing Suite

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