0
votes

I created a Python app to run functional tests for SAP GUI, and I'm having trouble starting the SAPGUI session, I use the scripts I capture using the Scripting Tracker, but this happens before I get a SAP session.

When I open SAP Logon I get a window with a list of environments, here I can choose from different servers to use, normally the first environment on the list is the one I need so I only need to send windows an enter to open the SAP normal session and start authenticating.

The window looks like this:

Taken from https://blogs.sap.com/wp-content/uploads/2015/07/1_742764.jpg

This is the code I use to open SAP and start the first environment:

os.startfile("C:\\Program Files (x86)\\SAP\\FrontEnd\\SAPgui\\saplogon.exe")
shell = win32com.client.Dispatch("WScript.Shell")
shell.SendKeys('{ENTER}')

Then I create a SAP Session like this:

sapGuiAuto = win32com.client.GetObject("SAPGUI")
sapApplication = sapGuiAuto.GetScriptingEngine
time.sleep(1)
sapConnection = sapApplication.Children(0)
session = sapConnection.Children(0)

The first block of code works randomly, like one of three times.

I've been reading a little about the com objects and I think I can use the win32com.client.GetObject("SAPGUI") from the beggining but I have been trying to do things like these without success:

Test 1

sapGuiAuto = win32com.client.GetObject("SAPGUI")
sapGuiAuto.SendKeys('{ENTER}')

Test 2

sapGuiAuto = win32com.client.GetObject("SAPGUI")
sapApplication = sapGuiAuto.GetScriptingEngine
time.sleep(1)
sapConnection = sapApplication.Children(0)
session = sapConnection.Children(0)
session.findById("wnd[0]").sendVKey(0)

Is there a better way to start SAP GUI than using win32com.client.Dispatch("WScript.Shell")? Does SAP Logon have a different ProgId?

Thanks

1

1 Answers

1
votes

I don't know Python but you could try the following:

'Here comes the full name of the connection from SAP Logon
myConnection = "DE2 [erpdd...]" 
os.startfile("C:\\Program Files (x86)\\SAP\\FrontEnd\\SAPgui\\saplogon.exe")
shell = win32com.client.Dispatch("WScript.Shell")
time.sleep(4)

sapGuiAuto = win32com.client.GetObject("SAPGUI")
sapApplication = sapGuiAuto.GetScriptingEngine
sapConnection = sapApplication.openconnection(myConnection)
session = sapConnection.Children(0)
session.findById("wnd[0]").maximize
'start authenticating
...

Regards, ScriptMan