How to store Body data in post request through jmeter - groovy

I am new to jmeter, I am using jmeter to test e-commerce website.
I have manage to script one scenario, which is to add a product in basket and test the response time.
Now, I have observed that when i click Add button on UI, their are two requests which are getting POSTED.
for eg: stocks are updated.
As of now, I have copied the BODY from stock and pasted in jmeter sampler, but in future i may change the Sales order and update scenario, hence i want to store this Body data(Stock request which is updated) of this request dynamically, as it will change corresponding to sales order number im providing.
The problem is I am not able to store the BODY data dynamically(Only if i change the sales order here).
I know i can use pre processor in this matter, but could anyone help me with the code to get the BODY data from the request and store dynamically before sending the sample.
Basically I need a solution where I am just updating my sales order number and rest of the things will be taken care dynamically, in my case the POSTING of Body data for updating the STOCK.
Thank you in advance!

See basically, you're talking about Correlation. As I can understand from your concern. You need the data for the product added to the cart. Which needs data from prev request. This can be easily managed by generating two requests. Extracting from first and using that info for the second one. This will not involve any hardcoding and will work for you in an efficient manner.

Something like:
vars.put('bodyData', sampler.getQueryString())
should do the trick for you, if you put this code into a JSR223 PreProcessor and add this PreProcessor as a child of the HTTP Request sampler which body you need to store - you will be able to access the request body as ${bodyData} later on where required
In the above example:
vars - stands for JMeterVariables class instance
sampler - is for HTTPSamplerProxy
More information: Top 8 JMeter Java Classes You Should Be Using with Groovy

Related

Dynamics Web API BATCH POST vs PATCH (remove navigation property value)

I am attempting to Disassociate a reference a contact reference to a single-valued navigation property by setting the value to null.
This works when I call the web api directly:
PATCH https://mydynamics.crm.dynamics.com/api/data/v9.2/contacts(00000000-0000-0000-0000-000000000000)
{ "firstname": "John",
"lastname":"Doe",
"lookup1#odata.bind": null,
"lookup2#odata.bind": null,
"lookup3#odata.bind": null
}
//[...other data omitted for brevity...]
So these lookup fields, lookup 1,2, and 3 would be reset to null (The association is removed).
We run a synchronization program that calls these operations in a batch and when this runs -- using the same payload -- the lookup fields fail to reset (although if I modified firstname or lastname - those fields would update). This is a POST batch call containing just the single PATCH operation in this case but it normally contains multiple operations.
All documentation I can find states that PATCH with multiple operation should be a POST call but it didn't reset my lookups.
If I change the POST batch to a PATCH batch...then it works as expected and all fields are updated correctly including the lookups!
Why the difference between BATCH POST vs PATCH?!
Is is a bug in the API?
Is it because I only had a single operation
in the batch? Would it work for multiple requests? (I didnt try).
Does using PATCH instead of POST have any negative affect on either a
single operation or multiple operations? Suppose a DELETE operation
was also in the batch for something? Microsoft says "Use a POST
request to submit a batch operation that contains multiple requests.
A batch request can include GET requests and change sets."
Why is the
documentation lacking on this topic?
Any help is appreciated!
Read your question while searching for related CRM WEB API challenges.
The response may be a bit overdue but here's what I think applies to your case:
POST, GET, PUT, PATCH, and DELETE are HTTP request types.
When using PATCH on MS CRM you can only edit a single record.
When you want to perform multiple operations at once you use a POST request to post a whole batch of different requests. This can include multiple PATCH requests.
How to POST a batch is described quite okay here:
https://learn.microsoft.com/en-us/power-apps/developer/data-platform/webapi/execute-batch-operations-using-web-api

Generate a variable in Groovy JMeter

