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

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

问题描述

它们之间有什么区别

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

何时使用哪个?

推荐答案

这是一个很好的问题,我已经看到人们在不应该使用Close()的情况下.我查看了Selenium Client& amp;的源代码. 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 Server上的会话.如果未关闭会话,则该会话的日志文件将保留在内存中.

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天全站免登陆