Set obj=createobject("Scripting.filesystemobject")
Set of=obj.getfile("c:\1.xls")
s=of.type
msgbox s
Important Topics & Useful Scripts on QTP. Ask your QTP related questions at this id - asrajaputra@gmail.com
Saturday, December 6, 2008
To Display the Files in a Particular Folder
folderpath="E:\macromedia-8"
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.GetFolder(folderpath)
Set fc = f.Files
For Each f1 in fc
s = s & f1.name &chr(10)
Next
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.GetFolder(folderpath)
Set fc = f.Files
For Each f1 in fc
s = s & f1.name &chr(10)
Next
Displaying numbers into Ascending Order By Using Arrays
''''''''''''''''Displaying numbers into Ascending Order By Using Arrays'''''''''''''''''''''''''''''''''''''''''
a=array(10,20,1,30,2,40,23,50,4)
For j=lbound(a) to ubound(a)
For i=lbound(a) to ubound(a)-1
If a(i)>a(i+1) Then
temp=a(i+1)
a(i+1)=a(i)
a(i)=temp
End If
Next
For i=lbound(a) to ubound(a)
x=x&chr(10)&a(i)
'y=a(i)&chr(10)
Next
msgbox(x)
a=array(10,20,1,30,2,40,23,50,4)
For j=lbound(a) to ubound(a)
For i=lbound(a) to ubound(a)-1
If a(i)>a(i+1) Then
temp=a(i+1)
a(i+1)=a(i)
a(i)=temp
End If
Next
For i=lbound(a) to ubound(a)
x=x&chr(10)&a(i)
'y=a(i)&chr(10)
Next
msgbox(x)
Swaping two numbers without using 3rd variable
a=inputbox ("Enter the first value:")
b=inputbox( "Enter the second value:")
msgbox "a= "&a&" b= "&b
a=int(a)+int(b)
b=int(a)-int(b)
a=int(a)-int(b)
msgbox "After swap a= "&a&" b= "&b
b=inputbox( "Enter the second value:")
msgbox "a= "&a&" b= "&b
a=int(a)+int(b)
b=int(a)-int(b)
a=int(a)-int(b)
msgbox "After swap a= "&a&" b= "&b
Write a Mathematical Table in Notepad through the VB Script
'To open notepad and write a mathematical table in that throug QTP and save in C Drive and close
Set obj=createobject("Scripting.filesystemobject")
Set of=obj.createtextfile("c:\mat.txt")
'set of=obj.opentextfile("c:\mat.txt")
For i=1 to 10
x=5*i
of.writeline"5 * "&i&" = "&x
Next
Set obj=createobject("Scripting.filesystemobject")
Set of=obj.createtextfile("c:\mat.txt")
'set of=obj.opentextfile("c:\mat.txt")
For i=1 to 10
x=5*i
of.writeline"5 * "&i&" = "&x
Next
Comparing Two Files
''''''''''''''''''''''''Compare both the text files and isolate the differences found in the file1 which are not present in file2''''''''''''''''''''''''''''''''
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' in a seperate ouput file.''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
dim found
set objfso = createobject("scripting.filesystemobject")
set objinputfile2 = objfso.opentextfile ("c:\text2.txt")
set objoutputfile = objfso.createtextfile("c:\output.txt")
do until objinputfile2.atendofstream
found = false
strnextline2 = objinputfile2.readline
set objinputfile1 = objfso.opentextfile ("c:\text1.txt")
do until objinputfile1.atendofstream
strnextline1 = objinputfile1.readline
if (strnextline2 = strnextline1) then
found = true
end if
loop
objinputfile1.close
if (found = false) then
objoutputfile.writeline strnextline2
end if
loop
objinputfile2.close
objoutputfile.close
set objinputfile1 = nothing
set objinputfile2 = nothing
set objoutputfile = nothing
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' in a seperate ouput file.''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
dim found
set objfso = createobject("scripting.filesystemobject")
set objinputfile2 = objfso.opentextfile ("c:\text2.txt")
set objoutputfile = objfso.createtextfile("c:\output.txt")
do until objinputfile2.atendofstream
found = false
strnextline2 = objinputfile2.readline
set objinputfile1 = objfso.opentextfile ("c:\text1.txt")
do until objinputfile1.atendofstream
strnextline1 = objinputfile1.readline
if (strnextline2 = strnextline1) then
found = true
end if
loop
objinputfile1.close
if (found = false) then
objoutputfile.writeline strnextline2
end if
loop
objinputfile2.close
objoutputfile.close
set objinputfile1 = nothing
set objinputfile2 = nothing
set objoutputfile = nothing
Writing Log Files
Public function generate_result()
starttime=time()
msgbox(nam)
MyArray=split(nam, ":", -1)
For i=0 to ubound(MyArray)
filename=filename+MyArray(i)
Next
Set fso=Createobject("Scripting.FileSystemObject")
Set textfile=fso.createtextfile("c:/result.txt")
textfile.Writeline("Automated Smoke Test Results")
textfile.WriteLine("````````````````````````````")
textfile.WriteBlanklines(3)
textfile.WriteLine("Run on Host : " & Environment.Value("LocalHostName"))
textfile.WriteBlanklines(1)
textfile.WriteLine("Run on Operating System : " & Environment.Value("OS"))
textfile.WriteBlanklines(1)
textfile.WriteLine("Run on : "&now)
textfile.WriteBlanklines(1)
endtime=time()
textfile.WriteLine("Test Start Time : " &starttime)
textfile.WriteLine("Test End Time : " & endtime)
textfile.WriteLine("Test Execution Time : " & datediff("s", startime, endtime)& " Seconds")
End Function
generate_result()
starttime=time()
msgbox(nam)
MyArray=split(nam, ":", -1)
For i=0 to ubound(MyArray)
filename=filename+MyArray(i)
Next
Set fso=Createobject("Scripting.FileSystemObject")
Set textfile=fso.createtextfile("c:/result.txt")
textfile.Writeline("Automated Smoke Test Results")
textfile.WriteLine("````````````````````````````")
textfile.WriteBlanklines(3)
textfile.WriteLine("Run on Host : " & Environment.Value("LocalHostName"))
textfile.WriteBlanklines(1)
textfile.WriteLine("Run on Operating System : " & Environment.Value("OS"))
textfile.WriteBlanklines(1)
textfile.WriteLine("Run on : "&now)
textfile.WriteBlanklines(1)
endtime=time()
textfile.WriteLine("Test Start Time : " &starttime)
textfile.WriteLine("Test End Time : " & endtime)
textfile.WriteLine("Test Execution Time : " & datediff("s", startime, endtime)& " Seconds")
End Function
generate_result()
VB Script Procedures
'There are two types of procedures
'1. Function Procedure
'2. Sub Procedure
'Function Procedure-A Function procedure is a series of VBScript statements enclosed by the Function
'and End Function statements. Function Procedure can able to return the value.
'Example:
Function demo_add(a,b)
demo_add=a+b
End Function
oVal=demo_add( 2,3)
msgbox oVal 'Returns 5
'In this example demo_add function returns a value to oVal.
'In Function procedures we can use function name to assign a value.
'Sub Procedure-A Sub procedure is a series of VBScript statements enclosed by the Sub
'and End Sub statements. Sub Procedure cannot return any value.
'Example:
Sub demo_sub(a,b, c)
c=a+b
End sub
demo_sub 2,3,x
msgbox x 'Returns 5
'This example will do the same as what function procedure is doing above.
'But in sub Procedure we need to use one more parameter to get values from the sub procedure.
'1. Function Procedure
'2. Sub Procedure
'Function Procedure-A Function procedure is a series of VBScript statements enclosed by the Function
'and End Function statements. Function Procedure can able to return the value.
'Example:
Function demo_add(a,b)
demo_add=a+b
End Function
oVal=demo_add( 2,3)
msgbox oVal 'Returns 5
'In this example demo_add function returns a value to oVal.
'In Function procedures we can use function name to assign a value.
'Sub Procedure-A Sub procedure is a series of VBScript statements enclosed by the Sub
'and End Sub statements. Sub Procedure cannot return any value.
'Example:
Sub demo_sub(a,b, c)
c=a+b
End sub
demo_sub 2,3,x
msgbox x 'Returns 5
'This example will do the same as what function procedure is doing above.
'But in sub Procedure we need to use one more parameter to get values from the sub procedure.
Use of Split Function
''''To display the words in a sentence by spliting with dellimeter''''
str1="arunsingh is a good boy"
var=split(str1," ")
lenarray=ubound(var)
For i=0 to lenarray
str2=var(i)
msgbox str2
Next
str1="arunsingh is a good boy"
var=split(str1," ")
lenarray=ubound(var)
For i=0 to lenarray
str2=var(i)
msgbox str2
Next
Use of RandomNumber Method
set da1=datatable.AddSheet("demo")
set da2=datatable.AddSheet("result")
set pm1=da1.addparameter("name","123")
set pm2= da1.addparameter("age","18")
For i=1 to 10
da1.setcurrentrow(i)
pm1.value="emp"&i
pm2.value=RandomNumber (30,60)
Next
x=da1.getrowcount
set pmr=da2.addparameter("resname","")
set pmg=da2.addparameter("age","")
k=1
For j=1 to x
ag=pm2.valuebyrow(j)
If ag>50 Then
pmr.valuebyrow(k)=pm1.valuebyrow(j)
pmg.valuebyrow(k)=ag
k=k+1
End If
Next
set da2=datatable.AddSheet("result")
set pm1=da1.addparameter("name","123")
set pm2= da1.addparameter("age","18")
For i=1 to 10
da1.setcurrentrow(i)
pm1.value="emp"&i
pm2.value=RandomNumber (30,60)
Next
x=da1.getrowcount
set pmr=da2.addparameter("resname","")
set pmg=da2.addparameter("age","")
k=1
For j=1 to x
ag=pm2.valuebyrow(j)
If ag>50 Then
pmr.valuebyrow(k)=pm1.valuebyrow(j)
pmg.valuebyrow(k)=ag
k=k+1
End If
Next
Use of Excel Object Model
''We can do excel operations using excel object model. To find excel object model in your system follow these steps..
'*********** ********* ********* ********* *
'creating an excel application object
Set Excel=CreateObject( "Excel.Application")
'Adding a sheet in that application
Set ExcelSheet = CreateObject( "Excel.Sheet" )
' Make Excel visible through the Application object.
ExcelSheet.Application.Visible = True
'Adding value into sheet
For i=1 to 10
ExcelSheet.ActiveSheet.Cells( i,1).Value = "This is column A, row"&i
Next
' Save the sheet.
ExcelSheet.SaveAs "C:\TEST.xls"
' Close Excel with the Quit method on the Application object.
ExcelSheet.Applicat ion.Quit
' Release the object variable.
Set ExcelSheet = Nothing
'*********** ********* ********* ********* *
''Copy this script in .vbs file and double click on it or paste this script in QTP and execute it.
''After that go C:\ drive you will find an excel application in the name of TEST.xls..
''Try to understand this by referring excel object model.. Revert me for any concerns..
'*********** ********* ********* ********* *
'creating an excel application object
Set Excel=CreateObject( "Excel.Application")
'Adding a sheet in that application
Set ExcelSheet = CreateObject( "Excel.Sheet" )
' Make Excel visible through the Application object.
ExcelSheet.Application.Visible = True
'Adding value into sheet
For i=1 to 10
ExcelSheet.ActiveSheet.Cells( i,1).Value = "This is column A, row"&i
Next
' Save the sheet.
ExcelSheet.SaveAs "C:\TEST.xls"
' Close Excel with the Quit method on the Application object.
ExcelSheet.Applicat ion.Quit
' Release the object variable.
Set ExcelSheet = Nothing
'*********** ********* ********* ********* *
''Copy this script in .vbs file and double click on it or paste this script in QTP and execute it.
''After that go C:\ drive you will find an excel application in the name of TEST.xls..
''Try to understand this by referring excel object model.. Revert me for any concerns..
How to set the value for a property
Setting.Add "Tester Name", "Mark Train"
MsgBox Setting("Tester Name")
MsgBox Setting("Tester Name")
Use of SetTOProperty
'For example..in first build there is a OK button..and u
'reorded on OK button..
'but in modified build it changed to Yes..and this
'modificatio is only for single build...in this situation u
'need to change that button name OK to Yes
'window("XXXX").winbutton("OK").SetTOProperty "Text","Yes"
'reorded on OK button..
'but in modified build it changed to Yes..and this
'modificatio is only for single build...in this situation u
'need to change that button name OK to Yes
'window("XXXX").winbutton("OK").SetTOProperty "Text","Yes"
To search for something in Google
url="ww.google.com"
systemutil.Run"IEXPLORE",url 'It is to Open a Browser with any website address
Set gBrowser=Browser("name:=Google.*")
Set gPage=gBrowser.Page("title:=Google.*")
gPage.webedit("name:=q","type:=text","html tag:=INPUT").set "QTP"
gpage.webbutton("name:=Google Search","index:=0","type:=submit","html tag:=INPUT").click
systemutil.Run"IEXPLORE",url 'It is to Open a Browser with any website address
Set gBrowser=Browser("name:=Google.*")
Set gPage=gBrowser.Page("title:=Google.*")
gPage.webedit("name:=q","type:=text","html tag:=INPUT").set "QTP"
gpage.webbutton("name:=Google Search","index:=0","type:=submit","html tag:=INPUT").click
To Check weather the text contains a particular word or not
To check/Uncheck all the checkboxes present on a web page
Systemutil.Run"IExplore","www.yahoomail.com"
wait(10)
Dim obj_DescChk
Set obj_DescChk=Description.Create
obj_DescChk("html tag").value="INPUT"
obj_DescChk("type").value="checkbox"
'obj_DescChk.remove "html tag" '''''''''''''''''''Would delete the html tag property from the collections
'If obj_DescChk("html tag").value=empty then ''''''''To Know the property is exists or not in the collection
'obj_DescChk("html tag").value="INPUT" '''''''''''''''''If not then add it to the collection
'end if
Dim AllChkBoxes, SingleChkBox
Set AllChkBoxes=Browser("title:=Yahoo! Mail: The best web-based email!").Page("title:=Yahoo! Mail: The best web-based email!").ChildObjects(obj_DescChk)
msgbox AllChkBoxes.count
For i=0 to AllChkBoxes.count-1
AllChkBoxes(i).set "ON"
Next
wait(10)
Dim obj_DescChk
Set obj_DescChk=Description.Create
obj_DescChk("html tag").value="INPUT"
obj_DescChk("type").value="checkbox"
'obj_DescChk.remove "html tag" '''''''''''''''''''Would delete the html tag property from the collections
'If obj_DescChk("html tag").value=empty then ''''''''To Know the property is exists or not in the collection
'obj_DescChk("html tag").value="INPUT" '''''''''''''''''If not then add it to the collection
'end if
Dim AllChkBoxes, SingleChkBox
Set AllChkBoxes=Browser("title:=Yahoo! Mail: The best web-based email!").Page("title:=Yahoo! Mail: The best web-based email!").ChildObjects(obj_DescChk)
msgbox AllChkBoxes.count
For i=0 to AllChkBoxes.count-1
AllChkBoxes(i).set "ON"
Next
To retrive data from datatable which is in 3rd row and 4th column
'in Action1 sheet change the names of A,B,C,D columns with A,B,C,D respectively.
'And type the values in D column
datatable.SetCurrentRow(3)
answer=datatable(4,2)'.Value(4,2) '4th column in 2nd Sheet
msgbox answer
'Run the program
'-------------------------------------
'To retrive value from 3rd row 4th column from external excel sheet
datatable.Importsheet "c:/1.xls",1,1
datatable.SetCurrentRow(3)
val=datatable.Value(4,1)'4th column 1st sheet
msgbox "The Value is "&val
'And type the values in D column
datatable.SetCurrentRow(3)
answer=datatable(4,2)'.Value(4,2) '4th column in 2nd Sheet
msgbox answer
'Run the program
'-------------------------------------
'To retrive value from 3rd row 4th column from external excel sheet
datatable.Importsheet "c:/1.xls",1,1
datatable.SetCurrentRow(3)
val=datatable.Value(4,1)'4th column 1st sheet
msgbox "The Value is "&val
How to paste a image in Word Document
Set objWord = CreateObject( "Word.Application")
objWord.Visible = True
Set objDoc = objWord.Documents. Add()
Set objSelection = objWord.Selection
Set objShape = objDoc.Shapes
objShape.AddPicture ("c:\image010.jpg")
objWord.Visible = True
Set objDoc = objWord.Documents. Add()
Set objSelection = objWord.Selection
Set objShape = objDoc.Shapes
objShape.AddPicture ("c:\image010.jpg")
To Know the No of Items in the Web Page
url="http://my.naukri.com/manager/createacc2.php?othersrcp=5421&wExp=N"
'url="www.gmail.com"
'url="www.yahoomail.com"
If Browser("index:=0").Exist(1) then' If Browser is already opened
a = Browser("index:=0").GetROProperty("hwnd")
Browser("hwnd:=" & a).Navigate(url)
Browser("hwnd:=" & a).Sync
wait(2)
Else' If Browser is not opened
SystemUtil.Run"IEXPLORE.exe"
a = Browser("index:=0").GetROProperty("hwnd")
Browser("hwnd:=" & a).Navigate(url)
Browser("hwnd:=" & a).Sync
wait(2)
End If
Set Editobj=Description.Create'For WebEdits
Editobj("html tag").value="INPUT"
Editobj("kind").value="singleline"
Set Butobj = Description.Create'For Buttons
Butobj("html tag").value = "INPUT"
Butobj("type").value="submit"
Set Listobj=Description.Create'For Web Lists
Listobj("html tag").value="SELECT"
Set Linkobj=Description.Create'For Link
Linkobj("html tag").value="A"
Set Checkboxobj=Description.Create'For Checkboxes
Checkboxobj("html tag").value="INPUT"
Checkboxobj("type").value="checkbox"
Set Radioobj=Description.Create'For Radio Buttons
Radioobj("html tag").value="INPUT"
Radioobj("html id").value="ctctype_i"
Set Edits = browser("hwnd:=" &a).Page("title:=.*").ChildObjects(Editobj)
Set Buttons = browser("hwnd:=" &a).Page("title:=.*").ChildObjects(Butobj)
Set Lists = browser("hwnd:=" &a).Page("title:=.*").ChildObjects(Listobj)
Set Links = browser("hwnd:=" &a).Page("title:=.*").ChildObjects(Linkobj)
Set Checks = browser("hwnd:=" &a).Page("title:=.*").ChildObjects(Checkboxobj)
Set Radios = browser("hwnd:=" &a).Page("title:=.*").ChildObjects(Radioobj)
msgbox "Total EditBoxes are : "&Edits.count
msgbox "Total Button are : "&Buttons.count
msgbox "Total Lists are : "&Lists.count
msgbox "Total Links are : "&Links.count
msgbox "Total checkBoxes are : "&Checks.count
msgbox "Total RadioButtons are : "&Radios.count
'url="www.gmail.com"
'url="www.yahoomail.com"
If Browser("index:=0").Exist(1) then' If Browser is already opened
a = Browser("index:=0").GetROProperty("hwnd")
Browser("hwnd:=" & a).Navigate(url)
Browser("hwnd:=" & a).Sync
wait(2)
Else' If Browser is not opened
SystemUtil.Run"IEXPLORE.exe"
a = Browser("index:=0").GetROProperty("hwnd")
Browser("hwnd:=" & a).Navigate(url)
Browser("hwnd:=" & a).Sync
wait(2)
End If
Set Editobj=Description.Create'For WebEdits
Editobj("html tag").value="INPUT"
Editobj("kind").value="singleline"
Set Butobj = Description.Create'For Buttons
Butobj("html tag").value = "INPUT"
Butobj("type").value="submit"
Set Listobj=Description.Create'For Web Lists
Listobj("html tag").value="SELECT"
Set Linkobj=Description.Create'For Link
Linkobj("html tag").value="A"
Set Checkboxobj=Description.Create'For Checkboxes
Checkboxobj("html tag").value="INPUT"
Checkboxobj("type").value="checkbox"
Set Radioobj=Description.Create'For Radio Buttons
Radioobj("html tag").value="INPUT"
Radioobj("html id").value="ctctype_i"
Set Edits = browser("hwnd:=" &a).Page("title:=.*").ChildObjects(Editobj)
Set Buttons = browser("hwnd:=" &a).Page("title:=.*").ChildObjects(Butobj)
Set Lists = browser("hwnd:=" &a).Page("title:=.*").ChildObjects(Listobj)
Set Links = browser("hwnd:=" &a).Page("title:=.*").ChildObjects(Linkobj)
Set Checks = browser("hwnd:=" &a).Page("title:=.*").ChildObjects(Checkboxobj)
Set Radios = browser("hwnd:=" &a).Page("title:=.*").ChildObjects(Radioobj)
msgbox "Total EditBoxes are : "&Edits.count
msgbox "Total Button are : "&Buttons.count
msgbox "Total Lists are : "&Lists.count
msgbox "Total Links are : "&Links.count
msgbox "Total checkBoxes are : "&Checks.count
msgbox "Total RadioButtons are : "&Radios.count
To Insert a Value in a Exce l Sheet
''''''''''''''''''''To Insert a Value in a Exce l Sheet'''''''''''''''''''''''''
Set xl=createobject("Excel.Application")
Set x=xl.workbooks.open("c:/arun.xls")
Set y=xl.activeworkbook.worksheets("sheet1")
xl.cells(1,1)="arun"
Set xl=createobject("Excel.Application")
Set x=xl.workbooks.open("c:/arun.xls")
Set y=xl.activeworkbook.worksheets("sheet1")
xl.cells(1,1)="arun"
Subscribe to:
Posts (Atom)
About Me
- RAJAPUTRA
- Hi all,I am Arun Singh Rajaputra. I am working as a Project Lead in a reputed organization in Hyderabad, AP, India. I have little bit knowledge on Quick Test Professional and like to share my knowledge to all through this blog.
Subscribe to get updates on this blog
NOTE
Some of the posts posted in this blog are collected and most of them have been prepared by me. If any one have objections regarding any copied posts. Please mail me so that I can take a necessary action on that post.