在RFT中查找窗口中指定名称的对象

本文介绍了一种使用自动化脚本启动计算器应用程序并执行基本算术运算的方法。通过调用特定的API函数,如getTopWindowWithCaption和getChildWindowWithText,可以实现对计算器界面元素的精确控制。

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

<!-- [if gte mso 10]> <mce:style><!-- /* Style Definitions */ table.MsoNormalTable {mso-style-name:普通表格; mso-tstyle-rowband-size:0; mso-tstyle-colband-size:0; mso-style-noshow:yes; mso-style-parent:""; mso-padding-alt:0cm 5.4pt 0cm 5.4pt; mso-para-margin:0cm; mso-para-margin-bottom:.0001pt; mso-pagination:widow-orphan; font-size:10.0pt; font-family:"Times New Roman";} -->

startApp ( "calc" );

sleep (3);

IWindow parent = getTopWindowWithCaption( "计算器" , true );

parent.activate();

IWindow btn_1 = getChildWindowWithText(parent, "1" , true , false );

btn_1.click();

IWindow btn_add = getChildWindowWithText(parent, "+" , true , false );

btn_add.click();

btn_1.click();

IWindow btn_equal = getChildWindowWithText(parent, "=" , true , false );

btn_equal .click();

getTopWindowWithCaption函数 根据指定的标题查找顶层窗口。

getChildWindowWithText函数如下,其作用是给定一个窗口对象,查找其中的指定名称的对象:

/**

* Given a particular context, find a child with the specified text.

* Typically this is used to locate a pushbutton with specific text

* on it, such as "OK" or "Cancel".

*

* @param parent The context from which the search is based, typically

* a top level window like a dialog box.

* @param text The text on the window being sought.

* @param ignoreCase <code> true </code> if the case of the text should not

* be taken into consideration.

* @param onlyImmediateChildren <code> true </code> if the search should be

* limited to only the children of the parent

* window and not recurse into the grandchildren .

* @return The control with the specified text or <code> null </code> if no

* child with the text is located.

*/

public IWindow getChildWindowWithText(IWindow parent,

String text,

boolean ignoreCase,

boolean onlyImmediateChildren)

{

if ( text == null )

return null ;

IWindow[] children = ( parent != null ? parent.getChildren() : null );

int length = ( children != null ? children. length : 0 );

for ( int i = 0; i < length; ++i )

{

IWindow child = children[i];

String childText = child.getText();

if ( ( !ignoreCase && text.equals(childText) ) ||

( ignoreCase && text.equalsIgnoreCase(childText) ) )

return child;

if ( !onlyImmediateChildren )

{

IWindow grandchild = getChildWindowWithText(child, text, ignoreCase, onlyImmediateChildren);

if ( grandchild != null )

return grandchild;

}

}

return null ;

}

以下是支持正则表达式的版本:

/**

* Given a particular context, find a child with text that matches the

* specified regular expression pattern.

* Typically this is used to locate a pushbutton with specific text

* on it, such as "OK" or "Cancel" when the format of the text is not specific

* (for instance when the text equals " OK " instead of a simple "OK").

*

* @param parent The context from which the search is based, typically

* a top level window like a dialog box.

* @param pattern The pattern for the text on the window being sought.

* @param ignoreCase <code> true </code> if the case of the text should not

* be taken into consideration.

* @param onlyImmediateChildren <code> true </code> if the search should be

* limited to only the children of the parent

* window and not recurse into the grandchildren .

* @return The control with the specified text or <code> null </code> if no

* child with the text is located.

*/

public IWindow getChildWindowWithTextPattern(IWindow parent,

String pattern,

boolean ignoreCase,

boolean onlyImmediateChildren)

{

Regex regex = ( ignoreCase ? new Regex(pattern, Regex. MATCH_CASEINDEPENDENT ) :

new Regex(pattern) );

IWindow[] children = ( parent != null ? parent.getChildren() : null );

int length = ( children != null ? children. length : 0 );

for ( int i = 0; i < length; ++i )

{

IWindow child = children[i];

String childText = child.getText();

if ( regex.matches(childText) )

return child;

if ( !onlyImmediateChildren )

{

IWindow grandchild = getChildWindowWithText(child, pattern, ignoreCase, false );

if ( grandchild != null )

return grandchild;

}

}

return null ;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值