UI Automation Testing with Visual Studio

After playing for a few weeks testing Windows Form applications, I would like to give you some tips and tricks:

  1. You can use Visual Studio 2010 or above to test any .NET framework application. Just create a new Test Project, add a Coded UI Test and you are ready to test .NET 1.0, 1.1, 2.0, 3.5, 4.0 and above applications.
  2. Never edit the file called UIMap.Designer.cs – That file is re created again when you record a test. You don’t need to change anything there.
  3. Try to name your recorded tests and your recorded asserts with a naming convention. I prefer to start the recorded tests with RecordedMethod… like RecordedMethodCreateOrder. Similar thing with asserts: AssertMethod… like AssertMethodOrderCreated
  4. After you have recorded a method called RecordedMethodCreateOrder, copy and paste that code in the partial class UIMap.cs. Rename it to something more useful like CreateOrder. You should change the code in this method that is not overwritten by the recorder.
  5. The Test View window is a must have panel to launch your desired tests. You can select them all to run or debug, or simply some of them
  6. Try to organize your TestMethods in independent classes based on your domain.
  7. It is always a good idea to have one Utility class with all common code that you use in your tests.
  8. Avoid duplicated code in UI Tests and in your application!
  9. Sometimes gets tricky to access or to select some controls. This set of utility functions for Coded UI test will help you. This was a life saver trying to work with WinListItem and WinList controls. Find them here

Enjoy!

VB2005 Tips and Tricks – part 10

The Wonderful World of Snippets

Type ? and hit your TAB key and you will get them.

If you know the first letters of your snippet, like for example “property”, type “prop?” and then hit the TAB key. The snippet that creates a property in a class will be launched.

Here are the most used snippets:

Application – Compiling, Resources and Settings -> Load an Assembly at Run Time; appLoad
Application – Compiling, Resources and Settings -> Write to a Text File; appLog
Collections and Arrays -> Iterate through a collection; colIter
Collections and Arrays -> Locate an Element in an Array; arrLoc
Connectivity and Networking -> Create an E-Mail Message; conEmail
Data – Designer features and ADO.NET -> Create a Server based SQLConnection to SQL Server; adoCreateSqlConn
Data – Designer features and ADO.NET -> Create a Parameterized SELECT Command; adoSelectwParams
Data Types – defined by Visual Basic -> Convert an Array of Bytes into a String; typeByteStr
Data Types – defined by Visual Basic -> Parse an E-Mail Address; typeParseEmail
File System – Processing Drives, Folders and Files -> Copy a File; filCopyFile
File System – Processing Drives, Folders and Files -> Create a File; filCreateFile
File System – Processing Drives, Folders and Files -> Read Text from a File; filReadText
File System – Processing Drives, Folders and Files -> Write Text to a File; filWriteText
Math -> Get a Random Number using the Random Class; mathRand
Security -> Encrypt a String; secEncrypt
Security -> Find the Current User’s Name; secUser
Windows Forms Applications -> Forms -> Determine which Control on a Windows Form is selected; formSelect
Windows Forms Applications -> Forms -> Display a Windows Form; formNew
Windows Forms Applications -> Controls and Components -> Add Menu Items to Windows Forms; menuAddItems
Windows Forms Applications -> Sound -> Play a Sound; sndPlay
Windows Operation System -> System Information -> Determine Desktop Display Resolution; sysRes
Windows Operation System -> System Information -> Determine the Windows System Directory; sysDir
XML -> Iterate named nodes in XML Document; xmlIter
XML -> Read XML from a File Using XmlTextReader; xmlReadTxt

VB2005 Tips and Tricks – part9

XML Comments

To use NDoc v. 1.3 with .NET Framework 2.0 do the following:

  1. Copy the bin\net\1.1 and rename it to 2.0
  2. In 2.0 folder, create a NDocGui.exe.config with:

<configuration>
   <startup>
      <supportedRuntime version=”v2.0.50727″ />
   </startup>
</configuration>

That’s all!. If you want to run the NDocConsole.exe just create another NDocConsole.exe.config with the same txt and you are done.

If you want to use the full power of XML Comments in VB, check Recommended XML Tags for Documentation Comments

VB2005 Tips and Tricks – part 8

Use Operators with Custom Objects – Overloading

 

Public Shared Operator + (objA as myClass, objB as myClass ) as MyClass
 'code
End Operator
ObjC = ObjA + ObjB
 

VB2005 Tips and Tricks – part 7

Make Simple Data Types Nullable

 

dim i as Nullable(Of Integer)
If not i.HasValue then
 Console.WriteLine("i is a null value")
End If

i=100
If i.HasValue then
 Console.WriteLine("Nullable integer i = " & i.Value)
End If

VB2005 Tips and Tricks – part 6

Build Typesafe Generic Classes

Example 1
Public Class GenericList(Of ItemType)
 'Some Code
End Class

Example 2
Public Class GenericList(Of Item Type)
Inherits CollectionBase

Public Function Add(ByVal value as ItemType) as Integer
 Return List.Add(value)
End Function

Public Sub Remove(ByVal value as ItemValue)
 List.Remove(value)
End Sub

Public ReadOnly Property Item(ByVal index as Integer) as ItemType
 Get
  Return CType(List.Item(index),ItemType)
 End Get
End Property

End Class
Program Code
Dim List as New GenericList(Of String)
List.Add("blue")
List.Add("red")

List.Add(Guid.NewGuid()) ' Will be detected by the compiler
Example 3 - More than one type parameter in the class
Public Class GenerichashTable(Of ItemType, KeyType)
 Inherits DictionaryBase
 'Code
End Class
 
Example 4 - Define Constraints to Parameters
Public Class SerializableList(Of ItemType as ISerializable)
 Inherits CollectionBase
 'Code
End Class

Public Class ControlCollection(Of ItemType as Control)
 Inherits CollectionBase
 'Code
End Class

If you want your class to create an instance of the Item
Public Class GenericList(Of ItemType as New)
 Inherits CollectionBase
 'Code
End Class

Example 5 - Define more than one constraint
Public Class GenericList(Of  ItemType as {ISerializable, New})
 Inherits CollectionBase
 'Code
End Class

Generics also works with structures, interfaces, deletates and methods.
 

VB2005 Tips and Tricks – part 5

Use Strongly Typed Configuration Settings

Double Click the My Project node in the Solution Explorer and select the Settings tab. Add the Name, Type, Scope and Value of your setting.

From your code, type:

dim aSetting as string
aSetting=My.Settings.YourSettingName

Updating User Settings

If you select User from the Scope drop-down list, you can modify that setting in runtime. The modify your setting and save it like this:

MySettings.YourSettingName = “NewValue”
MySettings.Save()

This will create a file under Documents and Settings\[UserName]\Local Settings\Application Data\[Application Name]\[Unique Directory] for the current user only with the selected value.

VB2005 Tips and Tricks – part 4

Use Strongly Typed Resources

  1. Double click My Project, Resources, select type images, and add an image. Change the resource name to MyImage
  2. Add a picture box or any other control that supports images and type: picturebox1.Image = My.Resources.MyImage

This will embed the image in your assembly. If you want to modify the image, replace the image inside the Resource Directory and recompile. There is no need to modify the code.

This approach is better than the ImageList because ImageList does not support typed resources. However, there are controls that do no support it and you have to fill the imagelist with the resource images in your code if you want to use it in this way.