2009年4月20日

        找到对象之后,就需要对其进行操作了。在对对象进行操作之前,需要了解RFT处理对象的方式。根据RFT的文档,Java对象的关系如下图:


        由此可见,绝大多数的对象都是继承于GuiTestObjectGuiSubitemTestObject。类似ButtonCheckBox这样的简单对象,自然是继承于GuiTestObject,而像ListTable这样的有内部子对象的复杂对象,一定是继承于GuiSubitemTestObject。根据这一规律,就可以分别建立你所需要的类了。

        负责Button的类如下:

package framework.widgets;

import java.awt.Point;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.rational.test.ft.object.interfaces.GuiTestObject;
import com.rational.test.ft.object.interfaces.TestObject;

public class WButton extends GuiTestObject {
    
    
public WButton(TestObject button) {
        
super(button);
    }
    
    
public void click() {
        
super.click();
    }
    
    
public void click(int x, int y) {
        
super.click(new Point(x, y));
    }
    
    
public void doubleClick() {
        
super.doubleClick();
    }
    
    
public boolean isEnabled() {
        
return super.isEnabled();
    }
}

    其他简单对象也可以继承ToggleGUITestObjectTextScrollTestObject等其他衍生于GuiTestObject类,这些类封装了很多实用的方法可以直接使用,具体请参考RFT文档中的API

    负责TabPane的类如下:

package framework.widgets;

import java.util.Vector;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.rational.test.ft.object.interfaces.GuiSubitemTestObject;
import com.rational.test.ft.object.interfaces.TestObject;
import com.rational.test.ft.script.Index;
import com.rational.test.ft.script.Text;
import com.rational.test.ft.vp.ITestData;
import com.rational.test.ft.vp.ITestDataElementList;
import com.rational.test.ft.vp.ITestDataList;

public class WTabbedPane extends GuiSubitemTestObject {

    
static final Logger logger = LoggerFactory.getLogger("WTabbedPane.class");
    
    
public WTabbedPane(TestObject tabbedPane) {
        
super(tabbedPane);
    }
    
    
public void clickTab(String tabName) {
        
this.click(new Text(tabName));
    }
    
    
public void clickTab(int index) {
        
this.click(new Index(index));
    }
    
    
public int getTabCount() {
        logger.info(
"Get tab count");
        ITestData data 
= (ITestData) super.getTestData("list");
        ITestDataList list 
= (ITestDataList) data;
        
return list.getElementCount();
    }

    
public int findTab(String text) {
        logger.info(
"Find the index of tab {}", text);
        Vector contents 
= this.getAllTabs();
        
for (int i = 0; i < contents.size(); i++) {
            
if (contents.get(i).toString().equals(text)) {
                
return i;
            }
        }
        
return -1;
    }

    
public String getTabText(int index) {
        logger.info(
"Get tab text with index {}", index);
        Vector contents 
= this.getAllTabs();
        
if (index < 0 || index >= contents.size()) {
            
return null;
        }
        
return (String) contents.get(index);
    }
    
    
public Vector getAllTabs() {
        logger.info(
"Get all tabs");
        ITestDataList dataList 
= (ITestDataList) super.getTestData("list");
        ITestDataElementList elementList 
= (ITestDataElementList) dataList
                .getElements();
        
return elementList.getElements();
    }
}

    由上面的例子可以看出来,对对象的操作可以分为两类:一类是施加行为,一类是读取数据。对此,RFT的API给出了详细的文档,例如对于List对象,文档如下:

    由此可看出,对于List对象,可以通过.class, .classIndex, .itemCount, .itemText, .priorLabel,accessibleContext.accessibleName,name和toolTipText这些属性进行识别。获取List对象后,它支持Text和Index这两类子对象,可通过ITestDataList接口获得全部列表元素和已选列表元素。上面getAllTabs()方法就是用来获得所有Tab选项的,可以作为参考。

    还有一些更为复杂的简单对象,如:TextField和Frame,以及复杂对象,如:Table和Tree。如果你能够理解上述处理对象的方法,那么完全可以编写符合自己项目需要的方法,对这些对象进行各种各样的操作。



