Untitled

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

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

                // 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!");

                // List all available connections
                Console.WriteLine("Available connections:");
                int connectionCount = sapGuiApp.Connections.Count;
                if (connectionCount == 0)
                {
                    throw new Exception("No active connections found in SAP Logon. Ensure you have an open and active connection.");
                }

                for (int i = 0; i < connectionCount; i++)
                {
                    dynamic connection = sapGuiApp.Connections(i);
                    if (connection != null)
                    {
                        Console.WriteLine($"Connection {i}: {connection.Name}");
                    }
                    else
                    {
                        Console.WriteLine($"Connection {i}: null");
                    }
                }

                // Attempt to get the first connection
                dynamic sapConnection = sapGuiApp.Connections(0); // First connection in SAP Logon
                if (sapConnection == null)
                {
                    throw new Exception("No active SAP connection found. Ensure a connection is active in SAP Logon.");
                }

                Console.WriteLine("Checking for active sessions...");
                int sessionCount = sapConnection.Children.Count;
                if (sessionCount == 0)
                {
                    throw new Exception("No active SAP session found. Ensure you are logged into the SAP system.");
                }

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

                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