Jmeter | Generate UUID (Unique Identifier)
What is UUID?
A universally unique identifier (UUID) is a 128-bit number used to identify information in computer systems. UUID is almost unique as its length and random.
Problem Statement:
As an Automation Tester,
I want to generate/create UUID
So that I can pass SAME UUID to all subsequent requests under Test Plan
Solution:
ALTHOUGH THERE IS NO DEDICATED SAMPLER IN JMETER TO GENERATE UUID / GUIDS BUT YOU CAN USE THE BUILT-IN FUNCTIONS TO GENERATE THEM.
The JMeter function __UUID function ensures that the Type 4 UUID (also known as GUID) structure will always be returned.
It consists of one group of 8 hexadecimal digits, followed by three groups of 4 hexadecimal digits each, followed by one group of 12 hexadecimal digits. The following example UUID shows the groupings of hexadecimal digits in an UUID:
c69e0dd1-ac6b-4f2b-8d59–5d4e8743eecd
Type 1 UUID is time-based, Type 2 UUID is name-based and Type 4 UUID is randomly generated. So anywhere a random GUID structure is required, feel free to use the __UUID function. It will guarantee the structure will have a unique nature, as in the example below:
([A-Z0–9]{8}-[A-Z0–9]{4}-[A-Z0–9]{4}-[A-Z0–9]{4}-[A-Z0–9]{12})
Implementation:
Jmeter function __UUID() can be used with different Jmeter component like pre-processor, beanshell , etc based on the need. Let’s explore few of them.
A) Through “User Parameters” (Pre Processor)
- Add User Parameters Pre Processor (Right click on Thread Group > Add > Pre Processor > User Parameter)
- Define your variable which will act as unique GUID / UUID (Ex: requestIndex) and specify value as ${__UUID()} (In-build function of Jmeter)
- Now, we have requestIndex variable available as UUID throughout the test plan which we can use in any subsequent requests. ${requestIndex}
As an example,
B) Through “Bean shell Pre Processor”
- Add Bean shell Pre Processor (Right click on Thread Group > Add > Pre Processor > Bean shell Pre Processor)
- Write Groovy script to generate UUID.
vars.put("requestIndex", UUID.randomUUID().toString())
print(requestIndex)
- Now, we have requestIndex variable available as UUID throughout the test plan which we can use in any subsequent requests. ${requestIndex}
- As an example,
Note: we can also use Bean shell sampler as well and have similar code inside.
For Better Debugging Purpose —
Just add below component in order to debug result better -
- Debug sampler (Right click on Thread Group > Add > Sampler > Debug sampler)
- View results Tree (Right click on Thread Group > Add > Listener > View results Tree)