a.Filters设置Show only the following Hosts 输入10.253.11.41:41630 b.点击Actions-Run Filterset now 开始操作系统抓取请求 c.File-Export Sessions-All Sessons菜单,选择Visual Studio WebTest格式导出文件。 d.将上述文件的内容拷贝到自己的xml文件中,删除 节点,保存。 e.将文件拷贝至项目的resource目录下.
@tag1 Scenario Outline:请重写场景描述 # Given prepare data for url "/address/create" with case " " SQL # | staffCode | | When I use "fiddlerData/test2.xls" file case-" " to send request Then the response status should be "200" And the JSON response "$.success" equals " " And the JSON response "$.message" equals " " And the JSON response "$.msgCode" equals " " And the JSON response "$.attributes" start with "null" # And the JSON response "$.model.id" start with " " Examples: |index| success|message| msgCode | |1 | true |操作成功|0000| 生成的feature文件位置:
每个feature文件的内容跟模板一致。
stepsDefines类中定义了feature的代码实现和断言:
package testcases; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import com.github.restdriver.serverdriver.RestServerDriver; import com.github.restdriver.serverdriver.http.Header; import com.github.restdriver.serverdriver.http.response.Response; import com.jayway.jsonpath.JsonPath; import cucumber.api.java.en.Given; import cucumber.api.java.en.Then; import cucumber.api.java.en.When; import static com.github.restdriver.serverdriver.RestServerDriver.body; import static com.github.restdriver.serverdriver.RestServerDriver.header; import static testcases.Common.hostUrl; import fiddler.module.ExcelManage; import fiddler.module.ReqestEntity; import util.ReadTxtFile;import jdbc.DBIdConstant; import jdbc.ManagedSqlUtil; import org.junit.Assert; import java.sql.ResultSet; import java.sql.SQLException; import java.util.*; /** * Created by linyanghua on 2017/8/28. */ public class StepsDefines { Response response = null; String paramters=""; ReqestEntity req = null; @When("^I send a GET request to \"(.*?)\"$") public void getRequest(String path,Map paramMap) throws InterruptedException { String cookie =Common.getcookie(Common.loginurl,Common.loginparam); Header header_cookie = header("Cookie",cookie); paramters =getStringParams(paramMap); response = RestServerDriver.get(hostUrl+path+"?"+paramters, header_cookie); } @When("^I send a POST request to \"(.*?)\" with text$" ) public void postRequest(String apiPath,Map paramMap) throws Throwable { String cookie =Common.getcookie(Common.loginurl,Common.loginparam); Header header_cookie = header("Cookie",cookie); paramters =getStringParams(paramMap); response = RestServerDriver.post(hostUrl+apiPath,body(paramters, Common.applicationContext), header_cookie); } @When("^I send a POST request to \"(.*?)\"$" ) public void postRequest(String apiPath) throws Throwable { String cookie =Common.getcookie(Common.loginurl,Common.loginparam); Header header_cookie = header("Cookie",cookie); response = RestServerDriver.post(hostUrl+apiPath,header_cookie); } /** * @param apiPath * @param paramMap * @throws InterruptedException * @return Response */ @When("^I send a POST request to \"(.*?)\" with json$") public void postRequestWithJson(String apiPath, Map paramMap) throws InterruptedException { String cookie =Common.getcookie(Common.loginurl,Common.loginparam); Header header_cookie = header("Cookie",cookie); paramters =getJSONParams(paramMap); response = RestServerDriver.post(hostUrl+apiPath,body(paramters, Common.applicationJson), header_cookie); } @When("^I use a \"(.*?)\" file to send a POST request to \"(.*?)\"$") public void postRequestWihtFile(String fileName, String path) throws InterruptedException { String cookie =Common.getcookie(Common.loginurl,Common.loginparam); Header header_cookie = header("Cookie",cookie); String paramters = ReadTxtFile.readtxtFile(fileName); response = RestServerDriver.post(hostUrl+path,body(paramters, Common.applicationJson), header_cookie); } @Then("^the JSON response equals$") public void assertResponseJson(String expected) { String responseJson = response.asText(); assertJsonEquals(responseJson, expected); } @Then("^the JSON response equals json file \"(.*?)\"$") public void theJSONResponseEqualsJsonFile(String fileName) { String responseJson = response.asText(); String fileJson = ReadTxtFile.readtxtFile(fileName); assertJsonEquals(responseJson, fileJson); } @Then("^the response status should be \"(\\d{3})\"$") public void assertStatusCode(int statusCode) { int responseStatusCode =response.getStatusCode(); Assert.assertEquals(statusCode,responseStatusCode); } @Then("^the JSON response \"(.*?)\" equals \"(.*?)\"$") public void assertEquals(String str, String expected) { String jsonValue = getJsonPathValue(response, str); Assert.assertEquals(expected,jsonValue); } @Then("^the JSON response \"(.*?)\" should be not null$") public void assertNotNull(String str) { String jsonValue = getJsonPathValue(response, str); Assert.assertNotNull(jsonValue); } @Then("^the JSON response \"(.*?)\" start with \"(.*?)\"$") public void assertStartWith(String str, String start) { String jsonValue = getJsonPathValue(response, str); boolean act_start=jsonValue.startsWith(start); Assert.assertEquals(act_start, true); } @Then("^the JSON response \"(.*?)\" end with \"(.*?)\"$") public void assertEndWith(String str, String end) { String jsonValue = getJsonPathValue(response, str); Assert.assertEquals(jsonValue.endsWith(end),true); } @Then("^the JSON response \"(.*?)\" include \"(.*?)\"$") public void assertInclude(String str, String include) { String jsonValue = getJsonPathValue(response, str); Assert.assertEquals(jsonValue.contains(include),true); } @Then("^the response model \"(.*?)\" size equals \"(.*?)\"$") public void assertModelJsonNode(String jsonPath,int size) { List medelJsonArray = getJsonArray(response, jsonPath); Assert.assertEquals(size,medelJsonArray.size()); } @Then("^the JSON response \"(.*?)\" size more than \"(.*?)\"$") public void assertSizeMoreThan(String jsonPath,int size) { List medelJsonArray = getJsonArray(response, jsonPath); Assert.assertTrue(medelJsonArray.size()>size); } @Then("^the response jsonArraylist \"(.*?)\" equals \"(.*?)\"$") public void assertJsonArrayList(String jsonPath,String str) { String medelJsonArray = getJsonArray(response, jsonPath).toString().replace("\"",""); String exceptedValue=str; Assert.assertEquals(str.equals(exceptedValue),true); } /** * @param method * @param path * @param contentType * @param map * @return * @throws Throwable */ @When("^I send a \"(.*?)\" request to \"(.*?)\" with contentType:\"(.*?)\"$" ) public Response sendRequestStep(String method,String path, String contentType, Map map)throws Throwable{ Response response = sendRequest(method, path, contentType,map); return response; } /** * @param path * @param casename * @param map * @throws Throwable */ @Given("^prepare data for url \"([^\"]*)\" with case \"([^\"]*)\" SQL$") public void prepareData(String path,String casename,Map map) throws Throwable { if (path.equals("case/List/phone")){ InitDBTestDataFactory.initCaseListPhoneDB(map,casename); }else if(path.equals("/caseCollection/list")){ InitDBTestDataFactory.initCaseCollectlistDB(map,casename); }else if(path.equals("/caseCollection/list")){ InitDBTestDataFactory.initCaseCollectlistDB(map,casename); }else if(path.equals("/caseCollection/insert")){ InitDBTestDataFactory.initCaseCollectInstertDB(map,casename); }else if(path.equals("/caseCollection/update")){ InitDBTestDataFactory.initCaseCollectUpdateDB(map,casename); }else if(path.equals("/caseCollection/delete")){ InitDBTestDataFactory.initCaseCollectdeleteDB(map,casename); }else if(path.equals("/staff/save")){ InitDBTestDataFactory.initStaffSaveDB(map,casename); } } /** * @param jsonPath * @param tableName * @param propertiName * @param map * @throws Throwable */ @Then("^the JSON response \"([^\"]*)\" equals table \"([^\"]*)\" property \"([^\"]*)\"$") public void theJSONResponseEqualsTable(String jsonPath, String tableName,String propertiName,Map map) throws Throwable { List listA =selectCheckDataFromDB(tableName,map,propertiName); List listB = getJsonArray(response, jsonPath); Collections.sort(listA); Collections.sort(listB); Assert.assertEquals(listA.size(),listB.size()); if (listA.size()!=0){ for (int i=0;i map){ Set keySet = map.keySet(); String where = ""; int i = 0; for (Object keyName:keySet){ i++; if (keySet.size()!=i){ where =where + keyName+"="+"\""+map.get(keyName)+"\""+" and "; }else { where = where + keyName+"="+"\""+map.get(keyName)+"\""; } } return where; } public List selectCheckDataFromDB(String table,Map map,String field1) throws SQLException{ String whereSql = getWhereSelectSql(map); String selectSql = "select "+field1+" from "+table+" where "+whereSql+";"; ResultSet rs = ManagedSqlUtil.selectBySql(selectSql, DBIdConstant.collectonDB); List list = new ArrayList (); while(rs.next()){ list.add(rs.getInt(0)); } return list; } public void assertJsonEquals(String responseJson,String excepted){ JSONObject jsonContent = JSON.parseObject(responseJson); JSONObject extContent = JSON.parseObject(excepted); boolean act_succ = JsonPath.read(jsonContent,"$.success"); boolean ext_succ = JsonPath.read(extContent,"$.success"); Assert.assertEquals(act_succ, ext_succ); String act_msg = JsonPath.read(jsonContent,"$.message"); String ext_msg = JsonPath.read(extContent,"$.message"); Assert.assertEquals(act_msg,ext_msg); String act_msgCode = JsonPath.read(jsonContent,"$.msgCode"); String ext_msgCode = JsonPath.read(extContent,"$.msgCode"); Assert.assertEquals(act_msgCode,ext_msgCode); String act_attr = JsonPath.read(jsonContent,"$.attributes"); String ext_attr = JsonPath.read(extContent,"$.attributes"); Assert.assertEquals(act_attr,ext_attr); } public String getJsonPathValue(Response response,String jsonPath){ String str=""; String responesJson = response.asText(); JSONObject jsonContent = JSON.parseObject(responesJson); if (JsonPath.read(jsonContent,jsonPath)!=null) { str = JsonPath.read(jsonContent,jsonPath).toString(); return str; }else { return "null"; } } // 返回JSONArray中某个字段的所有值 public List getJsonArray(Response response, String jsonPath){ String responesJson = response.asText(); JSONObject jsonContent = JSON.parseObject(responesJson); List list = JsonPath.read(jsonContent,jsonPath); return list; } /** * * @param map * @return StringParams */ public static String getStringParams(Map map){ Set keySet = map.keySet(); String paramters=""; int i = 0; for (Object keyName : keySet) { i++; if (map.get(keyName)!=null) { if (keySet.size() != i) { paramters = paramters + keyName + "=" + map.get(keyName) + "&"; } else { paramters = paramters + keyName + "=" + map.get(keyName); } } System.out.println("键名:" + paramters); } return paramters; } /** * * @param map * @return JSONParams */ public static String getJSONParams(Map map){ Map jsonMap = new HashMap (); Set keySet = map.keySet(); for (Object keyName:keySet){ if(map.get(keyName)!=null){ jsonMap.put(keyName,map.get(keyName)); } } String paramters = JSONObject.toJSONString(jsonMap); return paramters; } public Response sendRequest(String method,String path, String contentType, Map map) throws Throwable { if(method=="Post"&&contentType=="json"){ response = sendPostRequestWithJson(path,map); }else if (method=="Post"&&contentType=="form"){ response = sendPostRequest(path,map); }else if (method=="Get"){ response = sendGetRequest(path,map); } return response; } public Response sendGetRequest(String path,Map paramMap) throws InterruptedException { String cookie =Common.getcookie(Common.loginurl,Common.loginparam); Header header_cookie = header("Cookie",cookie); paramters =getStringParams(paramMap); response = RestServerDriver.get(hostUrl+path+"?"+paramters, header_cookie); return response; } public Response sendPostRequest(String apiPath,Map paramMap) throws Throwable { String cookie =Common.getcookie(Common.loginurl,Common.loginparam); Header header_cookie = header("Cookie",cookie); paramters =getStringParams(paramMap); response = RestServerDriver.post(hostUrl+apiPath,body(paramters, Common.applicationContext), header_cookie); return response; } public Response sendPostRequestWithJson(String apiPath, Map paramMap) throws InterruptedException { String cookie =Common.getcookie(Common.loginurl,Common.loginparam); Header header_cookie = header("Cookie",cookie); paramters =getJSONParams(paramMap); response = RestServerDriver.post(hostUrl+apiPath,body(paramters, Common.applicationJson), header_cookie); return response; } public void waitResponse(Response response) throws InterruptedException { int i; for ( i=0;i<5;i++){ Thread.sleep(3000); if (response!=null){ break; } } return ; } @When("^I use \"([^\"]*)\" file case-\"([^\"]*)\" to send request$") public void iUseAFileToSendRequest(String fileDir,int index) throws Throwable { String cookie = Common.getcookie(Common.loginurl, Common.loginparam); Header header_cookie = header("Cookie", cookie); req= ExcelManage.getRequestData(fileDir,"sheet1",index); if (req != null) { if (req.getMethod().equals("GET") && req.getParams() != "") { response = RestServerDriver.get(req.getUrl() + "?" + req.getParams(), header_cookie); } if (req.getMethod().equals("GET") && req.getParams() == "") { response = RestServerDriver.get(req.getUrl(), header_cookie); } if (req.getMethod().equals("POST") && req.getContentType().trim().equals("application/x-www-form-urlencoded")) { response = RestServerDriver.post(req.getUrl(), body(req.getParams(), Common.applicationContext), header_cookie); } if (req.getMethod().equals("POST") && req.getContentType().trim().equals( "application/json")) { response = RestServerDriver.post(req.getUrl(), body(req.getParams(), Common.applicationJson), header_cookie); } } } public static void main(String[] args) throws Throwable { StepsDefines sd = new StepsDefines(); sd.iUseAFileToSendRequest("fiddlerData/test2.xls",1); }