回顾
在之前我们介绍了,大致描述了如何使用编码的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,大家可以看到这里的代码实在是多得看不清,如果觉得阅读比较困难,可以跳过此段,先看看解释说明,然后再回来看。
UIMap.uitest
View Code <? 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 /> < 上海企业网站设计与制作e="color: #800000;">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 > </span><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><PropertyCondition Name="ClassName" Operator="Contains">WindowsForms10.Window</PropertyCondition><PropertyCondition Name="ControlType">Window</PropertyCondition></AndCondition><SupportLevel></span>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><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><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><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><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><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></AndCondition><SupportLevel>0</SupportLevel><Descendants /></UIObject></Descendants></UIObject></Descendants></TopLevelWindow></TopLevelWindows></UIMap></Maps><ValueMap><ParameterList /></ValueMap></UITest> 其实UIMap1.uitest这个XML的文件结构如下:
< UITest > < Configuration > … </ Configuration > < InitializeActions > … </ InitializeActions > < ExecuteActions > … </ ExecuteActions > < CleanupActions > … </ CleanupActions > < OnErrorActions > … </ OnErrorActions > < Maps > … </ Maps > < ValueMap > … </ ValueMap > </ UITest > 这里做一个简单的说明:
Configuration:是对环境进行一个描述和配置,对测试几乎没有影响,可以忽略。
InitializeActions,ExecuteActions,CleanupActions,OnErrorAction:其中InitializeActions指初始化需要执行的操作,ExecuteActions指测试过程中的操作(目前我们所通过录制所生成的操作都默认放在ExecuteActions,大家现在可以回过去仔细看看ExecuteActions节点的内容),CleanupActions指测试完毕需要清理资源的Actions,而OnErrorAction则指代异常处理Actions。
Maps:是我们需要关注的对象库,我们操作过程中所识别的所有控件都是会存放到这里的,对象识别我会在第三章的时候提及。
ValueMap:是值映射,类似于参数记载,实际功能也不是特别大。
在这些节点中,真正起到实际作用的是Actions和Maps。
之后我们再来查看UIMap1.Designer.cs,同样,觉得会看晕的可直接跳过,之后回来再看。
UIMap1.Designer.cs
View Code // ------------------------------------------------------------------------------ // <auto-generated> // This code was generated by coded UI test builder. // Version: 10.0.0.0 // // Changes to this file may cause incorrect behavior and will be lost if // the code is regenerated. // </auto-generated> // ------------------------------------------------------------------------------ namespace TestProject1.UIMap1Classes{ using System; using System.CodeDom.Compiler; using System.Collections.Generic; using System.Drawing; using System.Text.RegularExpressions; using System.Windows.Input; using Microsoft.VisualStudio.TestTools.UITest.Extension; using Microsoft.VisualStudio.TestTools.UITesting; using Microsoft.VisualStudio.TestTools.UITesting.WinControls; using Microsoft.VisualStudio.TestTools.UnitTesting; using Keyboard = Microsoft.VisualStudio.TestTools.UITesting.Keyboard; using Mouse = Microsoft.VisualStudio.TestTools.UITesting.Mouse; using MouseButtons = System.Windows.Forms.MouseButtons; [GeneratedCode( " Coded UITest Builder " , " 10.0.30319.1 " )] public partial class UIMap1 { /// <summary> /// InputErrorUidPwd - Use 'InputErrorUidPwdParams' to pass parameters into this method. /// </summary> public void InputErrorUidPwd() { #region Variable Declarations WinEdit uITbx_uidEdit = this .UI 系统登录Window.UITbx_uidWindow.UITbx_uidEdit; WinEdit uITbx_pwdEdit = this .UI 系统登录Window.UITbx_pwdWindow.UITbx_pwdEdit; WinButton uI 登录Button = this .UI系统登录Window.UI登录Window.UI登录Button; #endregion // Type 'errorUid' in 'Tbx_uid' text box uITbx_uidEdit.Text = this .InputErrorUidPwdParams.UITbx_uidEditText; // Type 'errorPwd' in 'Tbx_pwd' text box uITbx_pwdEdit.Text = this .InputErrorUidPwdParams.UITbx_pwdEditText; // Click ' 登录' button Mouse.Click(uI登录Button, new Point( 25 , 6 )); } /// <summary> /// AssertErrorWinow - Use 'AssertErrorWinowExpectedValues' to pass parameters into this method. /// </summary> public void AssertErrorWinow() { #region Variable Declarations WinText uI 用户名或密码错误Text = this .UI提示Window.UI用户名或密码错误Window.UI用户名或密码错误Text; #endregion // Verify that ' 用户名或密码错误!' label's property 'Exists' equals 'True' Assert.AreEqual( this .AssertErrorWinowExpectedValues.UI 用户名或密码错误TextExists, uI用户名或密码错误Text.Exists); } /// <summary> /// InputRightUidPwd - Use 'InputRightUidPwdParams' to pass parameters into this method. /// </summary> public void InputRightUidPwd() { #region Variable Declarations WinButton uIOKButton = this .UI 提示Window.UIOKWindow.UIOKButton; WinEdit uITbx_uidEdit = this .UI 系统登录Window.UITbx_uidWindow.UITbx_uidEdit; WinEdit uITbx_pwdEdit = this .UI 系统登录Window.UITbx_pwdWindow.UITbx_pwdEdit; WinButton uI 登录Button = this .UI系统登录Window.UI登录Window.UI登录Button; #endregion // Click 'OK' button Mouse.Click(uIOKButton, new Point( 22 , 19 )); // Type 'Admin' in 'Tbx_uid' text box uITbx_uidEdit.Text = this .InputRightUidPwdParams.UITbx_uidEditText; // Type '123456' in 'Tbx_pwd' text box uITbx_pwdEdit.Text = this .InputRightUidPwdParams.UITbx_pwdEditText; // Click ' 登录' button Mouse.Click(uI登录Button, new Point( 39 , 8 )); } /// <summary> /// AssertRightWinow - Use 'AssertRightWinowExpectedValues' to pass parameters into this method. /// </summary> public void AssertRightWinow() { #region Variable Declarations WinText uI 登陆成功Text = this .UI提示Window.UI用户名或密码错误Window.UI登陆成功Text; #endregion // Verify that ' 登陆成功!' label's property 'Exists' equals 'True' Assert.AreEqual( this .AssertRightWinowExpectedValues.UI 登陆成功TextExists, uI登陆成功Text.Exists); } /// <summary> /// CloseWindow /// </summary> public void CloseWindow() { #region Variable Declarations WinButton uIOKButton = this .UI 提示Window.UIOKWindow.UIOKButton; WinButton uICloseButton = this .UI 系统登录Window.UI系统登录TitleBar.UICloseButton; #endregion // Click 'OK' button Mouse.Click(uIOKButton, new Point( 28 , 13 )); // Click 'Close' button Mouse.Click(uICloseButton, new Point( 9 , 11 )); } #region Properties public virtual InputErrorUidPwdParams InputErrorUidPwdParams { get { if (( this .mInputErrorUidPwdParams == null )) { this .mInputErrorUidPwdParams = new InputErrorUidPwdParams(); } return this .mInputErrorUidPwdParams; } } public virtual AssertErrorWinowExpectedValues AssertErrorWinowExpectedValues { get { if (( this .mAssertErrorWinowExpectedValues == null )) { this .mAssertErrorWinowExpectedValues = new AssertErrorWinowExpectedValues(); } return this .mAssertErrorWinowExpectedValues; } } public virtual InputRightUidPwdParams InputRightUidPwdParams { get { if (( this .mInputRightUidPwdParams == null )) { this .mInputRightUidPwdParams = new InputRightUidPwdParams(); } return this .mInputRightUidPwdParams; } } public virtual AssertRightWinowExpectedValues AssertRightWinowExpectedValues { get { if (( this .mAssertRightWinowExpectedValues == null )) { this .mAssertRightWinowExpectedValues = new AssertRightWinowExpectedValues(); } return this .mAssertRightWinowExpectedValues; } } public UI 系统登录Window UI系统登录Window { get { if (( this .mUI 系统登录Window == null )) { this .mUI 系统登录Window = new UI系统登录Window(); } return this .mUI 系统登录Window; } } public UI 提示Window UI提示Window { get { if (( this .mUI 提示Window == null )) { this .mUI 提示Window = new UI提示Window(); } return this .mUI 提示Window; } } #endregion #region Fields private InputErrorUidPwdParams mInputErrorUidPwdParams; private AssertErrorWinowExpectedValues mAssertErrorWinowExpectedValues; private InputRightUidPwdParams mInputRightUidPwdParams; private AssertRightWinowExpectedValues mAssertRightWinowExpectedValues; private UI 系统登录Window mUI系统登录Window; private UI提示 Window mUI提示Window; #endregion } /// <summary> /// Parameters to be passed into 'InputErrorUidPwd' /// </summary> [GeneratedCode( " Coded UITest Builder " , " 10.0.30319.1 " )] public class InputErrorUidPwdParams { #region Fields /// <summary> /// Type 'errorUid' in 'Tbx_uid' text box /// </summary> public string UITbx_uidEditText = " errorUid " ; /// <summary> /// Type 'errorPwd' in 'Tbx_pwd' text box /// </summary> public string UITbx_pwdEditText = " errorPwd " ; #endregion } /// <summary> /// Parameters to be passed into 'AssertErrorWinow' /// </summary> [GeneratedCode( " Coded UITest Builder " , " 10.0.30319.1 " )] public class AssertErrorWinowExpectedValues { #region Fields /// <summary> /// Verify that ' 用户名或密码错误!' label's property 'Exists' equals 'True' /// </summary> public bool UI 用户名或密码错误TextExists = true ; #endregion } /// <summary> /// Parameters to be passed into 'InputRightUidPwd' /// </summary> [GeneratedCode( " Coded UITest Builder " , " 10.0.30319.1 " )] public class InputRightUidPwdParams { #region Fields /// <summary> /// Type 'Admin' in 'Tbx_uid' text box /// </summary> public string UITbx_uidEditText = " Admin " ; /// <summary> /// Type '123456' in 'Tbx_pwd' text box /// </summary> public string UITbx_pwdEditText = " 123456 " ; #endregion } /// <summary> /// Parameters to be passed into 'AssertRightWinow' /// </summary> [GeneratedCode( " Coded UITest Builder " , " 10.0.30319.1 " )] public class AssertRightWinowExpectedValues { #region Fields /// <summary> /// Verify that ' 登陆成功!' label's property 'Exists' equals 'True' /// </summary> public bool UI 登陆成功TextExists = true ; #endregion } [GeneratedCode( " Coded UITest Builder " , " 10.0.30319.1 " )] public class UI 系统登录Window : WinWindow { public UI 系统登录Window() { #region Search Criteria this .SearchProperties[WinWindow.PropertyNames.Name] = " 系统登录 " ; this .SearchProperties.Add( new PropertyExpression(WinWindow.PropertyNames.ClassName, " WindowsForms10.Window " , PropertyExpressionOperator.Contains)); this .WindowTitles.Add( " 系统登录 " ); #endregion } #region Properties public UITbx_uidWindow UITbx_uidWindow { get { if (( this .mUITbx_uidWindow == null )) { this .mUITbx_uidWindow = new UITbx_uidWindow( this ); } return this .mUITbx_uidWindow; } } public UITbx_pwdWindow UITbx_pwdWindow { get { if (( this .mUITbx_pwdWindow == null )) { this .mUITbx_pwdWindow = new UITbx_pwdWindow( this ); } return this .mUITbx_pwdWindow; } } public UI 登录Window UI登录Window { get { if (( this .mUI 登录Window == null )) { this .mUI 登录Window = new UI登录Window( this ); } return this .mUI 登录Window; } } public UI 系统登录TitleBar UI系统登录TitleBar { get { if (( this .mUI 系统登录TitleBar == null )) { this .mUI 系统登录TitleBar = new UI系统登录TitleBar( this ); } return this .mUI 系统登录TitleBar; } } #endregion #region Fields private UITbx_uidWindow mUITbx_uidWindow; private UITbx_pwdWindow mUITbx_pwdWindow; private UI 登录Window mUI登录Window; private UI系统登录 TitleBar mUI系统登录TitleBar; #endregion } [GeneratedCode( " Coded UITest Builder " , " 10.0.30319.1 " )] public class UITbx_uidWindow : WinWindow { public UITbx_uidWindow(UITestControl searchLimitContainer) : base (searchLimitContainer) { #region Search Criteria this .SearchProperties[WinWindow.PropertyNames.ControlName] = " Tbx_uid " ; this .WindowTitles.Add( " 系统登录 " ); #endregion } #region Properties public WinEdit UITbx_uidEdit { get { if (( this .mUITbx_uidEdit == null )) { this .mUITbx_uidEdit = new WinEdit( this ); #region Search Criteria this .mUITbx_uidEdit.SearchProperties[WinEdit.PropertyNames.Name] = " 密码 : " ; this .mUITbx_uidEdit.WindowTitles.Add( " 系统登录 " ); #endregion } return this .mUITbx_uidEdit; } } #endregion #region Fields private WinEdit mUITbx_uidEdit; #endregion } [GeneratedCode( " Coded UITest Builder " , " 10.0.30319.1 " )] public class UITbx_pwdWindow : WinWindow { public UITbx_pwdWindow(UITestControl searchLimitContainer) : base (searchLimitContainer) { #region Search Criteria this .SearchProperties[WinWindow.PropertyNames.ControlName] = " Tbx_pwd " ; this .WindowTitles.Add( " 系统登录 " ); #endregion } #region Properties public WinEdit UITbx_pwdEdit { get { if (( this .mUITbx_pwdEdit == null )) { this .mUITbx_pwdEdit = new WinEdit( this ); #region Search Criteria this .mUITbx_pwdEdit.WindowTitles.Add( " 系统登录 " ); #endregion } return this .mUITbx_pwdEdit; } } #endregion #region Fields private WinEdit mUITbx_pwdEdit; #endregion } [GeneratedCode( " Coded UITest Builder " , " 10.0.30319.1 " )] public class UI 登录Window : WinWindow { public UI 登录Window(UITestControl searchLimitContainer) : base (searchLimitContainer) { #region Search Criteria this .SearchProperties[WinWindow.PropertyNames.ControlName] = " Btn_Login " ; this .WindowTitles.Add( " 系统登录 " ); #endregion } #region Properties public WinButton UI 登录Button { get { if (( this .mUI 登录Button == null )) { this .mUI 登录Button = new WinButton( this ); #region Search Criteria this .mUI 登录Button.SearchProperties[WinButton.PropertyNames.Name] = " 登录 " ; this .mUI 登录Button.WindowTitles.Add( " 系统登录 " ); #endregion } return this .mUI 登录Button; } } #endregion #region Fields private WinButton mUI 登录Button; #endregion } [GeneratedCode( " Coded UITest Builder " , " 10.0.30319.1 " )] public class UI 系统登录TitleBar : WinTitleBar { public UI 系统登录TitleBar(UITestControl searchLimitContainer) : base (searchLimitContainer) { #region Search Criteria this .WindowTitles.Add( " 系统登录 " ); #endregion } #region Properties public WinButton UICloseButton { get { if (( this .mUICloseButton == null )) { this .mUICloseButton = new WinButton( this ); #region Search Criteria this .mUICloseButton.SearchProperties[WinButton.PropertyNames.Name] = " Close " ; this .mUICloseButton.WindowTitles.Add( " 系统登录 " ); #endregion } return this .mUICloseButton; } } #endregion #region Fields private WinButton mUICloseButton; #endregion } [GeneratedCode( " Coded UITest Builder " , " 10.0.30319.1 " )] public class UI 提示Window : WinWindow { public UI 提示Window() { #region Search Criteria this .SearchProperties[WinWindow.PropertyNames.Name] = " 提示 " ; this .SearchProperties[WinWindow.PropertyNames.ClassName] = " #32770 " ; this .WindowTitles.Add( " 提示 " ); #endregion } #region Properties public UI 用户名或密码错误Window UI用户名或密码错误Window { get { if (( this .mUI 用户名或密码错误Window == null )) { this .mUI 用户名或密码错误Window = new UI用户名或密码错误Window( this ); } return this .mUI 用户名或密码错误Window; } } public UIOKWindow UIOKWindow { get { if (( this .mUIOKWindow == null )) { this .mUIOKWindow = new UIOKWindow( this ); } return this .mUIOKWindow; } } #endregion #region Fields private UI 用户名或密码错误Window mUI用户名或密码错误Window; private UIOKWindow mUIOKWindow; #endregion } [GeneratedCode( " Coded UITest Builder " , " 10.0.30319.1 " )] public class UI 用户名或密码错误Window : WinWindow { public UI 用户名或密码错误Window(UITestControl searchLimitContainer) : base (searchLimitContainer) { #region Search Criteria this .SearchProperties[WinWindow.PropertyNames.ControlId] = " 65535 " ; this .WindowTitles.Add( " 提示 " ); #endregion } #region Properties public WinText UI 用户名或密码错误Text { get { if (( this .mUI 用户名或密码错误Text == null )) { this .mUI 用户名或密码错误Text = new WinText( this ); #region Search Criteria this .mUI 用户名或密码错误Text.SearchProperties[WinText.PropertyNames.Name] = " 用户名或密码错误! " ; this .mUI 用户名或密码错误Text.WindowTitles.Add( " 提示 " ); #endregion } return this .mUI 用户名或密码错误Text; } } public WinText UI 登陆成功Text { get { if (( this .mUI 登陆成功Text == null )) { this .mUI 登陆成功Text = new WinText( this ); #region Search Criteria this .mUI 登陆成功Text.SearchProperties[WinText.PropertyNames.Name] = " 登陆成功! " ; this .mUI 登陆成功Text.WindowTitles.Add( " 提示 " ); #endregion } return this .mUI 登陆成功Text; } } #endregion #region Fields private WinText mUI 用户名或密码错误Text; private WinText mUI登陆成功 Text; #endregion } [GeneratedCode( " Coded UITest Builder " , " 10.0.30319.1 " )] public class UIOKWindow : WinWindow { public UIOKWindow(UITestControl searchLimitContainer) : base (searchLimitContainer) { #region Search Criteria this .SearchProperties[WinWindow.PropertyNames.ControlId] = " 2 " ; this .WindowTitles.Add( " 提示 " ); #endregion } #region Properties public WinButton UIOKButton { get { if (( this .mUIOKButton == null )) { this .mUIOKButton = new WinButton( this ); #region Search Criteria this .mUIOKButton.SearchProperties[WinButton.PropertyNames.Name] = " OK " ; this .mUIOKButton.WindowTitles.Add( " 提示 " ); #endregion } return this .mUIOKButton; } } #endregion #region Fields private WinButton mUIOKButton; #endregion }} UIMap1.Designer.cs这个文件结构如下:
图6:UIMap1.Designer.cs
我们可以用UIMap1.uitest上的内容和UIMap1.Designer.cs上的内容做个对比,可以明显的发现他们之间有一定的对应性,那么疑问就产生了,我们在录制的时候,究竟先生成UIMap1.uitest还是UIMap1.Designer.cs?在对界面操作的代码中,他们哪个起作用?
为了说明这个,就得提到编码的UI测试操作步骤的录制原理,这里会按步骤说明:
(1)首先我们打开了录制生成器。
(2)使用录制生成器录制操作,对于每一个操作动作,录制生成器会根据你所进行的操作,解析被操作的对象(控件),并分析操作动作,并将对象(控件)的关键标识属性和操作动作按录制顺序存放在内存中。
(3)停止录制。
(4)点击“生成代码”按钮,录制生成器会读取之前放在内存中的对象(控件)的关键标识属性和操作动作按XML的形式存放在UIMap1.uitest里。
(5)之后代码生成器通过解析UIMap1.uitest,再生成UIMap1.Designer.cs。
然后进行回放的时候,真正在起作用其实的是UIMap1.Designer.cs,UIMap1.uitest的作用仅仅是用于生成UIMap1.Designer.cs。
我想介绍到这里很多曾经使用过VS2010编码UI测试的朋友应该明白了,为什么自己在UIMap1.Designer.cs添加或者删除的代码,在打开录制生成器之后,重新点击“生成代码”,之前的修改就消失了,原因就是因为UIMap1.Designer.cs里面的代码是根据UIMap1.uitest生成的。
因此,如果要想编写自定义代码,可以通过两种途径。
(1)通过修改UIMap1.uitest来控制UIMap1.Designer.cs代码的生成。
(2)将代码写到非UIMap1.Designer.cs文件的其他地方。
在本章的第二部分,将介绍如何通过修改UIMap1.uitest来控制UIMap1.Designer.cs代码的生成,预计可能在本周六或者周日发布,敬请关注~
PS:如何调用UIMap1.Designer.cs中的方法,这个时候可以新建一个CodedUItest.cs,取消录制,写一个这样的调用即可。
[TestMethod] public void CodedUITestMethod1(){ UIMap1Classes.UIMap1 uimap1 = new UIMap1Classes.UIMap1(); uimap1.InputErrorUidPwd(); uimap1.AssertErrorWinow(); uimap1.InputRightUidPwd(); uimap1.AssertRightWinow(); uimap1.CloseWindow();}