在RFT中根據指定的標題查詢頂層視窗

TIB發表於2010-01-18

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

    }

   

 

相關文章