如何通过保持 webDriver 活动关闭整个浏览器窗口? [英] How to close the whole browser window by keeping the webDriver active?

查看:157
本文介绍了如何通过保持 webDriver 活动关闭整个浏览器窗口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的批处理执行中,第一个场景打开了多个带有多个选项卡的浏览器.我想在开始第二个场景之前关闭所有这些浏览器.

In my batch execution, multiple browsers with multiple tabs are getting opened for first scenario. I wanted to close all these browsers before starting second scenario.

Driver.close() 只是关闭浏览器的一个选项卡.Driver.quit() 正在关闭所有浏览器并结束 WebDriver 会话.所以,我无法运行批处理.请为此提供解决方案.

Driver.close() is just closing one tab of the browser. Driver.quit() is closing all the browsers and also ending the WebDriver session. So , am unable to run the batch execution. Please provide a solution for this.

推荐答案

下面的解释应该解释 WebDriver 中 driver.closedriver.quit 方法之间的区别.我希望你觉得它有用.

The below explanation should explain the difference between driver.close and driver.quit methods in WebDriver. I hope you find it useful.

driver.closedriver.quit 是在 Selenium WebDriver 中关闭浏览器会话的两种不同方法.

driver.close and driver.quit are two different methods for closing the browser session in Selenium WebDriver.

了解它们并知道何时使用每种方法对于您的测试执行很重要.因此,我试图阐明这两种方法.

Understanding both of them and knowing when to use each method is important in your test execution. Therefore, I have tried to shed some light on both of these methods.

driver.close - 此方法关闭设置焦点的浏览器窗口.driver.quit 关闭 webdriver 的会话,同时driver.close 仅关闭存在 selenium 控制但 webdriver 会话尚未关闭的当前窗口,如果没有其他窗口打开并且您调用driver.close 然后它也关闭了 webdriver 的会话.

driver.close - This method closes the browser window on which the focus is set. driver.quit close the session of webdriver while driver.close only close the current window on which selenium control is present but webdriver session not close yet, if no other window open and you call driver.close then it also close the session of webdriver.

driver.quit – 此方法基本上调用 driver.dispose 一个现在内部方法,该方法依次关闭所有浏览器窗口并优雅地结束 WebDriver 会话.

driver.quit – This method basically calls driver.dispose a now internal method which in turn closes all of the browser windows and ends the WebDriver session gracefully.

driver.dispose - 如前所述,是 WebDriver 的内部方法,根据另一个答案 - 需要验证,它已被悄悄删除.这种方法在正常的测试工作流程中确实没有用例,因为前面的任何一种方法都应该适用于大多数用例.

driver.dispose - As mentioned previously, is an internal method of WebDriver which has been silently dropped according to another answer - Verification needed. This method really doesn't have a use-case in a normal test workflow as either of the previous methods should work for most use cases.

说明用例:每当您想结束程序时,您都应该使用 driver.quit.它将关闭所有打开的浏览器窗口并终止 WebDriver 会话.如果您没有在程序结束时使用 driver.quit,WebDriver 会话将不会正确关闭,文件也不会从内存中清除.这可能会导致内存泄漏错误.

Explanation use case: You should use driver.quit whenever you want to end the program. It will close all opened browser windows and terminates the WebDriver session. If you do not use driver.quit at the end of the program, the WebDriver session will not close properly and files would not be cleared from memory. This may result in memory leak errors.

…………

现在在这种情况下,您需要特定的浏览器.下面的代码将关闭除主窗口之外的所有子窗口.

Now In that case you need to specific browser. Below is code which will close all the child windows except the Main window.

String homeWindow = driver.getWindowHandle();
Set<String> allWindows = driver.getWindowHandles();

//Use Iterator to iterate over windows
Iterator<String> windowIterator =  allWindows.iterator();

//Verify next window is available
while(windowIterator.hasNext())
{
    //Store the Recruiter window id
    String childWindow = windowIterator.next();
}

//Here we will compare if parent window is not equal to child window 
if (homeWindow.equals(childWindow))
{
    driver.switchTo().window(childWindow);
    driver.close();
}

现在这里需要根据自己的需要修改或添加条件

Now here you need to modify or add the condition according to your need

if (homeWindow.equals(childWindow))
{
    driver.switchTo().window(childWindow);
    driver.close();
}

目前只检查主窗口是否等于子窗口.在这里,您需要指定条件,例如要关闭的 ID.我从未尝试过,所以只是向您建议了实现您的要求的方法.

Currently it is checking only if home window is equal to childwindow or not. Here you need to specify the condition like which id's you want to close. I never tried it so just suggested you the way to achive your requirement.

这篇关于如何通过保持 webDriver 活动关闭整个浏览器窗口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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