我正在使用新的 selenium webdriver,一切都很好,但是如果发生连接问题并且浏览器在没有我的指示的情况下关闭,或者由于各种其他原因,我会收到如下错误:-
Exception in thread "Thread-2" org.openqa.selenium.WebDriverException: Error communicating with the remote browser. It may have died.
在这些情况下,我想捕获它并调用一些代码来对表等运行更新,以通知用户这种情况已经发生,我该怎么做?
我试图变得聪明,并从“ Action ”数据库中运行它,代码基本上使用一个很大的“if action = XYZ call blah”列表按顺序运行这些 Action ,就像这样”
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 中吗?或者我是否需要在每个操作的下一个级别实现 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);
}
感谢您的任何建议,我真的不知道从哪里开始这个想法?!
最佳答案
解决方案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);
关于java - 捕获网络驱动程序断开连接问题并处理额外的 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18855123/