Monday 4 December 2017

SAP GUI - AUTOMATION (VBS AND JAVA)

 SAP GUI AUTOMATION

CONCEPTS DISCUSSED ON VBS AND JAVA:

Prerequisite:

1. SAP GUI With Scripting Enabled
2. Java i586 
3. Jacob


Here we are assuming that SAP GUI has scripting enabled, if there no way that scripting would be enabled then tools to look for would be UFT/UI Path. 

As our discussion is on Java mainly, I am more inclined towards enabling scripting. 

Lets start with VB Scripting First:

VBS: 
1. Once scripting is enabled you can see, record and playback button on gui as below,



2. Click on the same, which opens small widget containing play, record and stop button.

3. Click on record and enter user name and password.

4. Stop the recording, 

A file with .vbs extension would be created and you can replay this file using play button which opens the folder where script files are saved by default.

Sample vbs below:

Code Snippet:

If Not IsObject(application) Then
   Set SapGuiAuto  = GetObject("SAPGUI")
   Set application = SapGuiAuto.GetScriptingEngine
End If
If Not IsObject(connection) Then
   Set connection = application.Children(0)
End If
If Not IsObject(session) Then
   Set session    = connection.Children(0)
End If
If IsObject(WScript) Then
   WScript.ConnectObject session,     "on"
   WScript.ConnectObject application, "on"
End If


session.findById("wnd[0]").maximize
session.findById("wnd[0]/usr/txtRSYST-BNAME").text = ""
session.findById("wnd[0]/usr/pwdRSYST-BCODE").text = ""
session.findById("wnd[0]/usr/pwdRSYST-BCODE").caretPosition = 7
session.findById("wnd[0]").sendVKey 0




JAVA INTEGRATION:

We can make use of the recorded script to run via java. 
1. Download jacob 1.18, with java i586 

Here I am building simple java project no build tools used.

Steps:
1. Unzip jacob 
2. Copy dll to jre-> bin
3. Add jar to build path of java project.

Code Snippet below: 

Below snippet contains actions like type, doubleclick, select, press and I am using a bat which opens sapgui through command prompt.

import java.io.IOException;

import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.ComThread;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;

public class test {

public static void main(String[] args) throws IOException, InterruptedException {
new ProcessBuilder("cmd", "/c", "startup.bat").start();
Thread.sleep(5000);

ActiveXComponent SAPROTWr, GUIApp, Connection, Session, Obj;
Dispatch ROTEntry;
Variant ScriptEngine;

ComThread.InitSTA();

SAPROTWr = new ActiveXComponent("SapROTWr.SapROTWrapper");
try {
ROTEntry = SAPROTWr.invoke("GetROTEntry", "SAPGUI").toDispatch();
ScriptEngine = Dispatch.call(ROTEntry, "GetScriptingEngine");
GUIApp = new ActiveXComponent(ScriptEngine.toDispatch());

Connection = new ActiveXComponent(GUIApp.invoke("Children", 0).toDispatch());
// -Set session = connection.Children(0)-----------------------
Session = new ActiveXComponent(Connection.invoke("Children", 0).toDispatch());

Obj = new ActiveXComponent(Session.invoke("FindById", "wnd[0]/usr/txtRSYST-MANDT").toDispatch());
Obj.setProperty("Text", "100");

Obj = new ActiveXComponent(Session.invoke("FindById", "wnd[0]/usr/txtRSYST-BNAME").toDispatch());
Obj.setProperty("Text", "Z001T97");

Obj = new ActiveXComponent(Session.invoke("FindById", "wnd[0]/usr/pwdRSYST-BCODE").toDispatch());
Obj.setProperty("Text", "Sting3#");

Obj = new ActiveXComponent(Session.invoke("FindById", "wnd[0]/usr/txtRSYST-LANGU").toDispatch());
Obj.setProperty("Text", "EN");

Obj = new ActiveXComponent(Session.invoke("FindById", "wnd[0]").toDispatch());
Obj.invoke("sendVKey", 0);

Thread.sleep(2000);

Obj = new ActiveXComponent(
Session.invoke("FindById", "wnd[0]/usr/cntlIMAGE_CONTAINER/shellcont/shell/shellcont[0]/shell")
.toDispatch());
Obj.invoke("doubleClickNode", "0000000050");

Obj = new ActiveXComponent(Session
.invoke("FindById",
"wnd[0]/usr/tabsTABSTRIP_WB_TAB/tabpUCOMM11/ssub%_SUBSCREEN_WB_TAB:/POSDW/SAPLMONITOR_SEL:0201/ctxtSO_SORG-LOW")
.toDispatch());
Obj.setProperty("Text", "234");

Obj = new ActiveXComponent(
Session.invoke("FindById", "wnd[0]/usr/tabsTABSTRIP_WB_TAB/tabpUCOMM12").toDispatch());
Obj.invoke("select");

Obj = new ActiveXComponent(
Session.invoke("FindById", "wnd[0]/usr/tabsTABSTRIP_WB_TAB/tabpUCOMM13").toDispatch());
Obj.invoke("select");

Obj = new ActiveXComponent(
Session.invoke("FindById", "wnd[0]/usr/tabsTABSTRIP_WB_TAB/tabpUCOMM14").toDispatch());
Obj.invoke("select");

Obj = new ActiveXComponent(
Session.invoke("FindById", "wnd[0]/usr/tabsTABSTRIP_WB_TAB/tabpUCOMM11").toDispatch());
Obj.invoke("select");

Obj = new ActiveXComponent(Session
.invoke("FindById",
"wnd[0]/usr/tabsTABSTRIP_WB_TAB/tabpUCOMM11/ssub%_SUBSCREEN_WB_TAB:/POSDW/SAPLMONITOR_SEL:0201/ctxtSO_STOGR-LOW")
.toDispatch());
Obj.setProperty("Text", "21");

Obj = new ActiveXComponent(Session.invoke("FindById", "wnd[0]/tbar[1]/btn[17]").toDispatch());
Obj.invoke("press");

Obj = new ActiveXComponent(Session.invoke("FindById", "wnd[1]/usr/txtV-LOW").toDispatch());
Obj.setProperty("Text", "21");

Obj = new ActiveXComponent(Session.invoke("FindById", "wnd[1]/tbar[0]/btn[12]").toDispatch());
Obj.invoke("press");

Obj = new ActiveXComponent(Session.invoke("FindById", "wnd[0]/tbar[0]/btn[3]").toDispatch());
Obj.invoke("press");

ComThread.Release();
ComThread.quitMainSTA();

}

catch (Exception e) {
e.printStackTrace();
}

finally {
ComThread.Release();
System.exit(0);
}

}

}

**- I recorded the script using sap gui recorder and used the same in above snippet.



 

  

No comments:

Post a Comment