Resources for IT Professionals >
Forums Home
>
Feedback and Discussions Forums
>
MSDN and TechNet Search Feedback
>
Write CodedUITest by hand
Write CodedUITest by hand
- Is there any tutorial to teach writing coded UI tests without a builder? The action recorder is good, but the generated codes are too difficult to read anyway and too complicated to me. I tried to launch a AUT (Calculator) by calling ApplicationUndeTest.Launch() method, and then trying to find a button to click. The calculator is actually launched, but the Mouse.Click() alway throw a null reference exception. Here is my codes:
using System; using System.Text; using System.Collections.Generic; using System.Linq; using Microsoft.VisualStudio.TestTools.UnitTesting; using Microsoft.VisualStudio.TestTools.UITesting; using Microsoft.VisualStudio.TestTools.UITest.Extension; namespace TestProject1 { /// <summary> /// Summary description for GUITest /// </summary> [CodedUITest] public class GUITest { private static string CALCULATOR_PATH = "C:\\Windows\\System32\\calc.exe"; private static string CALCULATOR_ALTERNATE_PATH = "%SystemRoot%\\System32\\calc.exe"; private ApplicationUnderTest _application; private UITestControl _window; private TestContext _context; /// <summary> /// Gets or sets the test context which provides information about and functionality for the current test run. /// </summary> public TestContext TestContext { get { return _context; } set { _context = value; } } /// <summary> /// Launch Calculator before each test method /// </summary> [TestInitialize()] public void MyTestInitialize() { _application = ApplicationUnderTest.Launch(CALCULATOR_PATH, CALCULATOR_ALTERNATE_PATH); _window = new UITestControl(_application); _window.TechnologyName = "MSAA"; _window.SearchConfigurations.Add(SearchConfiguration.VisibleOnly); _window.SearchProperties.Add(WinProperties.Window.ControlType, "Window"); _window.SearchProperties[WinProperties.Window.Name] = "小算盤"; } // Use TestCleanup to run code after each test has run [TestCleanup()] public void MyTestCleanup() { if (_application != null) { _application.Close(); } Playback.Cleanup(); } [TestMethod] public void TestMethod1() { ClickButtonByText(_window, "1"); ClickButtonByText(_window, "2"); ClickButtonByText(_window, "3"); ClickButtonByText(_window, "+"); ClickButtonByText(_window, "3"); ClickButtonByText(_window, "2"); ClickButtonByText(_window, "1"); ClickButtonByText(_window, "="); } private void ClickButtonByText(UITestControl window, string text) { UITestControl control = new UITestControl(window); control.TechnologyName = "MSAA"; control.SearchConfigurations.Add(SearchConfiguration.VisibleOnly); control.SearchProperties.Add(WinProperties.Button.ControlType, "Button"); control.SearchProperties[WinProperties.Button.Name] = text; System.Console.WriteLine(control); Mouse.Click(control, control.GetClickablePoint()); } } }
The exception stack trace:Microsoft.VisualStudio.TestTools.UITesting.UITestControl.GetPropertyValue(String propertyName) Microsoft.VisualStudio.TestTools.UITesting.UITestControl.GetProperty(String propertyName) Microsoft.VisualStudio.TestTools.UITesting.UITestControl.GetPropertyInternal[T](String propertyName) Microsoft.VisualStudio.TestTools.UITesting.UITestControl.get_BoundingRectangle() Microsoft.VisualStudio.TestTools.UITesting.UITestControl.CaptureImage() Microsoft.VisualStudio.TestTools.UITesting.Playback.CaptureScreenShot(UITestControl control) Microsoft.VisualStudio.TestTools.UITesting.Playback.GetUITestControlString(UITestControl control) Microsoft.VisualStudio.TestTools.UITesting.Playback.AddUITestControlDescriptionToException(SystemException exception, IPlaybackContext context) Microsoft.VisualStudio.TestTools.UITesting.Playback.MapAndThrowException(SystemException exception, IPlaybackContext context) Microsoft.VisualStudio.TestTools.UITesting.Playback.MapAndThrowException(SystemException exception, String actionName, UITestControl uiControl) Microsoft.VisualStudio.TestTools.UITesting.Playback.MapAndThrowException(SystemException exception, String actionName, Object parameterValue, UITestControl uiControl) Microsoft.VisualStudio.TestTools.UITesting.UITestControl.GetPropertyValue(String propertyName) Microsoft.VisualStudio.TestTools.UITesting.UITestControl.GetProperty(String propertyName) Microsoft.VisualStudio.TestTools.UITesting.UITestControl.GetPropertyInternal[T](String propertyName) Microsoft.VisualStudio.TestTools.UITesting.UITestControl.get_BoundingRectangle() Microsoft.VisualStudio.TestTools.UITesting.UITestControl.GetClickablePoint() TestProject1.GUITest.ClickButtonByText(UITestControl window, String text) in d:\documents\visual studio 2010\Projects\TestProject1\TestProject1\GUITest.cs: line 76 TestProject1.GUITest.TestMethod1() in d:\documents\visual studio 2010\Projects\TestProject1\TestProject1\GUITest.cs: line 59
All Replies
- Follow the guidance in Balagans's website . Basically, generate the code using the Recorder and tear it out. The Recorder is good at finding and modeling the controls. If you want to see the structure of the projects I am working on, I can paste them.
- Well, I tried the same source code to test the calculator in Windows XP and it works fine. However, it doesn't work with the calculator in Windows 7, and I don't know why? Is it because the structure of UI different in these two versions of calculator?

