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

THỰC HÀNH Coded UI testing

13 46 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

Thông tin cơ bản

Định dạng
Số trang 13
Dung lượng 1,15 MB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

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à AddNumbers và mô tả của phương thức là Add two

Trang 1

Tạo Coded UI Project

1 Chọn File / New / Project

Chọn Visual C# / Test Đặt Name là CodedUITestProject1, chọn Coded UI Test Project , nhấn OK

2 Xuất hiện cửa sổ sau:

Chọn Record actions, edit UI map or add assertions , nhấn OK

Trang 2

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 nút 2, nhấn nút +, nhấn nút 3, nhấn nút = Nhấn nút Pause Recording (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 ghi

5 Nhấn nút Show Recorded Steps (alt-S) để xem các hành động đã được ghi nhận (recorded action)

Để hủy bỏ một hoặc nhiều hành động, chọn các hành động này rồi nhấn chuột phải, chọn Delete

Selected Action(s) – Del

Recording

Show Recorded Steps (Alt-S)

Generate Code (Alt-G) Close to finish recording Add assertions

Trang 3

6 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à AddNumbers và mô tả của phương thức là Add two numbers , nhấn nút Add and Generate

7 Nhấn nút Add Assertions , nhấn giữ chuột và di chuyển vào ô màn hình của ứng dụng Calculator để

mô tả điều khẳng định (assertion) Xuất hiện màn hình sau, nhấn nút Show UI Control Map để mở rộng cửa sổ

8 Chọn UIItem5Text , chọn mục DisplayText , nhấn chuột phải, chọn Add Assertion

Trang 4

9 Nhập mục Message on Assertion FailureAddition is failed Nhấn nút OK

10 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à Assert adding numbers Nhấn nút Add and Generate để tạo phương thức này

11 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 5

Biê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 CodedUITestProject1

{

/// <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.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

/// <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 CodedUITestProject1

{

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 CodedUITestProject1

{

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>

/// Add two numbers

/// </summary>

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

Trang 7

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));

}

/// <summary>

/// Assert adding numbers

/// </summary>

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

}

/// <summary>

/// Parameters to be passed into 'AddNumbers'

/// </summary>

[GeneratedCode("Coded UITest Builder", "12.0.21005.1")]

public class AddNumbersParams

{

#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

Trang 8

}

/// <summary>

/// 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 '5' label equals '5'

/// </summary>

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;

Trang 9

}

}

#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

}

Trang 10

[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

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

w