I have a Thread Group wherein I have three samplers. They are HTML Requests. The first one returns a person and gives me the name, age and address. The second one changes the attribute name of the person but has no response.
The third sampler is the same as the first.
The second sampler isn't working and doesn't change the name.
I want a failure message like: "The name of the person was $(name1) and should be changed to $(name2), but the name is $(name3)" or something like that.
I do the whole things with Groovy so is there any way to generate variables with the output of the first and third request and the input of the second?
Most probably you're looking for Response Assertion, you can conditionally mark samplers as failed depending on various criteria. It might be the case that you won't even need scripting like.
Just add Response Assertion as a child of 3rd request and configure it to expect ${name3} variable to be present in the response (you can use Perl-5 style regular expressions for this as well) and if the name will not be present - the sampler will get failed and you will see the relevant failure message
More information: How to Use JMeter Assertions in Three Easy Steps
I do not think you can do it the way you describe it, because samplers are processed sequentially, so you can not process first, third and go to second request. You might want to re-evaluate your approach.

Is there any After keyword is available like Background for running cucumber steps

I know Background keyword is available for running a common steps before running each scenario. Likewise is there anything like "After" keyword is available for the commons steps after each scenario, not the logical steps in java code like after hooks, i mean in gherkin steps itself. i need like below
Background
Given I use the API header information
| Content-Type | application/json;v=3 |
And I connect to postgresql
Scenario Outline:
And I get the "Request" payload from "5NB_CARD-A_Request" file for the scenario "CardA_Scenario1"
And I store the input payload individual field details for database validation
And I form a client with this resource url "/transaction"
When I make a POST call and capture the response
And I get the "response" payload from "5NB_CARD-A_Response" file for the scenario "CardA_Scenario1"
Examples:
| HTTPCode |
| 200 |
After
Then I validate the output response with expected data
And I verify the HTTP error code is "<HTTPCode>"
And I fetch and validate latest created data from "transaction" table
And I validate the created card is inserted into "field" table
Short answer, you can probably use an After hook, more on this here, but it's not recomended for your case. BDD is used for communication with non-technical stakeholders
From how you've written the scenario outline, it seems that this only runs once and if the response if different form 200, then the last 2 steps will fail (or even the first Then).
If the only thing you need to check is the happy flow, in which the response is 200, there is no need for Scenario Outline with Examples. Simply create one scenario.
Background
Given I use the API header information
| Content-Type | application/json;v=3 |
And I connect to postgresql
Scenario: Happy flow
And I get the "Request" payload from "5NB_CARD-A_Request" file for the scenario "CardA_Scenario1"
And I store the input payload individual field details for database validation
And I form a client with this resource url "/transaction"
When I make a POST call and capture the response
And I get the "response" payload from "5NB_CARD-A_Response" file for the scenario "CardA_Scenario1"
Then I validate the output response with expected data
And I verify the HTTP error code is "200"
And I fetch and validate latest created data from "transaction" table
And I validate the created card is inserted into "field" table
If you are expecting to add more response codes then it might be a good idea to rewrite the scenario outline in a different way. There is no need for After keyword for your verification (Then steps). You are writing them only once anyway, when using scenario outline.
Background
Given I use the API header information
| Content-Type | application/json;v=3 |
And I connect to postgresql
Scenario Outline:
And I get the "Request" payload from "5NB_CARD-A_Request" file for the scenario "CardA_Scenario1"
And I store the input payload individual field details for database validation
And I form a client with this resource url "/transaction"
When I make a POST call and capture the response
And I get the "response" payload from "5NB_CARD-A_Response" file for the scenario "CardA_Scenario1"
Then I validate the output response with expected data
And I verify the HTTP error code is "<HTTPCode>"
And I fetch and validate latest created data from "transaction" table
And I validate the created card is inserted into "field" table
Examples:
| HTTPCode |
| 200 |
Keep in mind that you will need to manage the last steps differently if you are adding more response codes.
It also might be a good idea to hide details such as | Content-Type | application/json;v=3 | and manage them inside the step definition.
Update:
What I gathered from your comment is that you want to have those last 4 steps (Then steps) written only once inside a feature file and all the scenarios to use them.
As far as I know, there is no After which can execute the verification steps same as Background does for the preconditions, in the Gherkin language.
There is a way to simplify this, but it reduces reeadability. For example, if you have 10 scenarios and 2 scenario outlines that use the exact same four Then steps, then you could try nesting all of them in a more general one and if steps come from different step definistion files, then you could use picocontainer to group them and reduce the number how many times you call them inside the feature file. More details here .
Problem is that your Then steps are a bit complicated to be written in just one or even 2 simpler steps, because you have 3 params and 5 verifications.
To conclude, I think it's better to have them written for each scenario / scenario outline. I would be difficult for another person to look at the feature file and not see any Then, only to find them at the bottom. A better way would be to try and group more scenarios in scenario outlines, so the steps don't repeat that much.
Hope it helps!

