简体   繁体   English

如何在 REST 请求中使用脚本生成的 UUID

[英]How to use script generated UUID in REST Request

I need to test a scenario where I need to generate a random UUID in order to use it lately as a part of a json body of a POST request.我需要测试一个场景,我需要生成一个随机 UUID,以便最近将它用作 json POST 请求正文的一部分。 My steps based on what I understand from the documentation are the following:根据我从文档中了解到的内容,我的步骤如下:

  1. UUID Generation UUID生成

在此处输入图像描述

  1. A property declaration like so像这样的财产声明

在此处输入图像描述

  1. Than a property transfer setted up in this way:比以这种方式设置的财产转让:

在此处输入图像描述

Now at this point if i run my step from the GenerateGUID to the property transfer i can see that the value has been succesfully transfered.现在,如果我从 GenerateGUID 运行我的步骤到属性转移,我可以看到该值已成功转移。

在此处输入图像描述

Now I need to use that property inside my Json like so:现在我需要在我的 Json 中使用该属性,如下所示:

在此处输入图像描述

I tried different solutions picked up around inte.net but with no luck, I'm new to SoapUI and this task has been assigned to me.我尝试了在 inte.net 周围找到的不同解决方案,但没有成功,我是 SoapUI 的新手,这个任务已经分配给我了。 Please can anyone spot what I'm missing or can point me the right direction?请任何人都可以发现我所缺少的东西或者可以指出正确的方向吗?

One thing that IMHO SoapUI documentation does not make clear, is that there are several different ways of accomplishing the same thing.恕我直言,SoapUI 文档没有说明的一件事是,有几种不同的方法可以完成同一件事。 Some more or less efficient.有些或多或少有效率。 In your case I see two options.在你的情况下,我看到两个选项。

=> Option 1 => 选项 1

If you need to use the value in only one place, that is, only in the payload but you do not need it later in something like an assertion.如果您只需要在一个地方使用该值,即仅在有效负载中使用该值,但稍后您不需要在断言之类的地方使用它。 In this case you can use inline dynamic property .在这种情况下,您可以使用内联动态属性

In your "CalculateObj Test" step, right in your payload, enter the following:在您的“CalculateObj 测试”步骤中,就在您的有效负载中,输入以下内容:

{ "guid": "${=java.util.UUID.randomUUID()}", ...other fields }

Every time you run this, a different UUID will be generated.每次运行它时,都会生成一个不同的 UUID。 Of course, you no longer need your three preceding steps.当然,您不再需要前面的三个步骤。

=> Option 2 => 选项 2

If you need to use the same UUID in multiple places, but you still need a different UUID each time you run the test.如果需要在多个地方使用相同的UUID,但每次运行测试时仍然需要不同的UUID。 Here you probably want to store the value as a testcase property and use simple property expansion .在这里您可能希望将值存储为测试用例属性并使用简单的属性扩展

Change your "Change GUID" script step to the following:将“更改 GUID”脚本步骤更改为以下内容:

def Guuid = java.util.UUID.randomUUID()
testRunner.testCase.setPropertyValue('guuid', Guuid)

Then in your "CalculateObj Test" step payload, enter the following:然后在您的“CalculateObj Test”步骤负载中,输入以下内容:

{ "guid": "${#TestCase#guuid}", ...other fields }

Subsequently, anywhere else in your testcase that you need to use this value, for example in an assertion, you can always refer to it as ${#TestCase#guuid} .随后,在您的测试用例中您需要使用该值的任何其他地方,例如在断言中,您始终可以将其称为${#TestCase#guuid} But each time you run the test, the value will be different.但是每次运行测试时,值都会不同。

I almost never use the Properties or the Property Transfer steps.我几乎从不使用 Properties 或 Property Transfer 步骤。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM