Showing posts with label DOT Net Factory. Show all posts
Showing posts with label DOT Net Factory. Show all posts

Thursday, October 6, 2011

Getting File Path List Using Wild Card Characters

In this article, I would like to share how to get the file path for the files which are residing in a folder using wild card.

Consider the case where we have a folder containing text files temp.txt, tem.txt, temp1.txt, temp2.txt.

In the above text file list if we want to get path for the files using “temp*.txt” wild card, GetFiles (System.IO.Directory) method can be used.

Dim inFile,oColFilesList,oArrayList,oDI

Set oArrayList=DotNetFactory.CreateInstance("System.Collections.ArrayList","")
Set oDI=DotNetFactory.CreateInstance("System.IO.Directory","mscorlib")

Set oColFilesList=oArrayList.Adapter(oDI.GetFiles("C:\Files List","temp*.txt"))

For inFile=0 to Cint(oColFilesList.Count)-1
Msgbox oColFilesList(inFile)
Next

Set oArrayList=Nothing
Set oDI=Nothing
Set oColFilesList=Nothing
The above code will retrieve paths of temp.txt, temp1.txt, and temp2.txt files but not for tem.txt file.

Saturday, January 23, 2010

Sending An Email Using QTP DOT Net Factory

Sending HTML formatted e-mail using QTP DOT Net factory. In the below code I have used Gmail account. You may try with other accounts also.

Dim vStUserName,vStPassword,vStServerName

vStUserName="Gmail Account Username"'Enter your Gmail Account User Name
vStPassword="Gmail Account Password"'Enter Your Gmail Account Password
vStServerName="smtp.gmail.com"'Gmail SMTP Server Name

vStFromAddress="FromAddress@gmail.com"'Enter valid From Address
vStToAddress="ToAddress@yahoo.com"'Enter Valid To Address

Set oCredentials=DotNetFactory.CreateInstance("System.Net.NetworkCredential","System",vStUserName,vStPassword)
Set oSMTPClient=DotNetFactory.CreateInstance("System.Net.Mail.SmtpClient","System",vStServerName)
Set oMessage=DotNetFactory.CreateInstance("System.Net.Mail.MailMessage","System")
Set oFromAddress =DotNetFactory.CreateInstance("System.Net.Mail.MailAddress","System",vStFromAddress)

oMessage.From=oFromAddress
oMessage.To.Add vStToAddress

oMessage.IsBodyHtml=True'If your message to be formatted with HTML, then make this property value as True or else False.
oMessage.Subject = "GMail Test"'Subject of your email
oMessage.Body = " This is the test text for Gmail email"

oSMTPClient.Port = 587
oSMTPClient.Credentials=oCredentials
oSMTPClient.EnableSSL = True
oSMTPClient.Send(oMessage)

'Kill All the objects
Set oMessage=Nothing
Set oFromAddress=Nothing
Set oCredentials=Nothing
Set oSMTPClient=Nothing

Tuesday, January 5, 2010

Working with Hashtable using QTP DOT Net Factory

Hashtable is similar to a Dictionary object. It stores values in key-item pairs. Hashtable can be implemented in QTP using DOT Net factory with .net framework mscorlib assembly.

The below line creates an instance of the Hashtable.

Set oHash=DotNetFactory.CreateInstance("System.Collections.Hashtable","mscorlib")

Now we are ready to work with Hashtable methods and properties in QTP.


Adding an element:
Syntax: Object.Add key, value

Example:
Set oHash=DotNetFactory.CreateInstance("System.Collections.Hashtable","mscorlib")
oHash.Add 1,"QTP"


Removing all elements:
Syntax: Object.Clear
Example: oHash.Clear
'This line clears all the elements in the Hashtable object

Counting Property:
The count property returns number of items in the hastable.
Msgbox oHash.Count


For more information about Hashtable, please navigate the below links,
http://msdn.microsoft.com/en-us/library/4yh14awz(VS.80).aspx
http://en.wikipedia.org/wiki/Hash_table
http://www.itl.nist.gov/div897/sqg/dads/HTML/hashtab.html