2.6 Bootstrap 的作用是什么
在第1 章中, 介绍了qing-automation 示例项目, 并对FunctionalTest Suites 和Catalogs and Libraries 这两个模块进行了讲解。第三个模块Data Organize 可以用来放XPath 等数据,当把数据放入这里时,别人可以共用。而本节要重点讲解第四个模块Bootstarp Data Suite(启动项),即:这个启动项模块要放入什么内容,图2-6-1 是Bootstrap 在首页的位置。
图2-6-1 Bootstarp Data 在首页的位置
在上一节中,我们讲解了数据库测试组件,在测试案例的脚本中,有如下连接数据库的步骤:
| s c r i p t | D b S l i m S e t u p | ! - c o m . m y s q l . j d b c . D r i v e r -!|jdbc:mysql://localhost:3306/automation|root||
如果每个数据库都有这个步骤的话,那脚本无疑是冗余的。我们知道,这句话是在读取数据库的连接配置信息,此时它就可以放入启动项模块里。
要想让我们启动项里的配置能被后台代码使用,那么我们运行启动项时,就应该把这些配置信息放入一个文件里(比如.properties 文件),当代码需要读取配置信息时,直接读取这个文件就行了。
我们在Bootstarp Data Suite 新建一个测试案例, 命名为SystemConfiguration,放入如下内容。
!2 Overview This page is used to create a set of bootstrap data used in all of the tests. !define TEST_SYSTEM {slim} !3 ''Database Configuration'' |script |systemConfiguration.Configuration | |set comments |database configuration | |set configuration|dbDriver=com.mysql.jdbc.Driver | |set configuration|dbUrl=jdbc:mysql://localhost:3306/automation| |set configuration|dbUsername=root | ''finish step will set this configuration to backend'' !|script| |finish | |
接着在src 文件夹中新建一个systemConfiguration 包,在包里再新
建一个Configuration 类,代码如下。
package systemConfiguration; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.util.Properties; /** * 此类用于创建一个 properties 文件,里面放的都是各种配置信息 * * */ public class Configuration { private String _str = null; private FileOutputStream out; /** * 放入注释 * @param comments */ public void setComments(String comments) { _str = _str + "#" + comments + "\r\n"; } /** * 放入配置信息。 * @param configration */ public void setConfiguration(String configration) { _str = _str + configration + "\r\n"; } /** * 把信息放到新建的文件'configuration.properties' 里。 * @throws IOException */ public void finish() throws IOException { byte[] buff = new byte[] {}; buff = _str.replace("null", "").getBytes(); out = new FileOutputStream(System.getProperty("user.dir") + "/configuration.properties"); out.write(buff, 0, buff.length); } /** * 从'configuration.properties' 文件里读取配置信息。 * @throws IOException */ public String getParam(String key) { Properties prop = new Properties(); try { prop.load(new FileInputStream(System.getProperty("user. dir") + "/configuration.properties")); } catch (IOException e) { e.printStackTrace(); } return prop.getProperty(key).trim(); } } |
接下来,我们编译代码,运行测试案例,看一下在项目根目录下有没有新建configuration.properties 文件,里面内容如下。
#database configuration dbDriver=com.mysql.jdbc.Driver dbUrl=jdbc:mysql://localhost:3306/automation dbUsername=root 在类DbSlimSetup 中加入下面这个构造器。 Configuration getConfiguration = new Configuration(); S t r i n g j d b c D r i v e r C l a s s = g e t C o n f i g u r a t i o n . getParam("dbDriver"); String connectURI = getConfiguration.getParam("dbUrl"); String username = getConfiguration.getParam("dbUsername"); String password =""; DbConnectionFactory.getDataSource( DEFAULT_CONNECTION_POOL_NAME, jdbcDriverClass, connectURI, username, password, DEFAULT_CONNECTION_POOL_MIN_IDLE, DEFAULT_ CONNECTION_POOL_MAX_AXTIVE); } 然后在DbSlimSelectQuery、DbSlimUpdateQuery、DbSlimWaitQuery这三个类里找到 DataSource dataSource = DbConnectionFactory.getDataSource(connectionPoolName); 这句话,并在这句话前加入如下代码。 try { DbSlimSetup dbsetUp = new DbSlimSetup(); } catch (Exception e1) { // TODO Auto-generated catch block e1.printStackTrace(); } |
这样,就不用每次都在测试案例里输入配置信息了,只需要在第一次运行这个Bootstrap Suite 就可以了。
Bootstrap Suite 不仅可以用来配置信息管理,还可以用于加载启动数据。我们可以建一个案例,把每个测试案例需要的数据用这个案例加入数据库中。
作业:
打开qing-automation 里的FrontPage 文件夹,把里面的content.txt 文件复制出来,放入FitNesse 项目里的FrontPage 文件夹下,把这个目录按照你公司(或者你自己)的需求整理好。
本文选自《测之重器——自动化测试框架搭建指南》第二章,本站经电子工业出版社和作者的授权。
版权声明:51Testing软件测试网获电子工业出版社和作者授权连载本书部分章节。任何个人或单位未获得明确的书面许可,不得对本文内容复制、转载或进行镜像,否则将追究法律责任。