Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PhantomJS and Selenium Webdriver - How to clear session

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
like image 340
schlingel Avatar asked Apr 03 '14 09:04

schlingel


People also ask

How does Selenium handle multiple sessions?

New Selenium IDE To create multiple sessions, we shall add the attributes – parallel and thread-count in the XML file. The attribute thread-count controls the number of sessions to be created while executing the tests in a parallel mode. The value of parallel attribute is set to methods.

What is PhantomJS WebDriver?

PhantomJS is a headless Webkit, which has a number of uses. In this example, we'll be using it, in conjunction with Selenium WebDriver, for conducting basic system tests directly from the command line. Since PhantomJS eliminates the need for a graphical browser, tests run much faster.

What is WebDriver session?

WebDriver is a remote control interface that enables introspection and control of user agents. It provides a platform- and language-neutral wire protocol as a way for out-of-process programs to remotely instruct the behavior of web browsers.

Why do we need session handling while using Selenium WebDriver?

Why do we need Session Handling? During test execution, the Selenium WebDriver has to interact with the browser all the time to execute given commands.


2 Answers

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.

like image 51
oberlies Avatar answered Oct 16 '22 04:10

oberlies


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();
like image 30
djangofan Avatar answered Oct 16 '22 04:10

djangofan