Rest Assured and Selenium

562 views
Skip to first unread message

Kiruthika Subramanian

unread,
Apr 24, 2013, 2:49:34 PM4/24/13
to rest-a...@googlegroups.com
Hi Johan,
Hope you are well.

I am logging into a web app with Selenium and from there on wanting to use REST Assured for REST API testing.
Web app login thro selenium works fine. Rest Assured is trying to login in parallel and fails.

I did a getCookies() soon after Selenium logs in but looks like Rest Assured does not recognize and is working (trying to login) in parallel.

Two questions:
1. Is it even possible to do what I am trying to do, Use Selenium and Rest Assured as one? I would assume yes.
2. How can I make Rest Assured use Selenium's session and cookies?

Please advice.

As always thank you for your time.

Thanks,
Kiruthika

Johan Haleby

unread,
Apr 25, 2013, 2:14:44 AM4/25/13
to rest-a...@googlegroups.com
Hi, 
  1. You could turn off all authentication by using given().auth().none(). ..
  2. Yes once you get the first response from Selenium extract the session cookie and attach it to REST Assured, given().sessionId(..) .. 
Regards,
/Johan

--
You received this message because you are subscribed to the Google Groups "REST assured" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rest-assured...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Kiruthika Subramanian

unread,
Apr 25, 2013, 10:42:30 AM4/25/13
to rest-a...@googlegroups.com
Thanks Johan.

Here is a snippet of my code:

PropertyFile propFile = PropertyFile.getInstance();

propFile.read("BrowserURL");

setUsername(propFile.read("CustomerUserName"));

setPassword(propFile.read("CustomerPassword"));

String serverName1 = propFile.read("RestUrl");

login();

System.out.println("Did the Cookie jar get there?");

     given().auth().none().expect().statusCode(200).body(

          "status", equalTo("ok")).

        when().

        get(serverName1+"/uriForREST/api/detail").asString();

    }

In the login() method, I login using selenium and collect the cookies with REST Assured.
Snippet:

public NavigationResult clickAndWaitForCookies(String 

url, String timeout, String errorMessage) {

NavigationResult result = open("https://serverURL");

System.out.println("LoggedIn looking for cookies?");

Response response = given().get("https://serverUrl/context");

Map<String, String> allCookies = response.getCookies();

System.out.println("I think I got em?! : " +allCookies);

String sessionId = response.getSessionId();

RestAssured.sessionId = response.getSessionId();

System.out.println("Might Have got the sessionId too?! : " +sessionId);

return result;

}



What am I missing?!
Please advice.

Thanks as always!!!

Kiruthika Subramanian

unread,
Apr 25, 2013, 10:43:22 AM4/25/13
to rest-a...@googlegroups.com
Also, I do get my cookie jar printed. How would I use those cookies?

Johan Haleby

unread,
Apr 26, 2013, 1:54:08 AM4/26/13
to rest-a...@googlegroups.com
To use the cookie apply it to rest assured:

given().cookie(<name>, <cookie_value>). ..

Or as I said before if the cookie represents a session id (jsessionid) you could use given.sessionId(..)

/Johan

--

Kiruthika Subramanian

unread,
Apr 26, 2013, 2:28:06 PM4/26/13
to rest-a...@googlegroups.com
Thanks Johan, thats not working, trying to find a work around.

Thanks for your time as always :-)

Johan Haleby

unread,
Apr 26, 2013, 5:58:03 PM4/26/13
to rest-a...@googlegroups.com
Is it getting the session id or setting the cookie that doesn't work?

Kiruthika Subramanian

unread,
Apr 27, 2013, 6:17:48 PM4/27/13
to rest-a...@googlegroups.com
Yeah, I am getting the sessionId and Cookies, printing it, looks fine.
But when I pass it to RA request, it does not recognize the session nor the cookie.

Kiruthika Subramanian

unread,
Apr 27, 2013, 11:12:57 PM4/27/13
to rest-a...@googlegroups.com
Johan,
I was able to get this working, As always, Thanks for your help.
Extracted all the cookie and Session information from my Selenium login and reused it for RA.

Have a nice weekend.

Thanks,
Kiruthika

Johan Haleby

unread,
Apr 28, 2013, 2:36:26 AM4/28/13
to rest-a...@googlegroups.com
Glad to hear you got it working. 

Regards,
/Johan

Karandeep Singh

unread,
Apr 3, 2014, 12:25:13 PM4/3/14
to rest-a...@googlegroups.com
Can you explain how you got this working?

Thanks!
Reply all
Reply to author
Forward
0 new messages