Skip to main content

Day 11: Handling Timeouts and Error Responses

Day 11: Handling Timeouts and Error Responses

In Rest Assured, handling timeouts and error responses is crucial for building robust and reliable API test suites. Timeouts help control the maximum time allowed for a request to complete, while error responses provide valuable information about the failure scenarios encountered during API interactions. Let's delve into more detail about handling timeouts and error responses using Rest Assured.

  1. Handling Timeouts:
  2. Timeouts are used to define the maximum time to wait for a response from an API endpoint. It is essential to set appropriate timeouts to prevent excessive waiting or hanging of test cases. Rest Assured provides options to configure timeouts for different stages of an HTTP request, such as connection timeouts and read timeouts. Here's how you can handle timeouts:

    Connection Timeout:

    The connection timeout is the maximum time allowed for establishing a connection with the server. You can set the connection timeout using the RestAssured.config() method as follows:

           
          RestAssured.config = RestAssured.config().connectionConfig(
            ConnectionConfig.connectionConfig().timeout(5000)
    );
    
             
    

    In the above code, we set the connection timeout to 5000 milliseconds (5 seconds). Adjust the timeout value based on the requirements of your API.

    Read Timeout:

    The read timeout is the maximum time allowed for receiving the response from the server after the connection has been established. You can set the read timeout using the RestAssured.config() method as follows:

            
           
           RestAssured.config = RestAssured.config().httpClient(
            HttpClientConfig.httpClientConfig().setParam("http.connection.timeout", 5000)
                    .setParam("http.socket.timeout", 5000)
    );
             
    

    In the above code, we set both the connection and socket timeouts to 5000 milliseconds (5 seconds). Adjust the timeout values based on your specific needs.

  3. Handling Error Responses:
  4. Error responses provide valuable information about failures encountered during API interactions. Rest Assured allows you to handle error responses and extract meaningful details for further analysis or reporting. When an error response is received, Rest Assured throws an exception, providing access to the response status code, response body, and other relevant information.
    To handle error responses, you can use assertions to verify the expected status code or extract error details from the response body. For example:

            
           Response response = RestAssured.get("https://api.example.com/users/123");
    int statusCode = response.getStatusCode();
    
    if (statusCode == 200) {
        // Process successful response
    } else {
        String responseBody = response.getBody().asString();
        // Extract and handle error details from the response body
        // Report or perform appropriate actions based on the error scenario
    }
             
    

    In the above code, we send a GET request to an API endpoint and retrieve the status code using getStatusCode(). If the status code indicates success (200), we can process the successful response. Otherwise, we extract the response body as a string and handle the error details.

By effectively handling timeouts, you can control the maximum wait time for API responses and prevent test cases from hanging indefinitely. Additionally, handling error responses allows you to extract and analyze important information when API interactions encounter failures. These practices contribute to building reliable and resilient API test suites using Rest Assured.

Comments

Popular posts from this blog

How to integrate Autoit with selenium

For handling Dialog boxes which are not web based, Then Autoit is the best Tool to handle this These are the following code should written in Selenium Selenium Code: try { String[] commands = new String[] {}; commands = new String[] { "Path" }; // location of Autoit EXE file Runtime.getRuntime().exec(commands); } catch (IOException e) { } Autoit code: if WinWaitActive("File Upload") Then ;MsgBox(2,"window found","Found the window") WinActivate("File upload") Send("!n") Sleep(5000) Send("File path") SEND("{ENTER}") ;location of the file you want to a to the form and submit ;Send("!O") Else MsgBox(1,"TimeOut","Timed out") EndIf Here we need to create a Exe file for the Autoit script and that path should be mentioned in selenium code.

Reflection API

The Reflection API allows Java code to examine classes and objects at run time.The new reflection classes allow you to call another class's methods dynamically at run time. With the reflection classes, you can also examine an instance's fields and change the fields' contents. The Reflection API consists of the java.lang.Class class and the java.lang.reflect classes: Field, Method, Constructor, Array, and Modifier. Example program: import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; // calling method whose name we store in a variable public class ReflectionAPI { public static void main(String[] args) throws SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException { String x="sampleTest"; //String meth=read name from xls file Method method= ReflectionAPI.class.getMethod(x, String.class); method.invoke(method, "welcome"); System.out.

Collection API

CollectionAPI: The Java Collections API's provide Java developers with a set of classes and interfaces that makes it easier to handle collections of objects. In a sense Collection's works a bit like arrays, except their size can change dynamically, and they have more advanced behaviour than arrays. Example program: import java.util.ArrayList; import java.util.Hashtable; public class CollectionAPI { /** * @param args */ public static void main(String[] args) { int names[] = new int[5]; ArrayList list = new ArrayList (); list.add("Ramu");//0 list.add("Venu");//1 list.add("Raju");//2 System.out.println(list.size()); for(int i =0; i<=list.size(); i++) { System.out.println(list.get(i)); } // key - value // key - unique Hashtable table = new Hashtable (); table.put("name", "Hyderabad"); table.put("place", "Mumbai"); table.put("na