服务热线:13616026886

技术文档 欢迎使用技术文档,我们为你提供从新手到专业开发者的所有资源,你也可以通过它日益精进

位置:首页 > 技术文档 > JAVA > 新手入门 > 基础入门 > 查看文档

rft与spring结合进行自动化脚本开发之一


  目前,大部分自动化脚本开发者利用rft的测试对象地图管理测试脚本的测试对象,rft的测试对象地图有两种类型:公有测试对象地图和私有测试对象地图。

  (1)目前,大部分脚本采用的都是私有测试对象地图,如果被测试的程序顶层对象发生变化,这样每个脚本关联的测试对象地图都要进行修改,之所以这样,有一个原因:在rft中测试对象地图不能够继承(仅仅可以合并),如果两个测试对象地图之间可以进行继承,这样把公用的测试对象放在父测试对象地图中,其他的测试对象地图继承这个公用的测试对象地图(同时继承公有的测试对象),如果被测试程序顶层框架发生变化,仅仅只修改父测试对象地图就可以了,但是目前rft中不能实现(据我了解)。

  (2)rft中还有一种类型的测试对象地图是公有的测试对象地图,公有的测试对象地图可以实现公用的测试对象重复使用,如果测试对象是一模一样的,rft在公有的测试对象地图中只保留此测试对象的一个实例,但是把多个测试对象都插入到一个测试对象地图中,同样又面临着难以管理的困难。

  (3)另外,rft中的测试对象地图与脚本紧紧的耦合,如果程序即使发生很小的变动,也要更新测试对象地图,修改脚本等等。

  (4)spring具有强大的功能:ioc,可以实现在xml中把一个测试对象注入到另一个测试对象中,再加上可以利用rft的find方法可以把spring的配置文件配置成一张rft测试对象地图。另外,一个spring配置文件可以被另一个spring文件import进来。这样可以把公有的测试对象放在一个spring配置文件中,然后有其他的spring配置文件进行导入,可以实现测试对象地图(spring的配置文件)的继承。另外,在spring的配置文件中,可以注入一个对象的属性,这样可以人工的更改这个测试对象的父框架,另外可以可以注入测试对象的识别属性,这样可以进行人为的进行测试对象识别属性的更改(这样可以更好的应对被测试程序的变化)。
例如:

  xml 代码
  
<!--从classpath(类路径)中导入base-test-object-map.xml文件 可以使用base-test-object-map.xml文件中定义的bean-->
<!--导入时候要包含完整的包名-->
<import resource="classpath:pkg/base-test-object-map.xml"/>

<!--设置待查找对象的识别属性 格式:propertyname-propertyvalue-->
<property name="objectproperties">
<list>
<!--属性名称和属性精确匹配用=-->
<!--属性名称和属性用正则表达式匹配用:-->
<value>.class=html.formvalue> 然后是怎么使用spring配置文件中的测试对象
<value>.name:.*formvalue>
list>
property>

<!--设置从什么对象开始查找 注入父测试对象-->
<!--browsertestobject 引用的是base-test-object-map.xml文件中已经定义的bean-->
<property name="parenttestobject">
<ref bean="browsertestobject"/>
property>

  在脚本中引用测试对象通过如下语句:

  java 代码

textguitestobject text_username = new textguitestobject((testobject)context.getbean("usernametext"));
text_username.settext("system");

  (5)采用spring管理测试对象,也不是说一劳永逸的,如果被测试程序发生变化,避免不了的也要修改spring配置文件。但是,至少比rft管理测试对象地图的修改量要小的多。

  (6)采用spring管理测试对象也有以下缺点:

  (1)需要测试脚本开发者有比较高的编程技能(熟悉springioc基本配置和对rft api比较熟悉)

  (2)被测试程序结构规范,最好每个html元素都有其名字,还有每次修改程序,如果html元素名称能不修改最后就不修改(rft可以通过组件名称查找对象)还有就是按钮上面的文本,采用rft管理测试对象地图也有此要求。

  (3)要求对被测程序的组件结构有充分了解(可以通过rft测试地图了解)

  (4)开发采用spring管理测试对象地图的脚本比开发有rft管理的测试对象地图时间要长的多,但是带来的好处也是明显的。

  (5)测试对象执行动作的速度也没有rft管理的测试对象地图快。

  base-test-object-map.xml 代码

  xml 代码

