有没有办法查看是否所有浏览器窗口都已关闭?我看到如果你在WebDriver上调用driver.quit()或driver.close(),sessionId就变为null。有没有办法检查?
我不想调用已关闭或退出的驱动程序,因为它会抛出WebDriverException。所以我想在继续之前检查浏览器的状态。
答案 0 :(得分:2)
只需设置
driver=null;
每次退出浏览器而不是检查
if (browser!=null){
//Attention: this comand is not supported
//as far as i know ;)
driver.doSomething();
}
或
try{
}catch (NullPointerException e)
e.printStackTrace();
System.err.print"DAMN";
}
或收到NullPointerException;)
答案 1 :(得分:2)
public bool InstanceExist
{
get
{
if (Instance != null)
{
try
{
return (Instance.WindowHandles != null); // allways returns true if browser instance exist or thrown error
}
catch (Exception e)
{
return false;
// means that browser was closed by user
}
}
return false; // means that it wasn't created yet or was closed by developer programmally
}
}
你需要检查3种情况:
使用此代码检查所有这些情况。
答案 2 :(得分:1)
实际上,当浏览器窗口消失时调用getWindowHandles会引发" UnreachableBrowserException"。
您必须将调用放入try-catch块并处理该错误。实际上,这是捕获意外浏览器窗口关闭的唯一已知的WORKING workournd。我在配置类中有一个静态方法来处理我的驱动程序:我重启我的浏览器:
protected static void loadPages() {
if (driver == null || driver.toString().contains("null")) { //null refers to an missing session id
driver = new FirefoxDriver();
//load all my page objects like
loginpage = new LoginPage(driver);
//....
//....
}
try {
if (driver.getWindowHandles() == null || driver.getWindowHandles().isEmpty()){ //will cause an UnreachableBrowserException if the browser really is not avalable.
try { //you actually dont need this try catch block
driver.quit();
} catch (Exception e) {
System.err.println("Quitting levtover driver did not work.");
}
driver = null; //you have to set the driver to null
loadPages();
}
} catch (UnreachableBrowserException ube) {
driver = null; //like above set to null to make sure no driver left
}
}
现在,你的当前测试将失败,但你将能够继续其余的测试。
答案 3 :(得分:0)
我认为检测所有窗户是否关闭的最简洁方法就像:
boolean allWindowsClosed = webDriver.getWindowHandles().isEmpty();
getWindowHandles为所有打开的窗口返回一组窗口句柄 - 请参阅http://selenium.googlecode.com/git/docs/api/java/org/openqa/selenium/WebDriver.html#getWindowHandles()
答案 4 :(得分:0)
在 C# 中
Total_health = 0
class Rover:
def __init__(self, broken_camera, dirty_solar_panels, system_failure, broken_solar_panels, broken_wheels, signal_interference, battery, bearing_failure, arm, xray, motor_failure):
self.broken_camera = broken_camera
self.dirty_solar_panels = dirty_solar_panels
self.system_failure = system_failure
self.broken_solar_panels = broken_solar_panels
self.broken_wheels = broken_wheels
self.signal_interference = signal_interference
self.battery = battery
self.arm = arm
self.xray = xray
self.motor_failure = motor_failure
Rover = Rover(False, False, False, False, False, False, False, 100, False, False, False)
Rover.battery = 100
def lines():
print("_____________________________________________")
# here this code wont work
Rover.broken_camera = True
# it subtracts 20 insted of 10
def health():
global Total_health
if Rover.broken_camera == True:
print("Broken Camera")
if Rover.dirty_solar_panels == True:
print("Dirty Solar Panels")
if Rover.broken_solar_panels == True:
print("Broken Solar Panels")
if Rover.broken_wheels == True:
print("Broken Wheels")
if Rover.battery < 21:
print("Low Battery 20%")
if Rover.arm == True:
print("Arm Damage")
if Rover.xray == True:
print("Broken XRAY")
if Rover.motor_failure == True:
print("Moror Faliure")
print("Total Health:", Total_health)
lines()
if Rover.broken_camera == True:
Total_health -= 10
if Rover.broken_camera == False:
Total_health += 10
if Rover.dirty_solar_panels == True:
Total_health -= 5
if Rover.dirty_solar_panels == False:
Total_health += 5
if Rover.broken_solar_panels == True:
Total_health -= 20
if Rover.broken_solar_panels == False:
Total_health += 20
if Rover.broken_wheels == True:
Total_health -= 10
if Rover.broken_wheels == False:
Total_health += 10
if Rover.battery < 21:
Total_health -= 5
if Rover.battery > 20:
Total_health += 5
if Rover.arm == True:
Total_health -= 20
if Rover.arm == False:
Total_health += 20
if Rover.xray == True:
Total_health -= 10
if Rover.xray == False:
Total_health += 10
if Rover.motor_failure == True:
Total_health -= 20
if Rover.motor_failure == False:
Total_health += 20
health()