webdriver.Dispose()、.Close() 和 .Quit() 之间的区别 [英] Difference between webdriver.Dispose(), .Close() and .Quit()

查看:22
本文介绍了webdriver.Dispose()、.Close() 和 .Quit() 之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这些有什么区别

  1. Webdriver.Close()
  2. Webdriver.Quit()
  3. Webdriver.Dispose()

什么时候用?

推荐答案

这是一个很好的问题,我看到人们在不应该使用 Close() 时使用了它.我查看了 Selenium Client & 的源代码.WebDriver C# 绑定并发现以下内容.

This is a good question I have seen people use Close() when they shouldn't. I looked in the source code for the Selenium Client & WebDriver C# Bindings and found the following.

  1. webDriver.Close() - 关闭驱动有焦点的浏览器窗口
  2. webDriver.Quit() - 调用 Dispose()
  3. webDriver.Dispose() 关闭所有浏览器窗口并安全结束会话
  1. webDriver.Close() - Close the browser window that the driver has focus of
  2. webDriver.Quit() - Calls Dispose()
  3. webDriver.Dispose() Closes all browser windows and safely ends the session

下面的代码将处理驱动程序对象,结束会话并关闭测试期间打开的所有浏览器,无论测试失败还是通过.

The code below will dispose the driver object, ends the session and closes all browsers opened during a test whether the test fails or passes.

public IWebDriver Driver;

[SetUp]
public void SetupTest()
{
    Driver = WebDriverFactory.GetDriver();
}

[TearDown]
public void TearDown()
{
    if (Driver != null)
      Driver.Quit();
}

总而言之,请确保在退出程序之前调用 Quit() 或 Dispose(),除非您确定自己在做什么,否则不要使用 Close() 方法.

In summary ensure that Quit() or Dispose() is called before exiting the program, and don't use the Close() method unless you're sure of what you're doing.

注意
我在尝试找出为什么我的 VM 的硬盘空间不足的相关问题时发现了这个问题.结果是一个异常导致 Quit() 或 Dispose() 不会在每次运行时被调用,然后导致 appData 文件夹填满硬盘驱动器.所以我们正确使用了 Quit() 方法,但代码无法访问.总结确保所有代码路径都将通过使用异常安全模式或实现 IDisposable 来清理您的非托管对象

Note
I found this question when try to figure out a related problem why my VM's were running out of harddrive space. Turns out an exception was causing Quit() or Dispose() to not be called every run which then caused the appData folder to fill the hard drive. So we were using the Quit() method correctly but the code was unreachable. Summary make sure all code paths will clean up your unmanaged objects by using exception safe patterns or implement IDisposable

还有
在 RemoteDriver 调用 Quit() 或 Dispose() 的情况下,也会关闭 Selenium 服务器上的会话.如果会话未关闭,该会话的日志文件将保留在内存中.

Also
In the case of RemoteDriver calling Quit() or Dispose() will also close the session on the Selenium Server. If the session isn't closed the log files for that session remain in memory.

这篇关于webdriver.Dispose()、.Close() 和 .Quit() 之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