QTP/UFT: Generate Alphanumeric and Random Number

”’## 3 digits Random idNumber and 7 digits Random serialNumber
    Dim start, ends
    start = 1
    ends = 19
    For m = start To ends
        idNumber = RandomNumber (111919)
        serialNumber = RandomNumber (11119991119999)
    Next

”## Generate Alphanumeric Number
Function generateAlphanumeric(StrLen)
Dim alpNumStr
Const MainStr= “0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ”
For i = 1 to StrLen
  alpNumStr=alpNumStr & Mid(MainStr,RandomNumber(1Len(MainStr)),1)
Next
generateAlphanumeric = alpNumStr
End Function
”## 3 digits Alphanumeric Number
serialNum = generateAlphanumeric(3)

QTP/UFT: Current Date Time Stamps and Julian-Date

”### Current Date Time Stamps
Dim currentDateTime, currentDate, currentTime, newDateYear
currentDateTime = Now
currentDate = Date
myDate = replace(currentDate,“/”,“-“)
myDay = Day(currentDate)
myMonth = Month(currentDate)
myYear = Year(currentDate)
myAbbMonth = MonthName(myMonth, true)
currentTime = Time
newDateYear = DateAdd(“yyyy”,3,currentDate)

” #### Use Date for currentDate or any date like “10/21/2011” #####
”cJulianDate = CalculateJulianDate(Date)
”msgbox cJulianDate
Function CalculateJulianDate(ByVal jDate)
    Dim jYear,jFirstDayOfYear,jDay,JulianDate
    ‘jDate = Date
    jYear = Year(jDate)
    jFirstDayOfYear = “01/01/”&jYear
    jDay = DateDiff(“d”Cdate(jFirstDayOfYear), Cdate(jDate)) + 1
    If Len(jDay)<3 Then
        jDayOnly = Right(“0” & jDay, 3)
        else
        jDayOnly = jDay
    End If
    JulianDate = (RIGHT(jYear,2))&jDayOnly
    CalculateJulianDate = JulianDate
End Function
” #### End Codify #####
 treeSelection = PbWindow(“w_Pb_frame”).PbWindow(“w_Pb_frame”).PbTreeView(“Pb_TreeView”).GetROProperty (“selection”)
serialNumber = trim(Right(treeSelection,5))  ”Last 5 digits from Right

”## If 3 digits Julian Date needed
cJulianDay = right(CalculateJulianDate(Date),3)

”## Add the All string number
addStrNumber = Code + cJulianDay + serialNumber

QTP/UFT: Input and Output Parameter

”##Create Test (Start) Parameter “myPin”
”##Create Action (Action1) Parameter “enterPinNumber”
”## Set the Parameter
Dim enterPinNumber
enterPinNumber = TestArgs(“myPin”)
‘msgbox enterPinNumber
”## Run Test with Test Parameter
PbWindow(“w_Pb_frame”).PbEdit(“Pb_edit”).SetSecure enterPinNumber

”## Run Action with Action Parameter
RunAction “openApp [open_Application]”, oneIteration, Parameter(“inputParameter”)

”## Create a Output Parameter “userIDNo”
”## Get the “idNum” Value at RunTime
idNum = trim(PbWindow(“w_Pb_frame”).PbWindow(“w_Pb_frame”).PbDataWindow(“dw_window”).GetCellData (11))
”## Set the Output Parameter Value
Parameter(“userIDNo”) = idNum

”## Select a Power Builder Cell that match from QTP/UFT Runtime Data Table
PbWindow(“w_Pb_frame”).PbWindow(“w_Pb_frame”).PbDataWindow(“dw_window”).SetCellData “#1”,“lastname”,DataTable.Value(“LastName”,addNewUser)
”Return Output Parameter “newUserLastName”
Parameter(“newUserLastName”) = DataTable.Value(“LastName”,addNewUser)

QTP\UFT: Action template

Action template in UFT

1.  Go to your UFT installation folder

2.  Go to dat folder and find ActionTemplate.mst file

UFT dat folder

3.  Open the ActionTemplate.mst file with Notepad

4.  Copy Paste below text and save it

Rem— Note: This is a Action Script for Power Builder App (Miah Borhanuddin – Jun, 17, 2014)

”’###########################################################
”Project Name: –    Application project Name
”Purpose: – 
”Author: –        Miah
”Date Created: – 4/9/2015
”Action Name: Action1
”Action Description:
”Resources:
”Change History: –
”***********************************************************
”’###########################################################

5.  Create a New GUI Test from UFT you will see following

UFT Action Template

 

QTP/UFT: Excel Data Import/Export

QTP/UFT: Excel Data Import/Export at Run-time DataTable
””##########################################################

