自动化测试~数据源
-
介绍
本篇介绍框架PhoenixAutotest中的数据源模块以及在项目中的使用示例。阅读本文的前提,是您已经完成了基础入门篇,如果您错过了请点击入门介绍。
所谓数据源,即把在测试过程中所要录入的数据集中地放在一个地方(源),而不是散落在脚本(代码)中。这样的好处显而易见,只能清晰,维护简便。为了能尽可能地满足各种实际情况,框架不仅提供了几种(XML、Excel等)内置的实现,用户还可以根据自己的情况来做订制、扩展。
- 支持的类型
目前框架支持三种格式的数据源文件,分别是:xml、yaml、excel。xml默认已经在框架中包含,其他的类型,需要另外在pom.xml中添加依赖。
另外,更加重要的是,框架支持您添加任意喜欢的文件格式。
通过下面的教程,您可以把数据中脚本中抽离出来,使得脚本更容易维护。但缺点是,下面的例子都是固定的数据,如果需要动态数据的话,请参考框架的参数化章节。
- XML
如果您使用的开发工具为Eclipse的话,建议先根据《Eclipse智能提示-XML》来配置XSD。
location为https://surenpi.com/schema/datasource/autotest.web.framework.datasource.xsd,key为datasource.surenpi.com。配置好后,根据下面的步骤来添加xml格式的数据源:
生成的xml文件如下:
<?xml version="1.0" encoding="UTF-8"?> <suren:dataSources xmlns:suren="https://datasource.surenpi.com" xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="https://datasource.surenpi.com https://surenpi.com/schema/datasource/autotest.web.framework.datasource.xsd " pagePackage="demo.page"> <suren:dataSource pageClass="BaiduPage"> <suren:page> <suren:field data="surenpi.com" name="keyword" /> </suren:page> </suren:dataSource> </suren:dataSources>
Page类修改如下:
package demo.page;
import org.suren.autotest.web.framework.annotation.AutoAttrLocator;
import org.suren.autotest.web.framework.annotation.AutoDataSource;
import org.suren.autotest.web.framework.annotation.AutoPage;
import org.suren.autotest.web.framework.selenium.WebPage;
import com.surenpi.autotest.datasource.DataSourceConstants;
import com.surenpi.autotest.webui.ui.Button;
import com.surenpi.autotest.webui.ui.Text;
@AutoPage(url = "https://baidu.com",
startPage = true)
@AutoDataSource(resource = "baidu-data.xml",
type = DataSourceConstants.DS_TYPE_XML)
public class BaiduPage extends WebPage
{
@AutoAttrLocator(tagName = "input", name = "id", value = "kw")
private Text keyword;
@AutoAttrLocator(tagName = "input", name = "value", value = "百度一下")
private Button searchBut;
public Text getKeyword()
{
return keyword;
}
public void setKeyword(Text keyword)
{
this.keyword = keyword;
}
public Button getSearchBut()
{
return searchBut;
}
public void setSearchBut(Button searchBut)
{
this.searchBut = searchBut;
}
}
测试类如下:
package demo;
import java.io.IOException;
import org.suren.autotest.web.framework.annotation.AutoApplication;
import org.suren.autotest.web.framework.settings.Phoenix;
import org.suren.autotest.web.framework.util.ThreadUtil;
import demo.page.BaiduPage;
@AutoApplication
public class BaiduTest
{
public static void main(String[] args) throws IOException
{
Phoenix phoenix = new Phoenix(BaiduTest.class);
// phoenix.init();
phoenix.initWithData();
BaiduPage page = phoenix.getPage(BaiduPage.class);
page.open();
// page.getKeyword().fillValue("PhoenixAutotest");
page.getKeyword().fillValue();
page.getSearchBut().click();
ThreadUtil.silentSleep(3000);
phoenix.close();
}
}
- Excel
如果要使用Excel格式数据源的话,需要先在pom.xml中添加下面的依赖:
<!-- https://mvnrepository.com/artifact/com.surenpi.autotest/autotest.datasource.excel --> <dependency> <groupId>com.surenpi.autotest</groupId> <artifactId>autotest.datasource.excel</artifactId> <version>1.0.0-20170706</version> </dependency>
然后,编辑Excel数据文件baidu-data.xlsx,按照下图所示的格式来填入数据(保存在src/main/resources目录中):
最后修改Page类,启用yaml配置,重新启动测试即可:
@AutoPage(url = "https://baidu.com",
startPage = true)
//@AutoDataSource(resource = "baidu-data.xml",
// type = DataSourceConstants.DS_TYPE_XML)
//@AutoDataSource(resource = "baidu-data.yaml",
// type = DataSourceConstants.DS_TYPE_YAML)
@AutoDataSource(resource = "baidu-data.xlsx",
type = DataSourceConstants.DS_TYPE_EXCEL)
public class BaiduPage extends WebPage
注意:sheet的名称为Page类名(全称),第一行为列名,从第二行开始就是Page类中的属性名和数据了。
- YAML
如果要使用YAML格式数据源的话,需要先在pom.xml中添加下面的依赖:
<dependency> <groupId>com.surenpi.autotest</groupId> <artifactId>autotest.datasource.yaml</artifactId> <version>1.0.0-20170706</version> </dependency>
然后,在src/main/resources下添加文件baidu-data.yaml:
demo.page.BaiduPage: keyword: phoenix_datasource
最后修改Page类,启用yaml配置,重新启动测试即可:
@AutoPage(url = "https://baidu.com",
startPage = true)
//@AutoDataSource(resource = "baidu-data.xml",
// type = DataSourceConstants.DS_TYPE_XML)
@AutoDataSource(resource = "baidu-data.yaml",
type = DataSourceConstants.DS_TYPE_YAML)
public class BaiduPage extends WebPage
-
系列文章
-
参考
本文为原创,如果您当前访问的域名不是surenpi.com,请访问“素人派”。
最后更新:2017-08-15 10:02:42




