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

18 Comments »

  1. hi thank u very much for this valuable information frnd

    Comment by girish | November 13, 2007 | Reply

  2. this is really awesome good job for those who worked for this

    Regards
    Rupa

    Comment by Rupa | January 31, 2008 | Reply

  3. Hai Kuldip, Hw r u?
    This is SreenivasaReddy From Polaris SW Lab,Gurgaon
    Presently i am Working as a Test Engineer With The Company wt i mentioned above. I appreciate u to Choose This Way.

    All The Best

    REGARDS
    SreenivasaReddy K

    Comment by SreenivasaReddy.K | February 1, 2008 | Reply

  4. [...] Next Step – QTP Script Examples [...]

    Pingback by 10: FileSystemObject « KR Testing Solutions | February 5, 2008 | Reply

  5. Can I control ‘Recovery Operations’ & “Post-Recovery Test Run Operations’ through scripts.
    Have tried lots of things and searched a lot.

    Waiting for positive reply.

    Many thanks

    Comment by Anup | February 21, 2008 | Reply

  6. Hi Kuldeep,
    Thanks a million for helping all the budding software testers.
    your blogs are of great help to us.
    Please focus on Regular Expressions, running Qtp tests on other Operating systems, Descriptive programming and new versions or releases on QTP.
    Also any realtime scenarios which will give us some exposure to realtime projects.

    Comment by Sheshadri | April 17, 2008 | Reply

  7. Hi Kuldeep,

    I just wanted to say “You are simply Great!!!”
    Thanks for this valuable information.

    Cheers
    Venu

    Comment by Venu Shinde | April 22, 2008 | Reply

  8. Great Job Kuldeep,It’s valuable information and helpfull.Hope you share some more information on Automation frame work and Automation test strategy.

    thank you verymuch

    Regards
    Lokesh

    Comment by Lokesh | May 9, 2008 | Reply

  9. Thanks, It’s realy helpfull info.
    I wish you the best,
    Best regards,
    Rostislav

    Comment by Rostislav | July 2, 2008 | Reply

  10. Hi
    you helped me lot , thanks for your information.

    Regards
    suresh

    Comment by suresh | July 7, 2008 | Reply

  11. Hi Kuldeep,
    i was wondering to get this Valuable information my good luck i got this website .I would like to Thanks a million for helping all the budding software testers.
    your blogs are of great help to us.

    Comment by Rathnam | July 25, 2008 | Reply

  12. Correct script for creating an excel sheet:

    Dim ExcelSheet
    Set XLApp = CreateObject(“Excel.Application”)
    Set ExcelSheet = CreateObject(“Excel.Sheet”)
    ExcelSheet.Application.Visible = True
    ExcelSheet.ActiveSheet.Cells(1,1).Value = “Hello QTP!”
    ExcelSheet.SaveAs “E:/malik/testingscript.xls”
    ExcelSheet.Application.Quit

    Set ExcelSheet = Nothing

    Comment by Malik Choudhari | July 30, 2008 | Reply

  13. This is really nice blog thanx keep writing let other get knowldge from here.I have theoritical knowldge about qtp i used trial version of qtp and i have knowldge of vb script can u plz let me know the other aspects of qtp .How can get to know whole qtp expert plz let me know from start to end.Thanx
    regards
    nidhi

    Comment by Nidhi | August 7, 2008 | Reply

  14. Excellent…really helpful….enjoy buddies

    Comment by Prince | September 2, 2008 | Reply

  15. can any post some more examples

    Comment by Prince | September 2, 2008 | Reply

  16. Hi,
    KD

    If u know me than plz reply.

    Here are good solution which i was finding specifically Excell sheet one.

    Thanks

    Comment by Sandip Mehta (Barejadi Boy) | November 19, 2008 | Reply

  17. Hi Its very nice and good site for getting the whole qtp data in single source. Thanks

    Comment by sowjanya | November 25, 2008 | Reply

  18. Hi kuldeep,

    What are the care should taken while designing driver script for QTP framework.

    Nirmal

    Comment by Nirmal Kumar | December 8, 2008 | Reply


Leave a comment