Dim WshShell
‘Dim WshSysEnv
Set WshShell = CreateObject (“WScript.Shell”)

OSType = Environment.Value(“OS”)
OsXp = “Microsoft Windows XP”
XpPath = “C:\Documents and Settings\”& Environment.Value(“UserName”) &“\My Documents\”
‘Win7Path = “C:\Users\”& Environment.Value(“UserName”) &”\Documents\”  ”User Profile
Win7Path = WshShell.ExpandEnvironmentStrings (“%USERPROFILE%”) &“\Documents\” ”Current loged on Profile

”’Import data from Local or from QC at RunTime –TableName = SheetName–
Function addQCdata(FileName,TableName)
If FilePath = “” Then ”’If Local Data path not Define
ExcelPath=(“[QualityCenter\Resources] Resources\QTP Resources\AppDAT\”& FileName)
Else ”’If Local Data path is Defined
ExcelPath=(FilePath & FileName &“.xls”)
End If
SystemUtil.CloseProcessByName(“Excel.exe”)
DataTable.AddSheet (TableName)
DataTable.ImportSheet ExcelPath, TableName, TableName
Set ExcelPath = nothing
End Function

”’Export RunTime Data to Documents Folder –TableName = SheetName–
”’Save Runtime Data in Documents Folder as a Excel format
Function SaveReport(FileName,TableName)
OSType = Environment.Value(“OS”)
UserID = Environment.Value(“UserName”)
OsXp = “Microsoft Windows XP”

If OSType = OsXp Then
DataTable.ExportSheet XpPath&FileName&“.xls”, TableName
Else
DataTable.ExportSheet Win7Path&FileName&“.xls”, TableName
End If
End Function

Rem — ############## OR ##############

”’Export RunTime Data to Documents Folder –TableName = SheetName–
Function exportData(FileName,TableName)
OSType = Environment.Value(“OS”)
UserID = Environment.Value(“UserName”)
OsXp = “Microsoft Windows XP”

If OSType = OsXp Then
DataTable.ExportSheet “C:\Documents and Settings\”& UserID &“\My Documents\”&FileName&“.xls”, TableName
Else
DataTable.ExportSheet “C:\Users\”& UserID &“\Documents\”&FileName&“.xls”, TableName
End If
Set OSType = nothing
Set UserID = nothing
Set OsXp = nothing
End Function

