What to do when my CUIT thows a PlaybackFailureException

Updated (Oct 11, 2011) Problem: I have a textbox that has a maximum length of five characters. I want to record a Data Driven CUIT to test that you cannot type in more than five characters.  However, when I attempt to set the textbox to a six character value the CUIT throws a PlaybackFailureException. Solution: Simply set Playback.PlaybackSettings.SkipSetPropertyVerification = true; before the call that throws the PlaybackFailureException and return it to false after. Explanation: After setting a property of any UI control, the record and playback engine performs a verification step to make sure that the set succeeded and the UI control now has the value it attempted to set it to. For example if you have a text box that only allows 5 characters and you attempt to set it to 6 characters the engine will throw a PlaybackFailureException. If you are trying to test that if I actually type 123456 that the value is 12345 you will have to set Playback.PlaybackSettings.SkipSetPropertyVerification = true; before your test attempts to fill in the value.

CUIT Demo of Feature Pack 2 Coded UI Test Editor

Problem: I have a Coded UI Test that is failing on Playback. Solution: Use the Coded UI Test Editor in Feature Pack 2 to adjust the UI Map and add actions to your test. Explanation: I felt a write up would be too hard to follow so I recorded a video instead which you can watch below. In this video we are going to cover a few features of the Coded UI Test Editor available in Feature Pack 2 for Visual Studio 2010. The Coded UI Test Editor greatly improves an automation engineers experience working with Coded UI Test. The features we are going to demonstrate today are splitting actions into separate methods, renaming methods and locating UI controls in a running application. Many coded UI demos show you the happy path scenario where everything works perfectly.  While helping clients implement Coded UI Test in the real world you quickly realize that the happy path can be hard to stay on.  The goal of this web cast is to show a recording that does not work as recorded and techniques we can use to adjust the recording to yield the desired results.

Having trouble testing my WPF app with Coded UI Test

Problem: I cannot find my WPF TextBlock using the Coded UI test because the value is data bound and changes. Solution: Set the Name attribute on the controls of your View which sets the AutomationId. Explanation: Many WPF developers only place the Name attribute on items they intend to access from a code behind.  With most WPF developers using MVVM and trying to keep their Views as light as possible there would be no need to use the Name attribute.  However, if you intend to use Microsoft's new Coded UI test to test your view the Name attribute is important. The Coded UI test uses the Accessibility Framework to locate the controls on the screen.  The more distinct attributes the item has the more likely the test will be able to locate that control during playback. The Name attribute on WPF controls sets the AutomationId and allows it to be used to locate the control.  This is important when the value of for example a TextBlock changes.  If the Name attribute is not set the actual content of the TextBlock which is going to be changing during execution will make it almost impossible to locate that control during playback

How do I validate data in my database during a web test

Problem: The only way I can verify the success of my web test is to read a value from a database. Solution: Create a custom validation rule that can validate the value in the database. Code: using System; using System.ComponentModel; using System.Data.SqlClient; using Microsoft.VisualStudio.TestTools.WebTesting; namespace TestUtil {    [DisplayName("SQL Validation Rule")]    [Description("Executes the query and compares the first column of the first row to the Expected value.")]    public class SQLValidationRule : ValidationRule    {       public SQLValidationRule()       {          IgnoreCase = true;       }       [Description("The query to execute and extract the first column from. In Select * from x where column={0}. You can leave the where clause if it is not needed.")]       public string Query { get; set; }       [DisplayName("Connection String")]       [Description("The full connection string to the database")]       public string ConnectionString { get; set; }       [DisplayName("Where Clause Context Parameter Name")]       [Description("The name of an optional context parameter to use if there is a where clause in the query.")]       public string WhereClauseContextParameterName { get; set; }       [DefaultValue(true)]       [DisplayName("Ignore Case")]       [Description("When set to true the case of the word is not used")]       public bool IgnoreCase { get; set; }       [DisplayName("Expected Value")]       [Description("The value to compare the first column too.")]       public string ExpectedValue { get; set; }       public override void Validate(object sender, ValidationEventArgs e)       {          string where = null;          if(!string.IsNullOrEmpty(WhereClauseContextParameterName))             where = e.WebTest.Context[WhereClauseContextParameterName].ToString();          string result = ExecuteQuery(ConnectionString, Query, where);          e.IsValid = string.Compare(result, ExpectedValue, IgnoreCase) == 0;       }       private string ExecuteQuery(string connectionString, string query, string where)       {          SqlConnection conn = new SqlConnection(connectionString);          string cmdText = string.Format(query, where);          SqlCommand cmd = new SqlCommand(cmdText, conn);          try          {             conn.Open();             SqlDataReader dr = cmd.ExecuteReader();             if(dr.Read())                return dr.GetValue(0).ToString();          }          catch(Exception e)          {             System.Diagnostics.Debug.WriteLine(e.Message);          }          finally          {             conn.Close();          }          return null;       }    } } Explanation: Creating a custom validation rule for web test is extremely simple.  Simply create a new public class that derives from Microsoft.VisualStudio.TestTools.WebTesting.ValidationRule and override the Validate method.  If you create the class in your test project it will become immediately available the next time you try to add a validation rule to a web request.  If you created the class in a separate class library simply add a reference to that class library in your test project. This particular validation rule has the five following properties: Query – This is the query to be executed on the database connection.  Only the first column of the first record is used in the comparison of this validation rule.  The query can have a single where condition in the where clause that uses a context parameter value.  For example “Select Name from Table1 where ID={0}”.  At runtime the validation rule will look up the value of the provided context parameter and replace {0} with the value stored in the context parameter.  Using a context parameter is completely optional. Connection String – The connection string to a SQL Server database. This can also be a context parameter entered in {{ContextParameterName}} format.  Otherwise you may simple enter a literal string. Where Clause Context Parameter Name – The context parameter to be used to replace the {0} if any of the query. Ignore Case – Identifies if case should be ignored or not during the string comparison. Expected Value – The value to compare the first column of the first row too.  This can also be a context parameter entered in {{ContextParameterName}} format.  Otherwise you may simple enter a literal string. You can download the file below. SQLValidationRule.cs (2.48 kb)

