java - 如何在使用 selenium 运行自动化测试时捕获互联网异常丢失

标签 java selenium selenium-webdriver browser-automation

我有一个测试套件,其中有 1000 个测试数据,并且必须使用 selenium 自动化对其进行测试。

假设我的 500 个测试已经运行并且我失去了互联网连接,这里我想处理互联网连接异常。

这可以使用 selenium 来完成吗?或者我们需要一个 3 方工具来处理这个问题。请推荐

提前致谢

最佳答案

我从来没有听说过 selenium web 驱动程序,但是,当经过特定时间时可以抛出一个异常,由 Selenium 提供,即 org.openqa.selenium.TimeoutException() (假设失去连接是超时的原因之一) 所以我们提出了一个名为Future的接口(interface)提供的异步调用的实际使用需求

A Future represents the result of an asynchronous computation

如Javadoc所述

其中有一个方法get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException 所以你可以使用它, 更改 Oracle Javadoc 提供的示例使用代码后,我想出了一些可以解决您的问题的方法

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

interface ArchiveSearcher {
    String search(String target) throws InterruptedException;
}

class ArchiveSearcherClass implements ArchiveSearcher {
    ArchiveSearcherClass() {

    }

    @Override
    public String search(String target) throws InterruptedException {
        synchronized (this) {
            this.wait(2000); //in your case you can just put your work here 
            //this is just an example
            if (("this is a specific case that contains " + target)
                    .contains(target))
                return "found";
            return "not found";
        }
    }

}

class App {
    ExecutorService executor = Executors.newSingleThreadExecutor();
    ArchiveSearcher searcher = new ArchiveSearcherClass();

    void showSearch(final String target)
      throws InterruptedException {
    Future<String> future
      = executor.submit(new Callable<String>() {
        public String call() throws InterruptedException {
            return searcher.search(target);
        }});
   System.out.println("remember you can do anythig asynchronously"); // do other things while searching
        try {
            System.out.println(future.get(1, TimeUnit.SECONDS)); //try to run this then change it to 3 seconds
        } catch (TimeoutException e) {
            // TODO Auto-generated catch block
            System.out.println("time out exception");
//          throw new org.openqa.selenium.TimeoutException(); 
        } catch (ExecutionException e) {
            System.out.println(e.getMessage());
        }

}}

public class Test extends Thread {

    public static void main(String[] args) {

        App app1 = new App();
        try {
            app1.showSearch("query");
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
}

关于java - 如何在使用 selenium 运行自动化测试时捕获互联网异常丢失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26072275/

相关文章:

java - 我该如何解决 "org.json.simple.JSONObject cannot be resolved"?

java - REST 客户端调用失败并显示 "SunCertPathBuilderException: unable to find valid certification path to requested target"

Java子类构造函数不调用父类构造函数

java - 尝试将 Neo4j 添加到 eclipse 中

linux - 无法使用 Linux 在 chrome 上执行 nightwatch 测试

c# - C# 中的 Selenium Grid 与 Nunit 并行执行

javascript - 让 WebDriver 使用 Protractor 和 cucumber 调整大小

selenium - 在 Selenium webdriver 中测试负流

java - 如何在 xpath 变量旁边附加 "descendant"子句?

python - 我怎样才能让 Selenium 点击 "Next"按钮直到它不再可能?