Thursday, February 5, 2009

QTP Scripts

(1)File locating Show Date & Time

Set f1 = CreateObject("Scripting.FileSystemObject")
Set f2 = f1.GetFile("E:\Lak.txt")
S = "File was Created on: "&f2.DateCreated
Msgbox S
####################################################

(2)Folder locating Show Date & Time

Set f1 = CreateObject("Scripting.FileSystemObject")
Set f2 = f1.GetFolder("E:\Lak")
S = "Folder was Created on: "&f2.DateCreated
Msgbox S
######################################################

(3)Counts the sub folders

Set a = CreateObject("Scripting.FileSystemObject")
Set b = a.GetFolder("D:\ssm")
Set c = b.SubFolders
For Each d in c
e=e&d.name&vbnewline
Next
msgbox e

########################################################

(4)Count the Check box in a Page

Set a=Description.Create
a("html tag").value="input"
a("type").value="checkbox"

Set b=Browser("Forms : Check Box - HTML").Page("Forms : Check Box - HTML").childobjects(a)
c=b.count
msgbox c

For i=0 to c-1
b(i).set "on"
Next


(5)Count the RadioButton in a Page(True or False)

Set a=Description.Create
a("html tag").value="input"
a("type").value="radio"

Set b=Browser("Forms : Check Box - HTML").Page("Forms : Radio Button -").childobjects(a)
c=b.count
msgbox c

For i=0 to c-1
b(i)
Next


(6)Creating New Notepad and Write the Data

Set obj=createobject ("scripting.filesystemobject")
Set notepad=obj.createtextfile("e:\\abc.txt")
notepad.writeline("hi sijil")
notepad.writeline("welcome to my blog")


(7)Creating a Folder

Set obj=createobject ("scripting.filesystemobject")
Set folder=obj.createfolder("e:\\kk")


(8)Creating a Folder include a Notepad Data

Set obj=createobject ("scripting.filesystemobject")
Set folder=obj.createfolder("e:\\abc")
Set notepad=obj.createtextfile("e:\\abc\script.txt")
notepad.writeline("hi")
notepad.writeline("welcome to my blog")


(9) Message Box will close by itself in 10seconds so dont click on OK button

Set a=createobject("wscript.shell")
msgbox_message="Message Box will close by itself in 10seconds so dont click on OK button"
msgbox_time="10"
msgbox_title="Testing"
a.popup msgbox_message,msgbox_time,msgbox_title

(10) Script to get count,names of all open browsers and to close them.

Set b=Description.Create
b("micclass").value="Browser"
Set obj=Desktop.ChildObjects(b)
msgbox obj.count
For i=0 to obj.count-1
c=obj(i).getroproperty("name")
msgbox(c)
obj(i).Close
Next

#####################################################################

(11) “Message Box” and “Browser” will close by itself in 10seconds so don't click on OK button

Set a=createobject("wscript.shell")
Set b=Description.Create
b("micclass").value="Browser"
Set obj=Desktop.ChildObjects(b)
For i=0 to obj.count-1
msgbox_message="Message Box and Browser will close by itself in 10seconds so dont click on OK button"
msgbox_time="10"
msgbox_title="Testing"
a.popup msgbox_message,msgbox_time,msgbox_title
obj(i).Close
Next


(12) To Count No. Of Links in WebPage(“A” or “a”[rep hyperlink])

set objlink = description.create
objlink("html tag").value = "A"
set n = browser("Google").page("Google").childobjects(objlink)
c = "Total Links are: "&n.count
msgbox c


(13)To Show the Tool Tip on a Page(Checkbox)

///////////Browser("Google").Page("Google").Link("Images").Click
a=Browser("Google").Page("Google").WebEdit("name:=q").Object.title
msgbox a

(14)To Show the “Links” Tool Tip on a Page

//////Browser("Google").Page("Google").WebEdit("q").Submit
c=Browser("Google").Page("Google").Link("name:=definition").object.title
msgbox c

(15)To Show the “Image” Tool Tip on a Page

