1. Trang chủ
  2. » Giáo Dục - Đào Tạo

THỰC HÀNH Coded UI testing

13 44 0

Đ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

Tạo Coded UI Project Chọn File / New / Project Chọn Visual C# / Test Đặt Name CodedUITestProject1, chọn Coded Xuất cửa sổ sau: Chọn Record actions, edit UI map or add assertions, nhấn OK UI Test Project, nhấn OK 3 Xuất cửa sổ UIMap – Coded UI Test Builder: Show Recorded Steps (Alt-S) Add assertions Recording Nhấn nút Start Recording dương), nghĩa ghi Gọi ứng dụng Recording (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 từ hình, nhấn nút 2, nhấn nút +, nhấn nút 3, nhấn nút = Nhấn nút Pause (Alt-R) để tạm dừng công việc ghi; sau này, nhấn nút Resume Recording để tiếp tục công việc Calculator ghi Nhấn nút Show Recorded Steps (alt-S) để xem hành động ghi nhận (recorded action) Để hủy bỏ nhiều hành động, chọn hành động nhấn chuột phải, chọn Delete Selected Action(s) – Del 6 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 AddNumbers mô tả phương thức Add two numbers, nhấn nút Add and Generate Nhấn nút Add Assertions, nhấn giữ chuột di chuyển vào hình ứng dụng Calculator để mô tả điều khẳng định (assertion) Xuất hình sau, nhấn nút Show UI Control Map để mở rộng cửa sổ Chọn UIItem5Text, chọn mục DisplayText, nhấn chuột phải, chọn Add Assertion 9 Nhập mục Message on Assertion Failure Addition is failed Nhấn nút OK 10 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 Assert adding numbers Nhấn nút Add and Generate để tạo phương thức 11 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 CodedUITestProject1 { /// /// 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.AddNumbers(); 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 CodedUITestProject1 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 CodedUITestProject1 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 { /// /// Add two numbers /// public void AddNumbers() { #region Variable Declarations WinButton uIItem2Button = this.UICalculatorWindow.UIItemWindow.UIItem2Button; WinButton uIAddButton = this.UICalculatorWindow.UIItemWindow1.UIAddButton; WinButton uIItem3Button = this.UICalculatorWindow.UIItemWindow2.UIItem3Button; WinButton uIEqualsButton = this.UICalculatorWindow.UIItemWindow3.UIEqualsButton; #endregion // Launch '%windir%\System32\calc.exe' ApplicationUnderTest uICalculatorWindow = ApplicationUnderTest.Launch(this.AddNumbersParams.UICalculatorWindowExePath, this.AddNumbersParams.UICalculatorWindowAlternateExePath); // Click '2' button Mouse.Click(uIItem2Button, new Point(29, 16)); // Click 'Add' button Mouse.Click(uIAddButton, new Point(23, 19)); // Click '3' button Mouse.Click(uIItem3Button, new Point(18, 16)); // Click 'Equals' button Mouse.Click(uIEqualsButton, new Point(28, 31)); } /// /// Assert adding numbers /// public void AssertMethod1() { #region Variable Declarations WinText uIItem5Text = this.UICalculatorWindow.UIItem5Window.UIItem5Text; #endregion // Verify that the 'DisplayText' property of '5' label equals '5' Assert.AreEqual(this.AssertMethod1ExpectedValues.UIItem5TextDisplayText, uIItem5Text.DisplayText, "Addition is failed"); } #region Properties public virtual AddNumbersParams AddNumbersParams { get { if ((this.mAddNumbersParams == null)) { this.mAddNumbersParams = new AddNumbersParams(); } return this.mAddNumbersParams; } } 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 AddNumbersParams mAddNumbersParams; private AssertMethod1ExpectedValues mAssertMethod1ExpectedValues; private UICalculatorWindow mUICalculatorWindow; #endregion } /// /// Parameters to be passed into 'AddNumbers' /// [GeneratedCode("Coded UITest Builder", "12.0.21005.1")] public class AddNumbersParams { #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 '5' label equals '5' /// public string UIItem5TextDisplayText = "5"; #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 UIItemWindow UIItemWindow { get { if ((this.mUIItemWindow == null)) { this.mUIItemWindow = new UIItemWindow(this); } return this.mUIItemWindow; } } public UIItemWindow1 UIItemWindow1 { get { if ((this.mUIItemWindow1 == null)) { this.mUIItemWindow1 = new UIItemWindow1(this); } return this.mUIItemWindow1; } } public UIItemWindow2 UIItemWindow2 { get { if ((this.mUIItemWindow2 == null)) { this.mUIItemWindow2 = new UIItemWindow2(this); } return this.mUIItemWindow2; } } public UIItemWindow3 UIItemWindow3 { get { if ((this.mUIItemWindow3 == null)) { this.mUIItemWindow3 = new UIItemWindow3(this); } return this.mUIItemWindow3; } } public UIItem5Window UIItem5Window { get { if ((this.mUIItem5Window == null)) { this.mUIItem5Window = new UIItem5Window(this); } return this.mUIItem5Window; } } #endregion #region Fields private UIItemWindow mUIItemWindow; private UIItemWindow1 mUIItemWindow1; private UIItemWindow2 mUIItemWindow2; private UIItemWindow3 mUIItemWindow3; private UIItem5Window mUIItem5Window; #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] = "132"; this.WindowTitles.Add("Calculator"); #endregion } #region Properties public WinButton UIItem2Button { get { if ((this.mUIItem2Button == null)) { this.mUIItem2Button = new WinButton(this); #region Search Criteria this.mUIItem2Button.SearchProperties[WinButton.PropertyNames.Name] = "2"; this.mUIItem2Button.WindowTitles.Add("Calculator"); #endregion } return this.mUIItem2Button; } } #endregion #region Fields private WinButton mUIItem2Button; #endregion } [GeneratedCode("Coded UITest Builder", "12.0.21005.1")] public class UIItemWindow1 : WinWindow { public UIItemWindow1(UITestControl searchLimitContainer) : base(searchLimitContainer) { #region Search Criteria this.SearchProperties[WinWindow.PropertyNames.ControlId] = "93"; this.WindowTitles.Add("Calculator"); #endregion } #region Properties public WinButton UIAddButton { get { if ((this.mUIAddButton == null)) { this.mUIAddButton = new WinButton(this); #region Search Criteria this.mUIAddButton.SearchProperties[WinButton.PropertyNames.Name] = "Add"; this.mUIAddButton.WindowTitles.Add("Calculator"); #endregion } return this.mUIAddButton; } } #endregion #region Fields private WinButton mUIAddButton; #endregion } [GeneratedCode("Coded UITest Builder", "12.0.21005.1")] public class UIItemWindow2 : WinWindow { public UIItemWindow2(UITestControl searchLimitContainer) : base(searchLimitContainer) { #region Search Criteria this.SearchProperties[WinWindow.PropertyNames.ControlId] = "133"; this.WindowTitles.Add("Calculator"); #endregion } #region Properties public WinButton UIItem3Button { get { if ((this.mUIItem3Button == null)) { this.mUIItem3Button = new WinButton(this); #region Search Criteria this.mUIItem3Button.SearchProperties[WinButton.PropertyNames.Name] = "3"; this.mUIItem3Button.WindowTitles.Add("Calculator"); #endregion } return this.mUIItem3Button; } } #endregion #region Fields private WinButton mUIItem3Button; #endregion } [GeneratedCode("Coded UITest Builder", "12.0.21005.1")] public class UIItemWindow3 : WinWindow { public UIItemWindow3(UITestControl searchLimitContainer) : base(searchLimitContainer) { #region Search Criteria this.SearchProperties[WinWindow.PropertyNames.ControlId] = "121"; this.WindowTitles.Add("Calculator"); #endregion } #region Properties public WinButton UIEqualsButton { get { if ((this.mUIEqualsButton == null)) { this.mUIEqualsButton = new WinButton(this); #region Search Criteria this.mUIEqualsButton.SearchProperties[WinButton.PropertyNames.Name] = "Equals"; this.mUIEqualsButton.WindowTitles.Add("Calculator"); #endregion } return this.mUIEqualsButton; } } #endregion #region Fields private WinButton mUIEqualsButton; #endregion } [GeneratedCode("Coded UITest Builder", "12.0.21005.1")] public class UIItem5Window : WinWindow { public UIItem5Window(UITestControl searchLimitContainer) : base(searchLimitContainer) { #region Search Criteria this.SearchProperties[WinWindow.PropertyNames.ControlId] = "150"; this.WindowTitles.Add("Calculator"); #endregion } #region Properties public WinText UIItem5Text { get { if ((this.mUIItem5Text == null)) { this.mUIItem5Text = new WinText(this); #region Search Criteria this.mUIItem5Text.SearchProperties[WinText.PropertyNames.Name] = "Result"; this.mUIItem5Text.WindowTitles.Add("Calculator"); #endregion } return this.mUIItem5Text; } } #endregion #region Fields private WinText mUIItem5Text; #endregion } } Toàn hình CodeUITestProject1 sau: Chọn Build / Build Solution để biên dịch 2 Chọn Test / Windows / Test Explorer để mở cửa sổ Test Explorer Chọn kiểm thử (test), nhấn chuột phải tên kiểm thử này, chọn kiểm thử Kết kiểm thử xuất cửa sổ Test Explorer Run Selected Tests để chạy 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 Verify that the 'DisplayText' property Expected Value Biên dịch chạy lại kiểm thử of '5' label equals '5', sửa giá trị 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 ... /// Summary description for CodedUITest1 /// [CodedUITest] public class CodedUITest1 { public CodedUITest1() { } [TestMethod] public void CodedUITestMethod1() { // To generate... WinButton uIItem2Button = this.UICalculatorWindow.UIItemWindow.UIItem2Button; WinButton uIAddButton = this.UICalculatorWindow.UIItemWindow1.UIAddButton; WinButton uIItem3Button = this.UICalculatorWindow.UIItemWindow2.UIItem3Button;... Fields private UIItemWindow mUIItemWindow; private UIItemWindow1 mUIItemWindow1; private UIItemWindow2 mUIItemWindow2; private UIItemWindow3 mUIItemWindow3; private UIItem5Window mUIItem5Window;

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

Xem thêm:

w