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

本文介绍了一种使用自动化脚本启动计算器应用程序并执行基本算术运算的方法。通过调用特定函数找到并激活计算器窗口,然后定位并点击数字和运算符按钮来完成1+1=2的操作。
109

被折叠的 条评论
为什么被折叠?



