BÀI GIẢNG Tạo Coded UI Project

11 14 0
BÀI GIẢNG Tạo Coded UI Project

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

Coded UI Testing Kiểm tra tên (text) đối tượng (tiêu đề, nút nhấn, …) Tạo project 2: nhấn chuột phải vào tên Solution: chọn Add Chọn Visual C# / Test / New Project Đặt Name CodedUITestProject2, chọn Coded UI Test Project, nhấn OK 2 Xuất cửa sổ sau: Chọn Record actions, edit UI map or add assertions, Xuất cửa sổ UIMap nhấn OK – Coded UI Test Builder: Show Recorded Steps (Alt-S) Add assertions Recording Nhấn nút Start Recording dương), nghĩa ghi (Alt-R) Close to finish recording Generate Code (Alt-G) (nút màu đỏ) trở thành nút Pause Recording (Alt-R) (màu xanh Gọi ứng dụng Calculator từ hình, nhấn chuột phải tiêu đề Pause, nhấn chuột phải nút bấm Sau ghi nhận xong hành động ứng dụng Calculator, nhấn nút Generate Code để tạo mã lệnh dự án Nhập tên phương thức CheckAssertions mô tả phương thức Check assertions, nhấn nút Add and Generate Nhấn nút Add Assertions, nhấn giữ chuột di chuyển vào tiêu đề ứng dụng Calculator, nhả chuột Chọn UICalculatorTitleBar, chọn mục DisplayText, nhấn chuột phải, chọn Add Assertion 8 Nhập mục Message Nhấn nút on Assertion Failure Title is incorrect Nhấn nút OK Add Assertions, nhấn giữ chuột di chuyển vào nút nhấn 6, 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 Button name is incorrect Nhấn nút OK 11 Tên mặc định phương thức kiểm tra điều khẳng định AssertMethod1 Nhập mô tả phương thức Check assertions Nhấn nút Add and Generate để tạo phương thức 12 Nhấn biểu tượng Close to finish recording cửa sổ UIMap việc ghi trình chạy ứng dụng Calculator – Coded UI Test Builder để kết thúc Biên dịch chạy kiểm thử Tập tin CodeUITest1.cs using using using using using using using using using using System; System.Collections.Generic; System.Text.RegularExpressions; System.Windows.Input; System.Windows.Forms; System.Drawing; Microsoft.VisualStudio.TestTools.UITesting; Microsoft.VisualStudio.TestTools.UnitTesting; Microsoft.VisualStudio.TestTools.UITest.Extension; Keyboard = Microsoft.VisualStudio.TestTools.UITesting.Keyboard; namespace CodedUITestProject2 { /// /// Summary description for CodedUITest1 /// [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 /// ///Gets or sets the test context which provides ///information about and functionality for the current test run /// 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; } } private UIMap map; } } Tập tin UIMap.cs namespace { using using using using using using using using using using using using CodedUITestProject2 System; System.Collections.Generic; System.CodeDom.Compiler; Microsoft.VisualStudio.TestTools.UITest.Extension; Microsoft.VisualStudio.TestTools.UITesting; Microsoft.VisualStudio.TestTools.UnitTesting; Keyboard = Microsoft.VisualStudio.TestTools.UITesting.Keyboard; Mouse = Microsoft.VisualStudio.TestTools.UITesting.Mouse; MouseButtons = System.Windows.Forms.MouseButtons; System.Drawing; System.Windows.Input; System.Text.RegularExpressions; public partial class UIMap { } } Tập tin UIMap.Designer.cs // -// // 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 // // -namespace { using using using using using using using using using using using using using CodedUITestProject2 System; System.CodeDom.Compiler; System.Collections.Generic; System.Drawing; System.Text.RegularExpressions; System.Windows.Input; Microsoft.VisualStudio.TestTools.UITest.Extension; Microsoft.VisualStudio.TestTools.UITesting; Microsoft.VisualStudio.TestTools.UITesting.WinControls; Microsoft.VisualStudio.TestTools.UnitTesting; Keyboard = Microsoft.VisualStudio.TestTools.UITesting.Keyboard; Mouse = Microsoft.VisualStudio.TestTools.UITesting.Mouse; MouseButtons = System.Windows.Forms.MouseButtons; [GeneratedCode("Coded UITest Builder", "12.0.21005.1")] public partial class UIMap { /// /// Check assertions /// 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)); } /// /// Check assertions /// 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 } /// /// Parameters to be passed into 'CheckAssertions' /// [GeneratedCode("Coded UITest Builder", "12.0.21005.1")] public class CheckAssertionsParams { #region Fields /// /// Launch '%windir%\System32\calc.exe' /// public string UICalculatorWindowExePath = "C:\\Windows\\System32\\calc.exe"; /// /// Launch '%windir%\System32\calc.exe' /// public string UICalculatorWindowAlternateExePath = "%windir%\\System32\\calc.exe"; #endregion } /// /// Parameters to be passed into 'AssertMethod1' /// [GeneratedCode("Coded UITest Builder", "12.0.21005.1")] public class AssertMethod1ExpectedValues { #region Fields /// /// Verify that the 'DisplayText' property of 'Calculator' title bar equals 'Calculator' /// public string UICalculatorTitleBarDisplayText = "Calculator"; /// /// Verify that the 'Name' property of '6' button equals '6' /// 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)) { this.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 } } Tồn hình CodeUITestProject2 sau: Chọn Build / Build Solution để biên dịch Chọn kiểm thử (test), nhấn chuột phải tên kiểm thử này, chọn kiểm thử Run Selected Tests để chạy Kết kiểm thử xuất cửa sổ Test Explorer Thay đổi thuộc tính điều khẳng định: double-click mục UIMap.uitest để cửa sổ này, chọn điều khẳng định, sửa thuộc tính sổ Properties Sau đó, biên dịch chạy lại kiểm thử Ví dụ: Chọn mục UICalculatorTitleBar, sửa giá trị Friendly lại kiểm thử Name Calculateur Biên dịch chạy Nếu kiểm thử bị sai, xuất thơng báo lỗi kết kiểm thử cửa sổ Test Explorer ... CodedUITestProject2 { /// /// Summary description for CodedUITest1 /// [CodedUITest] public class CodedUITest1 { public CodedUITest1() { } [TestMethod] public void CodedUITestMethod1()... WinTitleBar mUICalculatorTitleBar; private UIItemWindow mUIItemWindow; #endregion } [GeneratedCode( "Coded UITest Builder", "12.0.21005.1")] public class UIItemWindow : WinWindow { public UIItemWindow(UITestControl... Variable Declarations WinTitleBar uICalculatorTitleBar = this.UICalculatorWindow.UICalculatorTitleBar; WinButton uIItem6Button = this.UICalculatorWindow.UIItemWindow.UIItem6Button; #endregion // Verify

Ngày đăng: 20/05/2021, 17:23

Từ khóa liên quan

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan