【问题标题】:GhostDriver doesn't quit after Selenium test run在 Selenium 测试运行后 GhostDriver 不会退出
【发布时间】:2013-04-26 16:41:00
【问题描述】:

在我使用 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

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

【问题讨论】:

    标签: c# selenium nunit phantomjs ghostdriver


    【解决方案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 放在类的解构器中,或者如果你想要的,当类退出时,将调用解构函数并执行你的代码行。

    Using 语句会为您完成清理工作。

    【讨论】:

      【解决方案2】:

      使用完全相同的环境,我无法重现这一点。 (通过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();
      }
      

      【讨论】:

      • 谢谢,也许它已经修复了,然后......我只是对退出/处置格外谨慎 - 因为问题出在那个区域。
      • 显然没有发生任何事情。将接受答案,因为我现在无法重新测试。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-03-27
      • 2013-05-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-25
      • 1970-01-01
      相关资源
      最近更新 更多