Skip to main content

Posts

Showing posts from December, 2019

RestAssured rest API test framework for GET & POST API json verification with Extent report

package com.resttest; import static io.restassured.RestAssured.given; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.sql.DriverManager; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.regex.Pattern; import org.apache.commons.collections.iterators.EntrySetMapIterator; import org.apache.commons.io.FileUtils; import org.apache.commons.io.IOUtils; import org.json.JSONException; import org.json.JSONObject; import org.skyscreamer.jsonassert.JSONAssert; import org.skyscreamer.jsonassert.JSONCompareMode; import org.springframework.http.HttpEntity; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpMethod; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.web.client.RestTemplate; import org.testng.Assert; import org.testng.TestNG; import org.testng.annotations.AfterTest; import org.testn...

Adding Extent reporting for QA test automation framework

Define these globally: ExtentLoggerReporter logger = new ExtentLoggerReporter("/ebs/test/build/"); //The path given here will be used to store temp files for extent,create once. static ExtentReports extent; Define this method to initialise Extent configs before Test: @BeforeTest public void initialiseExtentReport() { logger.config().setAutoCreateRelativePathMedia(true); extent = new ExtentReports(); ExtentHtmlReporter html = new ExtentHtmlReporter("Extent.html"); extent.attachReporter(logger); extent.attachReporter(html); extent.loadConfig(new File(System.getProperty("user.dir")+"/extent-config.xml")); //extent-config.xml file content given below } For pom.xml add: <dependency>     <groupId>com.aventstack</groupId>     <artifactId>extentreports</artifactId>     <version>4.0.9</version> </dependency> On Our code, Wherever we are passing/failing the test step, we can use below c...

Extent report for Rest API test

Below code helps in failing or passing your Rest API test case & updating result to Extent report. Maven dependency(pom.xml): <dependency>     <groupId>com.aventstack</groupId>     <artifactId>extentreports</artifactId>     <version>4.0.9</version> </dependency> code: import com.aventstack.extentreports.ExtentReports; import com.aventstack.extentreports.reporter.ExtentHtmlReporter; import com.aventstack.extentreports.reporter.ExtentLoggerReporter; public class A{ ExtentLoggerReporter logger = new ExtentLoggerReporter("dir path to store extent result"); static ExtentReports extent; @Test public void testname(){ //Your code to capture json from get/post/put/delete API to "JSONObject jsonobjexpected" field. //your code to read actual json content from file/anywhere to "JSONObject jsonobjactual" field. try {         JSONAssert.assertEquals(jsonobjexpected.toString(...

How to execute linux command using java with printing output

There are two ways to execute linux commands in java, we illustrated both of them below, google to know difference between these. Below code is to print output of linux command to console using Runtime: import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public static void executelinuxcommand(String linux_command) { StringBuilder output = new StringBuilder(); try { Process process = Runtime.getRuntime().exec(linux_command); BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream())); String line; while ((line = reader.readLine()) != null) { output.append(line + "\n"); } int exitVal = process.waitFor(); if (exitVal == 0) { System.out.println("Linux command output :\n"+output.substring(0).toString()); } else { System.out.println("***** Linux command failed to execute"); } } catch (IOException e) { e.printStackTra...

How to use dynamic argument value in calling function name

import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; public class testlinux{ public static void main(String[] args) throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException{ int functionnameparam=4; String str1="Goodmorning",str2=" guys"; Method method=testlinux.class.getDeclaredMethod("methodcall"+functionnameparam, String.class);//here we passing String.class because we are passing String type arguments in below method,if you need to pass other type(int/long etc) update according to your class.         method.invoke(new Object(),str);//this is same as calling "methodcall4(str1);"         method.invoke(new Object(),str1+"/"+str2);//this is same as calling "methodcall4(str1+"/"+str2);" } public static void methodcall4(String mergedstr1andstr2) { System.out.println("str= "+mergedstr1andstr2)...