So, basically I am using selenium to automate testing against a web application.
Here is what the code is doing broken down into 2 parts:
Part 1:
This is basically some steps to log into the web application.
It has username and password and it sends a POST with POSTDATA to the server.
Everything here is done through Selenium, and you can see it completed when the test is being run, in a web browser as Selenium automates through.
Part 2:
This is basically some steps that sends usernames within the database to a php file on the server using a POST and POSTDATA. (In order to send this POST properly it requires authentication with the web app from Part 1.)
Everything here is done through Java.
Problem:
So the problem I am running into is that Part 1 and Part 2 seems to be treated as mutually exclusive. Once I authenticate with the web application using Selenium, the plain Java code is not using that session. I am having to copy the cookie from the HTTP Headers to directly insert it into the POST in Part 2 from Part 1. This seems to be a very manual process, in getting Part 2 to work.
So I was wondering if there was a way in Part 1 to have Selenium store the cookie somewhere after it authenticates and log ins. (maybe store the cookie into an object or something)
So that in Part 2 I can use Java to call that object and fill in the cookie into the required field, and complete a 2nd POST to the web application without having to manually give it a valid cookie.
After logging in, and the appropriate cookie has been sent to the client browser, you can then use the JavaScriptExecuter to get the value of that cookie to your Java.
Here's how I set up my JavaScriptExecuter (yours may look a little different):
//setup
ProfilesIni profile = new ProfilesIni();
FirefoxProfile ffprofile = profile.getProfile("SELENIUM");
driver = new FirefoxDriver(ffprofile);
js = (JavascriptExecutor) driver;
Here's a String containing JavaScript to read cookies. You only need to add it to the page (execute it) once for every page.
private final String readCookieFunction =
"function readCookie(name) { " +
"// name is the cookie you want to read - returns the value of specified cookie " +
"var nameEQ = name + '='; " +
"var ca = document.cookie.split(';'); " +
"for ( var i = 0; i < ca.length; i++) { " +
"var c = ca[i]; " +
"while (c.charAt(0) == ' ') " +
"c = c.substring(1, c.length); " +
"if (c.indexOf(nameEQ) == 0) " +
"return c.substring(nameEQ.length, c.length); " +
"} " +
"return null; " +
"} ";
So, if you reopen or refresh the page, you'll need to add/execute that again before executing the following JS which actually calls that function:
private final String readCookie = "return readCookie(name);"
Here, the JavaScript variable "name" is the name of your cookie - a string - so if your site under test is stackoverflow, the above JS would look something like this instead:
private final String readCookie = "return readCookie('stackoverflow.com');"
Anyway, after that initial setup, getting the value of the cookie is simple. Part of your test might look like this:
// Add the function to the page's JS
js.executeScript(readCookieFunction);
// Call that function to get the cookie's value from the page into Java
String cookieValue = (String) js.executeScript(readCookie);