我正在使用新的selenium webdriver,一切都很棒,但是如果发生连接问题并且浏览器在没有指示或其他各种原因的情况下关闭,我会收到如下错误: -
Exception in thread "Thread-2" org.openqa.selenium.WebDriverException: Error communicating with the remote browser. It may have died.
在这些情况下,我想抓住它并调用一些代码来运行针对表等的更新,以通知用户这已发生,我该怎么做?
我试图变得聪明并从“动作”数据库中运行它,代码基本上按顺序运行这些动作,使用一个非常大的“if action = XYZ call blah”列表,如此“
if (actionType.equals("NAVIGATETO")){
actionPassed = threadAction.NavigateTo(this);
}
else if(actionType.equals("TYPE")){
actionPassed = threadAction.Type(this);
}
else if(actionType.equals("CLOSEBROWSER")){
actionPassed = threadAction.CloseBrowser(this);
}...
我是否需要将每个包装在试一试并抓住?或者我是否需要在每个操作中实现下一级别的try和catch?这是一个行动的例子......
public boolean browserNav(individualThreadSession threadsesh){
if(threadsesh.stringValue.contains("BACK")){
threadsesh.driver.navigate().back();
}
else if(threadsesh.stringValue.contains("REFRESH")){
threadsesh.driver.navigate().refresh();
}
else if(threadsesh.stringValue.contains("GOTO")){
threadsesh.driver.navigate().to(threadsesh.stringValue);
}
return(true);
}
感谢您的任何建议,我真的不知道从哪里开始这个想法?!
答案 0 :(得分:3)
解决方案1
如果您正在使用JUnit执行,则可以扩展TestWatcher类:
public class TestRules extends TestWatcher {
@Override
protected void failed(Throwable e, Description description) {
// This will be called whenever a test fails.
}
因此,在您的测试课程中,您只需调用它:
public class testClass{
@Rule
public TestRules testRules = new TestRules();
@Test
public void doTestSomething() throws Exception{
// If the test fails for any reason, it will be caught be testrules.
}
解决方案2 看看EventFiringWebDriver。您可以将WebDriverEventListener附加到它并覆盖onException方法。
类似的东西:
EventFiringWebDriver driver = new EventFiringWebDriver(new FirefoxDriver());
WebDriverEventListener listener = new AbstractWebDriverEventListener() {
@Override
public void onException(Throwable t, WebDriver driver) {
// Take action
}
};
driver.register(listener);
答案 1 :(得分:0)
您可以使用QAF selenium testing framework - QAF formerly ISFW并在WebDriverListener中提供onFailure