selenium項目--讀取測試用例


讀取測試用例

一直我們都沒有考慮過讀取測試用例的事,我們現在這樣設計測試用例有兩個好的點,在執行方法時,打印測試用例,方便知道執行的內容是什么,在報告展示時,把測試用例的結果展示出來

實現方案:目前我們demo使用讀取excel的報方式,把測試用例讀取出來,然后再來展示,大概讀取的case內容就是這樣,注意sheet名,目前我創建是與類名相同,方便讀取excel數據,原本是使用app做為演示,但是app出現一個問題,后期演示框架我直接使用PC的selenium做為演示,app除了啟動那不一樣,所有用到的內容和pc是一樣的,我新建了一個包和一個類,如圖:

clip_image001

並在項目下新建了一個文件夾叫testcase,下面放着測試用例,

clip_image002

testcase案例如下圖,注意sheet名,我是和項目名相同

image

實現

1. 在publicmethod類(類中是公共方法)中 寫讀取Excel內容,並返回一個map

public static Map<String, List> readTestCase(String className, String xlsFileName) {

Map<String, List> map = new HashMap<String, List>();

List<String[]> list = new ArrayList<String[]>();

Workbook rwb = null;

Cell cell = null;

InputStream stream;

try {

stream = new FileInputStream(projectPath + "/testCase/" + xlsFileName + ".xls");

rwb = Workbook.getWorkbook(stream);

} catch (FileNotFoundException e) {

logTest.logError("讀取excel出現異常,請檢測名稱是否對應正確或其他異常!!!");

e.printStackTrace();

} catch (BiffException e) {

logTest.logError("讀取excel出現異常,請檢測名稱是否對應正確或其他異常!!!");

e.printStackTrace();

} catch (IOException e) {

logTest.logError("讀取excel出現異常,請檢測名稱是否對應正確或其他異常!!!");

e.printStackTrace();

}

Sheet sheet = rwb.getSheet(className);

int rows = sheet.getRows();//獲取的行

int coumn = sheet.getColumns();//獲取的列

String[] strkey = new String[rows - 1];// 存取testCase的值

for (int i = 1; i < rows; i++) {

String[] strValue = new String[coumn - 1];// 存取每一行的數據

strkey[i - 1] = sheet.getCell(0, i).getContents();

for (int j = 1; j < coumn; j++) {

strValue[j - 1] = sheet.getCell(j, i).getContents();

}

list.add(strValue);

}

// 把行的數據加入map中

for (int i = 0; i < strkey.length; i++) {

map.put(strkey[i], Arrays.asList(list.get(i)));

}

return map;

}

  2. 在新增加了一個方法用於打印測試方法

*

* @測試點: 獲取方法名,並打印方法

@param @param getcase 獲取的測試數據,也就是獲取讀取excel后的數據

@param @param methodName 方法名

* @備注: void

* @author zhangjun

* @date 2017年9月15日

@修改說明

*/

public static void getTestCase(Map<String, List> getcase,String methodName){

try {

List checkCase=getcase.get(methodName);

logTest.logInfo("測試項:"+checkCase.get(1));

logTest.logInfo("測試描述:"+checkCase.get(2));

logTest.logInfo("驗證點:"+checkCase.get(3));

} catch (Exception e) {

logTest.logWarn("沒有獲取到方法名"+e.getMessage());

}

3. 使用方式,我們創建的testng的監聽中使用,因為這樣做 1.在執行前測試方法前就讀取測試用例 2.在用例成功或者失敗后,都打印測試用例的測試點,監聽請查看文章《appuim項目實戰---監聽testng》

@Override
	public void onStart(ITestContext testContext) {
		super.onStart(testContext);
	    String projectName = testContext.getSuite().getName();//獲取當前的項目名稱
	   
		getTestcases=publicmethod.readTestCase(projectName, "rosewholesale");//sheet名一直是用我們
		logTest.logInfo("【" + testContext.getName() + " Start】");

	}
	@Override
	public void onTestFailure(ITestResult tr) {
		super.onTestFailure(tr);
		publicmethod.getTestCase(getTestcases, tr.getName());
	}
	@Override
	public void onTestSuccess(ITestResult tr) {
	super.onTestSuccess(tr);
	publicmethod.getTestCase(getTestcases, tr.getName());
	}

  

4. testng配置

<?xml version="1.0" encoding="gb2312"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">

<suite name="rosewholePC" >
 <listeners>
		<listener class-name="until.TestngListener" /> 
		<listener class-name="until.RetryListener" /> 
	</listeners>
	<test name="version" preserve-order="true">
	     
	     <classes>
			  <class name="seleniumdemo.rosewholePC">
				<methods>		
					 <include name="choose_product"/>	
					  <include name="choose_product2"/>	
					  <include name="choose_product3"/>	
				</methods> 
			</class> 
	     </classes>
		
	</test>
</suite>

5. rosewholePC類中的代碼

package seleniumdemo;

import java.util.Arrays;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.Assert;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import com.relevantcodes.extentreports.ExtentReports;
import until.logTest;

public class rosewholePC {
   WebDriver driver;
	@BeforeClass
	public void beforeClass(){
		
		System.setProperty("webdriver.chrome.driver", "driver/chromedriver.exe"); 		#注意這里是驅動地址
		DesiredCapabilities capabilities = DesiredCapabilities.chrome();  
	    capabilities.setCapability("chrome.switches", Arrays.asList("--incognito"));  
	    ChromeOptions options = new ChromeOptions();  
	    options.addArguments("--test-type");  
	    capabilities.setCapability("chrome.binary", "driver/chromedriver.exe");  
	    capabilities.setCapability(ChromeOptions.CAPABILITY, options);  
	    driver = new ChromeDriver(capabilities);
	    driver.get("https://www.rosewholesale.com/");
	}
	@Test
	public void choose_product(){
     driver.findElement(By.xpath("//li[3]/a[@class='nav_t']")).click();
     String geturl=driver.getCurrentUrl();
	 if(geturl.contains("cheap")){
		 logTest.logInfo("進入到women頁面成功");
	 }else{
		 logTest.logError("進入到women頁面失敗");
		 Assert.assertTrue(false);
	 }
	}
	
	@Test
	public void choose_product2(){
		 logTest.logInfo("進行第二個case的驗證。。。。。。。。。。。。。。****");
		driver.findElement(By.xpath("//*[@id='js_proList']/ul/li[1]/p[1]/a[1]/img")).click();
		WebElement addtobag=driver.findElement(By.xpath("//div[@class='pro_propertyBtn clearfix']"));
		if(addtobag!=null){
			logTest.logInfo("進入到商品詳情頁");
		}
		
	}
	@Test
	public void choose_product3(){
		 logTest.logInfo("我模擬一個錯誤的操作");
		 Assert.assertTrue(false);
		
	}
	
}

  

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM