The Resplendent Developer

Software Development and Software Quality Assurance

Cookies between Rest-Assured and Selenium

In my current job, I often have to leverage both Selenium and Rest-Assured in the same code. Selenium will execute all of the requisite browser automation, but occasionally, I will need to make a simple HTTP call. I could spin up an HttpClient object, but Rest-Assured is much more lightweight and legible. As one can imagine I need to pass the cookies from the browser into Rest-Assured. While not terribly difficult, it’s worth noting how to do it.

Here is a Java code snippet that demonstrates:

//Using fully qualified packages because both packages have a “Cookie” class
// driver represents an instance of the org.openqa.Selenium.WebDriver class you are working with.
Set = 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 com.jayway.restassured.response.Cookie.Builder(cookie.getName(), cookie.getValue()).build());

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

Comments are closed.