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
myList.Add("American")
myList.Add("Ukrainian")
myList.Add("bElarusian")
myList.Add("Indian")
myList.Add("123")
myList.Add("$@%^%@")

'//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
myList.TrimToSize

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

myList.Sort

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

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

myList.Reverse

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

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

'//SEARCHING
'//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
myList.Sort

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

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
oClip.Clear

'//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.