Thursday, July 10, 2014

Errors & Solutions in QTP

While running QTP script, many times we face different issues. Here is the list of few commonly faced problems and solutions.

--To be updated--

Logic to verify the variable is Blank

The below logic verifies given variable is empty, null, blankstring, nullobject or value with 0. If any of the above is matching it returns true otherwise false.

Function IsBlank(Value) 'returns True if Empty or NULL or Zero
    If IsEmpty(Value) or IsNull(Value) Then
         IsBlank = True
        Exit Function
    ElseIf VarType(Value) = vbString Then
         If Value = "" Then
          IsBlank = True
          Exit Function
         End If
    ElseIf IsObject(Value) Then
         If Value Is Nothing Then
          IsBlank = True
          Exit Function
         End If
    ElseIf IsNumeric(Value) Then
         If Value = 0 Then
          wscript.echo " Zero value found"
          IsBlank = True
          Exit Function
        End If
    Else
         IsBlank = False
    End If
End Function

Wednesday, July 9, 2014

To get all the JavaList's Items or Values or List, From QTP

These is no inbuilt method to get the JavaList Items or Values.
Use the below logic to get the JavaList items

Logic:

Set objStatus = JavaWindow("MainWindow").JavaInternalFrame("InternalFrame1").JavaList("Status:").Object
 

'Getting the count of no.of Items/Values in JavaList
intCount = objStatus.getItemCount()
 

'Displaying the Count of JavaList Items
msgbox intCount
 

'Reading one by one Values of JavaList
For i = 0 To intCount-1 Step 1
    msgbox  objStatus.getItemAt(i).toString               
Next



Observation:  I am using UFT11.5, & The above code run fine. But when i reopened my library file having above code and when i put my mouse cursor in above part of the code, UFT is crashing. So, If you also face the same issue try the below code.


Set objStatus = JavaWindow("MainWindow").JavaInternalFrame("InternalFrame1").JavaList("Status:")
 

'Getting the count of no.of Items/Values in JavaList
intCount = objStatus.Object.getItemCount()
 

'Displaying the Count of JavaList Items
msgbox intCount
 

'Reading one by one Values of JavaList
For i = 0 To intCount-1 Step 1
    msgbox  objStatus
.Object.getItemAt(i).toString               
Next

Monday, July 7, 2014

Read putty output without using getVisibleText method from QTP

While reading the Putty output from QTP/UFT through "getVisibleText" method, it is observed  sometime that it may not recognize the text properly due to various reasons. So to read the exact output of Putty here is the solution.

Choose the Logging option from putty, set to log to write "All session output". Store all the session output in a file and Search for the required text from this file. From putty, Also choose option "Always Overwrite it" to avoid popups while running our QTP script.

Here is the QTP/UFT code to do above settings, Login to putty, execute few commands and close the Putty and search for the required text from the saved file.

Note: Please add the below all properties to Object repository (Its simple, it won't take more than 10min)

Public Function startPutty()
Dim SettingsFile,strHost,strLoginUser,strPassword,strPort, strPuttyPath
    strHost="10.11.12.13"
    strLoginUser="User03"
    strPassword = "Password"
    strPort=22
    'Here the Putty Path
    strPuttyPath="C:\PuTTY\putty.exe"

    'Start Putty
    SystemUtil.Run strPuttyPath
  
       Wait(2)
    

                 'Doing settings to save the PuTTY output to a file.
    Window("PuttyConfig").WinTreeView("Category:").Select "Session;Logging"
    Window("PuttyConfig").WinRadioButton("All session output").Set
                  'Provide Any Path, Use the same path while reading
    Window("PuttyConfig").WinEdit("Log file name:").Set "C:\PuTTY\putty.log"   
    Window("PuttyConfig").WinRadioButton("Always overwrite it").Set
    Window("PuttyConfig").WinTreeView("Category:").Select "Session"
    Window("PuttyConfig").WinRadioButton("SSH").Set

       Wait(2)
    Window("PuttyConfig").WinEdit("HostName").Set strHost
       Wait(1)
    Window("PuttyConfig").WinEdit("Port").Set strPort
    Window("PuttyConfig").WinButton("Open").Click
          Wait(3)
    Window("PuTTY").Maximize
    Window("PuTTY").Type strLoginUser
    Window("PuTTY").Type micReturn
         Wait(3)
    Window("PuTTY").Type strPassword
    Window("PuTTY").Type micReturn
    Window("PuTTY").Type "ls -lrt *.sh"
    Window("PuTTY").Type micReturn
    Window("PuTTY").Type "exit"
    Window("PuTTY").Type micReturn
      
    If window("PuTTY").Exist(0) Then
        Window("PuTTY").Close
        Window("PuTTY").Dialog("PuTTY Exit Confirmation").WinButton("OK").Click
    End If
      
       searchPuttyOutput "createSimulatorFiles.sh"

End Function

Public Function searchPuttyOutput(stringToBeSearched)
     'In Debug mode if we stop the script without setting the below 2 parameters to Nothing, It gives error in next run. So Added below 2 lines to avoid unnecessary errors.

        Set qfile=nothing
        Set fso=nothing


    Set fso=createobject("Scripting.FileSystemObject")
    Set qfile=fso.OpenTextFile("C:\PuTTY\putty.log",1,True)
        'Read  the entire contents of  priously written file

    If Instr(qfile.ReadAll,stringToBeSearched) > 0 Then
        MsgBox "String Found :)"
    Else
        MsgBox "String Not Found :("
    End If
    'Close the files
    qfile.Close
  
    'Release the allocated objects
    Set qfile=nothing
    Set fso=nothing
End Function

Tuesday, July 1, 2014

Access the files from Network Shared Location from Windows command prompt

Sometimes you may need to access the files from shared location from command prompt. For this you can't change to the Shared location directly by "cd". 

Here is the solution for this.

Use the command "net use <drive letter to map> <Shared Location path>
Ex: net use Z: \\ServerNameOrIP\SharedFolderName

In above example Z: is the Drive Letter you want to map, & map the drive with location "\\ServerNameOrIP\SharedFolderName". Once you execute the command path automatically changed to the above shared folder. 

Note: Don't give \ (Back slash) at the end of the path. otherwise it gives the error "The network path was not found"

Once you execute the above command, there is a drive shows mapped in My Computer along with other mapped drives.

The above Drive mapping is permanent. If you want to unmap, from command prompt use the below command 

net use <drive letter to unmap> /delete

ex usage for the Z: drive 
net use Z: /delete


Note2: If the drive letter is already used, it throws the error 
"The local device name is already in use"
Then try with new Drive name which is not used.


To temporarily map the drive use the below commands

pushd <Shared folder path>
ex: pushd \\Servername\sharedfolder

PUSHD \\SERVER\SHARE will silently map a drive. It starts at Z: and moves backwards until it finds an available letter.

after your use execute the below command or you can directly exit from command prompt
popd