How to change the display name of my Find Text validation rule

Problem I can’t easily tell what my Find Text validation rule is searching for in my Web Test. Solution Change the DisplayName value in the Web Test xml to something more meaningful. From Solution Explorer right click on the Web Test and select Open With… then select XML (Text) Editor and click OK.  Once the file is open search the file for DisplayName="Find Text".  Replace Find Text with something more meaningful.  Save the file and close it.  Now right click on the Web Test from Solution Explorer and select Open With… and select the Web Test Editor (Default).  Now expand the Validation Rules folder under the desired web request.  The Find Text validation rule label will be the value you typed in. Explanation For some reason the creator of this validation run did not expose the DisplayName property so it could be change from the Properties Window.  However, the value is there and can be changed from the xml file.  This is very helpful when you have several Find Text validation rules on the same request.

How to Xcopy deploy using TFS 2010/2012

Problem I need my VS2010/VS2012 build to perform an “Xcopy deployment” of my ASP.NET application to an existing virtual directory. Solution Customize the build template to use the CopyDirectory activity to copy the ASP.NET application to the virtual directory. Explanation One of the benefits of ASP.NET development is the simply “Xcopy deployment”. ASP.NET applications require no changes to the registry and have no special installation requirements for the hosting server.  Therefore, you can use the drag-and-drop feature in Microsoft Windows Explorer, File Transfer Protocol (FTP), or the DOS Xcopy command to copy files from one computer to another. The only prerequisite of this technique is the virtual directory in IIS must already be created and configured. The goal of this build is to not have to install any special features or extensions in IIS to facilitate deployment of my ASP.NET application.  I want my environments to match production as close as possible and I never intend on installing IIS Extensions in production. When you configure a build definition that builds and ASP.NET application the binaries directory and drop location contain a folder named _PublishedWebsites.  Each ASP.NET application built during the build will have a sub directory that contains all the files needed for the application. To perform an “Xcopy deployment” we simply need to identify the source and destination directories.  We are going to store this information in arguments passed to the build.  To begin open the DefaultTemplate.xaml file in VS2010 and click the Arguments button at the bottom of the workflow designer to show the workflow arguments.  Add two string arguments  VDir and SiteDir. Now let’s add a nice coat of polish on our arguments.  Click the ellipses next to the default value of the Metadata argument to show the Process Parameter Metadata Editor window.  Click the Add button and enter in the following information and click OK. Parameter Name – VDirDisplay Name – Virtual DirectoryCategory – DeployDescription – The full UNC path to the virtual directory to copy the website too. Editor – leave blankRequired – leave unchecked View this parameter when – Always show the parameter and Parameter Name – SiteDirDisplay Name – Site DirectoryCategory – DeployDescription – The sub directory of _PublishedWebsites to copy from.Editor – leave blankRequired – leave uncheckedView this parameter when – Always show the parameter   We are simply going to add a CopyDirectory activity that uses the arguments we just created to perform the copy.  To begin we must locate the correct area of the build template to add our CopyDirectory activity.  I find the quickest way to do this is to click the Collapse All button at the top of the workflow designer window.  Now double click on the words Double-click to view on all of the following activities: 1.    Run On Agent2.    Try Compile, Test, and Associate Changesets and Work Items 3.    Sequence4.    Compile, Test, and Associate Changesets and Work Items5.    Try Compile and Test6.    Compile and Test7.    For Each Configuration in BuildSettings.PlatformConfigurations8.    Compile and Test for Configuration When we configured the metadata for our arguments we left the Required checkbox unchecked.  This will allow users of this build template to leave the values for VDir and SiteDir blank if they are not building an ASP.NET application or simply do not want to deploy them.  Therefore, we need to check the value of the arguments to determine if we need to perform the copy or not.  In the toolbox expand the Control Flow tab and drag and drop the If activity right above the If Not Disable Test activity.  Click the double down arrows in the title bar of the if to show its contents. In the Condition text box enter the following: Not String.IsNullOrWhiteSpace(VDir) From the toolbox drag and drop a Sequence activity from the Control Flow tab onto the Then side of the If.  Change the DisplayName of the sequence to Deploy ASP.NET Application. We add a sequence here so that we can use multiple activities. Click the double down arrows in the title bar of the sequence to show its contents.  This is where we are going to add the activities needed to copy the ASP.NET application to the virtual directory in IIS. Now we can add the CopyDirectory activity and set the properties to deploy our ASP.NET application during the build.  To get started simply drag and drop the CopyDirectory activity from the Team Foundation Build Activities tab into the Deploy ASP.NET Application sequence.  With the CopyDirectory activity selected set the following values in the properties window: •    Destination – Vdir•    Source  - BinariesDirectory + "\_PublishedWebsites\" + SiteDir Now save your xaml file, check in your changes and queue a new build. You can download a copy of the final file below. TFS2010 DeployTemplate.xaml (55.16 kb) TFS2012 DeployTemplate.11.1.xaml (75.68 kb)

