在RFT中根据指定的标题查找顶层窗口

本文介绍了RFT提供的底层接口IWindow及其使用方法,包括根据窗口标题查找并操作顶层窗口的具体实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在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;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值