Untitled

 avatar
unknown
plain_text
3 months ago
2.4 kB
6
Indexable
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Threading;

namespace SAPAutomation.utils
{
    public class SapHelper
    {
        private dynamic sapGuiApp;
        private dynamic sapConnection;
        private dynamic sapSession;

        public bool CheckSAPInstallation()
        {
            string sapLogonPath = @"C:\Program Files (x86)\SAP\FrontEnd\SAPgui\saplogon.exe";
            return System.IO.File.Exists(sapLogonPath);
        }

        public void StartSAPLogon()
        {
            Console.WriteLine("Starting SAP Logon...");
            string sapLogonPath = @"C:\Program Files (x86)\SAP\FrontEnd\SAPgui\saplogon.exe";
            Process.Start(sapLogonPath);
            Thread.Sleep(5000);
        }

        public void ConnectToSAP(string systemName)
        {
            Console.WriteLine("Connecting to SAP GUI...");
            Type sapGuiType = Type.GetTypeFromProgID("SapGui.ScriptingCtrl.1");
            if (sapGuiType == null)
                throw new Exception("SAP GUI Scripting Control not found.");

            sapGuiApp = Activator.CreateInstance(sapGuiType);
            sapConnection = sapGuiApp.OpenConnection(systemName);
            if (sapConnection == null)
                throw new Exception("Failed to open SAP connection.");
            
            Thread.Sleep(5000);
            sapSession = sapConnection.Children(0);
            if (sapSession == null)
                throw new Exception("No active SAP session found.");
        }

        public void EnterUsername(string username)
        {
            sapSession.FindById("wnd[0]/usr/txtRSYST-BNAME").Text = username;
        }

        public void EnterPassword(string password)
        {
            sapSession.FindById("wnd[0]/usr/pwdRSYST-BCODE").Text = password;
        }

        public void ClickLoginButton()
        {
            sapSession.FindById("wnd[0]/tbar[0]/btn[0]").Press();
            Thread.Sleep(5000);
        }

        public bool VerifySuccessfulLogin()
        {
            try
            {
                // Check if the main SAP screen is open (adjust the ID if needed)
                return sapSession.FindById("wnd[0]/titl") != null;
            }
            catch
            {
                return false;
            }
        }
    }
}
Editor is loading...
Leave a Comment