在基于JAVA界面的测试过程中,经常出现对象找不到或者对象被对话框遮挡的情况,如果不使用异常处理,测试脚本就会异常退出,笔者经过研究,发现在脚本对应的Helper类中加入对应的处理异常的代码可以解决该问题。
代码如下:
public void onObjectNotFound(ITestObjectMethodState testObjectMethodState)
{
DomainTestObject domains[] = getDomains();
for (int i = 0; i < domains.length; ++i) {
if (domains[i].getName().equals("Html"))
{
TestObject[] topObjects = domains[i].getTopObjects();
if (topObjects != null){
try{
for (int j = 0; j < topObjects.length; ++j){
if (topObjects[j].getProperty(".class").equals("Html.JDialog")){
logWarning("HtmlScript.onObjectNotFound - dismissing dialog.",getRootTestObject().getScreenSnapshot());
VP.recordException(this.getScriptName(), new Exception("object not found exception occur!"));
try{
((TopLevelTestObject)topObjects[j]).inputKeys("{enter}");
}catch(RuntimeException e) {
}
}
}
}
finally{
unregister(topObjects);
}
}
}
}
logWarning("objectNotFound Exception occured on line "+this.getLineNumber()+" of "+this.getScriptName(), getRootTestObject().getScreenSnapshot());
VP.recordException(this.getScriptName(), new Exception("objectNotFound Exception occured on line "+this.getLineNumber()+" of "+this.getScriptName()));
ObjectNotFoundExceptionHandler objNotFound = new ObjectNotFoundExceptionHandler(this,testObjectMethodState);
objNotFound.skipCurrentStatement();
}
public void onTestObjectMethodException(ITestObjectMethodState testObjectMethodState, TestObject foundObject) {
System.out.println("windowActiveeFailed");
if (testObjectMethodState.getThrowableClassName().equals(
"com.rational.test.ft.WindowActivateFailedException")) {
logWarning("an unexpted dialog is appeared on line "+this.getLineNumber()+" of "+this.getScriptName(),getRootTestObject().getScreenSnapshot());
VP.recordException(this.getScriptName(), new Exception("an unexpted dialog is appeared on line "+this.getLineNumber()+" of "+this.getScriptName()));
IWindow activeWindow = RationalTestScript.getScreen().getActiveWindow();
if (activeWindow != null) {
activeWindow.inputKeys("Enter{Enter}");
if(activeWindow.isShowing()){
activeWindow.close();
}
}
testObjectMethodState.findObjectAgain();
}
}
代码如下:
public void onObjectNotFound(ITestObjectMethodState testObjectMethodState)
{
DomainTestObject domains[] = getDomains();
for (int i = 0; i < domains.length; ++i) {
if (domains[i].getName().equals("Html"))
{
TestObject[] topObjects = domains[i].getTopObjects();
if (topObjects != null){
try{
for (int j = 0; j < topObjects.length; ++j){
if (topObjects[j].getProperty(".class").equals("Html.JDialog")){
logWarning("HtmlScript.onObjectNotFound - dismissing dialog.",getRootTestObject().getScreenSnapshot());
VP.recordException(this.getScriptName(), new Exception("object not found exception occur!"));
try{
((TopLevelTestObject)topObjects[j]).inputKeys("{enter}");
}catch(RuntimeException e) {
}
}
}
}
finally{
unregister(topObjects);
}
}
}
}
logWarning("objectNotFound Exception occured on line "+this.getLineNumber()+" of "+this.getScriptName(), getRootTestObject().getScreenSnapshot());
VP.recordException(this.getScriptName(), new Exception("objectNotFound Exception occured on line "+this.getLineNumber()+" of "+this.getScriptName()));
ObjectNotFoundExceptionHandler objNotFound = new ObjectNotFoundExceptionHandler(this,testObjectMethodState);
objNotFound.skipCurrentStatement();
}
public void onTestObjectMethodException(ITestObjectMethodState testObjectMethodState, TestObject foundObject) {
System.out.println("windowActiveeFailed");
if (testObjectMethodState.getThrowableClassName().equals(
"com.rational.test.ft.WindowActivateFailedException")) {
logWarning("an unexpted dialog is appeared on line "+this.getLineNumber()+" of "+this.getScriptName(),getRootTestObject().getScreenSnapshot());
VP.recordException(this.getScriptName(), new Exception("an unexpted dialog is appeared on line "+this.getLineNumber()+" of "+this.getScriptName()));
IWindow activeWindow = RationalTestScript.getScreen().getActiveWindow();
if (activeWindow != null) {
activeWindow.inputKeys("Enter{Enter}");
if(activeWindow.isShowing()){
activeWindow.close();
}
}
testObjectMethodState.findObjectAgain();
}
}