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);
		
	}
	
}

  

原文地址:https://www.cnblogs.com/chongyou/p/7567080.html