具有许多随机元素的随机变量值

发布于 2025-01-09 12:32:30 字数 1003 浏览 0 评论 0原文

我想在 POST 方法中使用 GET 方法中的随机值。

列表汽车 = 奥迪、保时捷、福特、大众、本田、雪铁龙

$['carTypes'][0]['carType']['enum']
Result: [Audi]

$['carTypes'][${=(int)(Math.random()*6)}]['carType']['enum']
Result: [Porsche] (random 1 car from the list of 5 available)

我想获得随机汽车列表,但不限于仅一辆车 - 随机汽车列表,但范围在 0 到 6 之间,不仅如此1值。

Result: [Audi,Porsche]
Result: [Ford, VW, Honda]
Result: [Citroen]

我已经尝试过这样的。

$['carTypes'][${=(int)(Math.random()*6)},${=(int)(Math.random()*6)}]['carType']['enum']
Result: [[Citroen, Honda]]

可能 2 个平括号 [[ 阻止我在 POST 方法中使用这些数据,如何去掉不必要的括号?

Groovy

import groovy.json.JsonOutput
Random random = new Random()
def list = ["Porsche","Ford","VW"]
def randomValue = random.nextInt(list.size())
def list2 = ["Porsche","Ford","VW"]
def randomValue2 = random.nextInt(list2.size())
def theValue = list2[randomValue2] +","+  list[randomValue]

我将感谢您的帮助。

I want to use a random value from the GET method, in the POST method.

List car = Audi, Porsche, Ford, VW, Honda, Citroen

$['carTypes'][0]['carType']['enum']
Result: [Audi]

$['carTypes'][${=(int)(Math.random()*6)}]['carType']['enum']
Result: [Porsche] (random 1 car from the list of 5 available)

I would like to get a list of random cars but not limited to just one car - random list of cars but in the range of 0 to 6, not only 1 value.

Result: [Audi,Porsche]
Result: [Ford, VW, Honda]
Result: [Citroen]

I have tried like this.

$['carTypes'][${=(int)(Math.random()*6)},${=(int)(Math.random()*6)}]['carType']['enum']
Result: [[Citroen, Honda]]

Probably 2 flat brackets [[ prevent me from using this data in the POST method, how to get rid of unnecessary brackets?

Groovy

import groovy.json.JsonOutput
Random random = new Random()
def list = ["Porsche","Ford","VW"]
def randomValue = random.nextInt(list.size())
def list2 = ["Porsche","Ford","VW"]
def randomValue2 = random.nextInt(list2.size())
def theValue = list2[randomValue2] +","+  list[randomValue]

I will be grateful for your help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

睫毛溺水了 2025-01-16 12:32:30

您可以在 GET 和 POST 请求之间创建一个 Groovy 脚本步骤,而不是将上述内容放入 post 步骤中。

在 Groovy 脚本中,您可以按照您想要的方式“构建”字符串,包括删除括号。脚本中的最后一行应该是返回语句,用于返回您构建的字符串。

在 POST 请求中,您可以使用 $ 功能从 groovy 脚本步骤“拉取”值。例如 ${Groovy 脚本名称#result}

Instead of putting the above in your post step, you could create a Groovy script step in between the GET and POST requests.

In the Groovy script, you can then 'build' the string exactly how you want, including the removal of the brackets. The last line in the script should be a return statement that returns the string you built.

In the POST request, you can then 'pull' in the value from the groovy script step using the $ functionality. E.g. ${Groovy Script Name#result}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文