Untitled
unknown
plain_text
3 months ago
5.7 kB
5
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 sapPath = @"C:\Program Files\SAP\FrontEnd\SAPGUI\saplogon.exe"; //return System.IO.File.Exists(sapPath); return true; } public void StartSAPLogon() { string sapPath = @"C:\Program Files\SAP\FrontEnd\SAPGUI\saplogon.exe"; Process.Start(sapPath); Console.WriteLine($"[{DateTime.Now}] SAP Logon started."); Thread.Sleep(5000); // Wait for SAP Logon to start ConnectToSAP(); } public void ConnectToSAP(string systemName = "SR5") { Console.WriteLine($"[{DateTime.Now}] Connecting to SAP GUI..."); 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."); sapGuiApp = Activator.CreateInstance(sapGuiType); sapConnection = sapGuiApp.OpenConnection(systemName); if (sapConnection == null) throw new Exception("Failed to open SAP connection. Ensure the system name is correct."); Console.WriteLine($"[{DateTime.Now}] Connection established!"); // Wait for session readiness int maxWaitTime = 15000; int waitInterval = 1000; int elapsedTime = 0; while (sapConnection.Children.Count == 0 && elapsedTime < maxWaitTime) { Console.WriteLine($"[{DateTime.Now}] Waiting for 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."); sapSession = sapConnection.Children(0); Console.WriteLine($"[{DateTime.Now}] SAP session is ready."); } public void EnterUsername(string username) { Console.WriteLine($"[{DateTime.Now}] Entering username..."); sapSession.FindById("wnd[0]/usr/txtRSYST-BNAME").Text = username; Thread.Sleep(500); } public void EnterPassword(string password) { Console.WriteLine($"[{DateTime.Now}] Entering password..."); sapSession.FindById("wnd[0]/usr/pwdRSYST-BCODE").Text = password; Thread.Sleep(500); } public void ClickLoginButton() { Console.WriteLine($"[{DateTime.Now}] Clicking login button..."); sapSession.FindById("wnd[0]/tbar[0]/btn[0]").Press(); Thread.Sleep(3000); } public bool VerifySuccessfulLogin() { try { // Check if the SAP status bar exists dynamic statusBar = sapSession.FindById("wnd[0]/sbar"); if (statusBar != null && !string.IsNullOrEmpty(statusBar.Text)) { Console.WriteLine($"[{DateTime.Now}] SAP Status Bar Message: {statusBar.Text}"); return true; // Login successful } // Alternative: Check if a known SAP element is visible dynamic mainToolbar = sapSession.FindById("wnd[0]/tbar[0]/okcd"); if (mainToolbar != null) { Console.WriteLine($"[{DateTime.Now}] SAP main window is visible."); return true; } return false; } catch { return false; } } public void CloseSAP() { try { Console.WriteLine($"[{DateTime.Now}] Closing SAP session..."); // Check if sapSession exists before closing if (sapSession != null) { sapSession.FindById("wnd[0]/tbar[0]/okcd").Text = "/nex"; // Exit SAP sapSession.FindById("wnd[0]").SendVKey(0); Thread.Sleep(2000); } else { Console.WriteLine($"[{DateTime.Now}] SAP session is null, skipping session logout."); } // Close SAP Logon if still running var sapProcesses = Process.GetProcessesByName("saplogon"); if (sapProcesses.Length > 0) { Console.WriteLine($"[{DateTime.Now}] SAP Logon is running, attempting to close..."); foreach (var process in sapProcesses) { process.Kill(); } } else { Console.WriteLine($"[{DateTime.Now}] No SAP Logon process found."); } Console.WriteLine($"[{DateTime.Now}] SAP session closed successfully."); } catch (Exception ex) { Console.WriteLine($"[{DateTime.Now}] Error closing SAP: {ex.Message}"); } } } }
Editor is loading...
Leave a Comment