gpt4 book ai didi

java - 捕获 webdriver 断开连接问题并处理额外的 Activity

转载 作者:行者123 更新时间:2023-12-04 04:41:14 24 4
gpt4 key购买 nike

我正在使用新的 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?这是一个 Action 的例子......
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 - 捕获 webdriver 断开连接问题并处理额外的 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18855123/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com