Java PhantomJS 和 Selenium Webdriver - 如何清除会话

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22832996/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 18:11:04  来源:igfitidea点击:

PhantomJS and Selenium Webdriver - How to clear session

javatestingseleniumphantomjsghostdriver

提问by schlingel

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,因为 selenium-grid 服务不够稳定。所以我像这样启动我的 phantomjs 实例:

phantomjs --webdriver=1234

回答by djangofan

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

回答by oberlies

The fact that PhantomJS keeps sessions between tests is a known problemin 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 版本。

回答by Giuseppe Lopes

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

更多信息在这里