gpt4 book ai didi

c# - Selenium 测试运行后 GhostDriver 没有退出

转载 作者:太空宇宙 更新时间:2023-11-03 13:43:17 24 4
gpt4 key购买 nike

在我使用 GhostDriver (PhantomJS) 对 Selenium 进行的第一个非常简单的测试中,测试通过了,但 PhantomJS.exe 没有退出。我在 Windows 7、PhantomJS 1.9.0、Selenium WebDriver API 2.32.1 和 NUnit 2.6.2.12296、.NET 3.5 上运行。

这是我的 C#/Nunit/WebDriver 代码:

[TestFixture]
public class Driver
{
IWebDriver driver;

[SetUp]
public void Setup()
{
driver = new PhantomJSDriver(@"D:\src\Tests\Drivers");
}

[TearDown]
public void Teardown()
{
driver.Quit();
driver.Dispose();
}

[Test]
public void GoogleSearch()
{
//Navigate to the site
driver.Navigate().GoToUrl("http://www.google.com");
Assert.AreEqual("Google", driver.Title);
}
}

这里是 PhantomJS 的所有内容:

PhantomJS is launching GhostDriver...
[INFO - 2013-04-26T16:38:56.417Z] GhostDriver - Main - running on port 64183
[INFO - 2013-04-26T16:38:56.630Z] Session [c9f2b8e0-ae8f-11e2-a7c1-159b6700bc86
] - CONSTRUCTOR - Desired Capabilities: {"browserName":"phantomjs","version":"",
"platform":"ANY"}
[INFO - 2013-04-26T16:38:56.649Z] Session [c9f2b8e0-ae8f-11e2-a7c1-159b6700bc86
] - CONSTRUCTOR - Negotiated Capabilities: {"browserName":"phantomjs","version":
"1.9.0","driverName":"ghostdriver","driverVersion":"1.0.3","platform":"windows-7
-32bit","javascriptEnabled":true,"takesScreenshot":true,"handlesAlerts":false,"d
atabaseEnabled":false,"locationContextEnabled":false,"applicationCacheEnabled":f
alse,"browserConnectionEnabled":false,"cssSelectorsEnabled":true,"webStorageEnab
led":false,"rotatable":false,"acceptSslCerts":false,"nativeEvents":true,"proxy":
{"proxyType":"direct"}}
[INFO - 2013-04-26T16:38:56.701Z] SessionManagerReqHand - _postNewSessionComman
d - New Session Created: c9f2b8e0-ae8f-11e2-a7c1-159b6700bc86
[INFO - 2013-04-26T16:38:59.470Z] ShutdownReqHand - _handle - About to shutdown

但它永远不会关闭...有什么想法吗?

最佳答案

[TestFixture]
public class Driver
{
[Start]
public void Start()
{
using (var driver = new PhantomJSDriver(@"D:\src\Tests\Drivers"))
{
driver.Url = "https://www.google.com";
Assert.AreEqual("Google", driver.Title);
}
}
}

这将解决您的问题,只要始终记住在可以或显式调用 driver.Dispose(); 时将事物封装在 using 语句中,如果它是公共(public)变量,则可以将 Dispose 放在析构函数中如果您愿意,可以在类的任何部分或一些很长的行中,当类退出时,将调用解构函数并执行您的代码行。

使用语句将为您进行清理。

关于c# - Selenium 测试运行后 GhostDriver 没有退出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16241288/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com