Using DotNetFactory Utility Object in QTP

[This post was originally written in March 2007]

Using DotNetFactory object you can access .NET base classes into your QTP scripts. This also supports using DLLs compiled from any .NET language. This features works on .NET Interop which means you can call .NET components in COM environment and vice versa.

Using ArrayList Class

Here is an example where we have a test scenario which requires sorting and searching of an array of values. We will use ArrayList class from System.Collections namespace. Collections namespace has variety of classes like Array, HashTable, Stack, LinkedList etc.

ArrayList class is enriched with various ready to use methods for Sorting, Reversing, and Searching items.

'//Create an instance of System.Collections.ArrayList using DotnetFactory.CreateInstance method

Set myList = DotnetFactory.CreateInstance("System.Collections.ArrayList")

'//Add items to the List Array

'//ArrayList.Capacity/ArrayList.Count shows the number of elements it have

Print "Capacity of ArrayList is: " & myList.Capacity
Print "Count of ArrayList is: " & myList.Count

'//ArrayList adds empty memory locations when new items are added to the list
'//using TrimToSize you can remove these empty locations from the list

'//You can sort the contents of an ArrayList using Sort method, for Ascending sort:


'//For displaying array contents in a String
strMsg = ""
For intCnt = 0 To myList.Count - 1
 strMsg = strMsg & myList.Item(CInt(intCnt)) & vbCrLf

Print "Sorted Array List: Asecending" & vbCrLf & strMsg

'//You can Sort an array Descending with Reverse method. But first you need to sort the list
'//using Sort method


'//For displaying array contents in a String
strMsg = ""
For intCnt = 0 To myList.Count - 1
 strMsg = strMsg & myList.Item(CInt(intCnt)) & vbCrLf

Print "Sorted Array List: Descending" & vbCrLf & strMsg

'//You can search an item using IndexOf and BinarySearch method. These method return zero based index
'//of an item for its first occurrence

Print "Indian item found at: " & myList.IndexOf("Indian") & vbCrLf & strMsg,,"IndexOf Search"
Print "indian item found at: " & myList.IndexOf("indian") & vbCrLf & strMsg,,"IndexOf Search"
'//For BinarySearch ArrayList should be sorted in Ascending Order

'//For displaying array contents in a String
strMsg = ""
For intCnt = 0 To myList.Count - 1
 strMsg = strMsg & myList.Item(CInt(intCnt)) & vbCrLf

Print ,"Binary Search: " & vbCrLf & "bElarusian item found at: " & myList.BinarySearch("bElarusian") & vbCrLf & strMsg
Print ,"Binary Search: " & vbCrLf & "Belarusian item found at: " & myList.BinarySearch("Belarusian") & vbCrLf & strMsg

'//ArrayList.Contains method searches for given item and returns True/False
Print myList.Contains("Indian")

Set myList = Nothing

This approach is much faster and reliable than writing code our own code.

Using Clipboard Object

You can access Clipboard object using System.Windows.Forms.Clipboard class. Here is an example showing how to Clear, Set and Get contents from Clipboard.

'//This is not supported in 8x versions

Set oClip = DotNetFactory.CreateInstance("System.Windows.Forms.Clipboard")

'//Clears the Clipboard

'//Sets sample text on Clipboard
oClip.SetText "DotNetFactory Rocks!!"

'//Retrieves text from Clipboard
strContents = oClip.GetText

Print strContents

File Input/Output

Using System.IO.File class you can create, read and write to files. Here is an example on writing to and reading from a file.

'//This is not supported in 8x versions

Set objFile = DotNetFactory.CreateInstance("System.IO.File")

'//Creates a new file with given text
objFile.WriteAllText "C:\Test.txt","DotNetFactory Rocks!!"

'//Retrieves text from given file
strContents = objFile.ReadAllText("C:\Test.txt")

Print strContents

This way you can use 100s of classes available in .NET Framework in your QTP Scripts.

Recording Screencast of TestComplete Test Runs

In this post we’ll explore the possibility of recording a screencast of TestComplete test runs, the other test tool I use extensively at work.

TestComplete does not have features to record screencast of test execution. However we can use Microsoft Expression Encoder 4 SDK to record videos programmatically from test scripts.

A recoded screencast of test run can help you in investigation of unattended execution or even demo the software or create training videos.

Microsoft Expression Encoder 4 has basic recording features with limited codec support. You can download it free from

While the commercial version Microsoft Expression Encoder 4 Pro provides number of additional features and codecs.

I will use the CLR Bridge & dotNET object available in TestComplete to call the Encoder SDK functions to create the screencast.

Using CLR Bridge in TestComplete we can use .NET assemblies in test scripts and extend the test script code to use features from standard .NET classes as well as custom libraries.

Setting up Encoder SDK in CLR Bridge

Let’s add the Encoder SDK library to the CLR Bridge in TestComplete Project Settings by following steps:

1. Go to TestComplete Project Setting by selecting Current Project Settings from Tools Menu
2. Select CLR Bridge option on left side of Project Properties Pane
3. Click Browse Files button to select the Encoder SDK DLL file. This will display Select Assembly dialog box
4. On Select Assembly dialog navigate to C:\Program Files\Microsoft Expression\Encoder 4\SDK folder and select the Microsoft.Expression.Encoder.dll file

This will add Encoder library to the TestComplete Project and provide access to the internal classes and functions using the dotNET object.

Using ScreenCaptureJob Class in TestComplete Script

Let’s create a instance of ScreenCaptureJob class from the Encoder SDK and call methods to start & stop video recording:

Sub NotePadEditCutTest()
	Dim wndNotepad, objScreenCaptureJob

	'Create a new ScreenCapture Job using it's Constructor
	Set objScreenCaptureJob = dotNET.Microsoft_Expression_Encoder_ScreenCapture.ScreenCaptureJob.zctor

	'Specify the path & name of the Output File
	objScreenCaptureJob.OutputScreenCaptureFileName = "C:\Data\NotePadEditCutTest.wmv"

	'Start the Video Recording using ScreenCaptureJob's Start Method
	Call objScreenCaptureJob.Start()

	'Test Code
	Call TestedApps.notepad.Run(1, True)
	Set wndNotepad = Aliases.notepad.wndNotepad
	Call wndNotepad.Edit.Keys("SOme Text")
	Call wndNotepad.MainMenu.Click("Edit|Select All")
	Call wndNotepad.MainMenu.Click("Edit|Cut")
	Call aqObject.CheckProperty(Aliases.notepad.wndNotepad.Edit, "wText", cmpEqual, "")

	'Stop the Video Recording using ScreenCaptureJob's Stop Method
	Call objScreenCaptureJob.Stop()
End Sub

The output screencast will be created in wmv format. This screencast can be edited in Expression Encoder UI.