Monday, July 25, 2011

QTP - Question and Answers - 1

1.  What are the differences between function and action?



Note: It depends on the comfortably/requirement for any person to decide which one is better among Action Vs Function.

2. How can you scroll down or up in a page using QTP?

There are couple of ways for doing this.

'For page up
Set Obj=Browser("browsername").Page("pagename").Object.body
Obj.doScroll("pageUp")

'For page down
Set Obj=Browser("browsername").Page("pagename").Object.body
Obj.doScroll("pageDown")

Or you can also the following Send Keys method:
'For page up
Set objShell=CreateObject("WScript.Shell")
objShell.SendKeys "{PGUP}"

'For page down
Set objShell=CreateObject("WScript.Shell")
objShell.SendKeys "{PGDN}"

Friday, July 22, 2011

QTP Framework - Retrieving folder paths

Assume that you are using some framework with different folders. You might need to access these folders multiple times in your script. In such cases it would be easy if you can access them directly with simple statements.

Please look at the following Folder structure:




This is just a sample folder structure. Now create a VBS file(say for example with the name PathToFolder.Vbs) with the following function:

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Function getFolderPath(ByVal s_Folder)
    Dim sCurrentPath
    Dim iLocation
    Dim sFolderPath
   
    sCurrentPath = Environment.Value("TestDir")

   iLocation =  Instr(1,sCurrentPath,"QTP Automation")

   sFolderPath = mid(sCurrentPath,1,iLocation+14)
 

    Select Case LCase(s_Folder)
        Case "commonlibs"
               sFolderPath = sFolderPath & "ABC Automation\Libraries\Common Libraries\"
        Case "ps_libs"
                  sFolderPath = sFolderPath & "ABC Automation\Libraries\Project Specific\"
        Case "logs"
                sFolderPath = sFolderPath & "ABC Automation\Logs\"
        Case "results"
                sFolderPath = sFolderPath & "ABC Automation\Results\"
        Case "commonscripts"
                sFolderPath = sFolderPath & "ABC Automation\Scripts\Common Scripts\"
        Case "ps_scripts_hl"
                sFolderPath = sFolderPath & "ABC Automation\Scripts\Project Specific Scripts\High Level\"
        Case "ps_scripts_ml"
                sFolderPath = sFolderPath & "ABC Automation\Scripts\Project Specific Scripts\Medium Level\"
        Case "ps_scripts_ll"
                sFolderPath = sFolderPath & "ABC Automation\Scripts\Project Specific Scripts\Low Level\"
        Case "testcase_hl"
                sFolderPath = sFolderPath & "ABC Automation\Test Cases\High Level\"
        Case "testcase_ml"
                sFolderPath = sFolderPath & "ABC Automation\Test Cases\Medium Level\"
        Case "testcase_ll"
                sFolderPath = sFolderPath & "ABC Automation\Test Cases\Low Level"
        Case "testdata_hl"
                sFolderPath = sFolderPath & "ABC Automation\Test Data\High Level\"
        Case "testdata_ml"
                sFolderPath = sFolderPath & "ABC Automation\Test Data\Medium Level\"
        Case "testdata_ll"
                sFolderPath = sFolderPath & "ABC Automation\Test Data\Low Level\"
      End Select

    getFolderPath = sFolderPath

End Function

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Note: Make sure that you associate the above library file with your script in which you would be using this.

Now if you want to access any file present under any of these folder, you can use the following syntax for the path rather than giving the complete path directly:

getFolderPath("<name of the folder>")&<name of the file>"

Say for example you want to open a text file with the name "Automation Logs.txt" present under logs folder... 

Dim fso, ts 

Set fso = CreateObject("Scripting.FileSystemObject")

Const ForReading = 1 'Set ForReading as 1

Set ts = fso.OpenTextFile(getFolderPath("Logs") & "Automation_Logs.txt", ForReading)