About Me

Gaurav Seth is a a Senior Automation Test Analyst with experience in QTP, Selenium and Ranorex tools and currently working on Automating in C# for a FX trading Client

VB Scripting with QTP Part 1

Below are the List of Few VB Scripting Functions


Left
Dim MyString, LeftString
MyString = "VBSCript"
LeftString = Left(MyString, 3)
msgbox LeftString

StrReverse
Dim MyStr
MyStr = StrReverse("VBScript")
msgbox MyStr

TIMER
a=Msgbox (TimeIt(10))
Function TimeIt(N)
Dim StartTime, EndTime
StartTime = Timer
For I = 1 To N
Next
EndTime = Timer
TimeIt = EndTime - StartTime
End Function

Right
Dim AnyString, MyStr1,MyStr2,MyStr3
AnyString = "Hello World" ' Define string.
MyStr1 = Right(AnyString, 1) ' Returns "d".
MyStr2 = Right(AnyString, 6) ' Returns " World".
MyStr3 = Right(AnyString, 20) ' Returns "Hello World".
msgbox MyStr1 & " " & MyStr2 &" " &MyStr3

Rnd for Randomize
Dim a, i
for i =1 to 10
a =Int((1000 - 50 + 1) * Rnd + 50)
msgbox a
next

randomize.vbs
a=Int((upperbound - lowerbound + 1) * Rnd + lowerbound)
msgbox a

Join.vbs
Dim MyString
Dim MyArray(3)
MyArray(0) = "Mr."
MyArray(1) = "John "
MyArray(2) = "Doe "
MyArray(3) = "III"
MyString = Join(MyArray)
msgbox MyString

InputBox.vbs
Dim Input
Input = InputBox("Enter your name")
MsgBox ("You entered: " & Input)

Do while.vbs
Dim Check, Counter
Check = True: Counter = 0
Do
Do While Counter < 20
Counter = Counter + 1
If Counter = 10 Then
Check = False
Exit Do
End If
Loop
Loop Until Check = False

Len.VBS
Dim MyString
MyString = Len("VBSCRIPT") ' MyString contains 8.
Msgbox MyString

split.vbs
Dim MyString, MyArray, Msg
MyString = "VBScriptXisXfun!"
MyArray = Split(MyString, "x", -1, 1)
' MyArray(0) contains "VBScript".
' MyArray(1) contains "is".
' MyArray(2) contains "fun!".
Msg = MyArray(0) & " " & MyArray(1)
Msg = Msg & " " & MyArray(2)
MsgBox Msg

Replace.vbs
Dim MyString1
MyString1 = Replace("kapy", "p", "Y")
MyString2 = Replace("PANT", "P", "X")
msgbox MyString1
msgbox MyString2

ccur.vbs
Dim MyDouble, MyCurr
MyDouble = 543.214588 ' MyDouble is a Double.
MyCurr = CCur(MyDouble * 2) ' Convert result of MyDouble * 2 (1086.429176) to a Currency (1086.4292).
msgbox MyCurr

Msgbox
Dim MyVar
MyVar = MsgBox ("Hello World!", 65, "MsgBox Example")

RGB.vbs
Function RevRGB(red, green, blue)
RevRGB= CLng(blue + (green * 256) + (red * 65536))
msgbox RevRGB
End Function

Time.vbs
Dim MyTime
MyTime = Time
msgbox myTime

Year
Dim MyDate, MyYear
MyDate = #October 19, 1962# ' Assign a date.
MyYear = Year(MyDate)
msgbox MyYear

Randomize 1
Dim MyValue, Response
Randomize ' Initialize random-number generator.
Do Until Response = vbNo
MyValue = Int((6 * Rnd) + 1) ' Generate random value between 1 and 6.
MsgBox MyValue
Response = MsgBox ("Roll again? ", vbYesNo)
Loop

Now
Dim MyVar
MyVar = Now ' MyVar contains the current date and time
msgbox Myvar

How to get the current date and time into a variable.
Dim fName
fName = fName & year(date)&month(date)&day(date)&"-"& hour(time)&minute(time)&second(time)
msgbox fname

How to kill excel sheets running in the background
set objWMIService = GetObject("winmgmts:\\.\root\CIMV2")
Set colProcess = objWMIService.ExecQuery ("Select * from Win32_Process Where Name = 'EXCEL.exe")
For Each objProcess in colProcess
objProcess.Terminate()
Next
Set objWMIService = Nothing
Set colProcess = Nothing

How to use the functions to convert the date into the DD/MM/YYYY format from YYYYMMDD 00:00:00
a = "20100906 03:34:56"
b=Split(a, " ")
YYYY=LEFT(b(0),4)
DD=RIGHT(b(0),2)
MM=RIGHT(LEFT(b(0),6),2)
finaldate= DD & "/" & MM &"/" & YYYY
msgbox finaldate

