在RFT中查詢視窗中指定名稱的物件

TIB發表於2010-01-18

        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 ;

    }

 

相關文章