在RFT中提供了丰富的底层接口(如IWindow),可以让我们实现很多底层的对象操作,例如根据给定的标题查找顶层窗口:
IWindow window = getTopWindowWithCaption( "计算器" , true );
if (window != null )
{
window.activate();
window.close();
}
IWindow[] windows = getTopWindowsWithCaptionPattern( ".*记事本" , true );
if (windows!= null )
{
for ( int i=0;i<windows. length ;i++)
{
windows[i].activate();
windows[i].close();
}
}
getTopWindowWithCaption函数如下,其作用是根据给定的标题(Caption)查找顶层窗口,返回第一个找到的窗口:
/**
* Given the expected caption find a top level window that has that caption.
* The first window found with the caption is returned.
*
* @param caption The caption of the desired window.
* @param ignoreCase Flags if case i significant in the caption comparisons.
* @return The first top level window with the specified text that is located.
*/
protected IWindow getTopWindowWithCaption(String caption, boolean ignoreCase)
{
if ( caption == null )
caption = "" ;
IWindow[] topWindows = getTopWindows ();
int length = ( topWindows != null ? topWindows. length : 0 );
for ( int i = 0; i < length; ++i )
{
String text = topWindows[i].getText();
if ( ( !ignoreCase && caption.equals(text) ) ||
( ignoreCase && caption.equalsIgnoreCase(text) ) )
return topWindows[i];
}
return null ;
}
函数中的getTopWindows方法将返回当前系统中的所有窗口对象。
getTopWindowsWithCaptionPattern函数如下,支持正则表达式给定Caption,返回所有匹配的窗口对象。
/**
* Given the expected caption pattern find all top level windows that have a caption
* that matches the pattern. Basically if the pattern is "^Notepad - " all top level
* windows that have a caption that starts with "Notepad - " will be returned.
*
* @param pattern The caption pattern of the desired windows.
* @param ignoreCase Flags if case is significant in the caption pattern matches.
* @return The set top level windows with the specified caption text pattern.
*/
protected IWindow[] getTopWindowsWithCaptionPattern(String pattern, boolean ignoreCase)
{
Regex regex = ( ignoreCase ? new Regex(pattern, Regex. MATCH_CASEINDEPENDENT ) :
new Regex(pattern) );
java.util.Vector matches = new java.util.Vector(10);
IWindow[] topWindows = getTopWindows ();
int length = ( topWindows != null ? topWindows. length : 0 );
for ( int i = 0; i < length; ++i )
{
String text = topWindows[i].getText();
if ( regex.matches(text) )
matches.add(topWindows[i]) ;
}
int resultLength = matches.size();
IWindow[] result = ( resultLength != 0 ?
new IWindow[resultLength] :
null );
for ( int i = 0; i < resultLength; ++i )
result[i] = (IWindow)matches.elementAt(i);
return result;
}