b=Browser("Web Templates - Flash").Page("Web Templates - Flash").Image("src:=http://www.templatesbox.com/data/premium.templates/images/full_site/thumbnails/12293353720Zf/12293353720Zf.jpg").getROProperty("alt")
msgbox b
//////////Browser("Web Templates - Flash").Page("Web Templates - Flash").Link("Full Site").Click

(16) To Show Current Date,Time and Date Only

msgbox now
msgbox time
msgbox date

(17)Creating a Word pad and Write the Data

Dim obj
Set obj=Createobject("word.application")
obj.Documents.add
obj.selection.typetext"Welcome"
obj.activedocument.saveas"d:\test.doc"

(18)Creating a Excelsheet and Wirte the Data

Set Excel=createObject("Excel.Application")
Set ExcelSheet=createObject("Excel.sheet")
ExcelSheet.Application.visible=true
Excel.ActiveSheet.cells(1,1).value=1111
Excel.ActiveSheet.cells(1,2).value=2222
Excel.ActiveSheet.cells(2,1).value=3333
Excel.ActiveSheet.cells(2,2).value=4444
ExcelSheet.SaveAs "d:\1.xls"

(19)Taking Value From Excel Sheet with Example

Browser("AutoPallet/AutoLoader™").Page("AutoPallet/AutoLoader™").WebButton("Login").Click

Set ex=createobject("Excel.Application")
Set a=ex.workbooks.open("D:\1.xls")
Set b=a.worksheets("Sheet1")
Dim login
For i=1 to 5
login="UserName: "&b.Cells(i,"A").value
msgbox login
Next
Browser("AutoPallet/AutoLoader™").Page("AutoPallet/AutoLoader™").WebEdit("liUSR").Set "admin"
dim pwd
For i=1 to 5
pwd="Password: "&b.Cells(i,"B").value
msgbox pwd
Next
Browser("AutoPallet/AutoLoader™").Page("AutoPallet/AutoLoader™").WebButton("Login").Click

(20)Counts the value from Excelsheet(initially create a excelsheet with Value)

DataTable.Import("c:\1.xls")
DataTable.SetCurrentRow("1")
For login_id=1 to 3
a=DataTable.Value("login_id")
msgbox a
DataTable.SetNextRow
Next



(21)Creating a Notpad and Write some data

Set f=createobject("scripting.filesystemobject")
Set f1=f.createtextfile("c:/file1.txt")
f1.writeline"aaa bbb"
f1.writeline"ccc ddd"
f1.writeline"eee fff"
f1.writeline"ggg hhh"
f1.writeline"iii jjj"
(22)Display Random Number

x=randomnumber.Value(0,100)
msgbox(x)
(23)Creating a Folder

Set obj=createobject ("scripting.filesystemobject")
Set notepad=obj.createfolder("c:\abc")

(24)Time out Messagebox

Set a=createobject("wscript.shell")
msgbox_message="Message Box will close by itself in 10seconds so dont click on OK button"
msgbox_time="10"
msgbox_title="Testing"
a.popup msgbox_message,msgbox_time,msgbox_title

(25)Value taken from Excel Sheet without Importing

path_of_Excel = "c:\1.xls"
sheet_of_Excel = "sheet1"
row_of_Excel = 1
column_of_Excel = 1
Function value_from_Excel(Excel_path,esheet,erow,ecolumn)
Set excel_object = createobject("Excel.Application")
excel_object.workbooks.open Excel_path
Set my_sheet = excel_object.sheets.item(esheet)
value= my_sheet.cells(erow,ecolumn)
excel_object.application.quit
value_from_Excel=Value
End Function
msgbox value_from_Excel(path_of_Excel, sheet_of_Excel, row_of_Excel ,column_of_excel)

(26)Passing Value From Data table to Notepad(initially create a notepad)

Browser("Welcome: Mercury Tours").Page("Welcome: Mercury Tours").Sync
Window("Notepad").Activate
Window("Notepad").Move 352,319
Window("Notepad").WinEditor("Edit").Type "a"
Window("Notepad").WinEditor("Edit").Type micReturn
rc = DataTable.Value("A",dtGlobalSheet)
msgbox rc
Window("Notepad").WinEditor("Edit").SetCaretPos 0,0
Window("Notepad").WinEditor("Edit").Type rc

(27) To Get the Background Color

