Replace string per variable in Robot Framework - robotframework

I'm new at Robot Framework and I'm working with a REST API. I'm trying to replace a string inside a json per a variable so I can use an environment variable in my Post and Put methods.
{"key1":"value1",
"key2": {"key3: "value3"}}
I need to replace the value of "value1" and the value of "value3" per a variable like %{value3}. How can I do this in Robot Framework?
Thanks!

Related

Jmeter-How to sent the variable value to the response of particular Request

I am working in Jmeter.
I need to sent the value for the variable name 'Reference' in the response of the particular request.
I am able to get the response using Bean shell Post processor using the string "vars.put("response", new String(data));"
I need the get the variable 'Reference' which is referred by id as id="reference"
I need to pass value to the variable 'Reference'.
Can anyone help?
Since JMeter 3.1 it's recommended to use JSR223 Test Elements and Groovy language for any form of scripting so if you want to store the response into the Reference JMeter Variable you can do it as follows:
Add JSR223 PostProcessor as a child of the request which response you want to store
Put the following code into "Script" area:
vars.put("Reference", prev.getResponseDataAsString());
where:
vars is a shorthand for JMeterVariables class instance
prev stands for the parent SampleResult
see JavaDoc for the above shorthands for all available functions/properties description and Top 8 JMeter Java Classes You Should Be Using with Groovy for comprehensive explanation with examples for the above and other JMeter API shortcuts available for Groovy scripts

How do I strip a section of URL and assign to a variable

I want to assign the current URL to a variable and then get a substring of this and assign to a second variable. The URL would be in the format of http://qasite.test/lists/123456789#
So I need 123456789# assigned to variable 2
I understand I can firstly get the URL by using:
${href}= Get Location
After working a lot I think following approach may work.(I don't have robot framework environment but following might work.)
robot framework string manipulations official docs
Use Fetch from right for your url (http://qasite.test/lists/123456789#) you will get this (1234567789#)
${string} = Convert to String http://qasite.test/lists/123456789#
${id} = Fetch From Right ${string} /
This link might help fetch from right
Now get the length of ${id} using Get Length of robot framework
Now use Get Substring starting index from (0 to length or length-1) You will get 123456789

How to use Pact Matcher for values other than 'Strings'

In pact-jvm (groovy on consumer side and gradle on the provider side), I'm trying to use Pact matchers like below:
name regexp(~/\w+/,'sony')
Will Pact matcher regex work only for Strings ? For values other than strings, do I need to use Pact term ?
If the answer is 'yes' for the above two questions. Please explain me how to use Pact term in groovy style.
I have tried using Pact term like below:
date Pact.Term(generate :"02/11/2013", matcher:/\d{2}\/\d{2}\/\d{4}/)
But getting groovy - MethodMissingException.
My complete response body for reference:
withBody {
id regexp('[0-9a-f]{8}-([0-9a-f]{4}-){3}[0-9a-f]{12}','e8cda07e-849f-49c2-94d6-aaa5c4ab7fcd')
name regexp(~/\w+/,'sony')
date Pact.Term(generate :"02/11/2013", matcher:/\d{2}\/\d{2}\/\d{4}/)
}
Regex only applies to strings.
If you want to match a date, I'd suggest using the date matcher, e.g.
withBody {
id regexp('[0-9a-f]{8}-([0-9a-f]{4}-){3}[0-9a-f]{12}','e8cda07e-849f-49c2-94d6-aaa5c4ab7fcd')
name regexp(~/\w+/,'sony')
date date("dd/MM/yyyy", "02/11/2013")
}
See the available DSL methods https://github.com/DiUS/pact-jvm/tree/master/pact-jvm-consumer-groovy#dsl-methods-1

Jmeter how to get the dynamic parameter value in the path

JMeter path contents some dynamically generated value.
Eg
[HTTP Request]
[path-home/user?p=er3562]
This "p" value is dynamically generated.
I want to get this parameter value in the first HTTP request path.
This parameter value should pass through each HTTP request path.
I am new to JMeter. Please help me to solve this?
You need to extract it somehow and convert into a JMeter Variable for later reuse. JMeter provides several PostProcessors for extracting data from different responses types, in your case the most suitable one will be Regular Expression Extractor.
Add Regular Expression Extractor as a child of the request which returns that path-home/user?p=er3562 value
Configure it as follows:
Apply to: depending on where the "interesting" value lives, the most "safe" setting is Main sample and sub-samples
Field to check: depending on where the "interesting" value comes from, in the majority of cases it's Body but in your case it may be i.e. URL
Reference Name: anything meaningful, it is JMeter Variable name, if JMeter finds anything it will store the result in a variable named accordingly to this field. I.e. path-home
Regular Expression: Perl5-style regular expression, in your case it would be something like: path-home/user\?p=er(\d+)
Template: if you're looking to extract a single value it will be $1$
Refer extracted value as ${path-home} where required.
References and tips:
You can use Debug Sampler and View Results Tree listener combination to view JMeter Variable names and test regular expressions against actual response
Using RegEx (Regular Expression Extractor) With JMeter
Perl 5 Regex Cheat sheet
JMeter Regular Expressions
By using correlation concept ,
regular expression extractor concept & try this key
p=er(.*?)

Jayway Jsonpath syntax for string array filter?

I am attempting to use the EvaluateJsonPath processor in Nifi, and am having trouble with the jayway jsonpath syntax.
My object looks like the following:
{"text":"my stuff", "tags":["abc", "xyz", "beq"]}
I want to route messages based on the tags - I want everything containing "xyz" to be routed one way, and everything not containing it to be routed another way.
Using http://jsonpath.herokuapp.com/ I've been testing and trying to figure out the syntax to filter based on a json object containing an array of strings matching. I can match based on overt index (so $.[?(#.tags[1] =~ /xyz/i)] works just fine), but I can't guarantee the order or number of objects in the tags field.
Is there a way to do this in the jayway json module? I saw filter the Json according to string in an array in JSONPATH which I've tried, but it doesn't appear to work in the simulator above.
I do not know how to do this in one EvaluateJsonPath processor step. But it can certainly be done in a two-step process:
Use EvaluateJsonPath to filter "xyz" tags out of the tags array, using a JsonPath expression like $.tags[?(# =~ /xyz/i)] and setting the processors return-type to json so an array may be returned. This will result in ["xyz"] for a match and [] for non-matching files
Use RouteOnAttribute to route based on the resulting array, with an expression like ${matchingTags:toLower():contains('xyz')}.
It might also be worth considering evaluating the JSON as text against a regular expression to match the tag.

Resources