PhantomJS和Selenium Webdriver - 如何清除会话

时间:2022-05-21 02:23:23

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.

我正在使用Selenium Webdriver(Java)和PhantomJS来测试复杂的JS驱动的网站。我的问题是,PhantomJS浏览器会在两次测试之间保持会话,从而导致测试设置出错。

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

如果我使用Firefox运行测试一切正常,因为Firefox为每个测试用例使用了一个干净的会话。

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();

我解决问题的第一个尝试是通过JS注入清除本地存储。 Cookie被Selenium公开的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.

但是不允许在不访问页面的情况下执行JavaScript。因此,在“about:blank”处启动浏览器会导致错误。

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

那么,如何配置我的phantomjs webdriver来清除会话?

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,因为硒网格服务结果不够稳定。所以我开始我的phantomjs实例:

phantomjs --webdriver=1234

3 个解决方案

#1


7  

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

PhantomJS在测试之间保持会话的事实是GhostDriver中的一个已知问题,即PhantomJS中的Selenium Webdriver实现。

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.

我想这个问题将通过PhantomJS 2版本修复。该错误已在GhostDriver 1.1.1中修复,但没有包含此GhostDriver版本的PhantomJS版本。

#2


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:

我知道如果你使用GhostDriver,Selenium Grid有一个“cleanSession”选项。此外,我很确定常规WebDriver类在本地WebDriver实例上有一个选项:

driver.manage().deleteAllCookies();

#3


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:

PhantomJS 2.0版解决了这个问题。如果您有Linux环境,则需要克隆源代码并进行编译,如下所示:

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

More info here

更多信息在这里

#1


7  

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

PhantomJS在测试之间保持会话的事实是GhostDriver中的一个已知问题,即PhantomJS中的Selenium Webdriver实现。

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.

我想这个问题将通过PhantomJS 2版本修复。该错误已在GhostDriver 1.1.1中修复,但没有包含此GhostDriver版本的PhantomJS版本。

#2


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:

我知道如果你使用GhostDriver,Selenium Grid有一个“cleanSession”选项。此外,我很确定常规WebDriver类在本地WebDriver实例上有一个选项:

driver.manage().deleteAllCookies();

#3


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:

PhantomJS 2.0版解决了这个问题。如果您有Linux环境,则需要克隆源代码并进行编译,如下所示:

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

More info here

更多信息在这里