Var=Browser("Welcome: Mercury Tours").Page("Welcome: Mercury Tours").Object.bgcolor
msgbox Var

(28)Create and Store Data into Simple XML File

Set doc = XMLUtil.CreateXML( )
doc.CreateDocument "Values"
Set root = doc.GetRootElement( )
root.AddChildElementByName "variable",""
Set node=root.ChildElements()
Set node=node.item(node.count)
node.AddChildElementByName "name","601A4"
doc.SaveFile "e:\data.xml"

(29)PLSQL Procedure(PLEASE CHECK)

http://www.qaforums.com/cgi-bin/forums/ultimatebb.cgi?ubb=next_topic;f=20;t=006805;go=newer

Public Function sqlRunStoredProcedure (sSProcName, sParameter1, sParameter2, sParameter3)
Set oADO_CMD = CreateObject("ADODB.Command") ' Create the database object
sConnectionStr = Environment("SQL_ConnectionStr") 'Get connection string
oADO_CMD.ActiveConnection = sConnectionStr ' Activate the connection
oADO_CMD.CommandType = 4 ' Set the command type to Stored Procedures
oADO_CMD.CommandText = sSProcName ' Set the command type to Stored Procedures
oADO_CMD.Parameters.Refresh ' Define Parameters for the stored procedure
If "" <> sParameter1 Then
oADO_CMD.Parameters(1).Value = sParameter1 ' Pass FIRST input value [optional]
If "" <> sParameter2 Then
oADO_CMD.Parameters(2).Value = sParameter2 ' Pass SECOND input value [optional]
If "" <> sParameter3 Then
oADO_CMD.Parameters(3).Value = sParameter3 ' Pass THIRD input value [optional]
End If
End If
End If
oADO_CMD.Execute() ' Execute the stored procedure
Set oADO_CMD = Nothing 'Clean up objects
End Function





(30)Creating Perfect XML

Dim objWriter,XmlWriter
Set objWriter = DotNetFactory.CreateInstance("System.Xml.XmlWriter","System.Xml")
set XmlWriter=objWriter.Create("e:\Desktop1234.xml")
XmlWriter.WriteStartElement("Books")
XmlWriter.WriteStartElement("Author")
XmlWriter.WriteAttributeString "Name","“Mohan"
XmlWriter.WriteEndElement()
XmlWriter.WriteStartElement("Title")
XmlWriter.WriteAttributeString "Title1","QTP"
XmlWriter.WriteEndElement()
XmlWriter.WriteFullEndElement()
XmlWriter.close()
Set objWriter =Nothing

(31)Simple creating a file and Read the File

Set oFile = DotNetFactory.CreateInstance("System.IO.File")
oFile.WriteAllText "e:\Test.txt","DotNetFactory works great!!"
strContents = oFile.ReadAllText("e:\Test.txt")
MsgBox strContents,,"File Contents"
Set oFile = Nothing

(32) To get count,names of all open browsers and to close them.

Browser("Welcome: Mercury Tours").Page("Welcome: Mercury Tours").Image("Sign-In").Click 21,11
Set b=Description.Create
b("micclass").value="Browser"
Set obj=Desktop.ChildObjects(b)
msgbox obj.count
For i=0 to obj.count-1
c=obj(i).getroproperty("name")
msgbox(c)
obj(i).Close
Next

(33) To Search Particular Value in a Excel Sheet

Set appExcel = CreateObject("Excel.Application")
appExcel.visible=true
Set objWorkBook = appExcel.Workbooks.Open ("c:\1.xls")
Set objSheet = appExcel.Sheets("Sheet1")
With objSheet.UsedRange
Set c = .Find ("user1")
For each c in objSheet.UsedRange
If c="user1" then
c.Interior.ColorIndex = 38
End If
Set c = .FindNext(c)
next
End With

'objWorkBook.save
'objWorkBook.close
'set appExcel=nothing

(34)To OpenFileDialogBox

Set fd=DotNetFactory.CreateInstance("System.Windows.Forms.OpenFileDialog","System.Windows.Forms")
fd.Filter="txt files (*.txt)|*.txt |All files (*.*) |*.*"
fd.FilterIndex=2
fd.ShowDialog()
msgbox fd.FileName

