Saturday, September 3, 2011

SAP Edit box(SAPGuiEdit) Validation Code

'***********************************************************
*******************************************************
'SAPGuiEdit Customize Field Validation Code -
'Name : fieldValidation()
'Purpose : To validate a text field
'Author : MindLess
'Input : Edit Box name, label text, error level
' : expectedVal special cases:
' : "<no_validation>" if validation is not required for the componenet
' : "<blank>" if it is required to check blank value i.e. expected value is blank.
'     : "<any_value>" to check any value except blank
'Output : Temp results stored in buffer excel file
'Return : NA
'Assumptions : None
'Note : None
'******************************************************************************************************************
sub fieldValidation(boxname,expectedVal,errorLevel)
If expectedVal = "<no_validation>" Then
Exit Sub
End If
'on error resume next
Dim sessionDesc,winDesc,childDesc,winDescModal,obj,window,blnEdtPresent,val,field,i
Set sessionDesc = description.Create()
sessionDesc("type").value = "GuiSession"
Set winDesc = description.Create()
winDesc("type").value = "GuiMainWindow"
Set winDescModal = description.Create()
winDescModal("type").value = "GuiModalWindow"
Set edt = description.Create()
edt("type").value = "Gui.*TextField"
edt("name").value = boxname
blnEdtPresent = false
If SAPGuiSession(sessionDesc).SAPGuiWindow(winDesc).Exist(2) Then
Set window =SAPGuiSession(sessionDesc).SAPGuiWindow(winDesc)
else
Set window = SAPGuiSession(sessionDesc).SAPGuiWindow(winDescModal)
End If
set obj = window.SAPGuiEdit(edt)

If obj.exist(2) Then
val= obj.getroproperty("text")
field = obj.getroproperty("DefaultTooltip")
If val<>"" Then
If expectedVal="" Or expectedVal="<any_value>" Then
blnEdtPresent=true
End If
If expectedVal<>"" and expectedVal=val Then
blnEdtPresent=true
End If
''***********to validate with blank and any value ************
Elseif expectedVal = "<blank>" Then
blnEdtPresent=true
'**************************************************************************************************************
Else
blnEdtPresent=false
End If
i=rowCount()
If blnEdtPresent = true Then
Reporter.ReportEvent 0,"Value displayed for "& field&" is - "&val,""&i+1
 
elseif blnEdtPresent= false and val<>"" then
Reporter.ReportEvent 1,""&errorLevel&" : "&"Value displayed for "&field&" is - "&val&" and expected was - "&expectedVal,""&i+1

elseif blnEdtPresent= false and val="" then
Reporter.ReportEvent 1,""&errorLevel&" : "&field&" is missing ",""&i+1
End If
End if

Set sessionDesc= nothing
Set winDesc = nothing
Set obj = nothing
Set window = nothing
Set winDescModal = nothing
end sub