RFT Best Practices - 2. 窗体处理

RFT Best Practices - 2. 窗体处理

 

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

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");   
}
  
 

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

你可能感兴趣的:(RFT Best Practices - 2. 窗体处理)