KR Testing Solutions

Quality is effected by your work process

QTP Scripts examples

1. Script for sending a mail using outlook express.

Dim objOutlookMsg,objOutlook
Set objOutlook = CreateObject(“Outlook.Application”)
Set objOutlookMsg = objOutlook.CreateItem(olMailItem)
objOutlookMsg.To = “”
objOutlookMsg.CC = “ “
objOutlookMsg.BCC = “”
objOutlookMsg.Subject = “test”
objOutlookMsg.Body = “”
objOutlookMsg.Attachments.Add “C:\Documents and Settings\Desktop\testreport.txt”
objOutlookMsg.Send

2. Script for opening a Notepad file.

In Expert view write a builtin funtion
invokeapplication”c:/whatever the path”
Run
 
Dim a
Set a = WScript.CreateObject (“WSCript.shell”)
a.run “notepad.exe”

Dim oShell
Set oShell = CreateObject (“WSCript.shell”)
oShell.run “cmd/notepad.exe”
Set oShell = Nothing

DsystemUtil.Run “Notepad.exe”

3. Script for connecting the oracle database

public void connectDB()
{
Const adOpenStatic = 3
Const adLockOptimistic = 3
Const adUseClient = 3
Set objCon=CreateObject(“ADODB.Connection”)
Set objRec=CreateObject(” ADODB.Recordset”)
objCon.Open “DRIVER={Oracle in OraHome92};SERVER={Servername};UID={Username};PWD={Password};DBQ={Dbnmae}”
objRec.CursorLocation = adUseClient
objRec.Open “Select {Col name} from tablename”,objCon,adOpenStatic,adLockOptimistic
While(objRec.EOF)
    objRec.MoveNext
    Msgbox ”Result” & objRec.Fields.Item(“{Col name}”)& vbCrLf 
Wend
objRec.Close
objCon.Close
Set objRec=Nothing
Set objCon=Nothing
Msgbox “Executed Sucessfully”
}

Use code only that which is inside the function connectDB

4. Two ways to connect QC with QTP.

Connect manually

  1. To connect to TD from QTP follow the steps…
  2. Open Qtp ==> Tools ==>
  3. Select TestDirector Connection ==>
  4. In Server Connction Box Enter TD address(URL of TD) ==>
  5. Click Connect==>
  6. In project Connection Box Enter the Details Domain,Project,User name and Password ==>
  7. Click Connect If you want to reconnect on startup check the reconnect on startup and save password for reconnection on startup.
  8. Then close.

Using Script

Set qtApp = CreateObject(“QuickTest.Application”)
qtApp.Launch
qtApp.Visible = True
qtApp.TDConnection. Connect “URL”, “DOMAIN”, “PROJECT”, “USERNAME”, “PASSWORD”, False

 5. Retrieve Data from Excel sheet

Dim FilePath, Excelone,ExcelSheet,Val
Set FilePath= CreateObject(“srcfilepath”)
Set ExcelSheet = FilePath.GetSheet(sheetid)
Set Val= ExcelSheet.ActiveSheet.Cells(1,1).value

‘Below script for creating an excel sheet with values 1,2,3
Dim Excel,ExcelSheet
Set Excelone=CreateObject(“Excel.Application”)
Set ExcelSheet=CreateObject(“Excel.Sheet”)
ExcelSheet.Application.visible=true
ExcelSheet.ActiveSheet.Cells(1,1).value=1
ExcelSheet.ActiveSheet.Cells(1,2).value=2
ExcelSheet.ActiveSheet.Cells(1,3).value=3
Excelone.Saveas “E:\tests.xls”
Excelone.quit
Set ExcelSheet=Nothing

6. Some more stuff related to QTP Script.

Set Word = CreateObject(“Word.Application”)
oWord.DisplayAlerts = False
oWord.Visible = False
oWord.documents.open “testWordDoc.doc”
Set Doc = oWord.ActiveDocument
Set Range = oDoc.content
oRange.ParagraphFormat.Alignment = 0
oRange.insertafter vbcrlf ‘& ” ” & vbcrlf
oRange.collapse(0)
oRange.InlineShapes.AddPicture “ImagePath.bmp”, False, True
oWord.ActiveDocument.Save
oWord.Application.Quit True
Set Range = Nothing
Set Doc = Nothing
Set Word = Nothing

Next Step – More Examples

November 11, 2007 Posted by kuldeep kumar | Learn VBScript for QTP (Descriptive Programming), QTP 9.0 | | 18 Comments