using strcomp(str1,str2,booleanvalue), we can compare two strings e.g: strcomp("abcd","ABCD",0) results 0 - avoids case sensitive strcomp("abcd","ABCD",1) results -1 follows case sensitive
2. swap two variables by using call by reference in vbscript how to swap two numbers by using a function in vb 6 command1 _ click() dim a,b as integer a=10 b=20 swap a,b(error here type mis match) text1.text=a text2.text=b end sub Public Function swap(byref x As Integer, ByRef y As Integer) Dim temp As Integer temp = x x = y y = temp End Function 3. how to arrange interger in ascending order a=array(1,5,6,3,10,8,2) For i=0 to 6 For j=0 to 6 If a(i)<a(j) Then t=a(i) a(i)=a(j) a(j)=t End If
Next
Next For i=0 to 6 msgbox a(i)
Next 4. how to add intergers array Arr[10] = (1,2,3,4,5,6,7,8,9,0) Dim arrNumbers(10) tally = 0 For counter =1 To 10 arrNumbers(counter) = counter sum = sum + counter Next WScript.Echo sum
5. Find all the files in a given folder using For Each Loop
'Get instance of all the files in a given folder Set oFSO = CreateObject("Scripting.FileSystemObject") Set oFolder = oFSO.GetFolder("D:\") Set oFiles = oFolder.Files
'Loop through all the files in 'D:\' Drive For Each file in oFiles
'Display the name of the file msgbox file.Name
Next
6. Find whether given year is a leap year 1. '1st Method 2. 3. 'The rules for leap year: 4. '1. Leap Year is divisible by 4 (This is mandotory Rule) 5. '2. Leap Year is not divisible by 100 (Optional) 6. '3. Leap Year divisble by 400 (Optional) 7. 8. Dim oYear 9. 10. oYear=1996 11. 12. If ((oYear Mod 4 = 0) And (oYear Mod 100 <> 0) Or (oYear Mod 400 = 0)) then 13. print "Year "&oYear&" is a Leap Year" 14. else 15. print "Year "&oYear&" is not a Leap Year" 16. End If 17. 18. 19. '45. 2nd Method 20. ' Checking 29 days for February month in specified year 21. Dim oYear 22. Dim tmpDate 23. 24. oYear=1996 25. tmpDate = "1/31/" & oYear 26. DaysinFebMonth = DateAdd("m", 1, tmpDate) 27. 28. If day(DaysinFebMonth )=29 then 29. print "Year "&oYear&" is a Leap Year" 30. else 31. print "Year "&oYear&" is not a Leap Year" 32. End If
7. Exception Handling a) Recovery Scenario - When you are not sure of the error and might occur anytime in the script execution. Some of the good areas for Recovery Scenario can be handling 'Object not Found' and then Failing your test, so that you can proceed to the next test case. b) On Error Resume Next - This is used to avoid any errors at runtime during execution, so as to prevent 'General Run Error' shown by QTP thus stopping script execution. Also, you can catch the error using If err.Number<>0 and performing the required steps. c) A good practice is to use .Exist statements when you want to check on an object (something you think might take time to be visible, or is necessary for that test case to continue ahead) d) CheckProperty and WaitProperty on an object. CheckProperty is to provide a CHECK on an object, while WAITProperty is to provide TIME for a specific property of the object to be TRUE. WaitProperty might not always confirm if the object exists or not.
8. How to connect to database. QTP Scripts for connecting to MS Access: Option Explicit Dim con,rs
Set con=createobject("adodb.connection") Set rs=createobject("adodb.recordset")
Do while not rs.eof VbWindow("Form1").VbEdit("val1").Set rs.fields("v1") VbWindow("Form1").VbEdit("val2").Set rs.fields("v2") VbWindow("Form1").VbButton("ADD").Click rs.movenext Loop
'Release objects'Release objects Set rs= nothing Set con= nothing
Note: The database we are using here is MS Access.Before running this script create a table in MS Acess.In the above script I used table called "emp" and column 'names as "v1" and "v2". "d:testdata.mdb" is path of the table which we created. The main use of this script is to use testdata of table(which is in ' database) in the application. In the above script we are passing values from database to Textboxes in Windows Application.
QTP Script for connecting to sqlserver: Option Explicit Dim con,rs
Set con=createobject("adodb.connection") Set rs=createobject("adodb.recordset")
con.open"Driver={SQL Server};server=MySqlServer;uid=MyUserName;pwd=MyPassword;database=pubs" rs.open "select * from emp",con
Do while not rs.eof VbWindow("Form1").VbEdit("val1").Set rs.fields("v1") VbWindow("Form1").VbEdit("val2").Set rs.fields("v2") VbWindow("Form1").VbButton("ADD").Click rs.movenext Loop
'Release objects'Release objects Set rs= nothing Set con= nothing
QTP Script for connecting to oracle: Option Explicit Dim con,rs
Set con=createobject("adodb.connection") Set rs=createobject("adodb.recordset")
con.open "Driver={Microsoft ODBC for Oracle};Server=QTPWorld; Uid=your_username;Pwd=your_password;" rs.open "select * from emp",con
Do while not rs.eof VbWindow("Form1").VbEdit("val1").Set rs.fields("v1") VbWindow("Form1").VbEdit("val2").Set rs.fields("v2") VbWindow("Form1").VbButton("ADD").Click rs.movenext Loop
'Release objects Set rs= nothing Set con= nothing
QTP Script for connecting to MySQL: Option Explicit Dim con,rs
Set con=createobject("adodb.connection") Set rs=createobject("adodb.recordset")
con.open"Driver={MySQL ODBC 3.51 Driver};Server=localhost;Database=myDB;User=Uname;Password=Pwd;Option=3;" rs.open "select * from emp",con
Do while not rs.eof VbWindow("Form1").VbEdit("val1").Set rs.fields("v1") VbWindow("Form1").VbEdit("val2").Set rs.fields("v2") VbWindow("Form1").VbButton("ADD").Click rs.movenext Loop
'Release objects Set rs= nothing Set con= nothing
QTP Script for connecting to Excel: Option Explicit Dim con,rs
Set con=createobject("adodb.connection") Set rs=createobject("adodb.recordset")
con.open "DRIVER={Microsoft Excel Driver (*.xls)};DBQ=C:\TestStatus.xls;Readonly=True" rs.open "SELECT count(*) FROM [Status$] where Status = 'Failed' ",con
Msgbox rs(0)
'Release objects Set rs= nothing Set con= nothing
QTP Script for connecting to Sybase:
Option Explicit Dim con,rs
Set con=createobject("adodb.connection") Set rs=createobject("adodb.recordset")
' Open a session to the database con.open"Driver={SYBASE SYSTEM 11};Srvr=myServerAddress;Uid=Uname;Pwd=Pwd;Database=myDataBase;" rs.open "select * from emp",con
Do while not rs.eof VbWindow("Form1").VbEdit("val1").Set rs.fields("v1") VbWindow("Form1").VbEdit("val2").Set rs.fields("v2") VbWindow("Form1").VbButton("ADD").Click rs.movenext Loop