To Close all Internet Explorers

Before going to run your script needs to close all IEs,it helps you to work with QTP without any object /browser identification problems.

just have a look here,

Public function closeAllBrowsers()
Dim qtStartTime, ieStartTime, strComputer, objWMIService,ieProcessList,qtProcessList

strComputer = “.”
Set objWMIService = GetObject(“winmgmts:\\” & strComputer & “\root\cimv2″)

Set ieProcessList = objWMIService.ExecQuery (“Select * from Win32_Process Where Name = ‘iexplore.exe’”) to get all list  of IE  which are in process
Set qtpProcessList = objWMIService.ExecQuery (“Select * from Win32_Process Where Name = ‘qtpro.exe’”) ‘To get all list of qtp which are in process

For Each qtpProcess in qtpProcessList
dtStartTime = qtProcess.CreationDate    Date Time Month at the time of creation
qtStartTime = WMIDateStringToDate(dtmStartTime) ‘Returns qtp start time
Next

For Each ieProcess in ieProcessList
dtStartTime = ieProcess.CreationDate
ieStartTime = WMIDateStringToDate(dtmStartTime)  ‘Returns IE start time

If ieStartTime > qtStartTime Then    ‘If any IE browsers opened (process running) before QTP  process then it will close all
SystemUtil.CloseProcessById ieProcess.processid
End If
Next
End Function

‘To get Process time
Function WMIDateStringToDate(dtStart)
WMIDateStringToDate = CDate(Mid(dtmStart, 5, 2) & “/” & Mid(dtmStart, 7, 2) & “/” & Left(dtmStart, 4) & ” ” & Mid (dtmStart, 9, 2) & “:” & Mid(dtmStart, 11, 2) & “:” & Mid(dtmStart, 13, 2))
End Function

Locale Settings

We can set as per our local time/zone,time formats…

1.SetLocale(lcid)

a.if lcid=0 then the locale is set to match the current system setting

Msgbox (SetLocale(0))–>1033

b.Msgbox (SetLocale(“en-gb”))–>1033

c.SetLocale(currentLocale)-->1033

Note:1033 denotes “English – United States” Zone

by using this u can customize your time formats,zones,etc

 

To Get repeat sequences of random numbers

Suppose if you want to test your application functionality with same repeated sequence of random number then use Rnd(-1)

here use Rnd argument number as -1

Script:

Dim MyValue, Response
Randomize   ‘ Initialize random-number generator.
Do Until Response = vbNo
MyValue = Int((9 * Rnd(-1)) + 1)   ‘ Generate random value between 1 and 9.
MsgBox MyValue
Response = MsgBox (“Is it same that prev value “, vbYesNo)
Loop

Find YOur Zone Time

'If you want find specific local time zone. Function Local_TimeZone()

Dim strComputer,oWMIService,oComputer, allColComputer
strComputer = “.”
Set oWMIService = GetObject(“winmgmts:” _
& “{impersonationLevel=impersonate}!\\” _
& strComputer & “\root\cimv2″)
Set allColComputer = oWMIService.ExecQuery _
(“Select * from Win32_TimeZone”)
For Each oComputer in allColComputer
msgbox oComputer.Description
Exit For
Next
Set oComputer = Nothing
Set allColComputer = Nothing
Set oWMIService = Nothing

End Function

How to lock your system automatically

’If you want lock your sustem once your script running is done automatically just you call the below function.

‘If you want lick ur system(for manual users)save this function in a .VBS file and then give one double click.

Function Lock_Mysystem()

Dim oSys,Syscmd

Set oSys = CreateObject("WScript.Shell")

Syscmd = "%windir%\SYSTEM32\rundll32.exe user32.dll,LockWorkStation"

oSys.Run Syscmd, 0, False

End Function
------------------------------------------------
Lock_Mysystem()'to call the function

Customize your excel

Function ExcelCustomization(resultsPath,Filename,nameSheet, rvalue)

Set ExcelFile = CreateObject(“Excel.Application”)
Set wb = ExcelFile.Workbooks.Open (resultsPath & Filename&”.xls”) ‘To  open  the  existing  work book