I get an unresolved reference to the object [#aspnet_Permissions]

Normal 0 false false false EN-US X-NONE X-NONE Problem When I import a database that uses ASP.NET Providers I get an unresolved reference to the object [#aspnet_Permissions]. Solution Modify the script to define the shape of the temp table by adding the additional SQL above the declaration of the cursor. IF(OBJECT_ID('tempdb.#aspnet_Permissions') IS NULL) BEGIN CREATE TABLE #aspnet_Permissions  ( Owner     sysname,  Object    sysname,    Grantee   sysname,    Grantor   sysname,    ProtectType char(10),    [Action]    varchar(60),    [Column]    sysname ) END Or just download the file below. Explanation The "#aspnet_Permissions" temp table must be created by ASPNET at the same time as the membership database is created. Because the table is not created in this script or referenced by this project the table appears as an external reference that DBPro cannot resolve. By adding the code the table can be resolved by the DBPro project and will not create the table if it already exist on the database server you are deploying too. aspnet_Setup_RestorePermissions.proc.sql (1.14 kb)

sysobject warnings in my DBPro projects

Problem My DBPro project references sysobjects and is causing build warnings for example: SQL04151: Procedure: [dbo].[aspnet_AnyDataInTables] has an unresolved reference to object [dbo].[sysobjects].    Solution Add the following DB reference to your database project: C:\Program Files (x86)\Microsoft Visual Studio 10.0\VSTSDB\Extensions\SqlServer\2008\DBSchemas\master.dbschema If you are targeting 2005 SQL Server use: C:\Program Files (x86)\Microsoft Visual Studio 10.0\VSTSDB\Extensions\SqlServer\2005\DBSchemas\master.dbschema

How to debug a Visual Studio Extension

Problem I am writing a class library to extend Visual Studio but I can't figure out how to debug it. Solution Right click on the class library project in Solution Explorer and select properties.  This will display the property pages for the selected project.  Click on the Debug tab on the left hand side.  Under the Start Action section select the second radio button Start External Application and browse to the exe you need to start. For Visual Studio extensions find the devenv.exe version you need.  Under the Start Options selection add the following Command line arguments:/rootsuffix ExpNow when you press F5 to debug your class library the debugger will launch another instance of Visual Studio to load your class library and allow you to debug it. Explanation Class libraries don't execute themselves and require an executable to load the library so we can debug it.  Therefore, we must instruct the debugger on which application to load to host our class library so we can debug it.The command line arguments we pass for a Visual Studio extension makes sure that we don’t corrupt our registry on our machine.  That argument tells the second instance of visual studio to use an experimental hive in the registry to make any needed registry changes.  This leaves our real registry clean.While developing an extension for Visual Studio there may become a time where the debugger has an issue starting the second instance of Visual Studio. One cause of this issue is a corrupted experimental hive.  You can execute Visual Studio from the command line passing in the following arguments to have it restore your experimental hive:devenv.exe /setup /rootsuffix Exp /ranu

How to reach nested controls using Coded UI Test Builder

Problem I have a span nested inside an anchor tag and I cannot reach it with CodedUI Test Builder. Solution Use the control navigation button in the Add Assertions dialog.   Explanation While trying to check the attributes on a link in a web application I could never see the font style to determine if it was bold or not.  After further investigation I realized the style I was looking for was not on the link but actually on the span inside the link.  However, whenever I used the cross-hair icon I was only able to select the hyperlink and never the span.  Then I noticed that there is a button in the Add Assertions dialog on the far right that allows you to move to other controls relative to the one currently selected.  Once I clicked the down arrow on the button I was moved to the nested span inside the hyperlink.