首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >GhostDriver在Selenium测试运行后不退出

GhostDriver在Selenium测试运行后不退出
EN

Stack Overflow用户
提问于 2013-04-27 00:41:01
回答 2查看 2K关注 0票数 2

在我用GhostDriver (PhantomJS)进行的第一个非常简单的Selenium测试中,测试通过了,但是PhantomJS.exe没有退出。我在Windows7,PhantomJS 1.9.0,Selenium WebDriver API2.32.1和NUnit 2.6.2.12296,.NET 3.5上运行。

下面是我的C#/Nunit/WebDriver代码:

代码语言:javascript
复制
[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的所有内容:

代码语言:javascript
复制
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

但它永远不会关机。有什么想法吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-07-19 09:18:33

使用完全相同的环境,我无法重现这段代码。(通过nunit.exe运行测试)

Windows7、PhantomJS 1.9.0、Selenium WebDriver API2.32.1和NUnit 2.6.2.12296、.NET 3.5。

因为Selenium和PhantomJS都升级到了较新的版本。我建议你也升级你的版本,看看这个问题是否会再次出现。

Seleniumselenium-dotnet-2.33.0.zip

PhantomJsphantomjs-1.9.1-windows.zip

另外,我认为你不需要像在source code中那样同时调用Quit()Dispose(),这就是Quit()方法的实现方式。

代码语言:javascript
复制
/// <summary>
/// Close the Browser and Dispose of WebDriver
/// </summary>
public void Quit()
{
   this.Dispose();
}
票数 0
EN

Stack Overflow用户

发布于 2015-02-07 11:20:47

代码语言:javascript
复制
[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
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16241288

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档