java - 捕获网络驱动程序断开连接问题并处理额外的 Activity

标签 java selenium selenium-webdriver webdriver

我正在使用新的 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/

相关文章:

java - 如何在 Selenium Java Web 驱动程序中选择以下元素?

java - 使用 Java 的 Selenium Webdriver ElementNotVisibleException

xpath - 如何单击span类中的动态元素? Selenium 网络驱动程序

java - 当页面中存在两个框架时如何移动物理鼠标

eclipse - Eclipse Mac 中缺少 JRE

PHP Tokenizer 的 Java 替代品

java - 带有红圈的Intellij Idea文件

java - 无法使用相对路径读取文件

python - 如何验证文本字段是否具有我输入的值?

angularjs - 无法让 Protractor 等待现有元素可点击(condition.elementToBeClickable)