在 Java 中,检查 Selenium WebDriver 是否退出的最佳方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18619121/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
In Java, best way to check if Selenium WebDriver has quit
提问by James Dunn
I need to check a collection of Page Objects to see, for each one, if quit() has been called on its WebDriver.
我需要检查一组页面对象,以查看每个页面对象是否在其 WebDriver 上调用了 quit()。
I've written the following method to check a WebDriver's state:
我编写了以下方法来检查 WebDriver 的状态:
public static boolean hasQuit(WebDriver driver) {
try {
driver.getTitle();
return false;
} catch (SessionNotFoundException e) {
return true;
}
}
Here's the problem: I don't like having to throw and catch an exception to discover the truth of a boolean, but it seems I have no choice since the WebDriver APIdoesn't provide a method to check if the driver has quit.
问题是:我不喜欢必须抛出和捕获异常来发现布尔值的真相,但似乎我别无选择,因为WebDriver API没有提供检查驱动程序是否退出的方法。
So my question is, is there a better way to check if a WebDriver has quit?
所以我的问题是,有没有更好的方法来检查 WebDriver 是否已退出?
I found a similar (and more general) question here, but the question did not have any code that had been tried, and the only answer was to always set the WebDriver to null after quiting (which I don't necessarily have control over).
我在这里发现了一个类似(更一般)的问题,但该问题没有任何已尝试过的代码,唯一的答案是在退出后始终将 WebDriver 设置为 null(我不一定可以控制) .
采纳答案by Dingredient
If quit()
has been called, driver.toString()
returns null:
如果quit()
已被调用,则driver.toString()
返回 null:
>>> FirefoxDriver: firefox on XP (null))
Otherwise, it returns a hashcode of the object:
否则,它返回对象的哈希码:
>>> FirefoxDriver: firefox on XP (9f897f52-3a13-40d4-800b-7dec26a0c84d)
so you could check for null when assigning a boolean:
所以你可以在分配布尔值时检查空值:
boolean hasQuit = driver.toString().contains("(null)");
回答by Sajan Chandran
StopClientmethod will be invoked after quit
(RemoteWebDriver Source) is called, may be you can subclass your instance of RemoteWebDriver
and override stopClient
method, set some flag and check for the flag to determine if the webdriver is closed (quit
).
StopClient方法将在quit
( RemoteWebDriver Source)被调用后被调用,可能您可以子类化您的实例RemoteWebDriver
并覆盖stopClient
方法,设置一些标志并检查该标志以确定 webdriver 是否已关闭 ( quit
)。
回答by James Dunn
There are basically two approaches you can take, depending on your situation.
根据您的情况,您基本上可以采用两种方法。
Situation 1: Extending WebDriver is not a realistic option.
情况 1:扩展 WebDriver 不是一个现实的选择。
This is the most common situation.Most developers working with Selenium in real-work situations who want a handy way of telling if a WebDriver has quit are working in an already established testing framework, and trying to refactor the framework to ensure that only your custom extensions of WebDrivers are used is probably more trouble than it's worth.
这是最常见的情况。大多数在实际工作中使用 Selenium 的开发人员想要一种方便的方式来判断 WebDriver 是否退出,他们正在一个已经建立的测试框架中工作,并尝试重构该框架以确保只使用您自定义的 WebDriver 扩展可能是比它值得的麻烦更多。
In this case, Sajan's answer and Gili's recommendation of his answer won't be useful, because overriding RemoteWebDriver#stopClient()
is not an option. (Besides, even if it was, most people are looking for a simple answer.)
在这种情况下,Sajan 的回答和 Gili 对他的回答的推荐将没有用,因为覆盖 RemoteWebDriver#stopClient()
不是一种选择。(此外,即使是这样,大多数人也在寻找一个简单的答案。)
As long as you are only using standard implementations of WebDriver that come with Selenium (FirefoxDriver, ChromeDriver, InternetExplorerDriver, SafariDriver, etc.), you can cast the WebDriver to a RemoteWebDriver and then check if the sessionId is null (Pat was on the right track, but a direct call to sessionId is better than using toString()
).
只要您仅使用 Selenium 附带的 WebDriver 的标准实现(FirefoxDriver、ChromeDriver、InternetExplorerDriver、SafariDriver 等),您就可以将 WebDriver 转换为 RemoteWebDriver,然后检查 sessionId 是否为 null(Pat 在右侧)跟踪,但直接调用 sessionId 比使用更好toString()
)。
public static boolean hasQuit(WebDriver driver) {
return ((RemoteWebDriver)driver).getSessionId() == null;
}
This answer should be good for 95% of all cases, because how often are you going to use a WebDriver that doesn't implement RemoteWebDriver? (Not very often.)
这个答案应该适用于 95% 的所有情况,因为您打算多久使用一次未实现 RemoteWebDriver 的 WebDriver?(不经常。)
Situation 2: You CANrealistically extend your WebDriver.
情况2:您CAN实事求是地扩展您的webdriver。
This situation is less common,but perhaps you are either:
(a) Working with a well-designed and abstracted framework, or
(b) Starting a selenium test framework from scratch.
这种情况不太常见,但也许您是:
(a) 使用设计良好且抽象的框架,或
(b) 从头开始创建 selenium 测试框架。
In this case, you can create your own interface that extends WebDriver:
在这种情况下,您可以创建自己的接口来扩展 WebDriver:
public interface CustomDriver extends WebDriver {
boolean hasQuit();
}
And then you can extend standard WebDrivers like so (in this example, ChromeDriver):
然后你可以像这样扩展标准的 WebDriver(在这个例子中,ChromeDriver):
public class CustomChromeDriver extends ChromeDriver implements CustomDriver {
boolean hasQuit = false;
@Override
public boolean hasQuit() {
return hasQuit;
}
@Override
public void stopClient() {
super.stopClient();
hasQuit = true;
}
}