artillery icon indicating copy to clipboard operation
artillery copied to clipboard

feat: Add custom functions that can be called inside test script

Open NivLipetz opened this issue 6 years ago • 6 comments

This feature enables dynamically adding functions to the contextFuncs object in core/lib/runner, which are functions that the Runner knows how to execute if they are called in the Artillery script. Currently, the only functions there are $randomString and $randomNumber, which might not be enough for some tests. We, for example, needed to randomly generate uuid and dates, and therefore needed new functions that support this. By having this feature, we could easily add these functions (and more if needed) to our test.

An example of usage can be seen in the test provided.

NivLipetz avatar Dec 27 '18 14:12 NivLipetz

I'm hesitant to introduce another way to load custom JS code; we should be able to reuse the existing config.processor option to provide additional functions for template strings.

Another thought is that perhaps the functions you need should just be added alongside the default ones, as they're likely to be something that others may want to use.

hassy avatar Jan 04 '19 12:01 hassy

I agree with you regarding adding another way to load custom JS code. I hard-coded the functions we needed and refactored out the templates to an external file, that way more templates can be added easily.

NivLipetz avatar Jan 09 '19 10:01 NivLipetz

I looked at this PR which seems to add custom functions support, but presumably the documentation for it hasn't been written yet, so can you please provide a short example of how to use custom functions, for example within the "config.target" property (to generate a dynamic query string parameter value when using websocket scenarios)?

An example of what I would like to be able to do:

config:
  target: "wss://my.websocket.host/?id={{ $myCustomRandomIdGeneratingFunction }} "

Thanks.

SandroGrzicic avatar Feb 22 '19 17:02 SandroGrzicic

Hi @SandroGrzicic sorry for the late reply.. you simply add () to the function call like this: $myCustomRandomIdGeneratingFunction()

config:
  target: "wss://my.websocket.host/?id={{ $myCustomRandomIdGeneratingFunction() }} "

NivLipetz avatar Apr 30 '19 05:04 NivLipetz

@hassy any update about this PR?

NivLipetz avatar Apr 30 '19 05:04 NivLipetz

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

CLAassistant avatar Apr 02 '22 16:04 CLAassistant