Combine multiple http request or not?

Some web design questions.
Combine POST with GET?
When user clicks a button, I need to send one POST to submit a form, then need to GET a json object to replace some DOM fields. Should I combine them into one request so save one round trip time?
Multiple GET json request?
When user clicks a button, I need to GET 3 or 4 json object. Should I send 3 or 4 GET request, or just one and combine the json into one large json at back-end?
So basically, I'm not sure which is heavier: round trip time VS a little complexed back-end and front-end logic.
Any comment is welcome!
if i understood your question right...you have a dependency on your get requests...that is to say the second get depends on the first...if that is the case, obviously you need to take consecutive get operations...however, if that is not the case that is if you know the order of get request and the response won't be affected by local conditions...then i suggest you do post/get operations on server side...trigger the first one an let the server handle the rest and get the result...
of course you would not want users do multiple get requests for one simple operation...

How? SOAPUI and Groovy - send different SOAP message (with property from file) each time?

I'm trying to use SOAPUI (4.0) to do load testing, and I want to have each SOAP request be different, with some attribute values and element values in the requests being populated from (for example) a text file.
The SOAP message is going to be the same for each request, except for the several attribute values and element values.
The SOAP message includes an unsigned SAML assertion, and that has some attributes that need potentially be different for each SOAP request. Among these attributes there's one called "IssueInstant" which is basically a date/timestamp string, and an "Id" attribute, which is a unique string per request.
Ideally, I'd like to be able to populate that "Id" attribute value from the text file.
I've been able to populate the IssueInstant automatically in SOAPUI, by including a small piece of Groovy code, to get the current date/time, re-format it, then store that in a property. This Groovy code is in the startup script in the SOAPUI testcase.
In the body of the SOAP message, I have a Subject element that I want to have populated from the text file.
After the IssueInstant, Id, and Subject are populated, I want SOAPUI to send the request.
So, for example, say the text file has:
id0001,cn=foo1,dc=whatever,dc=com
id0002,cn=foo2,dc=whatever,dc=com
id0003,cn=foo3,dc=whatever,dc=com
Then, when I run the SOAPUI load test, I'd like the first request to have Id=id0001 and subject cn=foo1,dc=whatever,dc=com, the second request to have Id=id0002 and subject cn=foo2,dc=whatever,dc=com, and the third request to have Id=id0003 and subject cn=foo3,dc=whatever,dc=com, and then the load test loops back through those 3 sets of values until it ends.
The thing that I'm having a hard time understanding is how to step through the file in the Groovy code and how the Groovy code is suppose to know which line in the text file is the next line from which to build the properties?
I hope that this explanation of what I'm looking for is clear enough. If not, please let me know, and I hope that someone can help.
In soapUI Pro there exist so called DataSource and DataSource Loop test steps. They are used for looping through a set of test data. For example a text file. If you have the ability to use soapUI Pro, I recommend you to have a look at this: http://soapui.org/Data-Driven-Testing/functional-tests.html
Otherwise you have to load the file via groovy.
how to step through the file in the Groovy code
I'm sure you'll find some code snippets via Google.
how the Groovy code is suppose to know which line in the text file is the next line from which to build the properties
Create a test case property with initial value 1. Always after reading a line increment the value by 1. By reading this property in your groovy code you always know which line to read.

Resources