Untitled

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

namespace SAPAutomation
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                // Attach to the SAP GUI scripting engine
                Type sapGuiType = Type.GetTypeFromProgID("SapGui.ScriptingCtrl.1");
                dynamic sapGuiApp = Activator.CreateInstance(sapGuiType);

                // Get the first connection and session
                dynamic sapConnection = sapGuiApp.Connections(0); // First connection in SAP Logon
                if (sapConnection == null)
                {
                    throw new Exception("No active SAP connection found.");
                }

                dynamic sapSession = sapConnection.Children(0); // First session in the connection
                if (sapSession == null)
                {
                    throw new Exception("No active SAP session found.");
                }

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

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

                // 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.");
            }
            catch (COMException ex)
            {
                Console.WriteLine($"COM Error: {ex.Message}");
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Error: {ex.Message}");
            }
        }
    }
}
Editor is loading...
Leave a Comment