How to write into an Existing Excel Sheet using VB Script
Set objExcel = CreateObject("Excel.Application")
Set objWorkbook = objExcel.Workbooks.Open("C:\GauravTest1.xlsx")
'objExcel.Application.Visible = True
objExcel.Workbooks.Add
objExcel.Cells(1, 1).Value = "Gaurav Seth55"
objExcel.ActiveWorkbook.Save "C:\GauravTest1.xlsx"
objExcel.ActiveWorkbook.Close
objExcel.Application.Quit



Instr.VBS ( To Check if particular text contains a particular word or not)

var="raju want to marry"
If instr(1,var,"want") Then
msgbox "Exist"
else
msgbox "doesnot Exist"
End If



To Take the Screenshots and then paste them in the word document
‘ Note that Filename of word has to exist at the specified path and you need to specifiy the name of the image


Dim sReportPath, sImagePath
sReportPath = "C:\QTP\ReportFiles\Filename1.doc"
sImagePath = "C:\QTP\ScreenShots\baby12.png"

Desktop.CaptureBitmap sImagePath, true
Set oWord = CreateObject("Word.Application")
oWord.DisplayAlerts = False
oWord.Visible = False
oWord.documents.open sReportPath

For i=1 to 3

Set oDoc = oWord.ActiveDocument
Set oRange = oDoc.content
oRange.ParagraphFormat.Alignment = 0
oRange.insertafter vbcrlf
oRange.collapse(0)
oRange.InlineShapes.AddPicture sImagePath, False, True
oWord.ActiveDocument.Save
Next

oWord.Application.Quit True
Set oRange = Nothing
Set oDoc = Nothing
Set oWord = Nothing


' FormatDate time function formats and returns the valid date or time expression

msgbox formatdatetime(Now, VBGeneralDate)
msgbox formatdatetime(Now, VBLongDate)
msgbox formatdatetime(Now,VBShortDate)
msgbox formatdatetime(Now, VBLongDate)
msgbox formatdatetime(Now, VBshorttime)

'Msgbox and its return types
a= "String"
msgbox a, VBOKonly
msgbox a, vbOKCancel
msgbox a,vbAbortRetryIgnore
msgbox a, vbYesNoCancel
msgbox a, vbYesNo
msgbox a, vbRetryCancel
msgbox a, VbCritical
msgbox a, vbQuestion
msgbox a, vbExclamation
msgbox a,VbInformation
msgbox a,vbDefaultButton1
msgbox a, vbDefaultButton2
msgbox a,vbDefaultButton4
msgbox a,vbApplicationModal
msgbox a,vbSystemModal

Response=msgbox(vbOK) '1
Response=msgbox (vbCancel) '2
Response=msgbox (VBAbort) '3
Response=msgbox(vbRetry) '4
Response=msgbox(VBIgnore) '5
Response=msgbox(VBYES) '6
Response=msgbox(VBNO) '7
Resposne=msgbox(VBOKCancel)


'VB Script Error Handling
On Error Resume Next
Err.Raise 23 ' Raise an overflow error.
MsgBox ("Error # " & CStr(Err.Number) & " " & Err.Description)
Err.Clear ' Clear the error.

'1 TO 4-,8,12,15 Unknown Run time error
''5- Invalid Procedure Call or Argument
'7 -out of memory
'9-Subscript out of Range
'10 -Array is fixed or temporarily locked
'11 -Division by Zero
'13-Type mismatch
'14- Out of String Space
'17 -Cant perform requested operation

' Using the Replace function of the Regular Expressions to Replace a word in the String
Function ReplaceTest(patrn, replStr)
Dim regEx, str1 ' Create variables.
str1 = "The quick brown fox jumped over the lazy dog."
Set regEx = New RegExp ' Create regular expression.
regEx.Pattern = patrn ' Set pattern.
regEx.IgnoreCase = True ' Make case insensitive.
ReplaceTest = regEx.Replace(str1, replStr) ' Make replacement.
End Function
MsgBox(ReplaceTest("fox", "cat")) ' Replace 'fox' with 'cat'.

' Creating the Text Files and writing lines to it
Dim fso, tf
Set fso = CreateObject("Scripting.FileSystemObject")
Set tf = fso.CreateTextFile("c:\baby.txt", True)
' Write a line with a newline character.
tf.WriteLine("Testing 1, 2, 3.")
' Write three newline characters to the file.
tf.WriteBlankLines(3)
' Write a line.
tf.Write ("This is a test.")
tf.Close

Description.Create Object. The will give the number of browsers that are opened and will give title of them

Dim oDesc 'Description Object
Dim colObject 'Object Collection

Set oDesc = Description.Create
'Remember to always use 'micclass' and not 'class name'
oDesc( "micclass" ).value = "Browser"

'We used Desktop as the parent here because, the Desktop Object holds all the Windows
Set colObject = Desktop.ChildObjects( oDesc )

'Retrieve # of open browsers
MsgBox colObject.Count

'Retrieve Titles of all open browsers
For x = 0 to colObject.Count - 1
MsgBox colObject(x).GetROProperty("title")
Next

No comments:

Post a Comment