Friday 26 September 2014

Capturing Screen shot and added to results

SystemUtil.Run "C:\Program Files\Internet Explorer\IEXPLORE.EXE", "www.gmail.com"

wait(2)

path = "C:\Documents and Settings\qa\Desktop\screenn.png"

Browser("Title:=.*").page("Title:=.*").CaptureBitmap path

Reporter.ReportEvent micPass, "Screenshot", "Captured screen shot", path

Working with files

QTP provides various features and built-in functions to handle the folder structure. By using “filesystemobject ”  you can easily work on folders and files.this is commonly used object forparameterization
there are several methods provided with fileobjectsystem in this post we will see some commonly used method with the example

 Methods:

BuildPath Method
Appends a name to an existing path. The BuildPath method inserts an additional path separator between the existing path and the name, only if necessary 
Function GetBuildPath(path)
Dim fso, newpath
Set fso = CreateObject("Scripting.FileSystemObject")
newpath = fso.BuildPath(path, "Sub Folder") 
GetBuildPath = newpath
End Function
Close Method
This method is used to close the open TextStream file. if you open any file for reading for writing purpose then you should close it once you complete with the operation. 
object.Close( );
Sub CreateAFile
   Dim fso, MyFile
   Set fso = CreateObject("Scripting.FileSystemObject")
   Set MyFile = fso.CreateTextFile("c:testfile.txt", True)
   MyFile.WriteLine("This is a test.")
   MyFile.Close
End Sub
Copy Method
This is one of useful method used. it Copies a specified file or folder from one location to another. 
Dim fso, MyFile
Set fso = CreateObject("Scripting.FileSystemObject")
Set MyFile = fso.CreateTextFile("c:testfile.txt", True)
MyFile.WriteLine("This is a test.")
MyFile.Close
Set MyFile = fso.GetFile("c:testfile.txt")
MyFile.Copy ("c:windowsdesktoptest2.txt")
CopyFile Method
Copies one or more files from one location to another. 
FileSystemObject.CopyFile "c:mydocumentsletters*.doc", "c:tempfolder"
this code will copy all docs file to desired location.
CopyFolder Method
Recursively copies a folder from one location to another. 
FileSystemObject.CopyFolder "c:mydocumentsletters*", "c:tempfolder"
this code will copy all folders from letters folder to tempfolder.
CreateFolder Method
following code use to Creates a folder in desired location. 
Function CreateFolderDemo
   Dim fso, f
   Set fso = CreateObject("Scripting.FileSystemObject")
   Set f = fso.CreateFolder("c:New Folder")
   CreateFolderDemo = f.Path
End Function
CreateTextFile Method
Creates a specified file name and returns a TextStream object that can be used to read from or write to the file. 
Sub CreateAfile
   Dim fso, MyFile
   Set fso = CreateObject("Scripting.FileSystemObject")
   Set MyFile = fso.CreateTextFile("c:testfile.txt", True)
   MyFile.WriteLine("This is a test.")
   MyFile.Close
End Sub
Delete Method
Deletes a specified file or folder. 
Dim fso, MyFile
Set fso = CreateObject("Scripting.FileSystemObject")
Set MyFile = fso.CreateTextFile("c:testfile.txt", True)
MyFile.WriteLine("This is a test.")
MyFile.Close
Set MyFile = fso.GetFile("c:testfile.txt")
MyFile.Delete
DeleteFile Method
Deletes a specified file. 
Sub DeleteAFile(filespec)
   Dim fso
   Set fso = CreateObject("Scripting.FileSystemObject")
   fso.DeleteFile(filespec)
End Sub
DeleteFolder Method
Deletes a specified folder and its contents. 
Sub DeleteAFolder(filespec)
   Dim fso
   Set fso = CreateObject("Scripting.FileSystemObject")
   fso.DeleteFolder(filespec)
End Sub
DriveExists Method
Returns true if the specified drive exists; false if it does not. 
Function ReportDriveStatus(drv)
   Dim fso, msg
   Set fso = CreateObject("Scripting.FileSystemObject")
   If fso.DriveExists(drv) Then
      msg = ("Drive " & UCase(drv) & " exists.")
   Else
      msg = ("Drive " & UCase(drv) & " doesn't exist.")
   End If
   ReportDriveStatus = msg
