Skip to main content

Posts

Showing posts with the label APITesting

Want to auto retry a failing request in postman based on a set number of max tries and with a certain amount of wait time?

  Of course, there are many ways to tackle it but I have mentioned below one of the ways where you can use the given code in the Tests tab of the postman. Unfortunately, in some of the lower environments, there can be a situation where you request fail at times due to infra issues, timeouts, etc., so in that case, this can be super helpful: var notExpectedText = 'Failed'; var maxTries = 3; var pauseBetweenTries = 5000; if (!pm.environment.get("tries")) {     pm.environment.set("tries", 1); } if ((pm.expect(pm.response.text()).to.not.include(notExpectedText)) && (pm.environment.get("tries") < maxTries)) {      var tries = parseInt(pm.environment.get("tries"), 10);      pm.environment.set("tries", tries + 1);      setTimeout(function() {}, pauseBetweenTries);      postman.setNextRequest('PaymentService'); //here you should give your request name that you want to retry on failure  } else {      pm.environment.uns

How to upload a pdf file using REST Assured?

 If you use this code as mentioned in few other blogs and videos:   Map<String, String> headers = new HashMap<String, String>(); headers.put("Content-Type", "multipart/form-data"); byte[] fileContent = FileUtils.readFileToByteArray(new File(filePath)); RestAssured.given().headers(headers).body(fileContent).post(url);   There are high chances that you will get errors related to content-type or "400 - Request is not a multipart request". So, the solution is to use: .multiPart("file", new File("/path/to/file"),"application/pdf"). Please note that I have used "application/pdf" as 3rd param in the multiPart method and this value should be passed as per the file type that you are uploading like for the png file it should be "application/octet-stream", for JSON file it should be "application/JSON". multiPart is an overloaded method that can take max 3 parameters:   a)

Postman: Parse XML and response data value verification using the cheerio library

How to parse XML and validate the response fields using the cheerio library in postman? Sample URL :  https://www.w3schools.com/xml/tempconvert.asmx Sample Request Payload : <soap12:Envelope xmlns:xsi=" http://www.w3.org/2001/XMLSchema-instance " xmlns:xsd=" http://www.w3.org/2001/XMLSchema " xmlns:soap12=" http://www.w3.org/2003/05/soap-envelope ">     <soap12:Body>         <FahrenheitToCelsius xmlns=" https://www.w3schools.com/xml/ ">             <Fahrenheit>108</Fahrenheit>         </FahrenheitToCelsius>     </soap12:Body> </soap12:Envelope> Expected Response : <?xml version="1.0" encoding="utf-8"?> <soap:Envelope xmlns:soap=" http://www.w3.org/2003/05/soap-envelope " xmlns:xsi=" http://www.w3.org/2001/XMLSchema-instance " xmlns:xsd=" http://www.w3.org/2001/XMLSchema ">     <soap:Body>         <FahrenheitToCelsiusResponse xmln

Steps to execute postman collection through newman

  Here is what you need to do to start executing the postman collection through newman on your system: 1. Install NodeJS – https://nodejs.org/en/download . Click on the 32-bit or 64-bit Windows Installer package, depending on your machine configuration. Skip this if NodeJS/npm is already installed on your system. 2. Open a command prompt, and type “node -v” and "npm -v". These commands should work and provide version number if nodeJS installation is successful. 3. Run this command to install newman:   npm install -g newman 4. Install npm package newman-reporter-htmlextra for creating reports.   npm install -g newman-reporter-htmlextra 5. Pull/Export the collection, environment json (if any), test data csv (if any) files from the Postman UI or code repo (git) to your local and run this command through the folder on your local where you have these files: Command where you have collection json and environment json file : newman run "<<your_collectio

Tips and tricks for automation using Postman/Newman

   1. How do we run multiple newman/postman collections sequentially (back to back) at one run ? Batch running multiple newman/postman test collections : Create a batch file like this and run: call newman run Collection1.postman_collection.json -e qa1.postman_environment.json -r htmlextra --reporters cli,junit,htmlextra --reporter-htmlextra-export TestReport1.html -d TestData1.csv call newman run Collection2.postman_collection1.json -e qa2.postman_environment1.json -r htmlextra --reporters cli,junit,htmlextra --reporter-htmlextra-export TestReport2.html -d TestData2.csv   Side note : Give a shot to https://medium.com/@mnu/run-multiple-postman-collection-in-parallel-stress-ee20801922ed in case you would like to run it in parallel (P.S. haven’t tried this yet)   2. How can we set environment variable based on another environment variable value ? Like for QA1, we need accountNo say "123" but for QA2, we need accountNo say "345", etc.   pm.test(&q

Best practices for API automation using Postman

1. Always use Collections and organize tests into folders for testing : Even Though the collection is the top-level of organization, we should have folders that can group together related to requests or demonstrate a detailed workflow. As our API grows in number and complexity, it will become important to organize our tests so they make sense and can be found easily. Therefore, we suggest using folders/sub-folders to group requests by resource, module type, test suite, and workflows. P.S. If require, we can run specific folder(s) inside a collection using “ –folde r ” argument. 2) Use global/environment/collection variables :  Postman also allows us to store data from previous tests into global/collection variables. These variables can be used exactly like environment variables. For example, there is an API that requires data received from another API ( chaining/correlation ). We can store the response attribute (or part of the response) and use that as part of a request head

RestAssured Vs Karate

RestAssured Vs Karate Initial Score: Rest Assured 0- Karate 0 1) RestAssured is really good but there is no built-in way to do a full equality match of a JSON payload in one step (not talking about extracting a particular field out of the response JSON). Contrary to this, Karate offers full JSON Comparison with so much ease (as shown in the first image below). And in case you have some node that shows dynamic value then you can simply ignore it by using hashtag # ignore for that node (as shown in the second image below). Note* There is no direct command to compare the whole response in RestAssured at once but we can still use JsonPath to parse the JSON file into a Map and then compare it with Hamcrest Matchers. Current Score: Rest Assured 0- Karate 1 2) Ease of using matchers: The powerful matching logic of Hamcrest is unbeatable in Rest Assured. Of course, we can use RegEx and Macros in Karate for the match but you need to learn that first. Final Score: Rest Assured 1

Lesson learned from API Testing

Of course, we should ensure that even our basic API tests are built to cover "Triple S" checks i.e. Status code, Schema, and Scenario checks but when we test our APIs, we should NOT just focus on the request and response part of our APIs but most importantly we should understand that how application(s) are going to consume our APIs. This will definitely give you a solid set of additional use cases from an end-user point of view to cover all your bases.