2014-09-27 40 views
1

我有一个测试套件,它有1000个测试数据,并且必须使用硒自动化进行测试。如何在使用硒进行自动化测试时捕获网络异常丢失

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

这可以使用硒,或我们需要一个3方工具来处理这个。请建议提前

感谢

+0

我简要地误读这是“利益豁免例外“ – 2014-09-27 08:29:21

回答

0

我从来没有听说过硒网络驱动程序,但是,有在特定的时间已经过去了,可以抛出一个异常,硒是org.openqa.selenium.TimeoutException()提供(假设失去连接是一个原因超时) 所以我们想出了一个真正的使用需要由被称为未来

A Future represents the result of an asynchronous computation 

接口提供一个异步调用,如通过规定的Javadoc

其中有一个方法get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException 这样你就可以改变由甲骨文的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(); 
     } 

    } 
}