Welcome To Automation Testing

Before starting with tips of automation of SAP using QTP Let me give a small introduction of SAP and QTP.

SAP stands for System Applications and Products. It is the name of both the online financial and

Administrative software and the company that developed it. SAP is made up of individual modules that perform various organizational system tasks.



Quick Test Professional (QTP) is an automated functional Graphical User Interface (GUI) testing tool that allows the automation of user actions on a web or client based computer application.

It is primarily used for functional regression test automation. QTP uses a scripting language built on top of VBScript to specify the test procedure, and to manipulate the objects and controls of the application under test. It supports many applications through the support of add-ins.



We will be using SAP add-in with QTP to work on the SAP automation.

Saturday, 17 November 2012

Get unique items from an array/Remove duplicate items from an array


There may be multiple ways to get unique elements from an array; I found using dictionary object to do the task is simple and the best. Let me first talk about dictionary objects.
A Dictionary object is a kind of array with items and keys. Items can be any form of data, and are stored in the array. Each item is associated with a unique key. The key is used to retrieve an individual item and is usually an integer or a string, but can be anything except an array.
I would be using dictionary object’s “CompareMode” property to get the unique elements from the array. Below code explains how comparemode works and how it will be useful in removing duplicate items.

Dim d
Set d = CreateObject("Scripting.Dictionary")
d.CompareMode = vbTextCompare
d.Add "a", "Athens"   ' Add some keys and items.
d.Add "b", "Belgrade"
d.Add "c", "Cairo"
d.Add "B", "Baltimore"   ' Add method fails on this line because the
                                                ' letter b already exists in the Dictionary.

This means if Comparemode is set to text compare, duplicate items cannot be added to dictionary objects. Same logic we will be using in our function. We will keep on adding the array items to dictionary objects. As CompareMode is set to vbTextCompare, duplicate items will not be added. Hence at last we will get a dictionary objects with unique/No duplicate items. We can then assign the dictionary object items to an array.

Public Function Remove_Duplicate_Items_From_Array(arrayName)

                'Create a dictionary object
                Set objDict = CreateObject("Scripting.Dictionary")
                objDict.CompareMode = vbTextCompare

                'iterate through the values
                For each val in arrayName
                      objDict(val) = val
                Next
               
                newArray = objDict.Items            ‘Assign the itesm to array
                Remove_Duplicate_Items_From_Array=newArray
               Set objDict=Nothing ' Release object
End Function

Monday, 15 October 2012

Working with SAPGuiTree - Search and Activate Node


Hi All. I am back with this new post. Many times this question has been asked on many forums. However, there are hardly any suitable/complete answer. One of my reader has requested me to post on this topic. I took some time out of my schedule and am posting my findings. I hope you find it useful.
Below code is a sample code which I have written. This uses a descriptive programming which means it will work for any SAPGuiTree. No need to bother about objects. This code searches for a particular node  and activates it. This function also gives the total node count and navigates through each one of them. Please leave your comment if you find it useful.


'************************************************************************​ 'Function Name: SAP_Search_And_Activate_Node_In_SAPGuiTree
'Description: Searches and activates a node in SAPGuiTree using the search node text parameter.
'Parameter: Search node text, enter a value that you want to search,
'                        For example if you are looking for Outbound delivery no, you can enter the full text or just delivery for search key
'Return Value: Returns True/False
'Author: Ankesh
'Creation Date:
'**********************************************************************

Function SAP_Search_And_Activate_Node_In_SAPGuiTree(strSearchNodeText)

    'get all the objects of SAPGUITree
    'Descriptive programming has been used
    Set ObjSAPGuiTree = SAPGuiSession("micclass:=SAPGuiSession").SAPGuiWindow("micclass:=SAPGuiWindow").SAPGuiTree("micclass:=SAPGuiTree").Object

    'Get the node keys , a key is a number/position in the
    'A key value starts from 1.
    Set ObjKeyValues = ObjSAPGuiTree.GetAllNodeKeys

    'get the total count
    ''This count indicates the number of items/nodes in the Tree
    intNodeCount = ObjKeyValues.Count

    blnFlag=False
    'Iterate through the nodes of the tree
    For i = 0 to intNodeCount-1
        'Get the node text
        strNodeText=ObjSAPGuiTree.GetNodeTextByKey(ObjKeyValues(i))

        'Check if the match was found for the key that you are looking for
        'if yes then activate the item
        If Instr(strNodeText,strSearchNodeText)>0 Then
            'Select the node and double click on it
            'This is equivalent to ActivateItem
            ObjSAPGuiTree.SelectNode ObjKeyValues(i)
            ObjSAPGuiTree.DoubleClickNode ObjKeyValues(i)
            blnFlag=True 'set a flag to indicate the macth was found
            Exit For
        End If
    
    Next

    'Chk the flag and return values to function
    If blnFlag Then
        SAP_Search_And_Activate_Node_In_SAPGuiTree=True
    Else
        SAP_Search_And_Activate_Node_In_SAPGuiTree=False
    End If

    'Release the objects
    Set ObjKeyValues=Nothing
    Set ObjSAPGuiTree=Nothing

