2009年3月31日

        找到对象之后,就需要对其进行操作了。在对对象进行操作之前,需要了解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 阅读(1437) | 评论 (2)编辑 收藏

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

    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 阅读(1173) | 评论 (0)编辑 收藏

导航

<2009年3月>
22232425262728
1234567
891011121314
15161718192021
22232425262728
2930311234

统计

常用链接

留言簿(2)

随笔分类

随笔档案

搜索

最新评论

阅读排行榜

评论排行榜