Untitled

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

namespace SAPAutomation
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                // Step 1: Start SAP Logon
                Console.WriteLine($"[{DateTime.Now}] Starting SAP Logon...");
                string sapLogonPath = @"C:\Program Files\SAP\FrontEnd\SAPGUI\saplogon.exe";

                // Start SAP Logon
                Process.Start(sapLogonPath);

                // Wait for SAP Logon to initialize
                Console.WriteLine($"[{DateTime.Now}] Waiting for SAP Logon to initialize...");
                Thread.Sleep(5000); // Adjust as needed based on system performance

                // Step 2: Connect to SAP GUI
                Console.WriteLine($"[{DateTime.Now}] 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($"[{DateTime.Now}] Successfully connected to SAP GUI Scripting Control!");

                // Step 3: Open SAP connection
                Console.WriteLine($"[{DateTime.Now}] OpenConnection called...");
                dynamic sapConnection = sapGuiApp.OpenConnection("SR5");
                if (sapConnection == null)
                {
                    throw new Exception("Failed to open SAP connection. Ensure the system name is correct.");
                }
                Console.WriteLine($"[{DateTime.Now}] Connection established!");

                // Step 4: Wait for session readiness
                Console.WriteLine($"[{DateTime.Now}] Checking for active sessions...");
                int maxWaitTime = 15000; // Max wait time in milliseconds
                int waitInterval = 1000; // Polling interval in milliseconds
                int elapsedTime = 0;

                while (sapConnection.Children.Count == 0 && elapsedTime < maxWaitTime)
                {
                    Console.WriteLine($"[{DateTime.Now}] Waiting for the SAP session to be ready...");
                    Thread.Sleep(waitInterval);
                    elapsedTime += waitInterval;
                }

                if (sapConnection.Children.Count == 0)
                {
                    throw new Exception("No active SAP session found after waiting.");
                }

                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($"[{DateTime.Now}] Session ready!");

                // Example: Maximize the SAP window
                Console.WriteLine($"[{DateTime.Now}] Maximize SAP window...");
                sapSession.FindById("wnd[0]").Maximize();
                Thread.Sleep(2000); // Pause for 2 seconds

                // Example: Perform login
                Console.WriteLine($"[{DateTime.Now}] Performing login...");
                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();
                Thread.Sleep(3000); // Pause for 5 seconds after login

                maxWaitTime = 3000;
                waitInterval = 500;
                elapsedTime = 0;

                while (elapsedTime < maxWaitTime)
                {
                    try
                    {
                        // Proverava da li postoji prozor "wnd[1]"
                        dynamic multiLogonWindow = sapSession.FindById("wnd[1]");
                        if (multiLogonWindow != null)
                        {
                            Console.WriteLine($"[{DateTime.Now}] Multi-login window detected, handling it...");

                            // Selektuj opciju "Continue with this logon, without ending any other logons in the system"
                            dynamic option2 = sapSession.FindById("wnd[1]/usr/radMULTI_LOGON_OPT2");
                            option2.Select(); // Select second option
                            option2.SetFocus(); // Focus on second option
                            option2.Select(); // Select second option
                            Thread.Sleep(500); // Pause

                            // Press OK (Green button)
                            sapSession.FindById("wnd[1]/tbar[0]/btn[0]").Press();
                            Console.WriteLine($"[{DateTime.Now}] Multi-login option handled.");
                            Thread.Sleep(2000);

                            break; // Break the loop
                        }
                    }
                    catch (Exception ex)
                    {
                        // If the window does not exist or there is an error, ignore and continue
                        //Console.WriteLine($"[{DateTime.Now}] Multi-login window not detected or error: {ex.Message}");
                        // If the window didn't show continue
                        //Console.WriteLine($"[{DateTime.Now}] No multi-login window detected, continuing execution...");
                    }

                    Thread.Sleep(waitInterval);
                    elapsedTime += waitInterval;
                }

                // If the window didn't show continue
                //Console.WriteLine($"[{DateTime.Now}] No multi-login window detected, continuing execution...");
                Console.WriteLine($"[{DateTime.Now}] Login operation performed successfully.");

                // Enter transaction code FB60
                sapSession.findById("wnd[0]/tbar[0]/okcd").text = "fb70";
                sapSession.findById("wnd[0]").sendVKey (0);
                sapSession.findById("wnd[0]/usr/tabsTS/tabpMAIN/ssubPAGE:SAPLFDCB:0510/ctxtINVFO-ACCNT").text = "53478";
                sapSession.findById("wnd[0]/usr/tabsTS/tabpMAIN/ssubPAGE:SAPLFDCB:0510/ctxtINVFO-BLDAT").text = "02/11/2025";
                sapSession.findById("wnd[0]/usr/tabsTS/tabpMAIN/ssubPAGE:SAPLFDCB:0510/txtINVFO-XBLNR").text = "rec_vin_001";
                sapSession.findById("wnd[0]/usr/tabsTS/tabpMAIN/ssubPAGE:SAPLFDCB:0510/txtINVFO-WRBTR").text = "5000";
                sapSession.findById("wnd[0]/usr/subITEMS:SAPLFSKB:0100/tblSAPLFSKBTABLE/ctxtACGL_ITEM-HKONT[1,0]").text = "41050000";
                sapSession.findById("wnd[0]/usr/subITEMS:SAPLFSKB:0100/tblSAPLFSKBTABLE/txtACGL_ITEM-WRBTR[4,0]").text = "5000";
                sapSession.findById("wnd[0]/usr/subITEMS:SAPLFSKB:0100/tblSAPLFSKBTABLE/ctxtACGL_ITEM-PRCTR[27,0]").text = "3030100";
                sapSession.findById("wnd[0]/usr/subITEMS:SAPLFSKB:0100/tblSAPLFSKBTABLE/ctxtACGL_ITEM-PRCTR[27,0]").setFocus();
                sapSession.findById("wnd[0]/usr/subITEMS:SAPLFSKB:0100/tblSAPLFSKBTABLE/ctxtACGL_ITEM-PRCTR[27,0]").caretPosition = 7;
                sapSession.findById("wnd[0]").sendVKey (0);
                sapSession.findById("wnd[0]/usr/subITEMS:SAPLFSKB:0100/tblSAPLFSKBTABLE/ctxtACGL_ITEM-HKONT[1,0]").setFocus();
                sapSession.findById("wnd[0]/usr/subITEMS:SAPLFSKB:0100/tblSAPLFSKBTABLE/ctxtACGL_ITEM-HKONT[1,0]").caretPosition = 3;
                sapSession.findById("wnd[0]").sendVKey (2);
                sapSession.findById("wnd[0]/usr/subBLOCK:SAPLKACB:1014/btnCOBL_XERGO").press();
                sapSession.findById("wnd[1]/tbar[0]/btn[18]").press();
                sapSession.findById("wnd[1]/tbar[0]/btn[0]").press();
                sapSession.findById("wnd[0]/tbar[0]/btn[3]").press();
                sapSession.findById("wnd[0]/tbar[0]/btn[11]").press();

                dynamic statusBar = sapSession.FindById("wnd[0]/sbar");
                string documentNumber = "";
                if (statusBar != null)
                {
                    string statusText = statusBar.Text;
                    Console.WriteLine($"[{DateTime.Now}] SAP Status Bar Message: {statusText}");

                    // Regex da pronađe broj u poruci
                    Match match = Regex.Match(statusText, @"\d+"); // Traži prvi broj u stringu
                    if (match.Success)
                    {
                        documentNumber = match.Value;
                        Console.WriteLine($"[{DateTime.Now}] Generated Document Number: {documentNumber}");
                        
                    }
                    else
                    {
                        Console.WriteLine("No document number found in status bar message.");
                    }
                }
                Console.WriteLine($"[{DateTime.Now}] FB70 transaction completed successfully.");

                sapSession.findById("wnd[0]/sbar").doubleClick();
                sapSession.findById("wnd[0]/shellcont").close();
                sapSession.findById("wnd[0]/tbar[0]/btn[3]").press();
                sapSession.findById("wnd[0]/tbar[0]/btn[15]").press();
                sapSession.findById("wnd[1]/usr/btnSPOP-OPTION1").press();

                sapSession.findById("wnd[0]/tbar[0]/okcd").text = "FBL5N";
                sapSession.findById("wnd[0]").sendVKey (0);
                sapSession.findById("wnd[0]/usr/ctxtDD_KUNNR-LOW").text = "53478";
                sapSession.findById("wnd[0]/usr/ctxtDD_KUNNR-LOW").caretPosition = 5;
                sapSession.findById("wnd[0]/tbar[1]/btn[8]").press();
                sapSession.findById("wnd[0]").sendVKey (71);
                sapSession.findById("wnd[1]/usr/txtRSYSF-STRING").text = documentNumber;
                sapSession.findById("wnd[1]/usr/txtRSYSF-STRING").caretPosition = 10;
                sapSession.findById("wnd[1]/tbar[0]/btn[0]").press();
                sapSession.findById("wnd[2]/tbar[0]/btn[0]").press();
                sapSession.findById("wnd[1]/tbar[0]/btn[0]").press();
                sapSession.findById("wnd[2]/tbar[0]/btn[12]").press();
                sapSession.findById("wnd[1]/tbar[0]/btn[12]").press();
                sapSession.findById("wnd[0]/tbar[0]/btn[3]").press();
                sapSession.findById("wnd[0]/tbar[0]/btn[15]").press();


                sapSession.findById("wnd[0]/tbar[0]/okcd").text = "fb75";
                sapSession.findById("wnd[0]").sendVKey (0);
                sapSession.findById("wnd[0]/usr/tabsTS/tabpMAIN/ssubPAGE:SAPLFDCB:0510/ctxtINVFO-ACCNT").text = "53478";
                sapSession.findById("wnd[0]/usr/tabsTS/tabpMAIN/ssubPAGE:SAPLFDCB:0510/ctxtINVFO-BLDAT").text = "02/11/2025";
                sapSession.findById("wnd[0]/usr/tabsTS/tabpMAIN/ssubPAGE:SAPLFDCB:0510/txtINVFO-XBLNR").text = "rec_vcm_001";
                sapSession.findById("wnd[0]/usr/tabsTS/tabpMAIN/ssubPAGE:SAPLFDCB:0510/txtINVFO-WRBTR").text = "5000";
                sapSession.findById("wnd[0]/usr/subITEMS:SAPLFSKB:0100/tblSAPLFSKBTABLE/ctxtACGL_ITEM-HKONT[1,0]").text = "41050000";
                sapSession.findById("wnd[0]/usr/subITEMS:SAPLFSKB:0100/tblSAPLFSKBTABLE/txtACGL_ITEM-WRBTR[4,0]").text = "5000";
                sapSession.findById("wnd[0]/usr/subITEMS:SAPLFSKB:0100/tblSAPLFSKBTABLE/ctxtACGL_ITEM-PRCTR[27,0]").text = "3030100";
                sapSession.findById("wnd[0]/usr/subITEMS:SAPLFSKB:0100/tblSAPLFSKBTABLE/ctxtACGL_ITEM-HKONT[1,0]").setFocus();
                sapSession.findById("wnd[0]/usr/subITEMS:SAPLFSKB:0100/tblSAPLFSKBTABLE/ctxtACGL_ITEM-HKONT[1,0]").caretPosition = 6;
                sapSession.findById("wnd[0]").sendVKey (2);
                sapSession.findById("wnd[0]/usr/subBLOCK:SAPLKACB:1014/btnCOBL_XERGO").press();
                sapSession.findById("wnd[1]/tbar[0]/btn[18]").press();
                sapSession.findById("wnd[1]/tbar[0]/btn[0]").press();
                sapSession.findById("wnd[0]/tbar[0]/btn[3]").press();
                sapSession.findById("wnd[0]/tbar[0]/btn[11]").press();

                statusBar = sapSession.FindById("wnd[0]/sbar");
                string documentNumber2 = "";
                if (statusBar != null)
                {
                    string statusText = statusBar.Text;
                    Console.WriteLine($"[{DateTime.Now}] SAP Status Bar Message: {statusText}");

                    // Regex da pronađe broj u poruci
                    Match match = Regex.Match(statusText, @"\d+"); // Traži prvi broj u stringu
                    if (match.Success)
                    {
                        documentNumber2 = match.Value;
                        Console.WriteLine($"[{DateTime.Now}] Generated Document Number: {documentNumber2}");
                        
                    }
                    else
                    {
                        Console.WriteLine("No document number found in status bar message.");
                    }
                }
                Console.WriteLine($"[{DateTime.Now}] FB75 transaction completed successfully.");


                sapSession.findById("wnd[0]/sbar").doubleClick();
                sapSession.findById("wnd[0]/shellcont").close();
                sapSession.findById("wnd[0]/tbar[0]/btn[15]").press();
                sapSession.findById("wnd[1]/usr/btnSPOP-OPTION1").press();


                sapSession.findById("wnd[0]/tbar[0]/okcd").text = "fbl5n";
                sapSession.findById("wnd[0]").sendVKey (0);
                sapSession.findById("wnd[0]/tbar[1]/btn[8]").press();
                sapSession.findById("wnd[0]").sendVKey (71);
                sapSession.findById("wnd[1]/usr/txtRSYSF-STRING").text = documentNumber2;
                sapSession.findById("wnd[1]/usr/txtRSYSF-STRING").caretPosition = 10;
                sapSession.findById("wnd[1]/tbar[0]/btn[0]").press();
                sapSession.findById("wnd[2]/tbar[0]/btn[0]").press();
                sapSession.findById("wnd[1]/tbar[0]/btn[12]").press();
                sapSession.findById("wnd[0]/tbar[0]/btn[3]").press();

                /** 
                sapSession.findById("wnd[0]/tbar[0]/okcd").text = "/nf-32";
                sapSession.findById("wnd[0]").sendVKey (0);
                sapSession.findById("wnd[0]/usr/ctxtRF05A-AGKON").text = "53478";
                sapSession.findById("wnd[0]/usr/ctxtRF05A-AGKON").caretPosition = 5;
                sapSession.findById("wnd[0]/tbar[1]/btn[16]").press();
                sapSession.findById("wnd[0]/usr/tabsTS/tabpMAIN/ssubPAGE:SAPDF05X:6102/tblSAPDF05XTC_6102").columns.elementAt(1).width = 13;
                sapSession.findById("wnd[0]/usr/tabsTS/tabpMAIN/ssubPAGE:SAPDF05X:6102/tblSAPDF05XTC_6102/txtRFOPS_DK-BELNR[1,10]").setFocus();
                sapSession.findById("wnd[0]/usr/tabsTS/tabpMAIN/ssubPAGE:SAPDF05X:6102/tblSAPDF05XTC_6102/txtRFOPS_DK-BELNR[1,10]").caretPosition = 7;
                sapSession.findById("wnd[0]").sendVKey (2);
                sapSession.findById("wnd[0]/tbar[0]/btn[3]").press();
                sapSession.findById("wnd[0]/usr/tabsTS/tabpMAIN/ssubPAGE:SAPDF05X:6102/btnICON_SELECT_ALL").press();
                sapSession.findById("wnd[0]/usr/tabsTS/tabpMAIN/ssubPAGE:SAPDF05X:6102/btnIC_Z-").press();
                sapSession.findById("wnd[0]/usr/tabsTS/tabpMAIN/ssubPAGE:SAPDF05X:6102/tblSAPDF05XTC_6102/txtRFOPS_DK-BELNR[1,10]").caretPosition = 3;
                sapSession.findById("wnd[0]").sendVKey (2);
                sapSession.findById("wnd[0]/tbar[0]/btn[3]").press();
                sapSession.findById("wnd[0]/usr/tabsTS/tabpMAIN/ssubPAGE:SAPDF05X:6102/tblSAPDF05XTC_6102/txtRFOPS_DK-BELNR[1,10]").caretPosition = 8;
                sapSession.findById("wnd[0]/usr/tabsTS/tabpMAIN/ssubPAGE:SAPDF05X:6102/btnIC_Z+").press();
                sapSession.findById("wnd[0]/usr/tabsTS/tabpMAIN/ssubPAGE:SAPDF05X:6102/tblSAPDF05XTC_6102/txtRFOPS_DK-BELNR[1,8]").setFocus();
                sapSession.findById("wnd[0]/usr/tabsTS/tabpMAIN/ssubPAGE:SAPDF05X:6102/tblSAPDF05XTC_6102/txtRFOPS_DK-BELNR[1,8]").caretPosition = 4;
                sapSession.findById("wnd[0]/usr/tabsTS/tabpMAIN/ssubPAGE:SAPDF05X:6102/btnIC_Z+").press();
                sapSession.findById("wnd[0]/tbar[0]/btn[11]").press();
                sapSession.findById("wnd[0]/sbar").doubleClick();
                sapSession.findById("wnd[0]/shellcont").close();
                sapSession.findById("wnd[0]/tbar[0]/okcd").text = "/n";
                sapSession.findById("wnd[0]").sendVKey (0);


                sapSession.findById("wnd[0]/tbar[0]/okcd").text = "fbl5n";
                sapSession.findById("wnd[0]").sendVKey (0);
                sapSession.findById("wnd[0]/usr/radX_CLSEL").select();
                sapSession.findById("wnd[0]/usr/ctxtSO_AUGDT-LOW").text = "02/11/2025";
                sapSession.findById("wnd[0]/usr/ctxtSO_AUGDT-HIGH").text = "02/11/2025";
                sapSession.findById("wnd[0]/usr/ctxtSO_AUGDT-HIGH").setFocus();
                sapSession.findById("wnd[0]/usr/ctxtSO_AUGDT-HIGH").caretPosition = 10;
                sapSession.findById("wnd[0]/tbar[1]/btn[8]").press();
                sapSession.findById("wnd[0]").sendVKey (71);
                sapSession.findById("wnd[1]/tbar[0]/btn[0]").press();
                sapSession.findById("wnd[2]/tbar[0]/btn[0]").press();
                sapSession.findById("wnd[1]/usr/txtRSYSF-STRING").text = documentNumber;
                sapSession.findById("wnd[1]/usr/txtRSYSF-STRING").caretPosition = 10;
                sapSession.findById("wnd[1]/tbar[0]/btn[0]").press();
                sapSession.findById("wnd[2]/tbar[0]/btn[0]").press();
                sapSession.findById("wnd[1]/tbar[0]/btn[12]").press();
                sapSession.findById("wnd[0]/tbar[0]/btn[15]").press();
                sapSession.findById("wnd[0]/tbar[0]/btn[15]").press();*/



                Thread.Sleep(15000);


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