Skip to main content

Day 9: Handling cookies and sessions

Day 9: Handling cookies and sessions

Handling cookies and sessions are an important aspect of testing web applications. Rest Assured provides several methods for handling cookies and sessions.



  1. Capturing cookies: You can capture cookies from a response using the extract().response() method, and then store them for later use. Here's an example:

  2. 
    Response response = given()
            .get("https://example.com/api")
            .then()
            .extract().response();
    
    String cookieValue = response.getCookie("session_id");
    
      
    

    In this example, the getCookie() method is used to extract the value of the session_id cookie from the response.

  3. Reusing cookies: You can reuse cookies in subsequent requests by setting them as a header using the header() method. Here's an example:

  4. 
           given()
            .header("Cookie", "session_id=" + cookieValue)
            .get("https://example.com/api")
            .then()
            .statusCode(200);
    
      
    

    In this example, the header() method is used to set the session_id cookie value as a header in the subsequent request.

  5. Managing sessions: You can manage user sessions by capturing and reusing cookies as described above. For example, you can log in to a website by sending a POST request with the user's credentials, and then capture the session cookie. You can then reuse the session cookie in subsequent requests to simulate a logged-in user session.

  6. 
           Response response = given()
            .param("username", "testuser")
            .param("password", "testpassword")
            .post("https://example.com/login")
            .then()
            .extract().response();
    
    String sessionCookie = response.getCookie("session_id");
    
    given()
            .header("Cookie", "session_id=" + sessionCookie)
            .get("https://example.com/profile")
            .then()
            .statusCode(200);
    
      
    

    In this example, the user logs in by sending a POST request to the login endpoint with the username and password parameters. The session cookie is then extracted from the response and set as a header in the subsequent request to the user's profile page.

    These are some of the ways in which Rest Assured can be used to handle cookies and sessions.

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