回顾
在之前我们介绍了如何用VS2010的UI测试功能创建一个简单的示例,大致描述了如何使用编码的UI测试进行录制和回放,在这章会着重描述VS2010是如何录制操作,并且生成代码,以及初步介绍如何通过自己写代码的方式进行测试。
内容简介
在上一章我们已经介绍过,建立一个编码的UI测试CodedUITest1.cs,录制完成之后,会自动生成一个UIMap1.uitest文件。
图1:录制完成后自动生成的UIMap1.uitest文件
事实上,在录制过程中起着关键作用的并非是CodedUITest1.cs,而是UIMap1.uitest,在录制过程中的所有代码都是生成在UIMap1.uitest,所有对于界面的操作,测试的判断也是在UIMap1.uitest文件完成,CodedUITest1.cs仅仅只是调用UIMap1.uitest中的方法(换言之只是个躯壳,编码的UI测试的灵魂还是在UIMap1.uitest)。
明白此点之后,接下来我们只需着重研究研究UIMap1.uitest即可,看看他是如何录制并生成代码的。
注:这一章的内容可能较之第一章比较难,在阅读的时候可尽量使用VS2010进行操作,便于理解。由于本章内容较长,分为两个部分,上部分介绍原理,下部分介绍通过修改UIMap1.uitest控制操作步骤代码的生成,预计在周六或者周日发出。
UIMap的录制与生成
首先我们新建一个测试项目:
图2:新建项目
建成之后,(可以看到系统自动默认生成了一个UnitTest.cs,我们可以先将它删除),右击该项目,选择添加à添加新项(注意,不是添加测试,是添加新项),在新项中选择Coded UI Test Map(编码的UI测试映射)。
图3:添加编码的UI测试映射
现在我们建立了一个UIMap1.uitest(可以看到这个UIMap1.uitest还没有生成UIMap1.cs和UIMap1.Designer.cs)。
图4:UIMap1.uitest
可以双击打开UIMap1.uitest,文件里面没有什么特殊的内容:
xml version="1.0" ?>
< UITest xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd ="http://www.w3.org/2001/XMLSchema" Name ="" Id ="3efe829a-76df-439e-af0d-8d5c62727a1e" AssemblyVersion ="10.0.21008.0" Version ="1.0" xmlns ="http://schemas.microsoft.com/VisualStudio/TeamTest/UITest/2010" >
UITest >
右击该UIMap1.uitest,选则Edit With Coded UI Test Builder(使用编码的UI测试生成器进行编辑),接下来会弹出录制操作用的生成器。
图5:Edit With Coded UI Test Builder
我们还是按照上一章中使用的示例程序和操作步骤进行录制四个方法InputErrorUidPwd()、AssertErrorWinow()、InputRightUidPwd()、AssertRightWinow(),(可再添加一个方法CloseWindow(),关闭这个示例程序,上一章没有提到)操作完毕之后,生成代码,我们可以看到UIMap1.cs和UIMap1.Designer.cs已经自动生成,而实际有生成代码的文件为UIMap1.uitest和UIMap1.Designer.cs,UIMap1.cs则为空。
现在可以一个个进去看看生成的代码(注:由于录制过程中的操作步骤每个人都可能有细微差别,例如点击坐标,这里我录的示例和大家实验的时候自己录的可能存在一些区别,你懂的)。
首先是UIMap1.uitest,大家可以看到这里的代码实在是多得看不清,如果觉得阅读比较困难,可以跳过此段,先看看解释说明,然后再回来看。
xml version="1.0" ?>
< UITest xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd ="http://www.w3.org/2001/XMLSchema" Name ="" Id ="3efe829a-76df-439e-af0d-8d5c62727a1e" AssemblyVersion ="10.0.21008.0" Version ="1.0" xmlns ="http://schemas.microsoft.com/VisualStudio/TeamTest/UITest/2010" >
< Configuration >
< Group Name ="IE" >
< Setting Name ="Version" Value ="8.0.6001.18702" WarningLevel ="1" />
< Setting Name ="InformationBar" WarningLevel ="1" />
< Setting Name ="AutoCompletePassword" WarningLevel ="1" />
< Setting Name ="AutoCompleteForm" Value ="no" WarningLevel ="1" />
< Setting Name ="DefaultBrowser" Value ="IEXPLORE.EXE" WarningLevel ="1" />
< Setting Name ="PopupBlocker" Value ="1" WarningLevel ="1" />
< Setting Name ="TabbedBrowsing" Value ="0" WarningLevel ="2" />
< Setting Name ="InternetZoneSecurity" Value ="66816" WarningLevel ="2" />
< Setting Name ="IntranetZoneSecurity" Value ="69632" WarningLevel ="2" />
< Setting Name ="TrustedZoneSecurity" Value ="70912" WarningLevel ="2" />
< Setting Name ="RestrictedZoneSecurity" Value ="73728" WarningLevel ="2" />
< Setting Name ="PhishingFilter" WarningLevel ="1" />
< Setting Name ="EnhancedSecurityConfiguration" WarningLevel ="1" />
Group >
< Group Name ="OS" >
< Setting Name ="Name" Value ="Microsoft Windows XP Professional" WarningLevel ="2" />
< Setting Name ="Version" Value ="Microsoft Windows NT 5.1.2600 Service Pack 3" WarningLevel ="2" />
< Setting Name ="IsUserAdmin" Value ="True" WarningLevel ="2" />
< Setting Name ="Is64BitOperatingSystem" Value ="False" WarningLevel ="2" />
< Setting Name ="IsTerminalServerSession" Value ="False" WarningLevel ="2" />
< Setting Name ="OSLanguage" Value ="1033" WarningLevel ="1" />
< Setting Name ="UserLocale" Value ="1033" WarningLevel ="1" />
< Setting Name ="DragFullWindows" Value ="True" WarningLevel ="2" />
< Setting Name ="ScreenResolutionWidth" Value ="1440" WarningLevel ="2" />
< Setting Name ="ScreenResolutionHeight" Value ="900" WarningLevel ="2" />
< Setting Name ="SystemDPIX" Value ="96" WarningLevel ="2" />
< Setting Name ="SystemDPIY" Value ="96" WarningLevel ="2" />
< Setting Name ="Aero" WarningLevel ="1" />
< Setting Name ="UACEnabled" WarningLevel ="2" />
< Setting Name ="UACPromptEnabled" WarningLevel ="1" />
< Setting Name ="WindowsAccessibilityAPIVersion" Value ="2.0" WarningLevel ="1" />
Group >
< Group Name ="TechnologyManagers" >
< Setting Name ="Web" WarningLevel ="1" />
< Setting Name ="UIA" WarningLevel ="1" />
Group >
Configuration >
< InitializeActions />
< ExecuteActions >
< SetValueAction UIObjectName ="UIMap1.UI 系统登录Window.UITbx_uidWindow.UITbx_uidEdit" >
< ParameterName />
< Value Encoded ="false" > errorUid Value >
< Type > String Type >
SetValueAction >
< SetValueAction UIObjectName ="UIMap1.UI 系统登录Window.UITbx_pwdWindow.UITbx_pwdEdit" >
< ParameterName />
< Value Encoded ="false" > errorPwd Value >
< Type > String Type >
SetValueAction >
< MouseAction UIObjectName ="UIMap1.UI 系统登录Window.UI登录Window.UI登录Button" >
< ParameterName />
< ModifierKeys > None ModifierKeys >
< IsGlobalHotkey > false IsGlobalHotkey >
< Location X ="25" Y ="6" />
< WheelDirection > 0 WheelDirection >
< ActionType > Click ActionType >
< MouseButton > Left MouseButton >
MouseAction >
< TestStepMarkerAction MarkerInformation ="InputErrorUidPwd" >
< ParameterName />
< StepId > -1 StepId >
< Direction > Start Direction >
< Outcome />
< Disabled > false Disabled >
< WorkItemId > 0 WorkItemId >
< MarkerRegionType > Action MarkerRegionType >
TestStepMarkerAction >
< AssertAction UIObjectName ="UIMap.UI 提示Window.UI用户名或密码错误Window.UI用户名或密码错误Text" >
< ParameterName />
< PropertyName > Exists PropertyName >
< ExpectedValue > True ExpectedValue >
< Type > Boolean Type >
< PropertyCondition > AreEqual PropertyCondition >
AssertAction >
< TestStepMarkerAction MarkerInformation ="AssertErrorWinow" >
< ParameterName />
< StepId > -1 StepId >
< Direction > Start Direction >
< Outcome />
< Disabled > false Disabled >
< WorkItemId > 0 WorkItemId >
< MarkerRegionType > Assertion MarkerRegionType >
TestStepMarkerAction >
< MouseAction UIObjectName ="UIMap1.UI 提示Window.UIOKWindow.UIOKButton" >
< ParameterName />
< ModifierKeys > None ModifierKeys >
< IsGlobalHotkey > false IsGlobalHotkey >
< Location X ="22" Y ="19" />
< WheelDirection > 0 WheelDirection >
< ActionType > Click ActionType >
< MouseButton > Left MouseButton >
MouseAction >
< SetValueAction UIObjectName ="UIMap1.UI 系统登录Window.UITbx_uidWindow.UITbx_uidEdit" >
< ParameterName />
< Value Encoded ="false" > Admin Value >
< Type > String Type >
SetValueAction >
< SetValueAction UIObjectName ="UIMap1.UI 系统登录Window.UITbx_pwdWindow.UITbx_pwdEdit" >
< ParameterName />
< Value Encoded ="false" > 123456 Value >
< Type > String Type >
SetValueAction >
< MouseAction UIObjectName ="UIMap1.UI 系统登录Window.UI登录Window.UI登录Button" >
< ParameterName />
< ModifierKeys > None ModifierKeys >
< IsGlobalHotkey > false IsGlobalHotkey >
< Location X ="39" Y ="8" />
< WheelDirection > 0 WheelDirection >
< ActionType > Click ActionType >
< MouseButton > Left MouseButton >
MouseAction >
< TestStepMarkerAction MarkerInformation ="InputRightUidPwd" >
< ParameterName />
< StepId > -1 StepId >
< Direction > Start Direction >
< Outcome />
< Disabled > false Disabled >
< WorkItemId > 0 WorkItemId >
< MarkerRegionType > Action MarkerRegionType >
TestStepMarkerAction >
< AssertAction UIObjectName ="UIMap.UI 提示Window.UI用户名或密码错误Window.UI登陆成功Text" >
< ParameterName />
< PropertyName > Exists PropertyName >
< ExpectedValue > True ExpectedValue >
< Type > Boolean Type >
< PropertyCondition > AreEqual PropertyCondition >
AssertAction >
< TestStepMarkerAction MarkerInformation ="AssertRightWinow" >
< ParameterName />
< StepId > -1 StepId >
< Direction > Start Direction >
< Outcome />
< Disabled > false Disabled >
< WorkItemId > 0 WorkItemId >
< MarkerRegionType > Assertion MarkerRegionType >
TestStepMarkerAction >
< MouseAction UIObjectName ="UIMap1.UI 提示Window.UIOKWindow.UIOKButton" >
< ParameterName />
< ModifierKeys > None ModifierKeys >
< IsGlobalHotkey > false IsGlobalHotkey >
< Location X ="28" Y ="13" />
< WheelDirection > 0 WheelDirection >
< ActionType > Click ActionType >
< MouseButton > Left MouseButton >
MouseAction >
< MouseAction UIObjectName ="UIMap1.UI 系统登录Window.UI系统登录TitleBar.UICloseButton" >
< ParameterName />
< ModifierKeys > None ModifierKeys >
< IsGlobalHotkey > false IsGlobalHotkey >
< Location X ="9" Y ="11" />
< WheelDirection > 0 WheelDirection >
< ActionType > Click ActionType >
< MouseButton > Left MouseButton >
MouseAction >
< TestStepMarkerAction MarkerInformation ="CloseWindow" >
< ParameterName />
< StepId > -1 StepId >
< Direction > Start Direction >
< Outcome />
< Disabled > false Disabled >
< WorkItemId > 0 WorkItemId >
< MarkerRegionType > Action MarkerRegionType >
TestStepMarkerAction >
ExecuteActions >
< CleanupActions />
< OnErrorActions />
< Maps >
< UIMap Id ="UIMap1" >
< TopLevelWindows >
< TopLevelWindow ControlType ="Window" Id ="UI 系统登录Window" FriendlyName ="系统登录" SpecialControlType ="None" SessionId ="154362" >
< TechnologyName > MSAA TechnologyName >
< WindowTitles >
< WindowTitle > 系统登录 WindowTitle >
WindowTitles >
< SearchConfigurations >
< SearchConfiguration > VisibleOnly SearchConfiguration >
SearchConfigurations >
< AndCondition Id ="SearchCondition" >
< PropertyCondition Name ="Name" & gt;系统登录 >
< PropertyCondition Name ="ClassName" Operator ="Contains" > WindowsForms10.Window PropertyCondition >
< PropertyCondition Name ="ControlType" > Window PropertyCondition >
AndCondition >
< SupportLevel > 0 SupportLevel >
< Descendants >
< UIObject ControlType ="Window" Id ="UITbx_uidWindow" FriendlyName ="Tbx_uid" SpecialControlType ="None" >
< TechnologyName > MSAA TechnologyName >
< WindowTitles >
< WindowTitle > 系统登录 WindowTitle >
WindowTitles >
< SearchConfigurations >
< SearchConfiguration > VisibleOnly SearchConfiguration >
SearchConfigurations >
< AndCondition Id ="SearchCondition" >
< PropertyCondition Name ="ControlName" > Tbx_uid PropertyCondition >
< PropertyCondition Name ="ControlType" > Window PropertyCondition >
AndCondition >
< SupportLevel > 0 SupportLevel >
< Descendants >
< UIObject ControlType ="Edit" Id ="UITbx_uidEdit" FriendlyName ="Tbx_uid" SpecialControlType ="None" >
< TechnologyName > MSAA TechnologyName >
< WindowTitles >
< WindowTitle > 系统登录 WindowTitle >
WindowTitles >
< SearchConfigurations >
< SearchConfiguration > VisibleOnly SearchConfiguration >
SearchConfigurations >
< AndCondition Id ="SearchCondition" >
< PropertyCondition Name ="Name" & gt;密码 : >
< PropertyCondition Name ="ControlType" > Edit PropertyCondition >
AndCondition >
< SupportLevel > 0 SupportLevel >
< Descendants />
UIObject >
Descendants >
UIObject >
< UIObject ControlType ="Window" Id ="UITbx_pwdWindow" FriendlyName ="Tbx_pwd" SpecialControlType ="None" >
< TechnologyName > MSAA TechnologyName >
< WindowTitles >
< WindowTitle > 系统登录 WindowTitle >
WindowTitles >
< SearchConfigurations >
< SearchConfiguration > VisibleOnly SearchConfiguration >
SearchConfigurations >
< AndCondition Id ="SearchCondition" >
< PropertyCondition Name ="ControlName" > Tbx_pwd PropertyCondition >
< PropertyCondition Name ="ControlType" > Window PropertyCondition >
AndCondition >
< SupportLevel > 0 SupportLevel >
< Descendants >
< UIObject ControlType ="Edit" Id ="UITbx_pwdEdit" FriendlyName ="Tbx_pwd" SpecialControlType ="None" >
< TechnologyName > MSAA TechnologyName >
< WindowTitles >
< WindowTitle > 系统登录 WindowTitle >
WindowTitles >
< SearchConfigurations >
< SearchConfiguration > VisibleOnly SearchConfiguration >
SearchConfigurations >
< AndCondition Id ="SearchCondition" >
< PropertyCondition Name ="ControlType" > Edit PropertyCondition >
AndCondition >
< SupportLevel > 0 SupportLevel >
< Descendants />
UIObject >
Descendants >
UIObject >
< UIObject ControlType ="Window" Id ="UI 登录Window" FriendlyName ="登录" SpecialControlType ="None" >
< TechnologyName > MSAA TechnologyName >
< WindowTitles >
< WindowTitle > 系统登录 WindowTitle >
WindowTitles >
< SearchConfigurations >
< SearchConfiguration > VisibleOnly SearchConfiguration >
SearchConfigurations >
< AndCondition Id ="SearchCondition" >
< PropertyCondition Name ="ControlName" > Btn_Login PropertyCondition >
< PropertyCondition Name ="ControlType" > Window PropertyCondition >
AndCondition >
< SupportLevel > 0 SupportLevel >
< Descendants >
< UIObject ControlType ="Button" Id ="UI 登录Button" FriendlyName ="登录" SpecialControlType ="None" >
< TechnologyName > MSAA TechnologyName >
< WindowTitles >
< WindowTitle > 系统登录 WindowTitle >
WindowTitles >
< SearchConfigurations >
< SearchConfiguration > VisibleOnly SearchConfiguration >
SearchConfigurations >
< AndCondition Id ="SearchCondition" >
< PropertyCondition Name ="Name" & gt;登录 >
< PropertyCondition Name ="ControlType" > Button PropertyCondition >
AndCondition >
< SupportLevel > 0 SupportLevel >
< Descendants />
UIObject >
Descendants >
UIObject >
< UIObject ControlType ="TitleBar" Id ="UI 系统登录TitleBar" FriendlyName ="系统登录" SpecialControlType ="None" >
< TechnologyName > MSAA TechnologyName >
< WindowTitles >
< WindowTitle > 系统登录 WindowTitle >
WindowTitles >
< SearchConfigurations >
< SearchConfiguration > VisibleOnly SearchConfiguration >
SearchConfigurations >
< AndCondition Id ="SearchCondition" >
< PropertyCondition Name ="ControlType" > TitleBar PropertyCondition >
AndCondition >
< SupportLevel > 0 SupportLevel >
< Descendants >
< UIObject ControlType ="Button" Id ="UICloseButton" FriendlyName ="Close" SpecialControlType ="None" >
< TechnologyName > MSAA TechnologyName >
< WindowTitles >
< WindowTitle > 系统登录 WindowTitle >
WindowTitles >
< SearchConfigurations >
< SearchConfiguration > VisibleOnly SearchConfiguration >
SearchConfigurations >
< AndCondition Id ="SearchCondition" >
< PropertyCondition Name ="Name" > Close PropertyCondition >
< PropertyCondition Name ="ControlType" > Button PropertyCondition >
AndCondition >
< SupportLevel > 0 SupportLevel >
< Descendants />
UIObject >
Descendants >
UIObject >
Descendants >
TopLevelWindow >
< TopLevelWindow ControlType ="Window" Id ="UI 提示Window" FriendlyName ="提示" SpecialControlType ="None" SessionId ="1203022" >
< TechnologyName > MSAA TechnologyName >
< WindowTitles >
< WindowTitle > 提示 WindowTitle >
WindowTitles >
< SearchConfigurations >
< SearchConfiguration > VisibleOnly SearchConfiguration >
SearchConfigurations >
< AndCondition Id ="SearchCondition" >
< PropertyCondition Name ="Name" & gt;提示 >
< PropertyCondition Name ="ClassName" > #32770 PropertyCondition >
< PropertyCondition Name ="ControlType" > Window PropertyCondition >
AndCondition >
< SupportLevel > 1 SupportLevel >
< Descendants >
< UIObject ControlType ="Window" Id ="UI 用户名或密码错误Window" FriendlyName ="用户名或密码错误!" SpecialControlType ="None" >
< TechnologyName > MSAA TechnologyName >
< WindowTitles >
< WindowTitle > 提示 WindowTitle >
WindowTitles >
< SearchConfigurations >
< SearchConfiguration > VisibleOnly SearchConfiguration >
SearchConfigurations >
< AndCondition Id ="SearchCondition" >
< PropertyCondition Name ="ControlId" > 65535 PropertyCondition >
< PropertyCondition Name ="ControlType" > Window PropertyCondition >
AndCondition >
< SupportLevel > 1 SupportLevel >
< Descendants >
< UIObject ControlType ="Text" Id ="UI 用户名或密码错误Text" FriendlyName ="用户名或密码错误!" SpecialControlType ="None" >
< TechnologyName > MSAA TechnologyName >
< WindowTitles >
< WindowTitle > 提示 WindowTitle >
WindowTitles >
< SearchConfigurations >
< SearchConfiguration > VisibleOnly SearchConfiguration >
SearchConfigurations >
< AndCondition Id ="SearchCondition" >
< PropertyCondition Name ="Name" & gt;用户名或密码错误! >
< PropertyCondition Name ="ControlType" > Text PropertyCondition >
AndCondition >
< SupportLevel > 1 SupportLevel >
< Descendants />
UIObject >
< UIObject ControlType ="Text" Id ="UI 登陆成功Text" FriendlyName ="登陆成功!" SpecialControlType ="None" >
< TechnologyName > MSAA TechnologyName >
< WindowTitles >
< WindowTitle > 提示 WindowTitle >
WindowTitles >
< SearchConfigurations >
< SearchConfiguration > VisibleOnly SearchConfiguration >
SearchConfigurations >
< AndCondition Id ="SearchCondition" >
< PropertyCondition Name ="Name" & gt;登陆成功! >
< PropertyCondition Name ="ControlType" > Text PropertyCondition >
AndCondition >
< SupportLevel > 1 SupportLevel >
< Descendants />
UIObject >
Descendants >
UIObject >
< UIObject ControlType ="Window" Id ="UIOKWindow" FriendlyName ="OK" SpecialControlType ="None" >
< TechnologyName > MSAA TechnologyName >
< WindowTitles >
< WindowTitle > 提示 WindowTitle >
WindowTitles >
< SearchConfigurations >
< SearchConfiguration > VisibleOnly SearchConfiguration >
SearchConfigurations >
< AndCondition Id ="SearchCondition" >
< PropertyCondition Name ="ControlId" > 2 PropertyCondition >
< PropertyCondition Name ="ControlType" > Window PropertyCondition >
AndCondition >
< SupportLevel > 0 SupportLevel >
< Descendants >
< UIObject ControlType ="Button" Id ="UIOKButton" FriendlyName ="OK" SpecialControlType ="None" >
< TechnologyName > MSAA TechnologyName >
< WindowTitles >
< WindowTitle > 提示 WindowTitle >
WindowTitles >
< SearchConfigurations >
< SearchConfiguration > VisibleOnly SearchConfiguration >
SearchConfigurations >
< AndCondition Id ="SearchCondition" >
< PropertyCondition Name ="Name" > OK PropertyCondition >
< PropertyCondition Name ="ControlType" > Button PropertyCondition >