End Function
Exists Method
Returns true if a specified key exists in the Dictionary object, false if it does not. 
Function KeyExistsDemo
   Dim d, msg   ' Create some variables.
   Set d = CreateObject("Scripting.Dictionary")
   d.Add "a", "Athens"   ' Add some   keys and items.
   d.Add "b", "Belgrade"
   d.Add "c", "Cairo"
   If d.Exists("c") Then
      msg = "Specified key exists."
   Else
      msg = "Specified key doesn't exist."
   End If
   KeyExistsDemo = msg
End Function
FileExists Method
Returns true if a specified file exists; false if it does not. 
Function ReportFileStatus(filespec)
   Dim fso, msg
   Set fso = CreateObject("Scripting.FileSystemObject")
   If (fso.FileExists(filespec)) Then
      msg = filespec & " exists."
   Else
      msg = filespec & " doesn't exist."
   End If
   ReportFileStatus = msg
End Function
FolderExists Method
Returns true if a specified folder exists; false if it does not. 
Function ReportFolderStatus(fldr)
   Dim fso, msg
   Set fso = CreateObject("Scripting.FileSystemObject")
   If (fso.FolderExists(fldr)) Then
      msg = fldr & " exists."
   Else
      msg = fldr & " doesn't exist."
   End If
   ReportFolderStatus = msg
End Function
GetFile Method
Returns a File object corresponding to the file in a specified path. 
Function ShowFileAccessInfo(filespec)
   Dim fso, f, s
   Set fso = CreateObject("Scripting.FileSystemObject")
   Set f = fso.GetFile(filespec)
   s = f.Path & "<br>"
   s = s & "Created: " & f.DateCreated & "<br>"
   s = s & "Last Accessed: " & f.DateLastAccessed & "<br>"
   s = s & "Last Modified: " & f.DateLastModified   
   ShowFileAccessInfo = s
End Function
GetFileName Method
Returns the last component of specified path that is not part of the drive specification. 
Function GetAName(DriveSpec)
   Dim fso
   Set fso = CreateObject("Scripting.FileSystemObject")
   GetAName = fso.GetFileName(DriveSpec)
End Function
GetFolder Method
Returns a Folder object corresponding to the folder in a specified path. 
Sub AddNewFolder(path, folderName)
   Dim fso, f, fc, nf
   Set fso = CreateObject("Scripting.FileSystemObject")
   Set f = fso.GetFolder(path)
   Set fc = f.SubFolders
   If folderName <> "" Then
      Set nf = fc.Add(folderName)
   Else
      Set nf = fc.Add("New Folder")
   End If
End Sub
GetSpecialFolder Method
Returns the special folder object specified. 
Dim fso, tempfile
Set fso = CreateObject("Scripting.FileSystemObject")

Function CreateTempFile 
   Dim tfolder, tname, tfile
   Const TemporaryFolder = 2
   Set tfolder = fso.GetSpecialFolder(TemporaryFolder)
   tname = fso.GetTempName   
   Set tfile = tfolder.CreateTextFile(tname)
   Set CreateTempFile = tfile
End Function

Set tempfile = CreateTempFile
tempfile.WriteLine "Hello World"
tempfile.Close
GetTempName Method
Returns a randomly generated temporary file or folder name that is useful for performing operations that require a temporary file or folder. 
Dim fso, tempfile
Set fso = CreateObject("Scripting.FileSystemObject")

Function CreateTempFile 
   Dim tfolder, tname, tfile
   Const TemporaryFolder = 2
   Set tfolder = fso.GetSpecialFolder(TemporaryFolder)
   tname = fso.GetTempName    
   Set tfile = tfolder.CreateTextFile(tname)
   Set CreateTempFile = tfile
End Function

Set tempfile = CreateTempFile
tempfile.WriteLine "Hello World"
tempfile.Close
OpenAsTextStream Method
Opens a specified file and returns a TextStream object that can be used to read from, write to, or append to the file. 
The iomode argument can have any of the following settings:
ConstantValueDescription
ForReading1Open a file for reading only. You can’t write to this file.
ForWriting2Open a file for writing. If a file with the same name exists, its previous contents are overwritten.
ForAppending8Open a file and write to the end of the file.
The format argument can have any of the following settings:
ConstantValueDescription
TristateUseDefault-2Opens the file using the system default.
TristateTrue-1Opens the file as Unicode.
TristateFalse 0Opens the file as ASCII.
Code :
Function TextStreamTest
   Const ForReading = 1, ForWriting = 2, ForAppending = 8
   Const TristateUseDefault = -2, TristateTrue = -1, TristateFalse = 0
   Dim fso, f, ts
   Set fso = CreateObject("Scripting.FileSystemObject")
   fso.CreateTextFile "test1.txt"   ' Create a file.
   Set f = fso.GetFile("test1.txt")
   Set ts = f.OpenAsTextStream(ForWriting, TristateUseDefault)
   ts.Write "Hello World"
   ts.Close
   Set ts = f.OpenAsTextStream(ForReading, TristateUseDefault)
   TextStreamTest = ts.ReadLine
   ts.Close
