Search This Blog

Sunday, November 20, 2011

Flex:- Function for selecting the item from the spark dropdown list.

' Name: Fun_SelectDropdownListValue
' Purpose: This function is used for selecting the item from the spark dropdown list
' Argument(s):
'     ip_ObjDropDown: Object under the test EX: SparkDropDownList
'     ip_strDropdownName: Name of the dropdown ex:Merchandise
'     ip_strSelectableItemValue: Value to be selected from the dropdown. Ex: All Mdse
' Returns:
'     1: If expected value is found under the dropdown
'     0: If expected value is NOT found under the dropdown
' Example: Call Fun_SelectDropdownListValue(objDropdown, "Merchandise", "All Mdse")
Public Function Fun_SelectDropdownListValue(ByRef ip_ObjDropDown, ip_strDropdownName, ip_strSelectableItemValue)
 Dim objDicListIndex, intTotalItemCnt, blnScrollBarExists, intMaxScrollValue, intCurrentScrollPoint
 Dim objSparkListLabelDescr, objSparkListLabelDescrChild
 Dim intChildIndex, strListLabel
 Set objDicListIndex = CreateObject("Scripting.Dictionary")
 '<<< Validate the current selection before making a new selection >>>
 strListLabel = ip_ObjDropDown.GetROProperty("selecteditem")
 If Trim(strListLabel) = Trim(ip_strSelectableItemValue) Then
  Fun_SelectDropdownListValue = 1
  Exit Function
 End If 

 'Get the total items under the list
 intTotalItemCnt = ip_ObjDropDown.GetItemsCount
 '<<< Get the maximum possible scroll point based on the existence of scroll bar object >>>
 Wait 2
 If ip_ObjDropdown.SparkScrollBar("verticalScrollBar").Exist(2) Then
  blnScrollBarExists = True
        intMaxScrollValue = ip_ObjDropDown.SparkScrollBar("verticalScrollBar").GetROProperty("maximum")
  blnScrollBarExists = False
  intMaxScrollValue = 0
 End If 

 '<<< Get all child objects of type SparkListLabel >>>
 Set objSparkListLabelDescr = Description.Create()
 objSparkListLabelDescr("automationclassname").Value = "SparkListLabel"
 '<<< Extract all child objects until the maximum scroll point is reached >>>
  '<<< Move the scroll bar based on the child objects extracted within the loop >>>
  If blnScrollBarExists = True Then
   intCurrentScrollPoint = 20 * objDicListIndex.Count
   ip_ObjDropDown.SparkScrollBar("verticalScrollBar").Change intCurrentScrollPoint
   Wait 2
   intCurrentScrollPoint = 1
  End If 
  '<<< Get child objects >>>
  Set objSparkListLabelDescrChild = ip_ObjDropDown.ChildObjects(objSparkListLabelDescr)
  '<<< Fill the dictionary object based on the child objects extracted >>>>
  For intChildIndex = 0 To objSparkListLabelDescrChild.Count -1
   strListLabel = objSparkListLabelDescrChild(intChildIndex).GetROProperty("label")
   If Trim(strListLabel) = Trim(ip_strSelectableItemValue) Then
    Fun_SelectDropdownListValue = 1 'Indicates success
    Exit Function
   ElseIf Not objDicListIndex.Exists(Trim(strListLabel)) And Trim(strListLabel) <> "" Then
    objDicListIndex.Add Trim(strListLabel), objDicListIndex.Count
   End If
  '<<< Exit the Loop on scroll bar reaching the maximum end point >>>
  If intCurrentScrollPoint > intMaxScrollValue Then
   Exit Do
  End If     
 subJCP_EventReport "Fail", "Unable to find item as: " & ip_strSelectableItemValue & " under the given dropdown: " & ip_strDropdownName, Browser("FlexApp")    
 Fun_SelectDropdownListValue = 0 'Indicates failure
End Function
'<<< ---------- Register this function to the SparkDropdownList object ----------- >>>
RegisterUserFunc "SparkDropDownList", "Fun_SelectDropdownListValue", "Fun_SelectDropdownListValue"

returnValue = .SparkDropDownList("timeDropDown").Fun_SelectDropdownListValue("Time", "Custom...")
  If Cstr(returnValue) <> "1" Then
  End If

QTP -> Function for providing the synch to the application object

' Name: Fun_SynchObjectToAppear
' Purpose: This function is used for providing the synch to the application object
' Argument(s):
'     ip_obj: Object under the test EX: SparkDropDownList
'     ip_intStartTime: Start time from which the max wait time  is evaluated. Ex: Timer value at the time of calling this function
'     ip_intWaitTimeInSec: Maximum wait time for object to appaer in the application and get visible
' Returns:
'      NON Zero Value: Indicates that the object is appeared on the screen and is visible.
'        0 Indicates that the object is not available under the application after waiting for the given max wait time.  
' Example: returnValue = Fun_SynchObjectToAppear(objSparkApplication, Timer, "360")
Public Function Fun_SynchObjectToAppear(ByRef ip_Obj, ip_intStartTime, ip_intWaitTimeInSec)
 Dim blnObjectLoaded, intStartTime, intEndTime
 blnObjectLoaded = False 'Set the flag to false to indicate that the object is not loaded
 intStartTime = ip_intStartTime 'Start tiem is set as the passed argument value
  If ip_Obj.Exist(1) Then
   If CStr(ip_Obj.GetRoProperty("visible")) = "True" Then
     blnObjectLoaded = True 'Reset the flag to indicate that the object is appeared on the application
     Exit Do 'Exit the loop if the expected object is exists and is visible in the application
   End If
  End If   
  intEndTime = Timer 'Set the current time as end time
 Loop Until intEndTime > intStartTime + ip_intWaitTimeInSec 'Break the loop if the end time reached as more than the max wait time

 '<<< Report the report based upon the load event on the tab selection >>>
 If blnObjectLoaded = True Then
   Fun_SynchObjectToAppear = (intEndTime - ip_intStartTime)/60 'Return the total time taken in minutes as a return value
   Fun_SynchObjectToAppear = "0" 'Return ZERO to indicate that the object is not appeared on the screen
 End If
