Coded UI Testing Kiểm tra tên text của các đối tượng tiêu đề, nút nhấn, … 1.. Nhấn biểu tượng Close to finish recording của cửa sổ UIMap – Coded UI Test Builder để kết thúc việc ghi của
Trang 1Coded UI Testing
Kiểm tra tên (text) của các đối tượng (tiêu đề, nút nhấn, …)
1 Tạo project 2: nhấn chuột phải vào tên của Solution: chọn Add / New Project
Chọn Visual C# / Test Đặt Name là CodedUITestProject2, chọn Coded UI Test Project, nhấn OK
Trang 22 Xuất hiện cửa sổ sau:
Chọn Record actions, edit UI map or add assertions, nhấn OK
3 Xuất hiện cửa sổ UIMap – Coded UI Test Builder:
4 Nhấn nút Start Recording (Alt-R) (nút màu đỏ) sẽ trở thành nút Pause Recording (Alt-R) (màu xanh dương), nghĩa là đang ghi
Gọi ứng dụng Calculator từ màn hình, nhấn chuột phải tại tiêu đề Pause, nhấn chuột phải tại nút bấm 6
5 Sau khi ghi nhận xong các hành động của ứng dụng Calculator, nhấn nút Generate Code để tạo mã lệnh của dự án Nhập tên của phương thức là CheckAssertions và mô tả của phương thức là Check assertions, nhấn nút Add and Generate
6 Nhấn nút Add Assertions, nhấn giữ chuột và di chuyển vào tiêu đề của ứng dụng Calculator, rồi nhả
chuột Chọn UICalculatorTitleBar, chọn mục DisplayText, nhấn chuột phải, chọn Add Assertion
Recording
Show Recorded Steps (Alt-S)
Generate Code (Alt-G) Close to finish recording Add assertions
Trang 38 Nhập mục Message on Assertion Failure là Title is incorrect Nhấn nút OK
9 Nhấn nút Add Assertions, nhấn giữ chuột và di chuyển vào nút nhấn 6, rồi nhả chuột Chọn UIItem6Button, chọn mục Name, nhấn chuột phải, chọn Add Assertion
10 Nhập mục Message on Assertion Failure là Button name is incorrect Nhấn nút OK
Trang 411 Tên mặc định của phương thức kiểm tra điều khẳng định là AssertMethod1 Nhập mô tả của phương thức là Check assertions Nhấn nút Add and Generate để tạo phương thức này
12 Nhấn biểu tượng Close to finish recording của cửa sổ UIMap – Coded UI Test Builder để kết thúc việc ghi của quá trình chạy ứng dụng Calculator
Trang 5Biên dịch và chạy kiểm thử
Tập tin CodeUITest1.cs
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Windows.Input;
using System.Windows.Forms;
using System.Drawing;
using Microsoft.VisualStudio.TestTools.UITesting;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Microsoft.VisualStudio.TestTools.UITest.Extension;
using Keyboard = Microsoft.VisualStudio.TestTools.UITesting.Keyboard;
namespace CodedUITestProject2
{
/// <summary>
/// Summary description for CodedUITest1
/// </summary>
[CodedUITest]
public class CodedUITest1
{
public CodedUITest1()
{
}
[TestMethod]
public void CodedUITestMethod1()
{
// To generate code for this test, select "Generate Code for Coded UI Test" from the shortcut menu and select one of the menu items
this.UIMap.CheckAssertions();
this.UIMap.AssertMethod1();
}
#region Additional test attributes
// You can use the following additional attributes as you write your tests:
////Use TestInitialize to run code before running each test
//[TestInitialize()]
//public void MyTestInitialize()
//{
// // To generate code for this test, select "Generate Code for Coded UI Test" from the shortcut menu and select one of the menu items
//}
////Use TestCleanup to run code after each test has run
//[TestCleanup()]
//public void MyTestCleanup()
//{
// // To generate code for this test, select "Generate Code for Coded UI Test" from the shortcut menu and select one of the menu items
//}
#endregion
/// <summary>
///Gets or sets the test context which provides
///information about and functionality for the current test run
///</summary>
public TestContext TestContext
{
get
{
return testContextInstance;
}
set
{
testContextInstance = value;
}
}
private TestContext testContextInstance;
public UIMap UIMap
{
get
{
if ((this.map == null))
{
this.map = new UIMap();
}
return this.map;
Trang 6}
}
private UIMap map;
}
}
Tập tin UIMap.cs
namespace CodedUITestProject2
{
using System;
using System.Collections.Generic;
using System.CodeDom.Compiler;
using Microsoft.VisualStudio.TestTools.UITest.Extension;
using Microsoft.VisualStudio.TestTools.UITesting;
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;
using System.Drawing;
using System.Windows.Input;
using System.Text.RegularExpressions;
public partial class UIMap
{
}
}
Tập tin UIMap.Designer.cs
//
-// <auto-generated>
// This code was generated by coded UI test builder
// Version: 12.0.0.0
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated
// </auto-generated>
//
-namespace CodedUITestProject2
{
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", "12.0.21005.1")]
public partial class UIMap
{
/// <summary>
/// Check assertions
/// </summary>
public void CheckAssertions()
{
#region Variable Declarations
WinTitleBar uICalculatorTitleBar = this.UICalculatorWindow.UICalculatorTitleBar;
WinButton uIItem6Button = this.UICalculatorWindow.UIItemWindow.UIItem6Button;
#endregion
// Launch '%windir%\System32\calc.exe'
ApplicationUnderTest uICalculatorWindow =
ApplicationUnderTest.Launch(this.CheckAssertionsParams.UICalculatorWindowExePath,
this.CheckAssertionsParams.UICalculatorWindowAlternateExePath);
// Right-Click 'Calculator' title bar
Mouse.Click(uICalculatorTitleBar, MouseButtons.Right, ModifierKeys.None, new Point(61, 9)); // Right-Click '6' button
Mouse.Click(uIItem6Button, MouseButtons.Right, ModifierKeys.None, new Point(24, 13)); }
Trang 7
/// <summary>
/// Check assertions
/// </summary>
public void AssertMethod1()
{
#region Variable Declarations
WinTitleBar uICalculatorTitleBar = this.UICalculatorWindow.UICalculatorTitleBar;
WinButton uIItem6Button = this.UICalculatorWindow.UIItemWindow.UIItem6Button;
#endregion
// Verify that the 'DisplayText' property of 'Calculator' title bar equals 'Calculator'
Assert.AreEqual(this.AssertMethod1ExpectedValues.UICalculatorTitleBarDisplayText,
uICalculatorTitleBar.DisplayText, "Title is incorrect");
// Verify that the 'Name' property of '6' button equals '6'
Assert.AreEqual(this.AssertMethod1ExpectedValues.UIItem6ButtonName, uIItem6Button.Name, "Button name is incorrect");
}
#region Properties
public virtual CheckAssertionsParams CheckAssertionsParams
{
get
{
if ((this.mCheckAssertionsParams == null))
{
this.mCheckAssertionsParams = new CheckAssertionsParams();
}
return this.mCheckAssertionsParams;
}
}
public virtual AssertMethod1ExpectedValues AssertMethod1ExpectedValues
{
get
{
if ((this.mAssertMethod1ExpectedValues == null))
{
this.mAssertMethod1ExpectedValues = new AssertMethod1ExpectedValues();
}
return this.mAssertMethod1ExpectedValues;
}
}
public UICalculatorWindow UICalculatorWindow
{
get
{
if ((this.mUICalculatorWindow == null))
{
this.mUICalculatorWindow = new UICalculatorWindow();
}
return this.mUICalculatorWindow;
}
}
#endregion
#region Fields
private CheckAssertionsParams mCheckAssertionsParams;
private AssertMethod1ExpectedValues mAssertMethod1ExpectedValues;
private UICalculatorWindow mUICalculatorWindow;
#endregion
}
/// <summary>
/// Parameters to be passed into 'CheckAssertions'
/// </summary>
[GeneratedCode("Coded UITest Builder", "12.0.21005.1")]
public class CheckAssertionsParams
{
#region Fields
/// <summary>
/// Launch '%windir%\System32\calc.exe'
/// </summary>
public string UICalculatorWindowExePath = "C:\\Windows\\System32\\calc.exe";
/// <summary>
/// Launch '%windir%\System32\calc.exe'
/// </summary>
public string UICalculatorWindowAlternateExePath = "%windir%\\System32\\calc.exe";
#endregion
}
/// <summary>
Trang 8/// Parameters to be passed into 'AssertMethod1'
/// </summary>
[GeneratedCode("Coded UITest Builder", "12.0.21005.1")]
public class AssertMethod1ExpectedValues
{
#region Fields
/// <summary>
/// Verify that the 'DisplayText' property of 'Calculator' title bar equals 'Calculator'
/// </summary>
public string UICalculatorTitleBarDisplayText = "Calculator";
/// <summary>
/// Verify that the 'Name' property of '6' button equals '6'
/// </summary>
public string UIItem6ButtonName = "6";
#endregion
}
[GeneratedCode("Coded UITest Builder", "12.0.21005.1")]
public class UICalculatorWindow : WinWindow
{
public UICalculatorWindow()
{
#region Search Criteria
this.SearchProperties[WinWindow.PropertyNames.Name] = "Calculator";
this.SearchProperties[WinWindow.PropertyNames.ClassName] = "CalcFrame";
this.WindowTitles.Add("Calculator");
#endregion
}
#region Properties
public WinTitleBar UICalculatorTitleBar
{
get
{
if ((this.mUICalculatorTitleBar == null))
{
this.mUICalculatorTitleBar = new WinTitleBar(this);
#region Search Criteria
this.mUICalculatorTitleBar.WindowTitles.Add("Calculator");
#endregion
}
return this.mUICalculatorTitleBar;
}
}
public UIItemWindow UIItemWindow
{
get
{
if ((this.mUIItemWindow == null))
{
this.mUIItemWindow = new UIItemWindow(this);
}
return this.mUIItemWindow;
}
}
#endregion
#region Fields
private WinTitleBar mUICalculatorTitleBar;
private UIItemWindow mUIItemWindow;
#endregion
}
[GeneratedCode("Coded UITest Builder", "12.0.21005.1")]
public class UIItemWindow : WinWindow
{
public UIItemWindow(UITestControl searchLimitContainer) :
base(searchLimitContainer)
{
#region Search Criteria
this.SearchProperties[WinWindow.PropertyNames.ControlId] = "136";
this.WindowTitles.Add("Calculator");
#endregion
}
#region Properties
public WinButton UIItem6Button
{
get
{
if ((this.mUIItem6Button == null))
{
Trang 9this.mUIItem6Button = new WinButton(this);
#region Search Criteria
this.mUIItem6Button.SearchProperties[WinButton.PropertyNames.Name] = "6";
this.mUIItem6Button.WindowTitles.Add("Calculator");
#endregion
}
return this.mUIItem6Button;
}
}
#endregion
#region Fields
private WinButton mUIItem6Button;
#endregion
}
}
Toàn bộ màn hình của CodeUITestProject2 như sau:
1 Chọn Build / Build Solution để biên dịch
2 Chọn kiểm thử (test), nhấn chuột phải trên tên của kiểm thử này, chọn Run Selected Tests để chạy
kiểm thử này
Trang 103 Kết quả kiểm thử sẽ xuất hiện trong cửa sổ Test Explorer
4 Thay đổi các thuộc tính của điều khẳng định: double-click tại mục UIMap.uitest để hiện cửa sổ này, chọn điều khẳng định, sửa các thuộc tính trong của sổ Properties Sau đó, biên dịch và chạy lại kiểm thử
Ví dụ: Chọn mục UICalculatorTitleBar, sửa giá trị của Friendly Name là Calculateur Biên dịch và chạy lại kiểm thử
Trang 115 Nếu kiểm thử bị sai, thì xuất hiện thông báo lỗi trong kết quả kiểm thử của cửa sổ Test Explorer