<!--sp-->xml version="1.0" encoding="gb2312"?>
<!--ctype beans public "-//spring//dtd bean//en" </sp-->
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>

<!--注册com.rational.test.ft.script.property属性编辑器-->
<bean id="customeditorconfigurer" class="org.springframework.beans.factory.config.customeditorconfigurer">
<property name="customeditors">
<map>
<entry key="com.rational.test.ft.script.property">
<bean class="pkg.testobjectpropertyeditor"/>
entry>
map>
property>
bean>

<!--实例化pkg.basetestobjectscript类-->
<bean id="basetestobjectscript" class="pkg.basetestobjectscript"/>

<!--调用basetestobjectscript中的非静态方法getbrowsertestobject产生一个browsertestobject测试对象-->
<bean id="browsertestobject"
factory-bean="basetestobjectscript"
factory-method="getbrowsertestobject"
singleton="false"/>
<!--调用basetestobjectscript中的非静态方法getdocumenttestobject产生一个documenttestobject测试对象-->
<bean id="documenttestobject"
factory-bean="basetestobjectscript"
factory-method="getdocumenttestobject"
singleton="false"/>

beans>

  base-test-object-map.xml 可以作为公有的测试对象地图,其中customeditorconfigurer向容器注册了一个属性编辑,pkg.basetestobjectscript是一个rft脚本,此脚本有getbrowsertestobject()和documenttestobject(),可以在这个类中放入基本的测试对象,通过spring将这些对象封装为bean, 然后有其他的spring配置文件来import,这样就实现了对象的继承。pkg.basetestobjectscript的代码如下:

  java 代码

package pkg;

import resources.pkg.basetestobjectscripthelper;

import com.rational.test.ft.*;
import com.rational.test.ft.object.interfaces.*;
import com.rational.test.ft.object.interfaces.siebel.*;
import com.rational.test.ft.script.*;
import com.rational.test.ft.value.*;
import com.rational.test.ft.vp.*;

public class basetestobjectscript extends basetestobjectscripthelper
{
public void testmain(object[] args)
{
}

public browsertestobject getbrowsertestobject()
{
return browser_htmlbrowser(document_h(),default_flags);
}

public guitestobject getdocumenttestobject()
{
return document_h();
}
}

  pkg.testobjectpropertyeditor代码如下,其中如果用精确匹配就用=作为分隔符,如果是通过正则表达式匹配就通过:作为分隔符,然后在程序内部就会做正则表达式的转换。

  java 代码

package pkg;

import com.rational.test.ft.script.property;
import java.beans.propertyeditorsupport;
import java.util.stringtokenizer;
import com.rational.test.ft.value.regularexpression;

public class testobjectpropertyeditor extends propertyeditorsupport
{
public void setastext(string text)
{
string delimiter = null;
object propvalue = null;

if(text == null || text.length() < 1 || (text.indexof(equal_mark) == -1 && text.indexof(colon) == -1))
{
throw new illegalargumentexception("识别属性为空或格式不正确 =表示进行精确匹配 :表示使用正则表达式匹配");
}

if(text.indexof(equal_mark) != -1)
{
delimiter = equal_mark;
}
else if(text.indexof(colon) != -1)
{
delimiter = colon;
}

//解析字符串
stringtokenizer st = new stringtokenizer(text, delimiter);
string name = st.nexttoken();
string value = st.nexttoken();

propvalue = value;

if(text.indexof(colon) != -1) //如果分隔符为 : 将propvalue设置为正则表达式
{
propvalue = new regularexpression(value, false);
}
setvalue(new property(name, propvalue));
}

public string getastext()
{
property property = (property)getvalue();
return property.getpropertyname() + "-" + property.getpropertyvalue();
}

public final string equal_mark = "="; //"=" 表示进行精确匹配
public final string colon = ":"; //":" 表示使用正则表达式匹配
}

  然后介绍一下其他的spring配置文件通过导入另一个spring配置文件实现,测试对象的继承。如下,通过import另一个配置文件,这个spring配置文件中的对象就可以使用被导入的spring配置文件中的测试对象,从而可以实现测试对象的继承。

  pkg/logon-map.xml

  xml 代码

<!--sp-->xml version="1.0" encoding="gb2312"?>
<!--ctype beans public "-//spring//dtd bean//en" </sp-->
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>

