Robotium雙用戶端測試架構,robotium架構
互連網的本質就是資訊交換,移動互連網更是如此, 所以許多移動互連網的服務類應用中有著身份地位不同的兩種使用者(例如:交易中的買家和賣家, 教學中的老師和學生, 打車中的車主和乘客)。最近的工作是給公司的某個產品建立android用戶端測試架構,使用的自動化測試技術是robotium。由於產品的使用者存在兩種身份(在這裡簡稱clientA, clientB),那麼就需要建立一個能夠同步兩種不同使用者的測試架構,這就是本文標題“Robotium雙用戶端測試架構”的由來。
1. 需求詳細分析
為了方便大家理解,我畫了張圖,不說廢話直接。
如所描述的,客戶身份A、B各有一些測試案例需要執行(圖中圓型隊列),當執行到紫色用例時需要兩個用戶端同步,這時執行到用例方法體內的客戶身份需要向同步伺服器通訊已獲得另一個客戶身份得同步資訊,如果對方已經準備就緒,用例方法開始同步執行,如果對方沒有就緒,告知伺服器自己已經就緒並且開始等待。
2.伺服器端核心代碼
還是先上代碼,稍後解釋。
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {String methodName = request.getParameter("testMethodName");//more development for methods syncString clientAReady = request.getParameter("clientAReady");String clientBReady = request.getParameter("clientBReady"); //debuglog("methodName:"+methodName);/*sync code*/if(clientAReady == null || clientBReady == null){return ;}if(clientAReady.equals("unknown")){//clientB requestGlobalResource.clientBMark = "ready";clientAReady = GlobalResource.clientAMark;}else if (clientBReady.equals("unknown")){//clientA requestGlobalResource.clientAMark = "ready";clientBReady = GlobalResource.clientBMark;}//寫回json資料}
重要的就是中間的if—else迴圈,主要功能是識別不同客戶身份,然後按照身份讀取另一身份的狀態資訊並且把自己的狀態給寫入伺服器。
3.用戶端核心代碼
用戶端代碼可以簡單分為三部分,第一是robotium測試架構建立,其次需要輪詢伺服器對方資料,最後是需要接收資料並更等待狀態位。
關於robotium測試架構建立不是本文的重點也比較簡單,筆者並不打算展開說明。附上相關資料:http://www.robotium.cn/archives/210,如果是大家做過android開發應該不難上手。
關於伺服器通訊,我使用的是HttpClient架構,使用方法為建立一個HttpClient對象,向服務發送資料請求,使用架構中提供的handler子類非同步接收資料並處理。代碼還是很簡單的。
輪詢資料
public void testSync () throws Exception{while(ClientBReady.equals("notReady")){NetManager.TestSyncServlet("testSync", "unknown", "ready", syncHandler);Thread.sleep(5000);}//robotium control code}
發送請求
package com.network.httpconnect;import com.network.httpconnect.AsyncHttpClient;import com.network.httpconnect.AsyncHttpResponseHandler;import com.network.httpconnect.RequestParams;public class NetManager {public static String getServiceBaseUrl() { return "http://localhost:8080/appTestServer/"; }public static int getTimeOut() { return 5 * 1000; }public static void TestSyncServlet(String testMethodName, String clientAReady, String clientBReady, AsyncHttpResponseHandler handler){String url = getServiceBaseUrl() + "TestSyncServlet";try{RequestParams params = new RequestParams();params.put("testMethodName", testMethodName);params.put("clientAReady", clientAReady);params.put("clientBReady", clientBReady);AsyncHttpClient client = new AsyncHttpClient();client.setTimeout(getTimeOut());client.post(url, params, handler);}catch (Exception ex){ex.printStackTrace();if (handler != null)handler.onFailure(null, ex.getMessage());}}}
接收資料
private AsyncHttpResponseHandler syncHandler = new AsyncHttpResponseHandler(){@Overridepublic void onSuccess(String content) {super.onSuccess(content);debuglog("content:"+ content);try {JSONObject receiveData = new JSONObject(content);ClientBReady = receiveData.getString("ClientBReady");} catch (JSONException e) {// TODO Auto-generated catch blocke.printStackTrace();}}@Overridepublic void onFailure(Throwable error, String content) {super.onFailure(error, content);}};
由於筆者水平有限,本文的實現可能不是非常完美,我已經將代碼上傳到github,不清楚的地方可以看源碼,歡迎大家一起討論。
github地址為:https://github.com/cibon/Robotium_two_clients_framework。
想瞭解基於Robotium架構的測試平台,有推薦的沒?
如果是測試APP的話,推薦一個叫易測雲的雲測試平台。
Android Robotium架構 自己寫的自動化的測試程式運行沒有結果
沒有具體報錯,無法詳細回答你,請上傳異常詳情