How To Pass Cookies From Selenium WebDriver To Rest-Assured

How to pass cookies from Selenium WebDriver to Rest-Assured? When you do automated testing at API and UI layer, there could be situations where you are doing both and that you need to pass properties from your API test to you UI test or vice versa.

In this example, we show how to pass cookies from Selenium WebDriver to Rest-Assured using Java.

Pass Cookies from Selenium to Rest-Assured

import io.restassured.RestAssured;
import io.restassured.http.Cookies;
import org.openqa.selenium.Cookie;
import org.openqa.selenium.WebDriver;
import org.testng.annotations.Test;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import static io.restassured.RestAssured.given;

public class RestAssuredWebDriverCookie {

    public void cookieTest() {
        WebDriver driver = new ChromeDriver();


        Set<Cookie> seleniumCookies = driver.manage().getCookies();

        // This is where the Cookies will live going forward
        List restAssuredCookies = new ArrayList();

        // Simply pull all the cookies into Rest-Assured
        for (org.openqa.selenium.Cookie cookie : seleniumCookies) {
            restAssuredCookies.add(new io.restassured.http.Cookie.Builder(cookie.getName(), cookie.getValue()).build());

        // Pass them into the Rest-Assured Call
                .cookies(new Cookies(restAssuredCookies))
                .queryParam("id", "1234")