通过java模拟浏览器行为,对bugfree系统进行操作。譬如:通过bug id,查询bug的信息;查询产品族;查询满足特定条件的bug列表;批量更新bug的状态;上报bug到bugfree系统等。
package com.yunos.qa;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;
public class BugfreeOperator {
private static final String API_KEY = "";
private static final String bugfreeUrl = "http://bugfree-external.aliyun-inc.com/bugfree/api3.php";
private String sessionId;
private SessionInfo sessionInfo;
public BugfreeOperator() {
}
private SessionInfo getSessionInfo() {
String jsonResult = null;
InputStream is = null;
try {
is = doPost(bugfreeUrl, "mode=getsid");
jsonResult = getResult(is);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (is != null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
if (jsonResult == null) {
return null;
}
SessionInfo sessionInfo = JsonParser.parseSessionInfo(jsonResult);
System.out.println("sessionId: " + sessionInfo.getSessionId());
System.out.println("rand: " + sessionInfo.getRand());
return sessionInfo;
}
/**
*
* 认证码。
#加密算法:
$auth = md5(md5($username.md5($password)).API_KEY.$rand)
其中$username为用户名,$password为该用户的明文密码,$rand为getsid方法获得的rand值。
* @param userName
* @param password
* @return
*/
public boolean login(String userName, String password) {
sessionInfo = getSessionInfo();
if (sessionInfo == null) {
return false;
}
String md5 = MD5.getMD5(password.getBytes());
System.out.println("md5: " + md5);
md5 = userName + md5;
md5 = MD5.getMD5(md5.getBytes());
md5 = md5 + API_KEY + sessionInfo.getRand();
String auth = MD5.getMD5(md5.getBytes());
Map<String, String> params = new HashMap<String, String>();
params.put("mode", "login");
params.put(sessionInfo.getSessionName(), sessionInfo.getSessionId());
params.put("username", userName);
params.put("auth", auth);
String jsonResult = null;
InputStream is = null;
try {
is = doPost(bugfreeUrl, params);
jsonResult = getResult(is);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (is != null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
if (jsonResult == null) {
return false;
}
System.out.println("jsonResult: " + jsonResult);
return JsonParser.parseLoginResult(jsonResult);
}
public void findProducts() {
if (sessionInfo == null) {
return;
}
Map<String, String> params = new HashMap<String, String>();
params.put("mode", "findproducts");
params.put(sessionInfo.getSessionName(), sessionInfo.getSessionId());
String jsonResult = null;
InputStream is = null;
try {
is = doPost(bugfreeUrl, params);
jsonResult = getResult(is);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (is != null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
if (jsonResult == null) {
return;
}
System.out.println("[findProducts] jsonResult: " + jsonResult);
return;
}
public void getBug(int id) {
if (sessionInfo == null) {
return;
}
Map<String, String> params = new HashMap<String, String>();
params.put("mode", "getbug");
params.put(sessionInfo.getSessionName(), sessionInfo.getSessionId());
params.put("id", Integer.toString(id));
String jsonResult = null;
InputStream is = null;
try {
is = doPost(bugfreeUrl, params);
jsonResult = getResult(is);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (is != null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
if (jsonResult == null) {
return;
}
System.out.println("[getBug] jsonResult: " + jsonResult);
return;
}