转载:一个UI自动化的小例子
2009-01-12 17:03 yufun 阅读(627) 评论(0) 收藏 举报转载自:http://peking2toronto.spaces.live.com/Blog/cns!A975CAF18FBB985B!411.entry?wa=wsignin1.0
随便用一个小例子来解释一下UI自动化的开发吧.
我先现在有一个Button是disable的状态,一旦Button enable,我们就Click弹出一个窗口.
我们使用的测试工具就有同步的功能.
1.自动化工具生成的程序(发现和操作控件,不能真正运行)
button=FindButton();
ClickButton(button);
2.傻瓜的自动化程序(通过加入sleep变成可以运行的程序)
button=FindButton();
Sleep(10);
ClickButton(button);
Sleep(10);
window=FindWindow();
3.简单的自动化程序(加入同步,使得更可靠和有效率)
button=FindButton();
WaitButtonEnable(button);
ClickButton(button);
window=WaitWindowOpen();
4.完整的自动化程序(保证100%可靠,没有测试程序bug,简单写了一下,没有包含exception的控制,时间急,可能也会有错误,不过就是这个意思)
Button button=null; 
for(int i=0;button==null&&i<3;i++) //如果FindButton不稳定,调用三次in case 
{ 
    button=FindButton(); 
    if(button==null) 
    { 
        Log.Error("Tryout{0}:Can not find button",i); //测试工具不稳定 
    } 
    else 
    { 
        break; 
    } 
}
if(button==null) 
{ 
    Log.Error("Cannot find button. Quit"); //测试工具找不到button,或者产品问题 
    Log.Screen();//截图,只是为了示例,以后不再单独写 
    return; 
}
if(!WaitButtonEnable(button)) 
{ 
    if(button.Enabled==true) //测试工具问题,没有得到enable的消息 
    { 
        Log.Error("enabled, but tool didn't detect"); 
    } 
    else//测试工具问题,不能成功检测button的状态,或者产品问题没有enable 
    { 
        Log.Error("don't enable");                      return;                                                                                                                                                                                                                                                                                                                 
    } 
}
Window window=null;
for(i=0;window==null&i<3;i++)//ClickButton不稳定,或者没有得到open event,或者产品问题 
{ 
    ClickButton(button); 
    window=WaitWindowOpen(); 
    if(window==null)//没有click或者没有得到消息,或者产品问题 
    { 
        int count=0; 
findwindow://FindWindow不稳定,重试3次 
        window=FindWindow(); 
        if(window!=null) //没有得到消息,但是窗口弹出 
        { 
            Log.Error("didn't get event"); 
            break; 
        } 
        else //没有click,或者产品问题, 或者FindWindow不稳定 
        { 
            Log.Error("Tryout{0}:didn't get window",i); 
            count++; 
            if(count>3) 
            { 
            } 
            else //FindWindow不稳定,workaround 
            { 
                Log.Error("goto{0}",count); 
                goto findwindow; 
            } 
        } 
    } 
    else //成功 
    { 
        break; 
    } 
}
if(window==null) 
{ 
    Log.Error("didn't get window, maybe tool or product problem."); 
    return; 
} 
 
                     
                    
                 
                    
                
 
                
            
         浙公网安备 33010602011771号
浙公网安备 33010602011771号