Automate SAP R/3 Login with QTP

Test automation for SAP R/3 applications using QTP has become quite an interesting topic and in absolute demand by most companies implementing SAP these days worldwide. As a result, it has become necessary to have, in hand, a ready library of functions which assist to implement QTP scripting for SAP R/3 application quickly and easily.

In this section, we will discuss how to login to SAP R/3 application using QTP and Vbscript when the user credentials are provided through an external spreadsheet. The steps towards building such script would be;
1)       Close any existing excel process running on the system.
2)       Open the spreadsheet where user credentials have been provided and read the 
           credentials.
3)       Read the details like Server name, Client details and Language from the spreadsheet
4)       Login to SAP.
5)       Quit Excel process and close all open objects.

Following sections helps to understand the code used for each steps.
1)       Close any existing excel process running on the system.

The  SystemUtil.CloseProcessByName(“Excel.exe”) method can be used to do this. One needs to note that SystemUtil is a QTP reserved object and can be used only with QTP.

However if one needs to achieve this using vbscript, then the following code may be used. This block of code may also be used to close multiple processes.

myComputer = "."

‘Enter the list of processes in the array. These processes will be closed by the block of code.
myArrayOfTargetProcess = Array("EXCEL.EXE")

Set objWMIService = GetObject("winmgmts:" _
 & "{impersonationLevel=impersonate}!\\" &myComputer & "\root\cimv2")
Set CollectionOfProcesses = objWMIService.ExecQuery("SELECT * FROM Win32_Process")

For Each IndividualProcess in CollectionOfProcesses
 For Each TargetProcess In myArrayOfTargetProcess
                                If LCase(IndividualProcess.Name) = LCase(TargetProcess) Then
                                intReturn = IndividualProcess.Terminate
 End If
                  Next
Next

2)       Open the spreadsheet where user credentials have been provided and read the   
          credentials.  For this, use the following code block.

    PathForLoginSpreadSheet = <enter the path for login spread sheet>
       'Create an excel application                   
                       Set  myExcelForLogin = CreateObject( "Excel.Application")
                      ‘Open the file in the specified location                     
                      myExcelForLogin.WorkBooks.open(PathForLoginSpreadSheet)
                      Set ExcelWorkBook = myExcelForLogin.ActiveWorkbook
                      Set ExcelLoginSheet = ExcelWorkBook.Worksheets("Login")

3)       Read the details like Server name, Client details and Language from the spreadsheet

Let’s assume that the data is entered in the second row of the excel sheet. Data for “Server”, “Client”, “User”, “Password” and “Language” are in the 2nd, 3rd, 4th, 5th and 6th column of the spreadsheet respectively.
This code is in continuation with the previous blocks.

With ExcelLoginSheet
               
                ‘Read the values for “Server”, “Client”, “User”, “Password” and “Language”
                SAPR3ServerName = .Cells(intRow,2)
                SAPR3Client = .Cells(intRow,3)
                SAPUserName=.Cells(intRow,4)
                SAPPassword=.Cells(intRow,5)
                UserLanguage = .Cells(intRow,6)
                               
End With                    
               
4)      Login to SAP.

Use the statement below:

SAPGuiUtil.AutoLogon SAPR3ServerName, SAPR3Client, SAPUserName, SAPPassword, UserLanguage

Please note that SAPGuiUtil.AutoLogon is a QTP reserved keyword.

5)      Quit all created objects.

    myExcelForLogin.Quit
                Set ExcelWorkBook =Nothing
    Set ExcelLoginSheet=Nothing
                Set myExcelForLogin =Nothing

Comments

  1. Thanks! Testing while implementing SAP is crucial and it seems to me that what you've posted here will indeed come in handy. I'd appreciate reading more about any other testing tools and functions that you might think will come in handy...

    ReplyDelete

Post a Comment

Popular posts from this blog

Automatic Replenishment ~ Warehouse Management (WM) in SAP

Teradata - Month End Date

Cross Domain Webix Post