End Function
OpenTextFile Method
Opens a specified file and returns a TextStream object that can be used to read from, write to, or append to the file. 
Sub OpenTextFileTest
   Const ForReading = 1, ForWriting = 2, ForAppending = 8
   Dim fso, f
   Set fso = CreateObject("Scripting.FileSystemObject")
   Set f = fso.OpenTextFile("c:testfile.txt", ForWriting, True)
   f.Write "Hello world!"
   f.Close
End Sub
Read Method
Reads a specified number of characters from a TextStream file and returns the resulting string. 
Function ReadTextFileTest
   Const ForReading = 1, ForWriting = 2, ForAppending = 8
   Dim fso, f, Msg
   Set fso = CreateObject("Scripting.FileSystemObject")
   Set f = fso.OpenTextFile("c:testfile.txt", ForWriting, True)
   f.Write "Hello world!"
   Set f = fso.OpenTextFile("c:testfile.txt", ForReading)
   ReadTextFileTest = f.Read(5)
End Function
ReadAll Method
Reads an entire TextStream file and returns the resulting string. 
Function ReadAllTextFile
   Const ForReading = 1, ForWriting = 2
   Dim fso, f
   Set fso = CreateObject("Scripting.FileSystemObject")
   Set f = fso.OpenTextFile("c:testfile.txt", ForWriting, True)
   f.Write "Hello world!"
   Set f = fso.OpenTextFile("c:testfile.txt", ForReading)
   ReadAllTextFile =   f.ReadAll
End Function
ReadLine Method
Reads an entire line (up to, but not including, the newline character) from aTextStream file and returns the resulting string. 
Function ReadLineTextFile
   Const ForReading = 1, ForWriting = 2
   Dim fso, MyFile
   Set fso = CreateObject("Scripting.FileSystemObject")
   Set MyFile = fso.OpenTextFile("c:testfile.txt", ForWriting, True)
   MyFile.WriteLine "Hello world!"
   MyFile.WriteLine "The quick brown fox"
   MyFile.Close
   Set MyFile = fso.OpenTextFile("c:testfile.txt", ForReading)
   ReadLineTextFile = MyFile.ReadLine    ' Returns "Hello world!"
End Function

Write Method
Writes a specified string to a TextStream file. 
Function WriteToFile
   Const ForReading = 1, ForWriting = 2
   Dim fso, f
   Set fso = CreateObject("Scripting.FileSystemObject")
   Set f = fso.OpenTextFile("c:testfile.txt", ForWriting, True)
   f.Write "Hello world!" 
   Set f = fso.OpenTextFile("c:testfile.txt", ForReading)
   WriteToFile =   f.ReadLine
End Function
WriteBlankLines Method
Writes a specified number of newline characters to a TextStream file. 
Function WriteBlankLinesToFile
   Const ForReading = 1, ForWriting = 2
   Dim fso, f
   Set fso = CreateObject("Scripting.FileSystemObject")
   Set f = fso.OpenTextFile("c:testfile.txt", ForWriting, True)
   f.WriteBlankLines 2 
   f.WriteLine "Hello World!"
   Set f = fso.OpenTextFile("c:testfile.txt", ForReading)
   WriteBlankLinesToFile = f.ReadAll
End Function
WriteLine Method
Writes a specified string and newline character to a TextStream file. 
Function WriteLineToFile
   Const ForReading = 1, ForWriting = 2
   Dim fso, f
   Set fso = CreateObject("Scripting.FileSystemObject")
   Set f = fso.OpenTextFile("c:testfile.txt", ForWriting, True)
   f.WriteLine "Hello world!" 
   f.WriteLine "VBScript is fun!"
   Set f = fso.OpenTextFile("c:testfile.txt", ForReading)
   WriteLineToFile = f.ReadAll
End Function

Working With FileSystem Object


We can read the content of a text file using FileSystemObject methods and properties. Below are the various methods and properties used to extract data from file.
1. Creating an instance of filesystem object

Before reading content from a file, we have to create instance of filesystemobject.

Set objFSO = CreateObject("Scripting.FileSystemObject")

2. Opening an existing file to read

