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

时间:2014-09-27 07:13:29

标签: java selenium selenium-webdriver browser-automation

我有一个测试套件,它有1000个测试数据,必须使用硒自动化进行测试。

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

这可以使用selenium来完成,或者我们需要一个3方工具来处理这个问题。请建议

提前致谢

1 个答案:

答案 0 :(得分:0)

我从未听说过selenium web驱动程序但是,有一个异常可以在特定时间过去时抛出,由Selenium提供,org.openqa.selenium.TimeoutException()(假设失去连接是超时的一个原因) 所以我们想出了一个名为Future

的接口提供的异步调用的真正需要
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();
        }

    }
}