在RFT中如何等待瀏覽器處於Ready狀態?

TIB發表於2010-01-23

在RFT中,如何等待一段時間,讓瀏覽器啟動並載入頁面處於Ready狀態?下面的程式碼可以實現:

        closeIEBrowsers();

        startBrowser("http://blog.csdn.net/testing_is_believing");

        if(waitForReady())

            System.out.println("Ready!");

        else

            System.out.println("NOT Ready!");

 

關閉所有IE瀏覽器的方法closeIEBrowsers,參見:

http://blog.csdn.net/Testing_is_believing/archive/2010/01/22/5233918.aspx

 

waitForReady方法如下所示:

    public static boolean waitForReady() {      

        long maxWaitTimeMillis = 60000;

       

        //get start time so can determine timeout

        long startTime = System.currentTimeMillis();

       

        TestObject to = findBrowser();

       

        while ((to ==null) && ((System.currentTimeMillis() - startTime) < maxWaitTimeMillis))

        {

            sleep(10);

            to = findBrowser();

        }

        if (to == null)

            return false;

           

    //get start time again for next timeout

        startTime = System.currentTimeMillis();

   

        while (!isReady(to) && (System.currentTimeMillis() - startTime) < maxWaitTimeMillis)

            sleep(2);

       

        if (isReady(to)) {

            RationalTestScript.unregister(new Object[]{to});           

            return true;

        }

        else {

            RationalTestScript.unregister(new Object[]{to});

            return false;

        }

    }

   

 

通過獲取測試物件的readyState屬性值來判斷頁面是否載入完成:

    public static boolean isReady(TestObject to) {

        return Integer.parseInt(to.getProperty(".readyState").toString()) == 4;       

    }

   

 

查詢瀏覽器例項的方法:

    public static BrowserTestObject findBrowser() {

        DomainTestObject domains[] = getDomains();

 

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

            try {

                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) {

                           

                                if (topObjects[j] instanceof BrowserTestObject)

                                {

                                    return (BrowserTestObject)topObjects[j];

                                }

                            }

                        } catch (Exception e) {

                            System.out.println("Exception in findBrowser: " + e);

                            e.printStackTrace();

                        }

                    }

                }

            } catch (com.rational.test.ft.TargetGoneException e) {

                //noop - continue if target has since disappeared

            }

        }

        //if we get here, we didn't find a browser

        return null;

    }

 

相關文章