ExcelFile.Visible=False
wb.Sheets(nameSheet).select                                                                                                                                                                                                                  ‘To select  specific XL sheet
RCols = wb.Sheets(nameSheet).UsedRange.Columns.Count  ‘To get  the  number of  columns used
Rrows=wb.Sheets(nameSheet).UsedRange.Rows.Count    ‘To get  the  number of   rows used
For cl=1 to RCols
wb. Sheets(nameSheet).Cells(1,cl).Interior.ColorIndex = 53  ‘To set the color to the interior  of  the XLSheet
wb. Sheets(nameSheet).Cells(1,cl).Font.ColorIndex = 19   ‘To set the color to the fonts  of  the XLSheet
wb. Sheets(nameSheet).Cells(1,cl).Font.Bold=True
wb. Sheets(nameSheet).Columns(cl).Autofit  ‘To set  the  columns  as autofit
wb. Sheets(nameSheet).Cells(1,cl).Borders(1).LineStyle = 1  ‘To Set the Borders
wb. Sheets(nameSheet).Cells(1,cl).Borders(2).LineStyle = 1
wb. Sheets(nameSheet).Cells(1,cl).Borders(3).LineStyle = 1
wb. Sheets(nameSheet).Cells(1,cl).Borders(4).LineStyle = 1

For j=2 to Rrows
rvalue = wb.Sheets(nameSheet).cells(j,cl).Value
If StrComp(rvalue,”PASS”,1)=0 Then
Status=”"
wb. Sheets(nameSheet).Cells(j,cl).Font.ColorIndex=50
wb. Sheets(nameSheet).Cells(j,cl).Font.Bold=True
wb. Sheets(nameSheet).Columns(cl).Autofit
wb. Sheets(nameSheet).Cells(j,cl).Borders(1).LineStyle = 1
wb. Sheets(nameSheet).Cells(j,cl).Borders(2).LineStyle = 1
wb. Sheets(nameSheet).Cells(j,cl).Borders(3).LineStyle = 1
wb. Sheets(nameSheet).Cells(j,cl).Borders(4).LineStyle = 1
ElseIf StrComp(rvalue,”FAIL”,1)=0 Then
Status=”"
wb. Sheets(nameSheet).Cells(j,cl).Font.ColorIndex=3
wb. Sheets(nameSheet).Cells(j,cl).Font.Bold=True
wb. Sheets(nameSheet).Columns(cl).Autofit
wb. Sheets(nameSheet).Cells(j,cl).Borders(1).LineStyle = 1
wb. Sheets(nameSheet).Cells(j,cl).Borders(2).LineStyle = 1
wb. Sheets(nameSheet).Cells(j,cl).Borders(3).LineStyle = 1
wb. Sheets(nameSheet).Cells(j,cl).Borders(4).LineStyle = 1
End If
wb. Sheets(nameSheet).Cells(j,cl).Interior.ColorIndex = 19
wb. Sheets(nameSheet).Columns(cl).Autofit
‘wb. Sheets(nameSheet).Cells(j,cl).Font.Bold=100
wb. Sheets(nameSheet).Cells(j,cl).Borders(1).LineStyle = 1
wb. Sheets(nameSheet).Cells(j,cl).Borders(2).LineStyle = 1
wb. Sheets(nameSheet).Cells(j,cl).Borders(3).LineStyle = 1
wb. Sheets(nameSheet).Cells(j,cl).Borders(4).LineStyle = 1
Next
Next
wb.Sheets(“Con”).select
CCols = wb.Sheets(“Con”).UsedRange.Columns.Count
Crows=wb.Sheets(“Con”).UsedRange.rows.count

For ccl=1 to CCols
wb. Sheets(“Con”).Cells(1,ccl).Interior.ColorIndex = 53
wb. Sheets(“Con”).Cells(1,ccl).Font.ColorIndex = 19
wb. Sheets(“Con”).Cells(1,ccl).Font.Bold=True
wb. Sheets(“Con”).Columns(ccl).Autofit                                                                                                                                                                                                ‘Set Column width
wb. Sheets(“Con”).Cells(1,ccl).Borders(1).LineStyle = 1    ‘To Set the Borders
wb. Sheets(“Con”).Cells(1,ccl).Borders(2).LineStyle = 1
wb. Sheets(“Con”).Cells(1,ccl).Borders(3).LineStyle = 1
wb. Sheets(“Con”).Cells(1,ccl).Borders(4).LineStyle = 1

