Tải bản đầy đủ (.pdf) (11 trang)

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

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (953.6 KB, 11 trang )

Coded 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

Chọn Visual

C# / Test.

/ New Project.

Đặt Name là CodedUITestProject2, 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,

3. Xuất hiện cửa sổ UIMap

nhấn OK.

– Coded UI Test Builder:

Show Recorded Steps (Alt-S)
Add assertions


Recording

4. Nhấn nút Start Recording
dương), nghĩa là đang ghi.

(Alt-R)

Close to finish recording
Generate Code (Alt-G)

(nút màu đỏ) sẽ trở thành nút Pause

Recording (Alt-R)

(màu xanh

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.


8. Nhập mục Message

9. Nhấn nút

on Assertion Failure


là Title

is incorrect.

Nhấn nút OK.

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.


11. 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
việc ghi của quá trình chạy ứng dụng Calculator.

– Coded UI Test Builder


để kết thúc


Biên dịch và 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>
/// 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;


}
}
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
// -----------------------------------------------------------------------------// <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
{
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
{

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


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


/// 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))
{


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 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
kiểm thử này.

Run Selected Tests

để chạy


3. 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
lại kiểm thử.

Name

là Calculateur. Biên dịch và chạy


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



×