8
votes

I'm using Selenium Webdriver (Java) and PhantomJS to test a complex JS driven website. My problem is, that the PhantomJS browser keeps the session between two tests which leads to errors in the test setup.

If I run the tests with Firefox everything works fine because Firefox uses a clean session for every test case.

My first attempt to solve the problem was to just clear the local storage by JS injection. Cookies are deleted by the Selenium exposed API driver.manage().deleteAllCookies();

But executing JavaScript without visiting a page is not allowed. So starting the browser at "about:blank" leads to an error.

So, how do I configure my phantomjs webdriver to clear the session?

I'm using phantomjs and webdriver because the selenium-grid services turned out to be not stable enough. So I start my phantomjs instance like that:

phantomjs --webdriver=1234
3
I JUST had the same question! I'm using selenium + phantomjs + wendriver.io + mocha. It just seem logical to start a new session for every test so that they are "clean"... - Mbrevda
Yeah, I think that's some kind of bug or leaky spec inside phantomjs. Because the current url is reset between two tests. - schlingel
I have two tests with two different url's, too. But the cookies are clearly leaking over - Mbrevda
I take that back, it seems to work just fine. Here is a test repo + tests: github.com/mbrevda/phantomjs-test - Mbrevda

3 Answers

7
votes

The fact that PhantomJS keeps sessions between tests is a known problem in GhostDriver, the Selenium Webdriver implementation in PhantomJS.

I suppose that this problem will be fixed with the PhantomJS 2 release. The bug is already fixed in GhostDriver 1.1.1, but there is no PhantomJS version which includes this GhostDriver version.

2
votes

I know that Selenium Grid has a "cleanSession" option if you use GhostDriver. Also, I am pretty sure the regular WebDriver class has a option for this on a local WebDriver instance:

driver.manage().deleteAllCookies();
0
votes

The version 2.0 of PhantomJS fix this issue. If you have a Linux Enviroment, you need clone the sources and compile, like this:

git clone git://github.com/ariya/phantomjs.git
cd phantomjs
git checkout 2.0
./build.sh

More info here