posted @ 2009-04-20 16:44 terrypang 阅读(1435) | 评论 (2)编辑 收藏

2009年3月31日

如何灵活查找窗体内的对象呢?和查找窗体类似,可以使用这些对象特有的属性,依照一定的查找逻辑进行处理。下面是一个典型的查找方法,以此为例进行说明:

    public TestObject getObject(ArrayList<Property> v) {

        rootTO.waitForExistence(waitMaxTime, waitCheckInterval);
        TestObject returnObject 
= null;
        TestObject to[] 
= null;
        
double timeNow = System.currentTimeMillis() / 1000;
        
double endTime = timeNow + waitMaxTime;
        v.add(
new Property("showing""true"));
        
while (returnObject == null && timeNow < endTime) {
            to 
= rootTO.find(atDescendant((Property[]) v.toArray(new Property[0])));
            
if (to.length > 1{
                
 throw new AmbiguousRecognitionException("Find more than one object.");
            }

            
if (to.length == 1{
                returnObject 
= to[0];
            }
 else
                sleep(waitCheckInterval);
            timeNow 
= System.currentTimeMillis() / 1000;
        }

        
return returnObject;
    }

上面的方法根据传入的参数集合对当前窗口中的所有对象进行查找。和之前的窗体查找一样,最好显示的添加showing=true参数,因为在Swing程序的运行过程中,内存中会对GUI元素进行缓存,可能一个界面消失了,但它还在内存中,等待着随后被显示。这样一来,就需要这个参数过滤到所有未被显示的GUI元素。在实际使用过程中,可以使用如下的方法进行调用: (调用前使用RFT的对象查看器确定待查找对象的唯一属性)

protected WButton getButton(String name) {
        ArrayList
<Property> v = new ArrayList<Property>();
        v.add(
new Property(".class""javax.swing.JButton"));
        v.add(
new Property("accessibleContext.accessibleName", name));
        TestObject to 
= og.getObject(v);
        
if (!Utility.exists(to))
            
throw new ObjectNotFoundException();
        
else
            
return new WButton(to);
    }
 

与窗口处理一样,如果某些参数需要使用正则表达式处理,可以使用下面的方法:

    protected WListBox getList(String label) {
        RegularExpression exp 
= new RegularExpression(".*JComboBox$|.*JList$"false);
        ArrayList
<Property> v = new ArrayList<Property>();
        v.add(
new Property(".class", exp));
        v.add(
new Property(".priorLabel", label));
        TestObject to 
= og.getObject(v);
        
if (!Utility.exists(to))
            
throw new ObjectNotFoundException();
        
else
            
return new WListBox(to);
    }
 

在对象查找过程中,可能需要各种不同的查找逻辑。例如,如果对象可能存在也可能不存在,在查找的时候就不需要等待并反复查找,这时候,可以使用如下的方法:

    public TestObject getObjectWithoutWait(ArrayList<Property> v) {
        rootTO.waitForExistence();
        TestObject returnObject 
= null;
        v.add(
new Property("showing""true"));
        TestObject to[] 
= rootTO.find(atDescendant((Property[]) v.toArray(new Property[0])));
        
if (to.length > 1{
            
throw new AmbiguousRecognitionException(
                    Find more than one object.);
        }

        
if (to.length == 1{
            returnObject 
= to[0];
        }

        
return returnObject;
    }

 

有时候,界面上有多个具有相同属性的对象,只能通过他们的编号来区分他们;有时候需要以某个确定对象为根来进行查找;有时候需要查找直接子对象而不是所有子对象,等等。并且,这些逻辑之间也存在排列组合的情况,实际使用中可以根据自身需要灵活处理。这些方法都是对上面基本方法的扩展,大家可以尝试自己来实现。

posted @ 2009-03-31 09:31 terrypang 阅读(1172) | 评论 (0)编辑 收藏

2009年3月21日

 

窗体是程序的基础。无论是主窗体,还是弹出窗体,他们往往都是需要首先定位的对象。窗体,作为一类特殊对象,他们都是根对象的直接子对象,针对这一特点,对他们定位就非常简单了。通常,通过窗体标题,就能很好的找到所需的窗体。具体方法如下。

private double waitCheckInterval = ((Double) getOption(IOptionName.WAIT_FOR_EXISTENCE_DELAY_BETWEEN_RETRIES))   
        .doubleValue();    
private double waitMaxTime = ((Double) getOption(IOptionName.MAXIMUM_WAIT_FOR_EXISTENCE))   
        .doubleValue();    
private TestObject rootTO = null;   
public boolean getRootWithCaption(String captionExpr) {   
    
double timeNow = System.currentTimeMillis() / 1000;   
    
double endTime = timeNow + waitMaxTime;   
    rootTO 
= null;   
    
while (rootTO == null && timeNow < endTime) {   
        RootTestObject root 
= RootTestObject.getRootTestObject();   
        TestObject[] ftWinObjects 
= null;   
        RegularExpression exp 
= new RegularExpression(captionExpr, false);   
        ArrayList
<Property> v = new ArrayList<Property>();   
        v.add(
new Property(".captionText", exp));   
        v.add(
new Property(".domain""Java"));   
        v.add(
new Property("showing""true"));   
        ftWinObjects 
= root.find(atChild((Property[]) v.toArray(new Property[0])));   
        
if (ftWinObjects != null{   
            
if (ftWinObjects.length > 1{   
                
throw new AmbiguousRecognitionException("Find more windows with capture: " + captionExpr);   
            }
   
            
if (ftWinObjects.length == 1{   
                rootTO 
= ftWinObjects[0];   
                
return true;   
            }
   
        }
 else {   
            sleep(waitCheckInterval);   
            timeNow 
= System.currentTimeMillis() / 1000;   
        }
   
    }
   
    
return false;   
}
  
 

上面的方法首先取得对象查找的间隔时间、最大等待时间,并声明了空的窗体对象。接下来进入方法,根据查找结果和最大等待时间来循环查找窗体。先获得根测试对象,然后查找其直接子对象,查找条件为:窗体标题符合正则表达式captionExpr的定义,属于Java域,并且当前为显示状态。最后处理查找结果,如果结果大于1个,则抛出异常;如果结果等于1,则返回true;如果结果为空,则等待并重新计算时间,并继续循环查找;如果最后仍未空并退出循环,则返回false

有的时候,窗口的出现并不是一定的,例如很多弹出窗口。这时候,对象查找并不需要循环等待,相应的方法应为:

public boolean getRootWithCaptionWithoutWait(String captionExpr) {   
    rootTO 
= null;   
    sleep(waitCheckInterval);   
    RootTestObject root 
= RootTestObject.getRootTestObject();   
    TestObject[] ftWinObjects 
= null;   
    RegularExpression exp 
= new RegularExpression(captionExpr + "$"false);   
    ArrayList
<Property> v = new ArrayList<Property>();   
    v.add(
new Property(".captionText", exp));   
    v.add(
new Property(".domain""Java"));   
    v.add(
new Property("showing""true"));   
    ftWinObjects 
= root.find(atChild((Property[]) v.toArray(new Property[0])));   
    
if (ftWinObjects != null{   
        
if (ftWinObjects.length > 1{   
            
throw new AmbiguousRecognitionException("Find more windows with capture: " + captionExpr);   
        }
   
        
if (ftWinObjects.length == 1{   
            rootTO 
= ftWinObjects[0];   
            
return true;   
        }
   
    }
   
    
return false;   
}
  
 

这样一来,就可以用统一的方法来进行窗体的查找。具体代码如下:

protected boolean ExistWin(String winName, boolean wait) {   
    release();   
    
if (wait)   
        
return og.getRootWithCaption(winName);   
    
else  
        
return og.getRootWithCaptionWithoutWait(winName);   
}
   
  
public boolean isDialog(String caption, boolean wait) {   
    
return super.ExistWin(caption, wait);   
}
  
 

前一个方法利用传入的窗体标题正则表达式,和窗体查找逻辑,进行窗体查找。后一个方法对其进行调用,返回是否查找成功的结果。事实上,这两个方法完全可以写成一个,但是在设计框架时,应考虑到自动化操作的各个环节,应把每一个环节独立开来,才能具有最大的灵活性。根据我的经验,对对象的查找和查找后的操作应独立成两个不同类来完成,其中对对象的查找应为一个RFTsuper class。其实也就是继承了RationalTestScript类的抽象类,对对象的操作应为一个Script,这个Scriptsuper class应为自己之前定义的对象查找类,而不是默认的RationalTestScript。这一部分后面还会讲到。

当需要关闭可能出现的错误窗口时,可以这样使用:

if (aDialog.isDialog(".*Error"false)) {    
    aDialog.close();    
}
 
 

当需要操作一定出现的窗口时,可以这样使用:

if (aDialog.isDialog("Warning"true)) {   
    aDialog.clickButton(
"Yes");   
}
  
 

至此,所有针对窗体的处理逻辑就基本完整了。当然,可能有些被测程序可以同时打开多个实例,这就需要支持同时获取并操作多个具有相同标题的窗体。这样的问题大家可以一同探讨如何处理。

posted @ 2009-03-21 20:57 terrypang 阅读(1214) | 评论 (1)编辑 收藏

 

经过这些年的使用,我认为RFT是一个十分优秀的自动化测试工具。尤其是目前的7.0.1.2版本,已经非常稳定,非常好用了。

与大多数其它自动化测试工具一样,RFT提供了很多基础设施,例如:对象的映射、动作的录制/回放、检查点、数据池、HtmlTxT格式的报告等等。这些基础设施确实能够大幅度提升这一工具的上手速度,但是我以为,正是这些基础设施使得RFT的使用者在使用过程中忽略了对这些基础设施背后内容的了解,以至于出现了问题束手无策。其实,RFT是一个开放的自动化测试开发平台,几乎所有他所提供的基础设施都有相应的API暴露给使用者,只要用好这些API,就能够非常轻松的定制出适合自己的自动化测试框架,运行中出现问题也非常容易定位分析。

使用RFT有一段时间的人,应该知道IBM有一个ITCL库,是专门针对RFT的,他封装了很多有用的方法,形成了一套开发框架,能够明显提高开发效率。不过,遗憾的是,这个库非常的老(大部分类生成日期都是05年),如果你使用最新的RFT7.0,由于很多底层API都变化了,这个库有不少部分都不能用了。

如果你仔细阅读过这个库,你就会发现RFT是一个多么灵活的工具,也正因为这一点,很多人都不明白如何能够正确的使用RFT。在我看来,RFT使用的最佳实践就是开发属于自己的测试框架。只有这样才能发挥RFT的真正威力。

利用这个系列,我结合手头的一个Swing界面的项目,介绍一下如何开发自己的测试框架。(Swing项目,运行之间不要忘记Enable Jvm)。

这里,作为测试框架,不应使用对象映射,而应该完全依赖于动态查找。这是因为它严重依赖RFT提供的基础设施,灵活性很差。具体内容随后将逐一介绍。

posted @ 2009-03-21 20:48 terrypang 阅读(1121) | 评论 (0)编辑 收藏
仅列出标题  

导航

<2024年4月>
31123456
78910111213
14151617181920
21222324252627
2829301234
567891011

统计

常用链接

留言簿(2)

随笔分类

随笔档案

搜索

最新评论

阅读排行榜

评论排行榜