Untitled

 avatar
unknown
plain_text
3 months ago
2.8 kB
5
Indexable
using System;
using System.Runtime.InteropServices;

namespace SAPAutomation
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                Console.WriteLine("Starting SAP Logon...");

                // Attach to the SAP GUI scripting engine
                Type sapGuiType = Type.GetTypeFromProgID("SapGui.ScriptingCtrl.1");
                if (sapGuiType == null)
                {
                    throw new Exception("SAP GUI Scripting Control not found. Ensure it is registered and installed.");
                }

                dynamic sapGuiApp = Activator.CreateInstance(sapGuiType);
                if (sapGuiApp == null)
                {
                    throw new Exception("Failed to create an instance of SAP GUI scripting application.");
                }
                Console.WriteLine("Successfully connected to SAP GUI Scripting Control!");

                // Open a new SAP connection (replace 'Your SAP System Name' with the actual system)
                Console.WriteLine("Opening a new SAP connection...");
                dynamic sapConnection = sapGuiApp.OpenConnection("Your SAP System Name");
                if (sapConnection == null)
                {
                    throw new Exception("Failed to open a new SAP connection. Ensure the system name is correct.");
                }

                Console.WriteLine("Successfully connected to the SAP system!");

                // Get the first session
                Console.WriteLine("Checking for active sessions...");
                dynamic sapSession = sapConnection.Children(0); // First session in the connection
                if (sapSession == null)
                {
                    throw new Exception("No active SAP session found. Ensure the connection is valid.");
                }

                Console.WriteLine("Successfully connected to the SAP session!");

                // Example: Maximize the SAP window
                sapSession.FindById("wnd[0]").Maximize();
                Console.WriteLine("SAP window maximized.");

                // Example: Interact with SAP elements
                sapSession.FindById("wnd[0]/usr/txtRSYST-BNAME").Text = "Autotest";
                sapSession.FindById("wnd[0]/usr/pwdRSYST-BCODE").Text = "Automation@1234";
                sapSession.FindById("wnd[0]/tbar[0]/btn[0]").Press();

                Console.WriteLine("Login operation performed successfully.");
            }
            catch (COMException ex)
            {
                Console.WriteLine($"COM Error: {ex.Message}");
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Error: {ex.Message}");
            }
        }
    }
}
Editor is loading...
Leave a Comment