<!--从classpath(类路径)中导入base-test-object-map.xml文件 可以使用base-test-object-map.xml文件中定义的bean-->
<!--导入时候要包含完整的包名-->
<import resource="classpath:pkg/base-test-object-map.xml"/>

<!--实例化pkg.testobjecthelper类-->
<bean id="loginformhelper"
class="pkg.testobjecthelper"
singleton="false">

<!--设置待查找对象的识别属性 格式:propertyname-propertyvalue-->
<property name="objectproperties">
<list>
<!--属性名称和属性精确匹配用=-->
<!--属性名称和属性用正则表达式匹配用:-->
<value>.class=html.formvalue>
<value>.name:.*formvalue>
list>
property>

<!--设置从什么对象开始查找 注入父测试对象-->
<!--browsertestobject 引用的是base-test-object-map.xml文件中已经定义的bean-->
<property name="parenttestobject">
<ref bean="browsertestobject"/>
property>
bean>

<!--调用loginformhelper中的getsontestobject 此方法返回testobject测试对象-->
<bean id="loginform"
factory-bean="loginformhelper"
factory-method="getsontestobject"
singleton="false">
bean>

<!--动态查找text_username测试对象-->
<bean id="usernametexthelper"
class="pkg.testobjecthelper"
singleton="false">
<property name="objectproperties">
<list>
<value>.class=html.input.textvalue>
<value>.name=usernamevalue>
list>
property>

<property name="parenttestobject">
<ref bean="loginform"/>
property>
bean>

<!--动态查找text_password测试对象-->
<bean id="usernametext"
factory-bean="usernametexthelper"
factory-method="getsontestobject"
singleton="false">
bean>

<bean id="passwordtexthelper"
class="pkg.testobjecthelper"
singleton="false">
<property name="objectproperties">
<list>
<value>.class=html.input.passwordvalue>
<value>.name=passwordvalue>
list>
property>

<property name="parenttestobject">
<ref bean="loginform"/>
property>
bean>

<bean id="passwordtext"
factory-bean="passwordtexthelper"
factory-method="getsontestobject"
singleton="false">
bean>

<!--动态查找ubmit_button测试对象-->
<bean id="submitbuttonhelper"
class="pkg.testobjecthelper"
singleton="false">
<property name="objectproperties">
<list>
<value>.class=html.input.submitvalue>
<value>.value=进入value>
list>
property>

<property name="parenttestobject">
<ref bean="loginform"/>
property>
bean>

<bean id="submitbutton"
factory-bean="submitbuttonhelper"
factory-method="getsontestobject"
singleton="false">
bean>

<!--点击菜单-->
<bean id="menuhelper"
class="pkg.menuhelper"
singleton="false">

<!--待点击菜单名称 必须按照先后顺序-->
<property name="menus">
<list>
<value>菜单名称1value>
<value>菜单名称2value>
<value>菜单名称3value>
<value>菜单名称4value>
list>
property>

<!--注入父测试对象 定义从什么对象开始查找菜单并点击-->
<property name="parenttestobject">
<ref bean="documenttestobject"/>
property>
bean>

beans>

  下面是上面的配置文件中用到的pkg.testobjecthelper类,这个类暴露出parenttestobject 和 objectproperties两个属性用spring来注入,从而确定从什么对象开始查找对象和以什么条件查找对象。通过getsontestobject() 方法返回找到的测试对象(只允许返回一个对象,如果找到多个就会抛出异常),其中还有个getsontestobjects()方法是为了返回多个测试对象而准备的。代码如下:

package pkg;

import com.rational.test.ft.ambiguousrecognitionexception;
import com.rational.test.ft.objectnotfoundexception;
import com.rational.test.ft.object.interfaces.testobject;
import com.rational.test.ft.script.property;
import com.rational.test.ft.script.subitemfactory;

public class testobjecthelper
{
public property[] getobjectproperties()
{
return objectproperties;
}

public void setobjectproperties(property[] objectproperties)
{
this.objectproperties = objectproperties;
}

public testobject getparenttestobject()
{
return parenttestobject;
}

public void setparenttestobject(testobject parenttestobject)
{
this.parenttestobject = parenttestobject;
}

public testobject getsontestobject()
{
return findsontestobject();
}

public void setsontestobject(testobject sontestobject)
{
this.sontestobject = sontestobject;
}

扫描关注微信公众号