1、原理及特色
參數放在XML文件中進行管理
用httpClient簡單封裝一個httpUtils工具類
測試用例管理使用了testNg管理,使用了TestNG參數化測試,經過xml文件來執行case。
測試報告這裏用到第三方的包ReportNG 項目組織用Maven
2、準備
使用工具:eclipse,maven
用到的第三方jar包:dom4j、reportng、testng
理解難點:httpUtils和xmlUtil工具類的封裝;dom4j使用;CookieStore的應用
3、框架構思
一、項目結構
二、用例執行流程
三、接口調用流程
四、調度腳本流程
4、框架實現
一、輸入參數
1.1 參數放在XML文件中進行管理
例:這裏測試獲取角色的接口輸入參數爲,page和rows,mapRole.xml內容以下
<?xml version="1.0" encoding="UTF-8"?>
<map>
<bean beanName="GetRole">
<!--Locator lists -->
<locator name="page" value="1"></locator>
<locator name="rows" value="10"></locator>
</bean>
</map>
1.2 封裝一個xmlUtil工具類負責讀取XML,使用第三方的jar包dom4j
1.2.1 xmlUtil中readXMLDocument方法返回值爲HashMap<String, String>
public static HashMap<String, String> readXMLDocument(String beanName,String xmlName){
}
參數xmlName(xml文件的名字); 參數beanName(xml文件中節點的名稱);
1.3 封裝一個CookieUtil工具類,經過CookieStore儲存cookie
1.3.1 CookieUtil類中setCookieStore方法返回值爲CookieStore
public CookieStore setCookieStore(HttpResponse httpResponse) {
}
1.4 用httpClient簡單封裝一個httpUtils工具類有get.post,put,delete方法
1.4.1 httpUtils中post封裝方法以下:
public CloseableHttpResponse post(String url, Map<String, String> params,CloseableHttpClient httpclient,CookieStore cookieStore){
}
二、返回參數
2.1 建立一個接口返回對象ResponseBean,
對象ResponseBean,包括status、statusCode、contentType、body、url、method、cookies
2.2 在工具類中在建立一個ReponseUtil工具類
ReponseUtil工具類負責將請求的返回數據CloseableHttpResponse 轉換成ResponseBean
public ResponseBean setResponseBean(CloseableHttpResponse httpResponse) {
}
三、測試用例
測試用例管理使用了testNg管理 ,使用了TestNG參數化測試,經過xml文件來執行case
3.1 測試case腳本
public class GetRoleTest {
static CookieStore cookieStore ;
static CookieUtil cookieUtil=new CookieUtil() ;
CloseableHttpClient client;
HttpUtils httpUtils=HttpUtils.getInstance();
@Parameters({ "url", "objBean" ,"statusCode","xmlName"})
@BeforeSuite
/*
* 登陸進入系統獲取JSESSIONID放入到CookieStore中
* */
public void TestLoginIn(String url ,String objBean, String statusCode,String xmlName) {
Map<String,String> params=xmlUtil.readXMLDocument(objBean,xmlName);
client = HttpClients.createDefault();
CloseableHttpResponse httpResponse= httpUtils.post(url, params, client, cookieStore);
//cookieUtil.printResponse(httpResponse);
cookieStore=cookieUtil.setCookieStore(httpResponse);
}
@Parameters({ "url", "objBean" ,"statusCode","body","xmlName"})
@Test(priority = 2)
public void TestGetRole(String url ,String objBean, String statusCode,String body,String xmlName) {
Map<String,String> params=xmlUtil.readXMLDocument(objBean,xmlName);
client = HttpClients.custom().setDefaultCookieStore(cookieStore).build();
CloseableHttpResponse httpResponse= httpUtils.post(url, params, client, cookieStore);
ResponseBean rb=new ReponseUtil().setResponseBean(httpResponse);
// add Assert
Assert.assertEquals("OK", rb.getStatus());
Assert.assertEquals(statusCode, rb.getStatusCode());
Assert.assertEquals(true, rb.getBody().contains(body));
}
@AfterSuite
public void closeClient(){
try {
// 關閉流並釋放資源
client.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
|
[注] 由於API接口測試時每次都要校驗Cookie,全部咱們每次都先執行登陸操做去獲取Cookie
3.2 xml文件的編寫
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="TestGetRole" parallel="classes" thread-count="5">
<parameter name="url" value="/sys/login" />
<parameter name="objBean" value="loginIn" />
<parameter name="status" value="OK" />
<parameter name="statusCode" value="200" />
<parameter name="xmlName" value="mapRole" />
<test name="TestGetRole" preserve-order="true">
<parameter name="url" value="/json/getRoleInfo" />
<parameter name="objBean" value="GetRole" />
<parameter name="status" value="OK" />
<parameter name="statusCode" value="200" />
<parameter name="body" value="roleName" />
<classes>
<class name="com.lc.testScript.GetRoleTest">
<methods>
<include name="TestGetRole" />
<!--<include name="TestGetRole2" />-->
</methods>
</class>
</classes>
</test>
</suite>
|
右鍵->run as ->TestNG Suite,這個場景的的測試用例就能夠運行了
四、測試報告和項目組織
測試報告這裏用到第三方的包ReportNG 項目組織用Maven
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
..........................................
..........................................
..........................................
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<xmlFileName1>TestGetRole.xml</xmlFileName>
.................這裏寫testNG對應的XML名稱----------------------
<xmlFileName10>TestGetUser.xml</xmlFileName>
</properties>
<dependencies>
..........................
</dependencies>
<build>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19</version>
<configuration>
<suiteXmlFiles>
<suiteXmlFile>src/test/java/testSuites/${xmlFileName}</suiteXmlFile>
.................略............
..............這裏的和properties中的xmlFileName想對應............
<suiteXmlFile>src/test/java/testSuites/${xmlFileName10}</suiteXmlFile>
</suiteXmlFiles>
</configuration>
</plugin>
<!-- 添加插件,添加ReportNg的監聽器,修改最後的TestNg的報告 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.5</version>
<configuration>
<properties>
<property>
<name>usedefaultlisteners</name>
<value>false</value>
</property>
<property>
<name>listener</name>
<value>org.uncommons.reportng.HTMLReporter</value>
</property>
</properties>
<workingDirectory>target/</workingDirectory>
</configuration>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
|
[注] 由於是maven的項目因此要將testSuite的xml文件放在maven的test目錄下,這樣右鍵pom.xml文件maven test,全部的測試用例就開始執行了
測試報告
框架目前存在的不足
一、
數據庫數據校驗這一塊的功能尚未完善,計劃用MyBatis
二、參數使用了xml文件配置雖然靈活但有些繁瑣,目前還沒想到好的解決方案,testlink是否能夠嘗試一下呢