End Function

Thursday, 27 September 2012

Generating random number in vbscript

I have received couple of mails regarding generating random number in vbscript. So here i am with the post on random number.

There are two ways of generating random numbers in VBScript which are as follows:



1. Using randomize keyword.

'consider the below example of generating a random number between 10 -99, i.e., 2 digit number.

intMaxValue=99 '
intMinValue=10

Randomize  'This will generate a unique number every time
         
intRndVariable= Int((intMaxValue-intMinValue+ 1) * Rnd +intMinValue)


intRndVariable will hold the random number.



2. Use RandomNumber function

intRandomNumber=RandomNumber(intLoweLimit,Upperlimit)

e.g., a=RandomNumber(10,99)

Monday, 6 August 2012

Close running Process from task manager


Below function will close running process from task manager.


Function CloseProcess(strProgramName)
Dim objShell
Set objShell = CreateObject("WScript.Shell")
objShell.Run "TASKKILL /F /IM " & strProgramName
Set objShell = Nothing
End Function


Taskkill is a commancd which ends one or more tasks or processes. Processes can be killed by process ID or image name.

"TASKKILL /F /IM " & strProgramName

/F :: Forcefully closing the application
/IM :: Image Name. Nothing but the visible exe file in task manager. Since we dont know the process id, i hv used Image name to kill the process. strProgramName contains the .exe file name which has to be killed.



To close browser say IE, just call the function as

Call CloseProcess("iexplorer.exe")'Closes IE
Call CloseProcess("Excel.exe") ''Closes excel.


Friday, 3 August 2012

Round function returns wrong result/ Round to Larger

I remember working with Round function. There was a scenario where it was returning wrong value.

'Let us see the example
dblValue=30.745
dblRoundedValue=Round (dblValue,2) 'Round for two digits after decimal

***************
Output was 30.74 instead of 30.75.

***************
Reason behind this is

The Round function performs round to even, which is different from round to larger. ... If expression is exactly halfway between two possible rounded values, the function returns the possible rounded value whose rightmost digit is an even number.
This is not what i was looking for. For me the values has to be 30.75. I developed a small piece of code which works absolutely fine for rounding values to larger.

Function Generic_RoundToLarger(dblNumber, intNumDigitsAfterDecimal)
   dblRoundedValue= CDbl(FormatNumber(dblNumber, intNumDigitsAfterDecimal))
   Generic_RoundToLarger=dblRoundedValue
End Function


Now when i call this function i get the expected value. 30.745 is returned as 30.75.

Hope this helps my reader.


Wednesday, 1 August 2012

Navigating to one t-code from another using QTP

Many of us may be aware of this topic which i am posting. But for me it took almos 5 months to discover this trick.:(

Let me explain what i use to do before discovering this cool trick.
Suppose you are on a t-code page , say VA01. After completing your operation, you need to navigate back to home page. I use to click on back button for this, which inturn was increasing OR size. This was not reliable too.
'Navigate back
SAPGuiSession("Session").SAPGuiWindow("").SAPGuiButton("Back").click

'Or to jump to another T-code from here, i was using /n in the t-code box

SAPGuiSession("Session").SAPGuiWindow("").SAPGuiOKCode("OK").Set "/nVA03"

For the above code to run, all the objects must be present in OR. Miss of any will result in error.

To resolve this we can use the 'Reset' option available in QTP.

SAPGuiSession("Session").Reset  // This code will take you to home page, doesnt matter where you are.
SAPGuiSession("Session").Reset "VA03"  //This code will take you to VA03 t-page from anywhere.

This will save lot of time and will optimize memory as well.

Wasn't it cool? Go ahead, use this option in your code.



Wednesday, 25 July 2012

How to log in to SAP server using user ID & password.


You can use the SAPGuiutil object to open connections. All you need to do is to pass the server name taht you want to log into followed by userid and password. Please find the below code

Sapguiutil.OpenConnection “A:Test Server”
SAPGuiSession("Session").SAPGuiWindow("SAP").SAPGuiEdit("User").Set strUserID
 SAPGuiSession("Session").SAPGuiWindow("SAP").SAPGuiEdit("Password").SetSecure strPassword
SAPGuiSession("Session").SAPGuiWindow("SAP").SAPGuiButton("Enter").Click