Skip to content

Commit

Permalink
Session Isolation within the same Process instance.
Browse files Browse the repository at this point in the history
Finally an API to support multi-cookie jar is available in PhantomJS:
ariya/phantomjs#11535.

This allows us to create a completely new CookieJar every time
we create a Session.

A version of PhantomJS with the new CookieJar API
hasn't been released yet: once the commit linked above
is part of a stable release, this will work.
Otherwise, stick with GhostDriver 1.1.0.

Fixes #170.
  • Loading branch information
detro committed Jan 12, 2014
1 parent 4537a16 commit e401410
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 4 deletions.
22 changes: 18 additions & 4 deletions src/session.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ ghostdriver.Session = function(desiredCapabilities) {
},
_windows = {}, //< NOTE: windows are "webpage" in Phantom-dialect
_currentWindowHandle = null,
_cookieJar = require('cookiejar').create(),
_id = require("./third_party/uuid.js").v1(),
_inputs = ghostdriver.Inputs(),
_capsPageSettingsPref = "phantomjs.page.settings.",
Expand Down Expand Up @@ -284,8 +285,12 @@ ghostdriver.Session = function(desiredCapabilities) {
_addNewPage = function(newPage) {
_log.debug("_addNewPage");

_decorateNewWindow(newPage); //< decorate the new page
_windows[newPage.windowHandle] = newPage; //< store the page/window
// decorate the new Window/Page
newPage = _decorateNewWindow(newPage);
// set session-specific CookieJar
newPage.cookieJar = _cookieJar;
// store the Window/Page by WindowHandle
_windows[newPage.windowHandle] = newPage;
},

// Delete any closing page from the "_windows" container of this session
Expand Down Expand Up @@ -487,9 +492,15 @@ ghostdriver.Session = function(desiredCapabilities) {

// Ensure a Current Window is available, if it's found to be `null`
if (_currentWindowHandle === null) {
// First call to get the current window: need to create one
page = _decorateNewWindow(require("webpage").create());
// Create the first Window/Page
page = require("webpage").create();
// Decorate it with listeners and helpers
page = _decorateNewWindow(page);
// set session-specific CookieJar
page.cookieJar = _cookieJar;
// Make the new Window, the Current Window
_currentWindowHandle = page.windowHandle;
// Store by WindowHandle
_windows[_currentWindowHandle] = page;
}
},
Expand Down Expand Up @@ -616,6 +627,9 @@ ghostdriver.Session = function(desiredCapabilities) {
for (k in _windows) {
_closeWindow(k);
}

// Release CookieJar resources
_cookieJar.close();
},

_getLog = function (type) {
Expand Down
55 changes: 55 additions & 0 deletions test/java/src/test/java/ghostdriver/IsolatedSessionTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package ghostdriver;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.Cookie;
import org.openqa.selenium.WebDriver;

import java.util.Set;

import static org.junit.Assert.assertFalse;

public class IsolatedSessionTest extends BaseTest {
// New Session Cookies will be stored in here
private String url = "http://www.google.com";
private Set<Cookie> sessionCookies;

@Before
public void createSession() throws Exception {
disableAutoQuitDriver();

WebDriver d = getDriver();
d.get(url);

// Grab set of session cookies
sessionCookies = d.manage().getCookies();

// Manually quit the current Driver and create a new one
d.quit();
prepareDriver();
}

@Test
public void shouldCreateASeparateSessionWithEveryNewDriverInstance() {
WebDriver d = getDriver();
d.get(url);

// Grab NEW set of session cookies
Set<Cookie> newSessionCookies = d.manage().getCookies();

// No cookie of the new Session can be found in the cookies of the old Session
for (Cookie c : sessionCookies) {
assertFalse(newSessionCookies.contains(c));
}
// No cookie of the old Session can be found in the cookies of the new Session
for (Cookie c : newSessionCookies) {
assertFalse(sessionCookies.contains(c));
}
}

@After
public void quitDriver() {
getDriver().quit();
}
}

0 comments on commit e401410

Please sign in to comment.