8

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
schlingel
  • 8,560
  • 7
  • 34
  • 62
  • 1
    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 Apr 03 '14 at 09:22
  • Yeah, I think that's some kind of bug or leaky spec inside phantomjs. Because the current url is reset between two tests. – schlingel Apr 03 '14 at 09:23
  • I have two tests with two different url's, too. But the cookies are clearly leaking over – Mbrevda Apr 03 '14 at 09:24
  • I take that back, it seems to work just fine. Here is a test repo + tests: https://github.com/mbrevda/phantomjs-test – Mbrevda Apr 03 '14 at 09:58

3 Answers3

7

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.

oberlies
  • 11,503
  • 4
  • 63
  • 110
2

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();
djangofan
  • 28,471
  • 61
  • 196
  • 289
  • I already mentioned that I don't want to use selenium grid. We had it in production and the thing was unstable in the long run. And I already use the deleteAllCookies code - as mentioned in the question. If you know which option and how to turn it on, please elaborate. – schlingel Apr 04 '14 at 09:48
  • If you look at the changelog on Selenium 2.40.0, it has a PhantomJS change. Were you using the latest 2.41.0 grid with ghostdriver? If not, it might be worth trying again. I assume you aren't using Chrome or IE. – djangofan Apr 04 '14 at 14:55
  • ... As stated I use PhantomJS as browser. And I don't use Selenium Grid. I just use regular selenium with its webdriver api. (phantomjs --webdriver=PORT starts the webdriver compatible phantomjs browser). But the problem is, that the session is persistant and therefore my webshop starts at a different start point when I run the second test case. – schlingel Apr 15 '14 at 10:26
  • The change in 2.40.0 might also affect client-side PhantomJS. Do you use the PhantomJS option: phantom.cookiesEnabled = true; ? You should have full access to cookies using a JavascriptExecutor object; did you try that? – djangofan Apr 15 '14 at 14:50
  • Just checked my dependency in my pom.xml. I use 2.40. Puncto Cookies-Enabled. I can rest the cookies via JS. But for that I have to load the page first. Execute the JS and load the Page again. (If I don't do that I get DOM Security exceptions). That sucks pretty much. – schlingel Apr 15 '14 at 15:43
  • @schlingel You are saying that you "delete all cookies" in your question. To me, this doesn't sound like you used `driver.manage().deleteAllCookies()` – oberlies May 15 '14 at 12:05
  • @oberlies I did though. The only thing I had to clear (or at least I tried to do that) by JS injection was the localStorage. Doesn't work nevertheless. But by running the tests with maven surefire in parallel beats PhantomJS in execution speed anyways. – schlingel May 15 '14 at 13:41
  • @schlingel That is very well possible, but you should put this information into your question, and not somewhere into the comments. You are saying "And I already use the deleteAllCookies code - as mentioned in the question", but "deleteAllCookies" does not occur in your question. So, please edit your question and e.g. explicitly mention the method in code formatting. – oberlies May 15 '14 at 14:00
  • I thought it would be obvious that I'd use the available API. But, why not. But if it makes you happy, I'll update the question for you. – schlingel May 15 '14 at 14:12
0

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

Mike G
  • 4,232
  • 9
  • 40
  • 66
Giuseppe Lopes
  • 101
  • 1
  • 7