(35) Script for copying a file from One Folder to Another Folder in QTP

Dim f
Set f = CreateObject("Scripting.FileSystemObject")
f.CopyFile "C:\1.xls", "E:\"
Msgbox "File Copied"


(36) The code to copy the contents of a sheet in one excel to another excel sheet

Set objExcel = CreateObject("Excel.Application")
objExcel.Visible = True
Set objWorkbook1= objExcel.Workbooks.Open("C:\1.xls")
Set objWorkbook2= objExcel.Workbooks.Open("C:\2.xls")
objWorkbook1.Worksheets("Sheet1").UsedRange.Copy
objWorkbook2.Worksheets("Sheet1").Range("A1").PasteSpecial Paste =xlValues
objWorkbook1.save
objWorkbook2.save
'objWorkbook1.close
'objWorkbook2.close
set objExcel=nothing

(37) Comparing two Excel Sheets

Set objExcel = CreateObject("Excel.Application")
objExcel.Visible = True
Set objWorkbook1= objExcel.Workbooks.Open("C:\1.xls")
Set objWorkbook2= objExcel.Workbooks.Open("C:\2.xls")
Set objWorksheet1= objWorkbook1.Worksheets(1)
Set objWorksheet2= objWorkbook2.Worksheets(1)
For Each cell In objWorksheet1.UsedRange
If cell.Value <> objWorksheet2.Range(cell.Address).Value Then
cell.Interior.ColorIndex = 3
Else
cell.Interior.ColorIndex = 0
End If
Next
set objExcel=nothing

(38) Log on Session Information

strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

Set colItems = objWMIService.ExecQuery("Select * from Win32_LogonSession")
For Each objItem in colItems
MsgBox "Authentication Package: " & objItem.AuthenticationPackage
MsgBox "Logon ID: " & objItem.LogonId
MsgBox "Logon Type: " & objItem.LogonType
MsgBox "Start Time: " & objItem.StartTime
Next



(39) List the File System Type

strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colDisks = objWMIService.ExecQuery("Select * from Win32_LogicalDisk")
For Each objDisk in colDisks
MsgBox "Device ID: "& vbTab & objDisk.DeviceID
MsgBox "File System: "& vbTab & objDisk.FileSystem
Next

(40) List a Specific Set of Files

strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colFiles = objWMIService. _
ExecQuery("Select * from CIM_DataFile where FileSize > 1000000")
For Each objFile in colFiles
MsgBox objFile.Name & " -- " & objFile.FileSize
Next

(41) List Detailed Summary Information for a File

Set objShell = CreateObject ("Shell.Application")
Set objFolder = objShell.Namespace ("E:\rec")
Set objFSO = CreateObject("Scripting.FileSystemObject")
Dim arrHeaders(13)
For i = 0 to 13
arrHeaders(i) = objFolder.GetDetailsOf (objFolder.Items, i)
Next
For Each strFileName in objFolder.Items
For i = 0 to 13
If i <> 9 then
MsgBox arrHeaders(i) _
& ": " & objFolder.GetDetailsOf (strFileName, i)
End If
Next
Next

(42) List Summary Information for a Set of Files

Const FILE_NAME = 0
Set objShell = CreateObject ("Shell.Application")
Set objFolder = objShell.Namespace ("C:\Work BPO")
For Each strFileName in objFolder.Items
MsgBox "File name: " & objFolder.GetDetailsOf _
(strFileName, FILE_NAME)
Next

(43) Compress a Folder

strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colFolders = objWMIService.ExecQuery _
("Select * from Win32_Directory where name = 'c: \\new'")
For Each objFolder in colFolders
errResults = objFolder.Compress
Next

3 comments:

Ravinder said...

Hi Parveen
Have you ever seen that QTP is clicking any link or button but action is not performed on that event?

Regards

Ravinder Singroha

Ravinder said...

Hi Parveen

How can you sync frames in QTP?
or what are the other ways of using syncs in QTP for pages browser or java script loading or any thing else ?
by descriptive programing
?
Thanks & Regards
Ravinder Singroha

**@J@¥** said...

Excellent Page...

Thank you so much