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