Excel数据驱动框架实战

本文详述了一个Excel数据驱动的自动化测试框架搭建过程,包括工程配置、页面对象、测试逻辑封装、日志记录及数据文件管理。通过分离页面元素定位、使用PageObject模式、封装常用操作和数据驱动测试,提高了测试效率和代码可维护性。
摘要由CSDN通过智能技术生成

由于该测试框架是我在工作的系统中搭建的,故不写系统网址,重点记录搭建的框架过程与重点介绍,方便以后察看。

一、系统介绍:

1)在系统中进行登陆

2)在系统登陆的情况下,进行新增操作

3)在新增的数据中进入详情界面,新增属性值

二、数据驱动框架搭建的详细过程:

1、新建一个java工程,并配置好工程中的WebDriver和TestNG环境,并导入Excel操作相关和Log4j相关的JAR文件到工程中。

2、在工程中新建4个Package,分别命名为:

1)cn.oms.modules,用于实现复用的业务逻辑封装方法。

2)cn.oms.pageobjects,用于实现被测系统的页面对象。

3)cn.oms.autotest,用于实现具体的测试脚本逻辑。

4)cn.oms.util,用于实现测试过程中调用的工具类方法,例如文件操作、mapObject、页面元素的操作方法等。

3、在cn.oms.util的Package下新建ObjectMap类,用于实现在外部配置文件中配置页面元素的定位表达式。ObjectMap代码如下:

import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
public class ObjectMap {
	Properties properties;
	public ObjectMap(String propFile){
		properties=new Properties();
		try{
			FileInputStream in=new FileInputStream(propFile);
			properties.load(in);
			in.close();
		}catch(IOException e){
			System.out.println("读取对象文件出错");
			e.printStackTrace();
		}
	}
	public By getLocator(String ElementNameInpropFile) throws Exception{
		//根据变量ElementNameInpropFile,从属性配置文件中读取对应的配置文件
		String locator=properties.getProperty(ElementNameInpropFile);
		//将配置对象中的定位类型存到locatorType变量,将定位表达式的值存入locatorValue变量
		String locatorType=locator.split(">")[0];
		String locatorValue=locator.split(">")[1];
		//在Eclipse中的配置文件均默认为ISO-8859-1编码存储,使用getBytes方法可以将字符串编码转换为UTF-8编码,以此来解决在配置文件读取中文为乱码的问题
		locatorValue=new String(locatorValue.getBytes("ISO-8859-1"), "UTF-8");
		//输出locatorType变量值和locatorValue变量值,验证是否赋值正确
		System.out.println("获取的定位类型:"+locatorType+"\t 获取的定位表达式"+locatorValue);
		//根据locatorType的变量值内容判断返回何种定位方式的By对象
		if(locatorType.toLowerCase().equals("id"))
			return By.id(locatorValue);
		else if(locatorType.toLowerCase().equals("name"))
			return By.name(locatorValue);
		else if((locatorType.toLowerCase().equals("classname"))||(locatorType.toLowerCase().equals("class")))
			return By.className(locatorValue);
		else if((locatorType.toLowerCase().equals("tagname"))||(locatorType.toLowerCase().equals("tag")))
			return By.className(locatorValue);
		else if((locatorType.toLowerCase().equals("linktext"))||(locatorType.toLowerCase().equals("link")))
			return By.linkText(locatorValue);
		else if(locatorType.toLowerCase().equals("partiallinktext"))
			return By.partialLinkText(locatorValue);
		else if((locatorType.toLowerCase().equals("cssselector"))||(locatorType.toLowerCase().equals("css")))
			return By.cssSelector(locatorValue);
		else if(locatorType.toLowerCase().equals("xpath"))
			return By.xpath(locatorValue);
		else {
			throw new Exception("输入的locator type未在程序中定义"+ locatorType);
		}
	}
}
4、在工程中添加一个存储页面定位方式和定位表达式的配置文件objectMap.properties,文件内容如下:

oms.loginPage.username=id>username
oms.loginPage.password=id>password
oms.loginPage.loginbutton=xpath>//button[@type='submit']
oms.StationManagePage.stationMenu=id>menu_0000000002
oms.StationManagePage.smanageMenu=id>menu_0000000060
oms.StationManagePage.createstationMenu=xpath>//a[@data-target='#modStationModel']
oms.StationManagePage.I_stationName=name>station_name
oms.StationManagePage.I_operMerchant=name>oper_merchant_id
oms.StationManagePage.I_selectedArea=xpath>//*[@id='city_box'][@ng-show='!!!required']/select[1]
oms.StationManagePage.I_selectedCity=xpath>//*[@id='city_box'][@ng-show='!!!required']/select[2]
oms.StationManagePage.I_selectedDistrict=xpath>//*[@id='city_box'][@ng-show='!!!required']/select[3]
oms.StationManagePage.I_establishTime=name>establish_time
oms.StationManagePage.I_saveButton=xpath>//button[@w5c-form-submit='modifyOwnOnlineStation();']
oms.StationManagePage.I_OKButton=xpath>//button[@data-bb-handler='ok']
oms.StationManagePage.I_search=xpath>//input[@type='search']
oms.StationManagePage.I_detailButton=xpath>//a[@class='btn']
oms.StationManagePage.I_locationManageButton=xpath>//a[@ng-click='stationLocationManage();']
oms.StationManagePage.I_newCreateButton=id>addSubmit
oms.StationManagePage.I_coordinateX=name>coordinate_x
oms.StationManagePage.I_coordinateY=name>coordinate_y
oms.StationManagePage.I_locationsaveButton=xpath>//button[@w5c-form-submit='modifyStationLocation();']
oms.StationManagePage.I_locationokButton=xpath>//button[@data-bb-handler='ok']
oms.StationManagePage.I_locationcloseButton=xpath>//*[@id='stationLocationList']/div/div/div[3]/button
测试页面中的所有页面元素的定位方式和定位表达式均可在此文件中进行定义,实现定位数据和测试程序的分离。在一个配置文件中修改定位数据,可以在测试脚本中全局生效,此方式可以大大提高定位表达式的维护效率。

5、在cn.oms.pageobjects的Package下新建Login_Page类,用于实现系统登陆页面的pageobject对象。Login_Page类的代码如下:

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import cn.oms.util.ObjectMap;

public class Login_Page {
	private WebElement element=null;
	//指定页面元素定位表达式配置文件的绝对文件路径
	private ObjectMap objectMap=new ObjectMap("E:\\test summary\\DataDrivenFrameWork\\objectMap.properties");
	private WebDriver driver;
	public Login_Page(WebDriver driver){
		this.driver=driver;
	}
	//返回登录页面中的用户名输入框页面元素对象
	public WebElement username() throws Exception{
		//使用ObjectMap类中的getLocator方法获取配置文件中关于用户名的定位方式和定位表达式
		element=driver.findElement(objectMap.getLocator("oms.loginPage.username"));
		return element;
	}
	//返回登录页面中的密码输入框页面元素对象
	public WebElement password() throws Exception{
		// 使用ObjectMap类中的getLocator方法获取配置文件中关于密码
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值