2014-09-27 79 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(); 
     } 

    } 
}