【appium關鍵字驅動之四】執行excel用例進階

一、前言

      通過關鍵字驅動的第一篇,我們知道,目前的測試結果是每一個步驟的結果,那麼可不可以寫一個更加直觀的,比如說一條用例一個結果呢,如下圖:

用例集:

用例:

二、執行

      設計好excel後,我們來改一下執行excel的方法,如下:

package com.keyword.casestoread;

import io.appium.java_client.android.AndroidDriver;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import com.keyword.lib.Common;

/**
 *  執行excel表的用例
 * @author Administrator
 *
 */
public class ExcelToRead {
	
	public static Logger log = Logger.getLogger(ExcelToRead.class.getName());
	
	public XSSFWorkbook excelwBook;
	public XSSFCell cell;
	public XSSFRow row;
	public String suitResult = "true";
	Common comm = new Common();
	
	public void readExcel(String path,AndroidDriver driver){
		String result = "true";
		String actualValue = null;
		XSSFCell cellactual  = null;
	
		try {
			InputStream excelFile = new FileInputStream(path);
			FileOutputStream fileout = null;
			
			excelwBook = new XSSFWorkbook(excelFile); //得到工作薄對象
			
			// 設置單元格的顏色樣式(紅色)
			XSSFCellStyle style = excelwBook.createCellStyle();    
			style.setFillPattern(XSSFCellStyle.SOLID_FOREGROUND);
			style.setFillForegroundColor(IndexedColors.RED.getIndex());
			
			// 設置單元格的顏色樣式(白色)
			XSSFCellStyle stylewhite = excelwBook.createCellStyle();    
			stylewhite.setFillPattern(XSSFCellStyle.SOLID_FOREGROUND);
			stylewhite.setFillForegroundColor(IndexedColors.WHITE.getIndex());
			
			XSSFSheet excelwSheet0 = excelwBook.getSheetAt(0);//獲取第一個sheet
			XSSFSheet excelwSheet1 = excelwBook.getSheetAt(1);//獲取第二個sheet
			
			int rowLength0 = excelwSheet0.getLastRowNum();//獲取第一個sheet的有效行數
			int rowLength1 = excelwSheet1.getLastRowNum();//獲取行數
			
			for (int j = 1; j <= rowLength0; j++){ //測試用例執行的遍數
				
				// 獲取用例ID
				Cell cellSuiteId0 = excelwSheet0.getRow(j).getCell(0);
				String suiteId0 = cellSuiteId0.getStringCellValue();
				
				// 獲取是否需要執行   爲yes則執行  爲No則不執行
				Cell cellRunMode = excelwSheet0.getRow(j).getCell(2);
				String runMode = cellRunMode.getStringCellValue().toUpperCase();
				
				// 如果需要執行
				if (runMode.trim().equals("YES")){
					log.log(Level.INFO, "開始執行第"+j+"條");
					
					for (int i = 1; i <= rowLength1; i++){
						
						// 獲取用例ID
						Cell cellSuiteId1 = excelwSheet1.getRow(i).getCell(0);
						String suiteId1 = cellSuiteId1.getStringCellValue();
						
						// 如果用例頁的用例ID和用例集的用例ID一致則執行
						if (suiteId1.trim().equals(suiteId0)){
							// 獲取定位方式
							Cell cellMode = excelwSheet1.getRow(i).getCell(3); 
							String mode = cellMode.getStringCellValue();
							
							//獲取元素值
							Cell cellElevalue = excelwSheet1.getRow(i).getCell(4);
							String elevalue = cellElevalue.getStringCellValue();
							
							//獲取操作方法
							Cell cellOper = excelwSheet1.getRow(i).getCell(5);
							String oper = cellOper.getStringCellValue();
							
							// 獲取輸入值
							Cell cellValue = excelwSheet1.getRow(i).getCell(6);
							String value = cellValue.getStringCellValue();
							
							//獲取預期結果
							Cell cellExpected = excelwSheet1.getRow(i).getCell(7);
							String expected = cellExpected.getStringCellValue();
							
							// 執行用例
							try {
								actualValue = comm.getOpera(mode,elevalue,oper, value,expected,driver);
							} catch (Exception e){
								result = "false";
								suitResult = "false";
								continue;
							}
							
							// 寫入用例結果
							row = excelwSheet1.getRow(i);//得到excel表的行
							cell = row.getCell(9);//得到excel指定行的單元格(測試結果)
							
							cellactual = row.getCell(8); //得到excel指定行的單元格(實際結果)
							
							// 如果實際結果不爲空且預期結果不爲無,則將實際結果寫入excel,並用實際結果和預期結果進行比較判斷測試是否通過
							if (!(actualValue == null) && !(expected.equals("無"))){
								if (cellactual == null){
									cellactual = row.createCell(8);// 創建單元格
									cellactual.setCellValue(actualValue);// 設定單元格的值
								} else {
									cellactual.setCellValue(actualValue);
								}
								
								if (expected.equals(actualValue)){
									result = "true";
								} else {
									result = "false";
									suitResult = "false";
								}
							}
							
							if (cell == null){
								cell = row.createCell(9);// 創建單元格
								cell.setCellValue(result);// 設定單元格的值
							} else {
								cell.setCellValue(result);
							}
							
							// 如果測試結果不通過,則標紅,通過則將背景色變爲白色
							if (result.equals("false")){
								cell.setCellStyle(style);
							} else{
								cell.setCellStyle(stylewhite);
							}
						}	
					} // 用例集的結束
					
					// 將測試集結果寫入excel
					row = excelwSheet0.getRow(j);
					XSSFCell cellresult = row.getCell(3); //得到excel指定行的單元格(測試集的測試結果)
					if (cellresult == null){
						cellresult = row.createCell(3);// 創建單元格
						cellresult.setCellValue(suitResult);// 設定單元格的值
					} else {
						cellresult.setCellValue(suitResult);
					}
					
					log.log(Level.INFO, "第"+j+"條用例執行完成");
					
				} else {
					log.log(Level.INFO, "沒有要執行的用例");
					break;
				}				
			}
			// 將測試結果寫入excel  並關閉
			fileout = new FileOutputStream(path);
			excelwBook.write(fileout);
			fileout.flush();
			fileout.close();

		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} 
	}
}

結果:

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章