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
Related
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"));
});
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 have a feature that generates a vehicle id and is stored as a variable in the feature. I want to pass this id as a part of the request URL in another feature as a sort of a teardown activity.
This is how I called it from a feature called activateVehicle.feature
Scenario : Activate a vehicle
* header X-API-Key = apiKey
* def result = callonce read('createVehicle.feature')
* def vehicleId = result.vId
# some workflow steps
........
........
........
# tear down - delete the vehicle created
* call read('deleteVehicle.feature'){ vehcileId: '#(vehicleId)' }
In the called feature - deleteVehicle.feature
Scenario: Delete a vehicle
* header X-API-Key = apiKey
* def myurl = 'https://xxx/vehicle'+ vehicleId +'?permanent=yes'
Given myurl
And request ''
When method delete
Then status 200
Am I right in the approach? I want to reuse deleteVehicle.feature in other workflows as well and hence not doing this operation in the same activateVehicle.feature(which would have been very easy). I referred to the documentation too but it shows how we can use the variables in in the request body but not as a variable that can be used anywhere in the called feature. I don't want to use it in the request body (but want to use it as a part of the request URL) For example:
Scenario:
Given url loginUrlBase
And request { userId: '#(username)', userPass: '#(password)' }
I also referred to How can I call a variable from one feature file to another feature file using Karate API Testing. I followed suit for a solution but am getting a javascript error:
feature.deleteVehicle: -unknown-:11 - javascript evaluation failed:
'https://xxx/vehicle'+ vehicleId +'?permanent=yes', ReferenceError: "vehicleId"
is not defined in <eval> at line number 1
feature.SVT: SVT.feature:80 - javascript evaluation failed: vehicleId: '#(vehicleId)' }, <eval>:1:14 Expected eof
but found }
vehicleId: '#(vehicleId)' }
^ in <eval> at line number 1 at column number 14
Can someone kindly help and advise please?
Can you simplify your example ? The only thing I can make out is you need a space after the call feature and before the call argument:
* call read('deleteVehicle.feature') { vehcileId: '#(vehicleId)' }
The pattern we generally recommend is to setUp not tearDown as tearDown has a risk of not executing if you had an error. That said, please see hooks: https://github.com/intuit/karate#hooks
Sometimes you should just keep it simple and call a feature (with args) only where you need it.
I’m having issue with xml post request where post method is not executed. When I try to post same request body in post man it worked.My test is success with 200 but actual request is not executed.
Please let me know if I’m missing
To pass the request body,I’m calling through java object and payload is correctly constructed and printed.In execution test is success and doesn’t print response.But actually test is not executed.
Only headers are printed.
***************** create-user.feature*****************
Feature: create ims user for provided country
Requires country code,
Background:
# load secrets from json
* def createuser = Java.type('com.user.JavaTestData')
* def create = createuser.createUser("US")
Scenario: get service token
Given url imscreateuserurl
And request create
When method post
Then status 200
* print response
***************** create-user.feature*****************
Here is java class
public class JavaTestData {
private static final Logger logger = LoggerFactory.getLogger(JavaTestData.class);
public static String createUser(String countryCodeInput) {
logger.debug("create user for country code input", countryCodeInput);
Unless you post a full working example, no one can help you. Pretty clear that the value of create is null or empty.
Also I personally think you are wasting your time using Java. The whole point of Karate is to avoid using Java as far as possible.
Look at these examples for ideas: https://github.com/intuit/karate/blob/master/karate-junit4/src/test/java/com/intuit/karate/junit4/xml/xml.feature
Edit: also refer to the doc on type-conversion: https://github.com/intuit/karate#type-conversion
#Peter, here is my feature file
Feature: create ims user for provided country
Requires country code,
Background:
# load secrets from json
* def createuser = Java.type('com.adobe.imscreateuser.JavaTestData')
* def create = createuser.createUser("US")
Scenario: get service token
Given url imscreateuserurl
And header Content-Type = 'application/xml; charset=utf-8'
And request create
When method post
Then status 200
* print response
I have performed print for create and showing complete payload.At when method post -> statement its going as null or empty...
Not sure where it is missing
I'm running a test in Jmeter. that test sends in the end the status of the test - "success" or "fail".
I've created a 'user defined variable' that is named 'subject' and assigned it with value 'success'.
within the http requests I've put 'BeanShell Assertion' that assigns the 'subject' variable with 'failure' if the test failed:
if( (ResponseCode != null) && (ResponseCode.equals ("200") == false))
{
//Failure=true;
vars.put("subject","failure");
}
now, in the SMTP sampler I'm sending ${subject} as the subject of the mail.
the sampler doesn't recognise the variable (it is empty).
any ideas?
Can you show the screenshot of your Test Plan? I'm particularly interested in where Beanshell Assertion lives.
JMeter Assertions have their scope, i.e. given the following test structure:
Sampler 1
Assertion
Sampler 2
Assertion will be applied to Sampler 1
In case of the following test plan:
Sampler 1
Sampler 2
Assertion
Assertion will be applied to both Sampler 1 and Sampler 2
Nothing is wrong with your code, it should be setting "subject" variable in case of non "200" response code.
By the way, there is a pre-defined JMeter variable - ${JMeterThread.last_sample_ok} - which returns "true" if previous sampler was successful and "false" in the other case. It is better to use it in combination with the If Controller as Beanshell has known performance issues and can become a bottleneck in case of heavy loads.
The problem was that when using the 'Beanshell Assertion' I didn't pass it the variable - ${subject}, so when the test succeeded it was like the variable was never assigned.