在RFT中關閉所有IE瀏覽器視窗的3種方法

TIB發表於2010-01-22

如果通過呼叫browser測試物件的close方法,則只能關閉一個瀏覽器視窗:

browser_htmlBrowser().close();

 

要想關閉多個IE瀏覽器視窗,可以通過殺死IE程式的方式:

        try {

            Runtime.getRuntime().exec("cmd /c tskill iexplore");

        } catch (IOException e) {

               System.out.println(e.toString());

        }

 

也可以通過查詢視窗名的方式來找到每個IE視窗並關閉:

              IWindow[] windows = getTopWindowsWithCaptionPattern(".*Microsoft Internet Explorer",true);

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

              {

                     windows[i].close();

              }

 

    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;

    }

 

 

還可以通過查詢Domain物件的方式找到所有Html.HtmlBrowser型別的物件,然後呼叫其close方法:

  for (int i = 0; i < domains.length; ++i) {

    if (domains[i].getName().equals("Html")) {

        //We found an Html domain.

        TestObject[] topObjects = domains[i].getTopObjects();

        if (topObjects != null) {

            try {

                for (int j = 0; j < topObjects.length; ++j) {

                    //found a browser window

                    if (topObjects[j]

                        .getProperty(".class")

                        .equals("Html.HtmlBrowser")) {

                        BrowserTestObject browser =

new BrowserTestObject(topObjects[j]);

                         browser.activate();

                        browser.close();

                         }

                 }

             } catch (Exception e) {

             System.out.println("Error in closing All Browsers: " + e);

           }

        }

      }

    }

 

 

相關文章