Skip to content

Commit

Permalink
update: extract configure to xutest.properties.
Browse files Browse the repository at this point in the history
  • Loading branch information
ityoung committed Apr 15, 2018
1 parent 818813c commit 8c72281
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 26 deletions.
25 changes: 18 additions & 7 deletions src/com/xutest/Connect.java
Original file line number Diff line number Diff line change
@@ -1,25 +1,35 @@
package com.xutest;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Properties;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import com.xutest.PropertiesReader;

public class Connect {
// 与 x-utest 建立连接

String base_url;
String app_id;
String app_key;
String pro_id;
String token;

Connect(String url, String id, String key){
base_url = url;
app_id = id;
app_key = key;
// 构造函数, 属性值
Connect() throws IOException {
PropertiesReader pr = new PropertiesReader();
Properties prop = pr.getProperties();
base_url = (String) prop.get("base_url");
app_id = (String) prop.get("app_id");
app_key = (String) prop.get("app_key");
pro_id = (String) prop.get("project_id");
}

// x-utest 认证, 获取 token
Expand Down Expand Up @@ -50,12 +60,13 @@ public void auth() throws Exception {
in.close();

//打印结果
String jsonStr=response.toString();
String jsonStr = response.toString();

//转换成JSON数据并查找token
JSONObject jarr= JSON.parseObject(jsonStr);//JSON.parseArray(jsonStr);
JSONObject jarr = JSON.parseObject(jsonStr);//JSON.parseArray(jsonStr);
token = jarr.getJSONObject("data").getString("token");
}

// 发送测试结果到 x-utest
public String post_test_result(JSONObject test_data) throws Exception {
String url = base_url + "/testdata/create-test-data/?token=" + token;
Expand Down Expand Up @@ -84,7 +95,7 @@ public String post_test_result(JSONObject test_data) throws Exception {
in.close();

//返回结果
String jsonStr=response.toString();
String jsonStr = response.toString();
return jsonStr;
}
}
24 changes: 24 additions & 0 deletions src/com/xutest/PropertiesReader.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.xutest;


import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Map.Entry;
import java.util.Properties;
import java.util.Set;

public class PropertiesReader {

//读取配置文件, 加载配置文件到Properties
public static Properties getProperties() throws IOException{
//创建一个Properties
Properties properties = new Properties();
//建立输入字符流对象
FileReader fileReader = new FileReader("src/xutest.properties");
//加载配置文件的数据到Properties
properties.load(fileReader);
return properties;
}
}
26 changes: 13 additions & 13 deletions src/com/xutest/XListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import com.alibaba.fastjson.JSONArray;
import com.xutest.Connect;

import java.io.IOException;


public class XListener extends TestListenerAdapter {
long start_time;
Expand All @@ -30,10 +32,10 @@ public void onFinish(ITestContext tr) {
JSONArray details = new JSONArray();
for (ITestResult FailedTests : tr.getFailedTests().getAllResults()) {
JSONObject detail = new JSONObject();
String test_class = FailedTests.getTestClass().getName().toString();
String test_class = FailedTests.getTestClass().getName();
detail.put("status", "failures");
detail.put("note", FailedTests.getThrowable().toString());
detail.put("test_case", FailedTests.getName().toString());
detail.put("test_case", FailedTests.getName());
detail.put("explain", test_class);
details.add(detail);
}
Expand All @@ -55,19 +57,17 @@ public void onFinish(ITestContext tr) {
//TODO: you need to write your own function to get your project's version.
test_result.put("pro_version", "0.0.0.0");

//TODO: these should call from configure file.
//change these config to your own x-utest.
test_result.put("pro_id", "5a7fb0f047de9d5cf3d13a45");
String base_url = "http://192.168.0.29:8011";
String app_id = "376223a60d7811e883dc448a5b61a7f0";
String app_key = "cf71ab7e937620de5767ecc08780a69b";

// send to x-utest system
Connect http = new Connect(base_url, app_id, app_key);
try {
http.auth();
http.post_test_result(test_result);
} catch (Exception e) {
Connect xutest = new Connect();
test_result.put("pro_id", xutest.pro_id);
try {
xutest.auth();
xutest.post_test_result(test_result);
} catch (Exception e) {
e.printStackTrace();
}
} catch (IOException e) {
e.printStackTrace();
}

Expand Down
12 changes: 6 additions & 6 deletions src/testng.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<!-- 该xml实现调用XListener监听器,将测试结果上传至 x-utest 系统 -->
<suite name="suite1" >
<test name="test" preserver-order="true">
<!-- 添加XListener监听器,XListener类在com.x-utest.XListener中 -->
<listeners>
<listener class-name="com.xutest.XListener" />
</listeners>
<test name="test">
<classes >
<class name="com.testdemo.DemoTest" />
<class name="com.test.TestDemo" />
</classes>
<!-- 添加XListener监听器,XListener类在com.x-utest.XListener中 -->
<listeners>
<listener class-name="com.xutest.XListener" />
</listeners>
</test>
</suite>
4 changes: 4 additions & 0 deletions src/xutest.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
base_url=http://192.168.0.29:8011
app_id=376223a60d7811e883dc448a5b61a7f0
app_key=cf71ab7e937620de5767ecc08780a69b
project_id=5a7fb0f047de9d5cf3d13a45

0 comments on commit 8c72281

Please sign in to comment.