Ask a questionAsk a question
 

QuestionIs there any tutorial to teach writing coded UI test by hand without a builder?

  • Sunday, November 08, 2009 2:08 AMSpirit Du Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    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