如何使用UIAutomation进行ios自动化测试工具

The page is temporarily unavailable
nginx error!
The page you are looking for is temporarily unavailable.
Please try again later.
Website Administrator
Something has triggered an error on your
This is the default error page for
nginx that is distributed with
It is located
/usr/share/nginx/html/50x.html
You should customize this error page for your own
site or edit the error_page directive in
the nginx configuration file
/etc/nginx/nginx.conf..Net软件UI界面测试自动化--UIAutomation技术-编程10000问-电脑编程网.Net软件UI界面测试自动化--UIAutomation技术作者:wangyong0921 和相关&&
&&&&&& 在目前进行软件测试时,都或多或少的引入了自动化测试的概念,而且市面上也有好多软件自动化方面相关的工具,比如QTP,比如LoadRunner,但是这些工具要么售价不菲,要么对某些方面功能支持的不够全面,那么我们在引入软件自动化测试时,都需要考虑哪些方面呢?当然是最符合自己项目的工具最合适,同时费用也比较低,那么除了市面上这些商业软件外,还有没有哪些方法可以自己动手来做软件的自动化测试呢?答案是肯定的.
&&&&& 本文将介绍实现软件自动化测试其中一种测试方法------UIAutomation技术,当然,此种方法仅限于对.Net软件使用.
&&&&& 反射技术具体是什么原理,本文将不做任何介绍,大家可以去网上搜索一下,有很多这方面的文章介绍,本文只介绍如何使用UIAutomation技术进行.NET软件的UI界面的自动化测试.
&&&&& 废话少说,多做实事!(本文所有代码均在VS2008环境下测试通过)
&&&&& 一. 创建待测试程序
&&&&&&&&& 1. 启动VS2008,建立一个C#的WinForm工程,并将工程文件名命名为AUT(Application Under Test)
&&&&&&&&& 2. 创建的Form上的按钮布局,如下图所示
&&&&&&&&&&&&&&
&&&&&&&&& 3. 一个菜单(包含一个以及菜单File以及一个二级菜单Exit),一个TextBox,一个ComboBox,一个Button以及一个ListBox
& & & & private system.windows.forms.menustrip menustrip1;
private system.windows.forms.toolstripmenuitem f
private system.windows.forms.toolstripmenuitem e
private system.windows.forms.textbox textbox1;
private system.bobox combobox1;
private system.windows.forms.button button1;
private system.windows.forms.listbox listbox1;
&&&&&&&&&&&&&
4. 给Button按钮添加一个消息响应函数&&&&
& & & &private void button1_click(object sender, eventargs e)
string tb = textbox1.
string cb = combobox1.
if (tb == cb)
listbox1.items.add(&result is a tie&);
else if (tb == &paper& && cb == &rock& ||
tb == &rock& && cb == &scissors& ||
tb == &scissors& && cb == &paper&)
listbox1.items.add(&the textbox wins&);
listbox1.items.add(&the combobox wins&);
&&&&&&&&&& 5. 给Exit菜单添加一个消息响应函数&&&&&&&
& & & & private void exittoolstripmenuitem_click(object sender, eventargs e)
application.exit();
&&&&&&& 6.给ComboBox控件添加三个Item,分别为paper,rock,scissions
&&&&&&& 7. 编译待测试程序,生成文件名为AUT.exe的待测试程序
&&& 二. 创建测试程序&& &&&&&&& 1. 启动VS2008,创建一个C#控制台程序,并命名为AutomationUITest
&&&&&&& 2. 在引用中添加以下三个类: UIAutomationClient, UIAutomationClientsideProviders以及UIAutomationTypes
&&&&&&& 3. 在工程中添加以下using语句
& & & &
& & & &using system.windows.
& & & &using system.
& & & &using system.
& & & &using system.&&&&&&& 4. 定义启动测试程序以及获取窗体句柄的函数
& & & & ///&summary&
///根据传入的路径启动相应的可执行程序,并返回进程id
///&/summary&
public static int32 startexe(string strexepath)
if (null == strexepath)
process ps = process.start(strexepath);
thread.sleep(3000);
return ps.
///&summary&
///根据进程id,查找相应窗体,并返回窗体句柄
///&/summary&
public static automationelement getwindowhandle(int32 pid, int iwaitsecond)
automationelement targetwindow =
int iwaittime = 0;
process ps = process.getprocessbyid(pid);
targetwindow = automationelement.fromhandle(ps.mainwindowhandle);
while (null == targetwindow)
if (iwaittime & iwaitsecond)
thread.sleep(500);
targetwindow = automationelement.fromhandle(ps.mainwindowhandle);
catch (system.exception ex)
string msg = &没有找到指定的窗口,请确认窗口已经启动!&;
throw new invalidprogramexception(msg, ex);
}&&&&&&& 5.& 定义操作TextBox的相关函数\
& & & & ///&summary&
///根据窗口句柄以及textedit的automationid,返回textedit句柄
///&/summary&
public static automationelement gettextedithandle(automationelement parentwindowhandle, string sautomationid)
propertycondition condition =
propertycondition codedit =
andcondition andconditon =
automationelement targetedithandle =
if (null == parentwindowhandle)
condition = new propertycondition(automationelement.automationidproperty, sautomationid);
codedit = new propertycondition(automationelement.controltypeproperty, controltype.edit);
andconditon = new andcondition(condition, codedit);
targetedithandle = parentwindowhandle.findfirst(treescope.children, andconditon);
catch (system.exception ex)
string msg = &没有找到指定的textedit,请确认textedit的automationid是否正确!&;
throw new invalidprogramexception(msg, ex);
///&summary&
///根据textedit句柄,在textedit内填写数据
///只能设置单行输入的textedit
///&/summary&
public static bool settexteditdata(automationelement textedithandle, string strdata)
valuepattern vptextedit =
if (!textedithandle.current.isenabled)
throw new invalidoperationexception(&the control is not enabled.\n\n&);
if (!textedithandle.current.iskeyboardfocusable)
throw new invalidoperationexception(&the control is not focusable.\n\n&);
vptextedit = textedithandle.getcurrentpattern(valuepattern.pattern)
if (null == vptextedit)
if (vptextedit.current.isreadonly)
throw new invalidoperationexception(&the control is read-only.&);
vptextedit.setvalue(strdata);
}&&&&&&& 6. 定义操作ComboBox的相关函数
& & & & ///&summary&
///根据窗口句柄以及combobox控件automationid,返回该combobox控件句柄
///&/summary&
public static automationelement getcomboboxhandle(automationelement parentwindowhandle, string sautomationid)
automationelement comboboxhandle =
propertycondition namecondition =
propertycondition typecondition =
andcondition andcondition =
if (null == parentwindowhandle || null == sautomationid)
namecondition = new propertycondition(automationelement.automationidproperty, sautomationid);
typecondition = new propertycondition(automationelement.controltypeproperty, bobox);
andcondition = new andcondition(namecondition, typecondition);
comboboxhandle = parentwindowhandle.findfirst(treescope.children, andcondition);
if (null == comboboxhandle)
///&summary&
///根据combobox控件句柄,设置数据
///&/summary&
public static bool setcomboboxitemdata(automationelement comboboxhandle, string strdata)
automationelement textedithandle =
propertycondition typecondition =
valuepattern vptextpattern =
if (null == comboboxhandle || null == strdata)
typecondition = new propertycondition(automationelement.controltypeproperty, controltype.edit);
textedithandle = comboboxhandle.findfirst(treescope.children, typecondition);
if (null == textedithandle)
if (!textedithandle.current.isenabled)
throw new invalidoperationexception(&the control is not enabled.\n\n&);
if (!textedithandle.current.iskeyboardfocusable)
throw new invalidoperationexception(&the control is not focusable.\n\n&);
vptextpattern = textedithandle.getcurrentpattern(valuepattern.pattern)
if (null == vptextpattern)
if (vptextpattern.current.isreadonly)
throw new invalidoperationexception(&the control is read-only.&);
vptextpattern.setvalue(strdata);
}&&&&&&& 7.& 定义操作Button的相关函数
& & & & ///&summary&
///根据窗口句柄以及button的automationid,返回button的句柄
///&/summary&
public static automationelement getbuttonhandle(automationelement parentwindowhandle, string sautomationid)
propertycondition condition =
propertycondition codbutton =
andcondition andconditon =
automationelement targetbuttonhandle =
if (null == parentwindowhandle)
condition = new propertycondition(automationelement.automationidproperty, sautomationid);
codbutton = new propertycondition(automationelement.controltypeproperty, controltype.button);
andconditon = new andcondition(condition, codbutton);
targetbuttonhandle = parentwindowhandle.findfirst(treescope.children, andconditon);
catch (system.exception ex)
string msg = &没有找到指定的按钮,请确认按钮automationid是否正确!&;
throw new invalidprogramexception(msg, ex);
///&summart&
///根据button按钮句柄,进行鼠标左键单击
///&/summary&
public static bool buttonleftclick(automationelement buttonhandle)
object objbutton =
invokepattern ivkpbutton =
if (null == buttonhandle)
if (!buttonhandle.trygetcurrentpattern(invokepattern.pattern, out objbutton))
ivkpbutton = (invokepattern)
ivkpbutton.invoke();
catch (system.exception ex)
string msg = &鼠标左键单击失败!&;
throw new invalidprogramexception(msg, ex);
}&&&&&&& 8. 定义操作ListBox的相关函数
& & & & ///&summary&
///根据窗口句柄以及listbox控件automationid,返回该listbox控件句柄
///&/summary&
public static automationelement getlistboxhandle(automationelement parentwindowhandle, string sautomationid)
automationelement listboxhandle =
propertycondition namecondition =
propertycondition typecondition =
andcondition andcondition =
if (null == parentwindowhandle || null == sautomationid)
namecondition = new propertycondition(automationelement.automationidproperty, sautomationid);
typecondition = new propertycondition(automationelement.controltypeproperty, controltype.list);
andcondition = new andcondition(namecondition, typecondition);
listboxhandle = parentwindowhandle.findfirst(treescope.children, andcondition);
if (null == listboxhandle)
///&summary&
///根据listbox控件句柄以及itemcount,选择该item
///&/summary&
public static string getlistboxitemname(automationelement listboxhandle, int iitemcount)
automationelementcollection listboxhandlecollection =
propertycondition typecondition =
if (null == listboxhandle || 0 & iitemcount)
typecondition = new propertycondition(automationelement.controltypeproperty, controltype.listitem);
listboxhandlecollection = listboxhandle.findall(treescope.children, typecondition);
if (null == listboxhandlecollection)
if (iitemcount &= listboxhandlecollection.count)
return listboxhandlecollection[iitemcount].current.
}&&&&&&& 9. 定义操作菜单的相关函数
& & & & ///&summary&
///根据窗口句柄,以及menubar控件automationid,返回该menubar控件句柄
///&/summary&
public static automationelement getmenubarhandle(automationelement parentwindow, string sautomationid)
automationelement mbhandle =
propertycondition namecondition =
propertycondition typecondition =
andcondition andcondition =
if (null == parentwindow || null == sautomationid)
namecondition = new propertycondition(automationelement.automationidproperty, sautomationid);
typecondition = new propertycondition(automationelement.controltypeproperty, controltype.menubar);
andcondition = new andcondition(namecondition, typecondition);
mbhandle = parentwindow.findfirst(treescope.children, andcondition);
if (null == mbhandle)
///&summary&
///根据menubar控件句柄以及一级菜单名称,弹出一级菜单
///&/summary&
private static automationelement popupmenubaritem(automationelement mbhandle, string firmenutitle)
invokepattern ipmenu =
automationelement menuhandle =
propertycondition namecondition =
propertycondition typecondition =
andcondition andcondition =
if (null == mbhandle || null == firmenutitle)
namecondition = new propertycondition(automationelement.nameproperty, firmenutitle);
typecondition = new propertycondition(automationelement.controltypeproperty, controltype.menuitem);
andcondition = new andcondition(namecondition, typecondition);
menuhandle = mbhandle.findfirst(treescope.children, andcondition);
if (null == menuhandle)
ipmenu = menuhandle.getcurrentpattern(invokepattern.pattern)
if (null == ipmenu)
ipmenu.invoke();
///&summary&
///根据menubar控件句柄以及传入的菜单名称,选择相应的菜单
///传入的菜单名称,必须以空字符串结束
///&/summary&
public static bool selectmenubaritem(automationelement mbhandle, string[] strtitle)
automationelement menuitemhandle =
menuitemhandle =
foreach (string str in strtitle)
if (&& == str)
menuitemhandle = popupmenubaritem(menuitemhandle, str);
if (null == menuitemhandle)
thread.sleep(400);
} &&&&&&& 10.& 添加测试代码
& & & & static void main(string[] args)
string path = directory.getcurrentdirectory() + &\\aut.exe&;
console.writeline(&\nstart application under test exe&);
int32 processid = startexe(path);
console.writeline(&application under test processid: & + processid.tostring());
console.writeline(&\nget main window handle&);
automationelement mwh = getwindowhandle(processid, 3000);
console.writeline(&main window handle: & + mwh.current.nativewindowhandle.tostring());
console.writeline(&\nget textbox handle&);
automationelement tbh = gettextedithandle(mwh, &textbox1&);
console.writeline(&textbox handle: & + tbh.current.nativewindowhandle.tostring());
console.writeline(&\nget combobox handle&);
automationelement cbh = getcomboboxhandle(mwh, &combobox1&);
console.writeline(&combobox handle: & + cbh.current.nativewindowhandle.tostring());
console.writeline(&\nget button handle&);
automationelement bth = getbuttonhandle(mwh, &button1&);
console.writeline(&button handle: & + bth.current.nativewindowhandle.tostring());
console.writeline(&\nget listbox handle&);
automationelement lbh = getlistboxhandle(mwh, &listbox1&);
console.writeline(&listbox handle: & + lbh.current.nativewindowhandle.tostring());
console.writeline(&\nset textbox value&);
settexteditdata(tbh, &paper&);
console.writeline(&\nset conboxbox value&);
setcomboboxitemdata(cbh, &rock&);
console.writeline(&\npush mouse left button&);
buttonleftclick(bth);
string result = getlistboxitemname(lbh, 0);
if (result == &the textbox wins&)
console.writeline(&\ntest seceions = pass&);
console.writeline(&\ntest seceions = failed&);
console.writeline(&\nget menu handle&);
automationelement mbh = getmenubarhandle(mwh, &menustrip1&);
console.writeline(&menu handle: & + mbh.current.nativewindowhandle.tostring());
string[] strtitle = { &file&, &exit&, && };
console.writeline(&\nclose application under test in 3 seconds....&);
thread.sleep(3000);
selectmenubaritem(mbh, strtitle);
console.readline();
catch (system.exception ex)
console.writeline(&\nerror msg: & + ex.message);
}&&&&&&& 11.& 编译测试程序,OK,大功告成
相关资料:|||||||.Net软件UI界面测试自动化--UIAutomation技术来源网络,如有侵权请告知,即处理!编程Tags:                &                    如何使用UIAutomation进行iOS自动化测试_百度知道
如何使用UIAutomation进行iOS自动化测试
提问者采纳
请点击【追问】希望我的回答对您有所帮助.cnblogs.html如若满意.com/vowei/archive//2631949。看看这个帖子.cnblogs您好,请点击右侧【采纳答案】.html" target="_blank">/vowei/archive//2631949://www,望采纳,很高兴为您解答,如若还有问题:<a href="http
来自团队:
其他类似问题
为您推荐:
自动化测试的相关知识
等待您来回答
下载知道APP
随时随地咨询
出门在外也不愁}

我要回帖

更多关于 ios自动化测试工具 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信