在我用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
但它永远不会关闭......任何想法?
答案 0 :(得分:1)
[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();
,您可以将Dispose放在类的解构函数中或者那些长的那些如果你想要这些行,当退出类时,将调用解构函数并执行你的代码行。
使用语句将为您进行清理。
答案 1 :(得分:0)
我无法使用完全相同的环境重现这一点。 (通过nunit.exe
运行测试)
Windows 7,PhantomJS 1.9.0,Selenium WebDriver API 2.32.1和NUnit 2.6.2.12296,.NET 3.5。
由于Selenium和PhantomJS都升级到新版本。我建议你也升级你的,看看这个问题是否会再次出现。
Selenium:selenium-dotnet-2.33.0.zip
PhantomJs:phantomjs-1.9.1-windows.zip
同样是旁注,我认为您不需要同时调用Quit()
和Dispose()
,因为source code,这就是Quit()
方法的实现方式。
/// <summary>
/// Close the Browser and Dispose of WebDriver
/// </summary>
public void Quit()
{
this.Dispose();
}