问题描述
有一个测试,比如:
import //needed imports
public class TestClass{
WebDriver driver;
@Before
public void setUp() {
//some code
}
@Test
public void test1() {
//some code, including init of driver (geckodriver)
}
//@After
// public void tearDown() {
// driver.quit();
//}
}
因此,我启动了 geckodriver,并使用 firefox 实例成功运行了我的测试.但我不想在每次运行后关闭 Firefox 窗口,因为我只想分析我拥有的内容,并在测试运行后修复任何需要的内容(稍后我将取消注释 driver.quit()).同时,每次调用而不关闭驱动程序都会导致对我 PC 上的 RAM 产生过度影响(并且无关紧要 - 我是否在测试后手动关闭浏览器):
So, I inited geckodriver, and successfully running my tests, using firefox instances. But I want Not to close firefox window after each run, because I just want to analyse what I have, and fix any needed, after test run(I'm going to unComment driver.quit() later).At the same time, each calling without closing the driver leads to over-impact to RAM on my PC(and does not matter - did I close browser manually, or not, after test):
所以,问题是:有没有办法关闭geckodriver"的进程(更准确地说 - 做 smth,它将关闭 taskmgr 中的 geckodriver.exe 进程),但在测试完成后不会关闭浏览器?例如,在测试本身中添加一些方法,无论如何......这不会影响我的工作/测试本身,我只想添加一些优化.
So, question is:is there any way to close the process(more precisely - do smth, which will close geckodriver.exe process in taskmgr) of "geckodriver", but will NOT close the browser after test finished? e.g., adding some method in test itself, whatever... This not impacts my work/test itself, I just want to add some optimizing.
推荐答案
根据你的问题注释掉 driver.quit()
只是 不要关闭 firefox每次运行后的窗口,因为我只想分析我所拥有的
不会成为最佳实践的一部分.
As per your question commenting out driver.quit()
just Not to close firefox window after each run, because I just want to analyse what I have
won't be a part of best practices.
对于任何详细分析,我们可以创建日志条目并拍摄快照.
在按照最佳实践通过 Selenium
自动化时,您应该调用 quit()
方法中的tearDown() {}
.调用 quit()
DELETE
s 通过发送 "quit" 命令和 {"flags":["eForceQuit"]} 并最终在 /shutdown EndPoint
上发送 GET 请求.下面是一个例子:
While automating through Selenium
as per the best practices you should invoke the quit()
method within the tearDown() {}
. Invoking quit()
DELETE
s the current browsing session through sending "quit" command with {"flags":["eForceQuit"]} and finally sends the GET request on /shutdown EndPoint
. Here is an example below :
1503397488598 webdriver::server DEBUG -> DELETE /session/8e457516-3335-4d3b-9140-53fb52aa8b74
1503397488607 geckodriver::marionette TRACE -> 37:[0,4,"quit",{"flags":["eForceQuit"]}]
1503397488821 webdriver::server DEBUG -> GET /shutdown
因此在调用 quit()
方法后,Web Browser
会话和 WebDriver
实例被完全杀死.因此,您不必合并任何额外的步骤,这将是一个开销.
So on invoking quit()
method the Web Browser
session and the WebDriver
instance gets killed completely. Hence you don't have to incorporate any additional steps which will be an overhead.
如果你想执行杀死悬空的 WebDriver
实例,例如GeckoDriver.exe
实例您可以使用以下任一代码块来杀死任何悬空的 WebDriver
实例:
Still if you want to execute kill the dangling WebDriver
instances e.g. GeckoDriver.exe
instances you can use either of the following code block to kill any of the dangling WebDriver
instances :
Java 解决方案(Windows):
Java Solution(Windows):
import java.io.IOException;
public class Kill_ChromeDriver_GeckoDriver_IEDriverserver
{
public static void main(String[] args) throws Exception
{
Runtime.getRuntime().exec("taskkill /F /IM geckodriver.exe /T");
Runtime.getRuntime().exec("taskkill /F /IM chromedriver.exe /T");
Runtime.getRuntime().exec("taskkill /F /IM IEDriverServer.exe /T");
}
}
Python 解决方案(Windows):
Python Solution (Windows):
import os
os.system("taskkill /f /im geckodriver.exe /T")
os.system("taskkill /f /im chromedriver.exe /T")
os.system("taskkill /f /im IEDriverServer.exe /T")
Python 解决方案(跨平台):
import os
import psutil
PROCNAME = "geckodriver" # or chromedriver or IEDriverServer
for proc in psutil.process_iter():
# check whether the process name matches
if proc.name() == PROCNAME:
proc.kill()
这篇关于Selenium:如何在不调用 driver.quit() 的情况下停止影响 PC 内存的 geckodriver 进程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!