End Function
'<<< ---------- Function registration----------- >>>
RegisterUserFunc "SparkDropDownList", "Fun_SynchObjectToAppear", "Fun_SynchObjectToAppear"
RegisterUserFunc "SparkTextInput", "Fun_SynchObjectToAppear", "Fun_SynchObjectToAppear"
RegisterUserFunc "SparkCheckBox", "Fun_SynchObjectToAppear", "Fun_SynchObjectToAppear"
RegisterUserFunc "SparkButton", "Fun_SynchObjectToAppear", "Fun_SynchObjectToAppear"

Example : -

 '<<< Synch provided until the filter object gets displayed in the application >>>
 Set objSparkApplication = Browser("FlexApp").FlexApplication("app").SparkApplication("erfr-flex-gui.swf").SparkSkinnableContainer("showHidePane").SparkButton("Filters")
 intWaitTimeInSec = intConstWaitShort * 60
 '<<< Calling user defined function for providing application object synchronization >>>
 returnValue = Fun_SynchObjectToAppear(objSparkApplication, Timer, intWaitTimeInSec)

QC-> Code to get the user id and Password from QC resources tab

Preconditions: -
You need to upload a excel sheet in the QC resources tab, as below:

Key    Value
UID   naresh

'Name:   Fun_GetCredentials
'Purpose:  This function will get the credential details from the QC resource
'Argument(s): ip_intResourceId: Resource ID of the item
'Returns:  0: Indicates failure
'     Credential details as: UserID|Password
'Example: returnValue = Fun_GetCredentials("1055")
'Revised by:
' **********************************************************************************************************************
Function Fun_GetCredentials(ip_intResourceId)
 Dim objQCResource, objDataSheet, intDSRowCnt, intDSRowIndex, strUserID, strPwd
 On Error Resume Next
  'Get the resource object from QC with resource id as ip_intResourceId
  Set objQCResource = QCUtil.QCConnection.QCResourceFactory.Item(ip_intResourceId)

  'Download the resource file into the result directory
  objQCResource.DownloadResource Environment("ResultDir"), True

  'Get the sheet 1 of resource file from result directory into the design time datatable at sheet 1
  DataTable.ImportSheet Environment("ResultDir") & "\" & objQCResource.Filename, 1, 1

  'Get the object Datasheet from the design time datatable
  Set objDataSheet = DataTable.GetSheet(1)  
  intDSRowCnt = objDataSheet.GetRowCount

  'Loop for each row and validate the Key column, based on key value get value column value
  For intDSRowIndex = 1 To intDSRowCnt
   Select Case UCase(Trim(DataTable.Value(intConstKeyColIndex, 1)))
    Case "UID"
     strUserID = Trim(DataTable.Value(intConstValueColIndex, 1))
    Case "PWD"
     strPwd = Trim(DataTable.Value(intConstValueColIndex, 1))
   End Select
  'Set the return value
  If Trim(strUserID) <> "" And Trim(strPwd) <> "" Then
   Fun_GetCredentials = strUserID & "|" & strPwd
   Fun_GetCredentials = 0
   subJCP_EventReport "Fail", "Required credential details are not found. " & VBNewLine & "User ID = " & strUserID & VBNewLine & "Password = " & strPwd, Nothing
   Exit Function
  End If 
  'If unexpected error is reported here
  If Trim(CStr(Err.Description)) <> "" Then
    Fun_GetCredentials = 0
    subJCP_EventReport "Fail", "Unable to get login credentials. Error: " & Err.Description & VBNewLine &  Err.source, Nothing
  End If
 On Error GoTo 0

 'Delocate the reference
 Set objQCResource = Nothing
 Set objDataSheet = Nothing
End Function

Then use the below function like :

Example: -
 '<<< ---- If Credential pop up is loaded, get the JCP credentials from the Datatable () :Begins ---->>>
 If Browser("FlexApp").Dialog("Credential Popup").Exist(intConstWaitShort) Then
  returnValue = Fun_GetCredentials("1035")
  If Trim(CStr(returnValue)) = "0" Then
   arrCredentials = Split(returnValue, "|")
   Browser("FlexApp").Dialog("Credential Popup").WinEdit("User name:").Set arrCredentials(0)
   Browser("FlexApp").Dialog("Credential Popup").WinEdit("Password:").Set arrCredentials(1)
   Browser("FlexApp").Dialog("Credential Popup").WinButton("OK").Click
  End If
 End If
 '<<< --- If Credential pop up is loaded, get the JCP credentials from the Datatable () :Ends ------->>>