I have a postman test script to use with test runner. I am trying to pass dynamic value from file to validate response with no success. I am able to pass value to request, but not able to use value from data file in test script. I want to validate response with data passed from CSV file. Is something like below possible in first place?
pm.test("Body matches string", function () {
pm.expect(pm.response.text()).to.include('{{$column1}}');
});
Found that variable usage in test script is different. Below works.
pm.test("Body matches string", function () {
pm.expect(pm.response.text()).to.include(pm.variables.get("column1"));
});
Related
So my issue is that I want to have 2 tests for a single api call - one pass and one fail with missing params.
Here is what I have:
pm.test("Successful Login", function () {
pm.response.to.have.status(200);
});
pm.test("Missing Parameters", function () {
const currentUsername = pm.environment.get("username");
pm.environment.set("username", null);
pm.response.to.have.status(400);
//pm.environment.set("username", currentUsername);
});
So as you can see, I set username to null for the second test, only to set it back to is original value after the test. What I found was that instead of running the script sequentially, postman set my username to null before the first test could have been run, so I fail the first test. What should I do ?
Ok guys. Apparently you cannot set variables in the testing scripts because the testing script is run after the api call has been made. This needed to be set in the pre-request script. As for how to set all various tests in just on request I dont think this can be done. Therefore, I am just making a new request per test case.
i would like post different API body every time the test case run.
i have set the variable at POST object
e.g. testID default value test0001
then the HTTP body as below, test and verify passed.
{
“drugId”: “$testID”,
}
what syntax/command i can use in test case like parameterize test step, so first time test case run
drugId = test0001
second time test case run, it will be
drugId = test0002
Your HTTP body should be something like
{
“drugId”: “${testID}”
}
And your request in code should look something like this
response = WS.sendRequest(findTestObject('requestObject',[('testID'): 'test0001']))
where requestObject is your request saved in the Object Repository.
Implementation
Now, if you want to iterate this 10 times, you can do the following:
create a new test case called "callee" with the following content
response = WS.sendRequest(findTestObject('requestObject',[('testID'): testID]))
create another test case called "caller" with the following content
String test = "test000"
for(i=0;i<10;i++){
WebUI.callTestCase(findTestCase("callee"), ["testID":"${test+i.toString()}"], FailureHandling.OPTIONAL)
}
run the "caller" test
I making an api request using karate where one of the api request params takes a filter condition (which is a java script object).
I am using a literal notation to create a java script object as shown below. This code is in a separate filter.js file.
function() {
var params = {
val1:[],
val2:[]
};
return params;
}
Now i call the above .js file in the karate scenario as below:
Scenario: Test
Given path 'filtertest/'
* param filter = call read('classpath:feature/common/filter/filter.js')
When method get
Ran the above and when i check the log, the api throws bad request error. I looked at the request url and there i can see that the ':' in the js file where I am assigning a value to a object key is replaced with %3A which i believe is the ASCII encoding for ':'. (the param with its values below)
?filter=%7B%22val1%22%3A%5B%5D%2C%22val2%22%3A%5B%5D
What I want is the ':' to come as it is from the .js call as the server side expects the filter param values as key value pairs.
Is there a way I can achieve this?
If your server cannot decode an encoded : it is a bug: https://www.w3schools.com/tags/ref_urlencode.asp
If you really need this - the workaround is to use the url keyword and build it manually, path and param will always encode.
Given url baseUrl + '/filtertest?filter=foo:bar'
I'm using the tests of Postman to log into the console some of the details included in a JSON response.
The part of the test that logs is the following:
var data = JSON.parse(responseBody);
if (data.result[0] !== undefined) {
console.log(data.result[0].number, "|", data.result[0].category;
}
else console.log(QUERYSTRING_PARAMETER, "is not present");
I've tried many sintaxes/formats to have the value of the QUERYSTRING_PARAMETER passed to the test. However when data.result is empty with every sintax I've tried, the test simply logs QUERYSTRING_PARAMETER not defined. How can I pass the value from the query string parameters in the URL to the test to be evaluated/logged?
Thanks in advance
I'm not sure if it corresponds to what you need, but if you want data from your query, you may use the 'request' object (refer to the Postman sandbox, paragraph 'Request/response related properties').
You can get request method, url, headers and (maybe the one for you) data.
Alexandre
I'm trying to do some testing using JMeter but I'm facing an issue trying to do some complex stuff.
I have a login HTTP request test that comes back with a response which includes an auth_token. I need to add ":" at the end and encode it to base64 to use that value on the request of another test.
I've been reading that it can be done using BeanShell but I could not achieve it yet. I will appreciate if someone could give me some steps to perform this task.
I assume you know how to get this auth_token into a JMeter Variable via i.e. Regular Expression Extractor
If you're have JMeter Plugins installed - you can use __base64Encode() function like:
${__base64Encode(${auth_token},auth_token_encoded)}
If you don't have the plugins/cannot have/don't want to have - here is how to do it with Beanshell.
Add Beanshell PostProcessor somewhere after Regular Expression Extractor (or other PostProcessor you're using to fetch the auth_token value
Put the following code into the Beanshell PostProcessor "Script" area:
import org.apache.jmeter.protocol.http.util.Base64Encoder;
String auth_token = vars.get("auth_token");
String auth_token_encoded = Base64Encoder.encode(auth_token);
vars.put("auth_token_encoded", auth_token_encoded);
See How to Use BeanShell: JMeter's Favorite Built-in Component to get started with Beanshell scripting.
Both cases assume:
you have "auth_token" value stored in ${auth_token} JMeter Variable
you will be able to access the encoded value as ${auth_token_encoded}
I had a similar test case where I need to put a file as Base64 encoded String into the body of a HTTP Request.
Instead of a BeanShell I used the groovy script functionality¹:
{
"example": "${__groovy(new File('${SCRIPT_PATH}/test.file').bytes.encodeBase64())}"
}
If you already have a String this snippet would work similar:
{
"example": "${__groovy('string to encode'.bytes.encodeBase64())}"
}
Or this is the usage with a user defined variable:
{
"example": "${__groovy('${STRING_VARIABLE}'.bytes.encodeBase64())}"
}
¹ ${SCRIPT_PATH} is a user defined variable pointing – in my case – to the folder of the loaded jmx-file: ${__BeanShell(org.apache.jmeter.services.FileServer.getFileServer().getBaseDir();)}${__BeanShell(File.separator,)}