RFT的異常處理方式

TIB發表於2010-01-23

RFT中,異常處理的方式與QTP有所不同。QTP中的異常處理主要依賴VB ScriptOn Error Resume Next以及恢復場景(Recovery Scenario的機制。

 

RFT中,由於指令碼繼承於RationalTestScript,因此處理可以在指令碼中使用JAVAtry-catch語句做一般的異常處理外,還可以重寫IobjectManagerEventListener介面的各種事件,例如找到多個相同物件時的onAmbiguousRecognition事件、找不到物件時的onObjectNotFound事件等。

 

下面的指令碼重寫了onAmbiguousRecognition事件進行定製處理,testObjectMethodState物件的findObjectAgain方法讓RFT再次查詢物件。

 

    public void onAmbiguousRecognition(ITestObjectMethodState testObjectMethodState,

            TestObject[] choices,

            int[] scores)

    {

        System.out.println("有"+choices.length+"個相同的物件!");

        for(int i=1;i<choices.length;i++)

        {

            TopLevelSubitemTestObject window = (TopLevelSubitemTestObject)choices[i];

            window.close();        

        }

        // 再次查詢物件

        testObjectMethodState.findObjectAgain();

    }

 

 

以下指令碼重寫了onObjectNotFound事件。(參考RFT的幫助文件Handling unexpected active window

 

/**

* Overrides the base implementation of onObjectNotFound. Whenever

* this event occurs, look through all the active domains (places

* where objects might be found). For HTML domains (Java

* and other domains are skipped) finds all the top objects.

* If the top object is an Html Dialog,

* types an Enter key to dismiss the dialog.

* Logs a warning when this happens.

*/

public void onObjectNotFound(ITestObjectMethodState testObjectMethodState)

{

    if (!testObjectMethodState.getTestObject().

              getPropertyFromMap(IMapPropertyName.DOMAIN).equals("Html"))

    {

        return;

    }

 

   boolean dismissedAWindow = false;

   DomainTestObject domains[] = getDomains();

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

   {

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

       {

           // HTML domain is found.

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

           if (topObjects != null)

           {

               try

               {

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

                   {

                       if (topObjects[j].getProperty(".class").equals("Html.Dialog"))

                       {

                           // A top-level HtmlDialog is found.

                           logWarning("HtmlScript.onObjectNotFound - dismissing dialog.");

                           try

                           {

                               dismissedAWindow = true;

                               ((TopLevelTestObject)topObjects[j]).inputKeys("{enter}");

                           }

                           catch(RuntimeException e) {}

                       }

                   }

               }

               finally

               {

                   //unregister all references to top objects

                   unregister(topObjects);

               }

           }

                      

       }

   }

   if (dismissedAWindow)

   {

       //  try again

       testObjectMethodState.findObjectAgain();

   }

   else

   {

       logWarning("HtmlScript.onObjectNotFound; no Html Dialog to dismiss");

   }

}

相關文章