question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Scenario with Conditional Loop

See original GitHub issue

I would like to know if the following is possible.

I am attempting to create the following scenario:

  • Post - Login to the system
  • Post - Process File by sending post file data to endpoint
  • Loop until processing is complete - Get - Get the status of the file processing and Check Status of File to see if it is finished (100).
  • Break out of Loop if the check status of the file is at 100

I tried the following scenario:

flow: - post: url: “/login” log: “Logging into {{ $url }}” json: user: “admin” password: “admin” capture: json: “$.session_id” as: “id” - post: url: “/file” beforeRequest: “setFileBody” capture: json: “$.data_id” as: “data_id” -
loop: -
get: url: “/file/{{ data_id }}” log: “Checking {{ $url }}” log: “{{ $loopCount }}” headers: apikey: ‘{{ id }}’ capture: json: “$.scan_results.progress_percentage” as: “result” afterResponse: “setCounter” count: 100000

I then have the following afterResponse hook to attempt to set the loopCount to 100000, if the status of 100 is reached. function setCounter(requestParams, response, context, ee, next) {

    console.log(`Loop Count ${context.vars.loopCount}`)
    if (context.vars.result === 100) {
        console.log("In Setter")
        context.vars.loopCount = 100000
    }
    return next();   
}

This however does not appear to work. In debug mode, I can see that the system goes into the the setting of the loopCount but it does not appear to break out of the loop.

Is there a better approach or way to break out of the loop and check if my inner “get” response has the right data? (i.e -result = 100)

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

5reactions
thxmikecommented, Jun 1, 2017

It would be similar to the existing loop construct

loop:
-
    get:
    url: "/file/{{ data_id }}"
    
count: 100000

But instead of count maybe break or while. Something like:

loop:
-
    get:
    url: "/file/{{ data_id }}"
    
while: {{ data_id < 1000 }}

and the while or break is an expression. Where (data_id) is a variable that is settable in some preRequest and postResponse hook.

0reactions
hassycommented, Sep 22, 2017

This can be done with the new whileTrue attribute on loops in Artillery 1.6.0-9. Docs here:

https://artillery.io/docs/http-reference/#experimental-looping-with-custom-logic

Read more comments on GitHub >

github_iconTop Results From Across the Web

4. Conditionals and loops — Beginning Python Programming ...
A special character that causes the cursor to move to the beginning of the next line.
Read more >
What are some scenarios when you need to use while loop ...
The “while” loop is more flexible. So you use it when the logic doesn't fit into the rigid model of the “for” loop....
Read more >
Conditionals and Loops - Introduction to Programming in Java
The while loop enables us to execute a group of statements many times. This enables us to express lengthy computations without writing lots...
Read more >
Python 3 Notes: Conditional Loops and while
This tutorial explains how to make a simple while loop, which is based on a conditional statement. · In this scenario, you can...
Read more >
Practical 4: For- and While- Loops, If-statements - learnOnline
Create a for- loop to repeatedly execute statements a fixed number of times. Create a while- loop to execute commands as long as...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found