”’To use this Function You have to Use a SetUp.xls File  –TableName = SheetName–
”’Download the SetUp.xls File From Quality Center (ALM)
”’Define the Path in the DataPath Sheet
”’Example:
”’ScriptName : 02-UserLogin
”’LocalDataFilePath: C:\Users\userid\Documents\AppData\Automation_Data\
”’Move the ” SetUp.xls ” file to My Documents for Xp or Documents for Documents
Function AddALMData(FileName,TableName)
On Error Resume Next
SystemUtil.CloseProcessByName(“Excel.exe”)
DataTable.AddSheet (“DataPath”)
If OSType = OsXp Then
DataTable.ImportSheet XpPath&“SetUp.xls” ,“DataPath”,“DataPath”
Else
DataTable.ImportSheet Win7Path&“SetUp.xls” ,“DataPath”,“DataPath”
End If
pathRows=DataTable.GetSheet(“DataPath”).GetRowCount
For p=1 to pathRows
DataTable.GetSheet(“DataPath”).SetCurrentRow(p)
If DataTable.Value(“ScriptName”,“DataPath”)=Environment.Value(“TestName”Then
If DataTable.Value(“LocalDataFilePath”,“DataPath”) > “0” Then
FilePath= DataTable.Value(“LocalDataFilePath”,“DataPath”)
‘msgbox FilePath
else
Reporter.ReportEvent micDone, “DataFile”“App Data File is not Setup from Local”
End If
End If
Next

If FilePath = “” Then
ExcelPath=(“[QualityCenter\Resources] Resources\QTP Resources\AppDATA\”& FileName)
Reporter.ReportEvent micDone, “DataFile”“App Data File is Setup from QC”
Else
ExcelPath=(FilePath & FileName &“.xls”)
Reporter.ReportEvent micDone, “DataFile”“App Data File is Setup from Local”
End If
DataTable.AddSheet (TableName)
DataTable.ImportSheet ExcelPath, TableName, TableName
‘   DataTable.ImportSheet ExcelPath, TableName, Environment.Value(“ActionName”)     ”## You can edit script to make it Datatable Per Action
Set FilePath = nothing
Set ExcelPath = nothing
End Function

”’ How to Use It, Call it in your Script, FileName, SheetName
Call AddALMData(“AppTestDataCNN”,“UserLogon”)

rowCnt = DataTable.GetSheet(“UserLogon”).GetRowCount
For i=1 To rowCnt
DataTable.GetSheet(“UserLogon”).SetCurrentRow(i)
”’Your Condition
”’Your Condition
”’Your Condition
Next

 

QTP/UFT: REMOVE QC/ALM HTML STRING FROM ORIGINAL STRING


'UTILITY.QFL
'REMOVE QC/ALM HTML STRING FROM ORIGINAL STRING
'Created by : Miah, Create date 05/24/2013
'strFul = Origional string
'strRemov1 = Any unwanded string first time will remove
'strRemov2 = Any unwanded string second time will remove
Public Function strReplace(strFul,strRmov1,StrRmov2)
	dCot = "&quot;"
	strReplace =Replace(strFul, strRmov1, "")
	strReplace =Replace(strReplace, strRmov2, "")
	strReplace =Replace(strReplace, dCot, "'")
End Function

'FIND LAST FOLDER NAME FROM TEST PLAN DIRECTORY
'Created by : Miah, Create date 05/28/2013
Public Function findFoldNm(fulPath)
	spltStr=Split(fulPath,"\")
	cnt = uBound(spltStr) + 1
	'MsgBox cnt
	itr =0
	for each x in spltStr
		itr = itr + 1
		if cnt = itr Then
			findFoldNm = x
		End If
	next
End Function

'Get KEY and VALUE IN ARRY 
'input keyValuArray = (Test1=1, Test2=2) and key = (test2) output will be 2 
Public Function getKeyValue(keyValu,key)
	aryKeyValue =""
	For i = 1 To UBound(keyValu) Step 1
		If key= keyValue(keyValu(i))(0) Then
			getKeyValue= keyValue(keyValu(i))(1)
			Exit For
		End If
	Next
End Function

'GET KEY and VALUE
'Created by : Miah, Create date 06/05/2013
'input Test=1 output will be 1
Public Function keyValue(keyVal)
	spltStr=Split(keyVal,"=")
	If UBound(spltStr) = 0 Then
		keyVal = keyVal & "="
		spltStr=Split(keyVal,"=")
	End If
	spltStr(0) = Replace(spltStr(0)," ","")'blank space not allowed in key
	keyValue = spltStr
End Function

'DELETE EXCEL FILE
Public Function dltExlFile(fPath)
    Set fso = CreateObject("Scripting.FileSystemObject")
    fExist = fso.FileExists(fPath & ".xls")
    If (fExist = True) Then
    	fso.DeleteFile(fPath & ".xls")
    End If
End Function

'REMOVE ANY UNWANTED STRING FROM ORIGIONAL STRING
'strFul = Origional string
'strRemov1 = Any unwanded string first time will remove
'strRemov2 = Any unwanded string second time will remove
Public Function strReplace(strFul)
	Dim strRmov (15)

	strRmov(1)="<html><body><div align=""left"">"
	strRmov(2)="</div></body></html>"
	strRmov(3)="<html>"
	strRmov(4)="<body>"
	strRmov(5)="</html>"
	strRmov(6)="</body>"
	strRmov(7)="<html><body>"
	strRmov(8)="</html></body>"
	strRmov(9)="</span></font></div></body></html>"
	strRmov(10)="</span></font>"
	strRmov(11)="<font face=""Arial""><span style=""font-size:8pt"">"
	strRmov(12)="<div align=""left""><font face=""Arial""><span style=""font-size:8pt"">"
	strRmov(13)="<div align=""left"">"
	strRmov(14)="</div>"
	strRmov(15)="&nbsp;"
	
	strReplace =strFul
	For i = 1 To UBound(strRmov) Step 1
		strReplace =Replace(strReplace, strRmov(i), "") 'Remove all Unwanted character
		strReplace = Replace(strReplace, vbCrLf, "")'Remove new line from the string
	Next
	strReplace =Replace(strReplace, "&quot;", "'")
	
End Function

'Find login information from the text file
Public Function getLogInfo(key)
	getLogInfo=""
	fPath = Environment.Value("txFilePath")

	Set filSys = CreateObject("Scripting.FileSystemObject")
	Set filOpn = filSys.OpenTextFile(fPath) 'Get main text file
    
	Do While filOpn.AtEndOfStream <> True And getLogInfo=""
		redFile = filOpn.ReadLine
    	ck = InStr(redFile, key)
    	If (ck <> 0) Then
    		spltStr=Split(redFile,"=")
			If UBound(spltStr) > 0 Then    		
    			getLogInfo = spltStr(1)
    		End If
     	End If
	Loop ' End For loop here
End Function