Why do interface automation?
Available at: http://www.sohu.com/a/152383408_99919686
Example institutions are as follows:
The jar package required for the architecture to use the premise:
1.httpclient.jar
2. Apache POI: poi-bin-3.14.zip
Implementation
1. Create an Excel to add the name of the use case, the URL of the interface, and the interface message to Excel
2. Create a Java project (maven can also be used, this tutorial will take the java project as an example)
Create the ExcelReader class
public class ExcelReader { private static XSSFSheet ExcelWSheet; private static XSSFWorkbook ExcelWBook; private static XSSFCell Cell; private static XSSFRow Row; public static String[][] getExpectationData(String file, String sheetName) { try { FileInputStream ExcelFile = new FileInputStream(file);//Get the Excel file ExcelWBook = new XSSFWorkbook(ExcelFile); // get the worksheet name ExcelWSheet = ExcelWBook.getSheet(sheetName); // get the total number of rows int rowNum = ExcelWSheet.getLastRowNum(); List<String[]> results = new ArrayList<String[]>(); for (int i = 1; i <= rowNum; i++) { // current line XSSFRow row = ExcelWSheet.getRow(i); int colNum = row.getLastCellNum(); String[] data = new String[colNum]; // all columns in the current row for (int j = 0; j < colNum; j++) { try { data[j] = getCellValue(row.getCell(j)); } catch (NullPointerException e) { // If the cell is empty, use this to handle data[j] = ""; } } // Store the data of the data[] array in list<[]> results.add(data); } String[][] returnArray = new String[results.size()][rowNum]; for (int i = 0; i < returnArray.length; i++) { returnArray[i] = (String[]) results.get(i); } return returnArray; } catch (Exception e) { return null; } } /** * Judge and convert the format of each cell in Excel */ public static String getCellValue(XSSFCell xssfCell) { String cellValue = ""; DecimalFormat df = new DecimalFormat("#"); switch (xssfCell.getCellType()) { case HSSFCell.CELL_TYPE_STRING: cellValue = xssfCell.getRichStringCellValue().getString().trim(); break; case HSSFCell.CELL_TYPE_NUMERIC: cellValue = df.format(xssfCell.getNumericCellValue()).toString(); break; case HSSFCell.CELL_TYPE_BOOLEAN: cellValue = String.valueOf(xssfCell.getBooleanCellValue()).trim(); break; case HSSFCell.CELL_TYPE_FORMULA: cellValue = xssfCell.getCellFormula(); break; default: cellValue = ""; } return cellValue; } }
3. Create testng test class
public class Demo3 { @Test(dataProvider = "interfaceTest") public void getInterface(String case_1, String url, String assert_1) throws IOException { CloseableHttpClient httpclient = HttpClients.createDefault(); // Create a default httpClient instance HttpGet httpget = new HttpGet(url); // Create an instance of the GET method and pass in the address to be connected System.out.println(url); Reporter.log("Test request interface URL is: " + url); // set request headers httpget.addHeader("content-type", "application/x-www-form-urlencoded; charset=UTF-8"); CloseableHttpResponse response = httpclient.execute(httpget); HttpEntity entity = response.getEntity(); // read response, get response entity String getResult = EntityUtils.toString(entity, "UTF-8");// Convert the response to String type utf-8 encoding Reporter.log("The message returned by the interface is: " + getResult); if (getResult != null) { System.out.println(case_1); System.out.println("Interface response: " + getResult + "\n"); // print the response content EntityUtils.consume(entity); } httpclient.close(); // close the connection Assert.assertEquals(assert_1, getResult); // 断言 Reporter.log(case_1); // output report } @DataProvider(name = "interfaceTest") public static Object[][] words() throws IOException { // test data preparation String file = "";//Write your excel path here Object[][] records; records = ReadExcel.getExpectationData(file, "");//This is to write the sheetname in your Excel return records; } }Thanks for the idea of this article: https://blog.csdn.net/yan1234abcd/article/details/62038230