Set objTxtFile = objFSO.OpenTextFile(FilePath,iomode)

Iomode can have following values:

0 – For reading – opens the file for reading.

1 – For Writing – opens the file for writing, does overwrite the existing content in file.

8 – For Appending – opens the file for appending, new content is appended at the end of file.

3. ReadAll method

It stores all the content of a file in a variable as string.sContent = objTxtFile.ReadAll

4. ReadLine method

It allows a script to read individual lines in a text file.

sContent = objTxtFile.Readline.

To read through all lines, use a Do Loop that continues until the AtEndOfStream property is True.

Below code snippet shows how to read through each line.

i=0

Do Until objTxtFile.AtEndOfStream

sContent = objTxtFile.ReadLine

sDataArray(i) = objTxtFile.ReadLine

MsgBox sContent

i = i+1

Loop

Content of each line can be stored in an array also.

5. Read(chr)

 The Read method allows you to read only a specified number of characters.

Stores number of characters as defined in chr.

sContent = objTxtFile.Read(5)

6. Skip(chr)

This method skips the number of characters defined in chr from the text file

skip(5)

7. SkipLine

This method skips line of data from the text file.

skipline

8. AtEndOfLine Property

 Returns true if the file pointer is positioned immediately before the end-of-line marker in a TextStream file. Can be useful to find characters in a line or reading characters till end of line.

Do While file.AtEndOfLine <> True

9. Line property

gives the current line number of the textStream.

objTxtFile.line

10. Column Property

 Read-only property that returns the column number of the current character position in a TextStream file.

objTxtFile.Column     

11. OpenasTextStream Method

This method is used with getfile method to read from file.

Set objFSO = CreateObject("Scripting.FileSystemObject")

Set objTxtFile = objFSO.getFile(FilePath)

var objts = objTxtFile.OpenAsTextStream(iomode)

Working with DataBase in QTP



Working with DataBase in QTP - Understanding Connection and Recordset object
QTP Interacts with database creating an instance of ADODB object using ADO. ADO is a Microsoft technology and stands for ActiveX Data Objects and is a programming interface to interact and access data from a database.

Connection Object

Before a database can be accessed by QTP, An object for database connection has to be established.

Set adocon = createobject(“Adodb.connection”)

Once the object is created, we need to set connection string to connect to the database. We can define a connection string with details of database including database provider, database, and user credentials for access.

Some useful methods and properties of connection object are as follows:

Properties:

adocon.connectionstring – This property sets or returns the details used to create a connection to a data source. Includes details of driver, database,username and password.

Strconnection = "Driver=… "Server=svrnme;uid=username;pwd=password;"
For e.g to connect to a database for excel the strConnection would be like:
 strcon = "Driver={Microsoft Excel Driver (*.xls)};Dbqls="+xfilename +";ReadOnly=0;"


adocon.ConnectionTimeout – this defines the time  to wait for a connection to be established.

adocon.provider – This sets or gets connection provider name.

adocon.state – gives status whether connection is on or off.

Methods

adocon.open – opens a database connection based on the connection string provided. adocon.Open connectionstring,userID,password,options
adocon.Execute – execute the sql statement provided
adocon.execute “Select * from table”

adocon.close – This closes the adodb connection.

RecordSet Object:
 Once a connection has been established, we can create recordset object to hold a set of record from database. A recorset consists of records and column

Set rs = createobject(“”Adodb.recordset”)

Some useful methods and properties of RecordSet Objects are as follows:

Properties:

BOF property returns True  if the current record position is before the first record in the Recordset,

EOF property returns True if the current record position is after the last record in the Recordset, otherwise it returns False. For a empty recordset,i.e no records in the recordset or empty recordset, value of BOF and EOF is false. So the property can be used in a loop to validate RecordSet does not return any records.

MaxCount Property returns the maximum value of records to be returned in a recordset.
rs.MaxCount = 20 will return 20 rows of data in recordset.


Methods:

rs.cancel – cancels an existing execution.
rs.clone – returns a clone of existing recorset and assigns to an object
set rsclone = rs.clone
rs.Close - closes instance of recordset

rs.open – opens a recordset based on query specified.
rs.open sqlquery, adocon
where sqlquery is query executed and adocon is connection object.

rs.move – moves the pointer in a recordset by specified count as defined in numrec
rs.move numrec, start.
Also  movenext,moveprevious, movefirst, movelast can be used to move to specified location in recordset.

rs.fields.count gives number of items in the fields collection.

rs.field.item(i) returns specified item from the collection