QTP -30 Working with .NET Classes
Concept: Using DotNetFactory object (utility object) to get DotNET built-in libraries.
30.1 Using the DotNetFactory object
The DotNetFactory object has only method and also as defaultmethod: “CreateInstance” Method.
“CreateInstance” Methodhas the third option parameter: arguments.
Set var_CreateInstance = DotNetFactory.CreateInstance("System.Windows.Forms.Form", "System.Windows.Forms") |
30.2 Passing parameters to Class Constructors
The following code will meet “PasswordChar not found” error.
Set oTextBox = DotNetFactory("System.Windows.Forms.TextBox","System.Windows.Forms") oTextBox.PasswordChar = "*" Set oTextBox = Nothing |
Because Password char Property need .NET character parameter,not VBS string parameter. So here is the solution:
oTextBox.PasswordChar = CByte(Asc( "*")) |
30.3 Working with the Clipboard
Set Obj = DotNetFactory("Microsoft.VisualBasic.Devices.Computer","Microsoft.VisualBasic") print "Clipboard = " & Obj.Clipboard.GetText() Obj.Clipboard.Clear()
Obj.Clipboard.SetText "added text" print "Clipboard = " & Obj.Clipboard.GetText() |
30.4 Getting Computer Information
'Get the .NET Computer object Set Obj = DotNetFactory("Microsoft.VisualBasic.Devices.Computer","Microsoft.VisualBasic")
'Get the computer info object Set compInfo = Obj.Info
'Print details about system Print "AvailablePhysicalMemory - " & compInfo.AvailablePhysicalMemory Print "AvailableVirtualMemory - " & compInfo.AvailableVirtualMemory Print "OSFullName - " & compInfo.OSFullName Print "OSPlatform - " & compInfo.OSPlatform Print "OSVersion - " & compInfo.OSVersion Print "TotalPhysicalMemory - " & compInfo.TotalPhysicalMemory Print "TotalVirtualMemory - " & compInfo.TotalVirtualMemory
'Destroy the object Set Obj = Nothing |
30.5 Playing a wav file
Set Obj = DotNetFactory("Microsoft.VisualBasic.Devices.Audio","Microsoft.VisualBasic")
Obj.Play "C:\WINDOWS\Media\Shutdown.wav"
'Destroy the object Set Obj = Nothing |
30.6 Accessing the Registry
'Get the .NET Computer object Set Obj = DotNetFactory("Microsoft.VisualBasic.Devices.Computer","Microsoft.VisualBasic")
Set Registry = obj.Registry IEPathKey = "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\IEXPLORE.EXE"
'Get the path of iexplore.exe from registry Print "Internet explore location - " & Registry.GetValue(IEPathKey, "","")
'Set a value in registry Registry.SetValue "HKEY_CURRENT_USER\Software\MyApp", "Path", "C:\Test.exe" |
30.7 Ping an IP Address
Set obj = DotNetFactory("Microsoft.VisualBasic.Devices.Network","Microsoft.VisualBasic")
'Max wait for 10 sec for ping pingSuccess = obj.Ping ("127.0.0.1" ,10000) ‘return Boolean value |
30.8 Evaluating keyboard Control Key Status
Set obj = DotNetFactory("Microsoft.VisualBasic.Devices.Keyboard","Microsoft.VisualBasic") If obj.AltKeyDown Then print “The key is keyed” |
30.9 .Net Arrays Revisited
Set arrArray = DotNetFactory("System.Collections.ArrayList","System")
With arrArray 'Add few elements .Add "e" .Add "b" .Add "c" .Add "a" .Add "d"
'Get the Index of A: 3 Print "Index of A - " & .IndexOf("a")
.Sort Print "Index of A after Sorting array - " & .IndexOf("a") ‘0
.Reverse Print "Index of A after reversing array - " & .IndexOf("a") ‘4
Print "Value at index 1 - " & .Item(1) ‘d End With
Set arrArray = Nothing |
30.10 Working with .NET Stacks
Set Stack = DotNetFactory("System.Collections.Stack","System")
'Add objects to Queue Stack.Push "Tarun" Stack.Push "Varun"
x = Stack.Pop 'Prints Varun Print x
x = Stack.Peek 'Prints Tarun Print x
x = Stack.Pop 'Prints Tarun Print x
Set Stack = Nothing |
30.11 Working with .NET Queues
Set Queue = DotNetFactory("System.Collections.Queue","System")
'Add objects to Queue Queue.Enqueue "Tarun" Queue.Enqueue 1
x = Queue.Dequeue 'Prints Tarun Print x
x = Queue.Peek 'Prints 1 Print x
x = Queue.Dequeue 'Prints 1 Print x
Set Queue = Nothing |
30.12 Working with .NET Date and Time Formatting
Set DateTime = DotNetFactory ("System.DateTime")
'GMTTime Print "GMT Time (MM/dd/yyyy hh:mm tt) - " & DateTime.Now.ToUniversalTime.toString("MM/dd/yyyy hh:mm tt")
'Current Time Print "Date (dd-MMM-yyyy) - " & DateTime.Now.toString("dd-MMM-yyyy") Print "Date (dddd, dd-MMMM-yyyy) - " & DateTime.Now.toString("dddd, dd-MMMM-yyyy") Print "DateTime (dd-MMM-yyyy hh:mm:ss tt) - " & DateTime.Now.toString("dd-MMM-yyyy hh:mm:ss tt")
Set DateTime = Nothing |
30.13 Sending Emails using .NET
Set oSmptClient = DotNetFactory("System.Net.Mail.SmtpClient", "System") oSmptClient.Host = "GVW1343EXA.americas.hpqcorp.net" oSmptClient.Port = "110"
oSmptClient.Send “from@where.com”, “to@where.com” , “Subject”, “Body” |
Some Complex emails:
Set oMailMessage = DotNetFactory("System.Net.Mail.MailMessage")
'Set the From mail address with a display name also
Set oMailMessage.From = DotNetFactory("System.Net.Mail.MailAddress","System","xing-xing.tan@hp.com", "Frank Tan")
'Add a recipient to TO list with email address only oMailMessage.To.Add DotNetFactory("System.Net.Mail.MailAddress","System","xing-xing.tan@hp.com")
'Add a recipient to CC list with mail address and display name oMailMessage.CC.Add DotNetFactory("System.Net.Mail.MailAddress","System","xing-xing.tan@hp.com","I am someone") 'Add a recipient to BCC list oMailMessage.BCC.Add DotNetFactory("System.Net.Mail.MailAddress","System","xing-xing.tan@hp.com") 'Subject line of the email oMailMessage.Subject = "Test Mail from QTP" 'Body of the email oMailMessage.Body = "<h1>Test E - Mail from QTP" 'Mark the body as a HTML type oMailMessage.isBodyHTML = True 'Add and attachment to the email oMailMessage.Attachments.Add DotNetFactory("System.Net.Mail.Attachment","System","C:\1.txt") oMailMessage.Priority = oMailMessage.Priority.High
oSmptClient.Send oMailMessage ‘oSmptClient is object created above |
30.14 Covering Images to other file formats:
Set oImageLib = DotNetFactory.CreateInstance("System.Drawing.Image") Set oImageFormats = DotNetFactory.CreateInstance ("System.Drawing.Imaging.ImageFormat","System.Drawing", Nothing) oImage.Save "C:\Users\Public\Pictures\QTP.gif", oImageFormats.gif ‘ Or use other format: oImageFormats.Bmp etc 'Destroy the image oImage.Dispose |
30.15 Using .NET Forms to get user Input:
注释:VBS的图形化编程不是重点。下面是最简单Form的生成。
Set oForm = DotNetFactory("System.Windows.Forms.Form","System.Windows.Forms")
With oForm 'Title of the form .Text = "Enter environment details"
'Set the startup position a CenterScreen .StartPosition = .StartPosition.CenterScreen
'Set the form style as fixed tool window .FormBorderStyle = .FormBorderStyle.FixedToolWindow
'Set the size of the window .Size = GetSize(300,150)
.Show() End With
'Function to get a Size object Public Function GetSize(x,y) 'Create a Size object with constructor int, int Set GetSize = DotNetFactory("System.Drawing.Size","System.Drawing", x, y) End Function |