For crc=2 to crowCount+1
wb. Sheets(“Con”).Cells(crc,ccl).Interior.ColorIndex = 19
wb. Sheets(“Con”).Cells(crc,ccl).Font.Bold=200
wb. Sheets(“Con”).Columns(ccl).Autofit
wb. Sheets(“Con”).Cells(crc,ccl).Borders(1).LineStyle = 1
wb. Sheets(“Con”).Cells(crc,ccl).Borders(2).LineStyle = 1
wb. Sheets(“Con”).Cells(crc,ccl).Borders(3).LineStyle = 1
wb. Sheets(“Con”).Cells(crc,ccl).Borders(4).LineStyle = 1
If StrComp(Environment.Value(“EOT”),”TT”,1)=0 Then
For rc= crowCount+4 to Crows
For cc=1 to 2
wb. Sheets(“Con”).Cells(rc,cc).Interior.ColorIndex = 19
wb. Sheets(“Con”).Cells(rc,cc).Font.ColorIndex = 53
wb. Sheets(“Con”).Cells(rc,cc).Font.Bold=200
wb. Sheets(“Con”).Columns(cc).Autofit
wb. Sheets(“Con”).Cells(rc,cc).Borders(1).LineStyle = 1
wb. Sheets(“Con”).Cells(rc,cc).Borders(2).LineStyle = 1
wb. Sheets(“Con”).Cells(rc,cc).Borders(3).LineStyle = 1
wb. Sheets(“Con”).Cells(rc,cc).Borders(4).LineStyle = 1
Next
Next
End If
Next
Next
wb.Save
ExcelFile.Quit
Set ExcelFile = nothing
Set wb= nothing
Set ExcelSheet = nothing
End If

End Function

How to get a Specific string from an array

Suppose if you want to get a specific string within an array that you don’t know on which position it is located,in this case we can go for filter condition.

Filter function help you to filter a specific sub string in a string

Return Value:

Returns a zero-based array containing a subset of a string array based on a specified filter criteria.

Filter(InputStrings, Value[, Include[, Compare]])
Arguments:
InputStrings :
Required. One-dimensional array of strings to be searched. 
Value :
Required. String to search for. 
Include :
Optional. Boolean value indicating whether to return substrings that include  or exclude Value. If Include is True, Filter returns  the subset of the array that contains Value as a substring. If Include is False, Filter returns the subset of the array that does not  contain Value as a substring. 
Compare :
Optional. Numeric/Text comparision
Example:
Dim MyIndex
Dim MyArray (3)
MyArray(0) = "Sunday"
MyArray(1) = "Monday"
MyArray(2) = "Tuesday"
MyIndex = Filter(MyArray, "Mon") ' MyIndex(0) contains "Monday".
Remarks:If no matches of Value are found within InputStrings,  Filter returns an empty array. An error occurs if InputStrings is  Null or is not a one-dimensional array.
The array returned by the Filter function contains only enough  elements to contain the number of matched items.
Thanks,
Chinnu

Get Proper/Specific Text from a String

1. To get Position of 1st occurence of the string within the string

mvar=Instr(“VB Script is fun!”,”is”)

msgbox(mvar)’returns 1st occurence position of the string/letter within another string

2.To get specific length of the string

MyVar = Mid(“VB Script is fun!”, mvar, 2)
msgbox(MyVar)  ‘ Returns ” is”

3.Suppose if you want to get specific text from a string,when you know the required string within another string but you dont know where it is in.

In this case you can use the following method to get directly:

mvar=Mid(“VB Script is fun!”,Instr(“VB Script is fun!”,”is”),2)
msgbox(mvar)  ‘ Returns  “is”

Example: Try this “I want to be a good quality engineer” find the specific text “quality”,whether it is presented in the given string or not? and get the same

Thanks,

Chinnu

Follow

Get every new post delivered to your Inbox.