Its been a long time and I am back after a very tight work schedule.
Earlier I have written an article about dealing with scenarios which involve both UI as well as API testing using Selenium and SoapUI.
Since that time, Rest Assured has been evolved as a most commonly used API Test Automation Framework and also it goes well with Java.
Last week I came across a Selenium Conference talk where I got to know about an interesting library called Selenium to Rest Assured Adapter.
This will simplify some of our automation tests, which needs automation at both UI and API levels.
In my previous posts, Selenium Webdriver – Get SessionID from a Web Application and SoapUI Get SessionID we have explored how to extract a session information from Selenium and passing it into our API Automated tests using SoapUI.
Now we are gonna explore the same using Rest Assured using the Selenium-To-RestAssured library.
For this, we need to download the latest jar file from Download Selenium-To-RestAssured Jar file and you have to add it to your classpath in your IDE – Eclipse or IntelliJ Idea.
If you are using Maven to manage the dependencies in your project, you can add the following to your pom.xml
<dependency> <groupId>uk.co.mwtestconsultancy</groupId> <artifactId>selenium-to-restassured</artifactId> <version>0.1</version> <scope>system</scope> </dependency>
In selenium, to get a cookie from AUT, we use,
driver.manage().getCookieNamed("COOKIE NAME");
To use the cookie in rest assured tests, simply we have to create an instance for the CookieAdapter class and using the convertToRestAssured method like below. Then we can use the cookie in our RestAssured API Tests.
org.openqa.selenium.Cookie cookieToConvert = driver.manage().getCookieNamed("COOKIE NAME"); CookieAdapter cookieAdapter = new CookieAdapter(); io.restassured.http.Cookie adaptedCookie = cookieAdapter.convertToRestAssured(seleniumCookie); given() .cookie(convertedCookie) .get("http://my-url");
The above snippet is extremely useful if we are automating API testing with an application that has a complex login process. We can log into the application via the browser using selenium, grab the necessary logged in Cookies from the browser, close the browser down, and then use the Cookies in our API Tests.
This Selenium-To-RestAssured is a dual way adapter and it converts cookie from RestAssured into a selenium cookie as well.
This can be used when you made an HTTP login request and you have to extract the response Cookie and store them in the browser.
Once we have the converted cookie we can add them to our browser like below.
io.restassured.http.Cookie cookieToConvert = response.getDetailedCookie("COOKIE NAME") CookieAdapter cookieAdapter = new CookieAdapter(); org.openqa.selenium.Cookie convertedCookie = cookieAdapter.convertToSelenium(cookieToConvert); driver.manage().addCookie(convertedCookie); driver.navigate().refresh(); // We refresh the page so it reads the newly added cookies
This library is an open source and you can dig deeper into the source code here – Selenium-To-RestAssured-Github
Thanks to Mark Winteringham for this innovative creation.