Beispiel #1
0
 private void ep_Session_Expanded(object sender, RoutedEventArgs e)
 {
     _app = null;
     try
     {
         _app = SAPTestHelper.GetSAPGuiApp(1);
         List <SAPSessionVM> sessions = new List <SAPSessionVM>();
         for (int i = 0; i < _app.Children.Count; i++)
         {
             GuiConnection cn = _app.Children.ElementAt(i) as GuiConnection;
             for (int j = 0; j < cn.Children.Count; j++)
             {
                 GuiSession s = cn.Children.ElementAt(j) as GuiSession;
                 if (!s.Busy)
                 {
                     sessions.Add(new SAPSessionVM()
                     {
                         Id          = s.Id,
                         System      = s.Info.SystemName,
                         Transaction = s.Info.Transaction
                     });
                 }
             }
         }
         lv_Sessions.ItemsSource = sessions;
     }
     catch { }
 }
        public GuiSession GetSession()
        {
            CSapROTWrapper sapRotWrapper = new CSapROTWrapper();
            object         sapGuilRot    = sapRotWrapper.GetROTEntry("SAPGUI");

            if (sapGuilRot == null)
            {
                throw new NoSapGuiFoundException("No rot object found - is Sap Gui running?");
            }
            object engine = sapGuilRot.GetType().InvokeMember(
                "GetScriptingEngine",
                BindingFlags.InvokeMethod,
                null,
                sapGuilRot,
                null);

            if (engine == null)
            {
                throw new NoSapGuiFoundException("no engine - wie kann das sein?");
            }
            GuiApplication sapGuiApp = engine as GuiApplication;

            GuiConnection connection = sapGuiApp?.Children.Cast <GuiConnection>().ElementAt(index);

            if (connection == null)
            {
                throw new NoSapGuiFoundException("no connection - Are you logged into any system?");
            }
            foreach (SAPFEWSELib.GuiSession session in connection.Sessions)
            {
                return(new SapGuiSession(session));
            }
            return(null);
        }
 private GuiApplication GetApplication()
 {
     if (application == null)
     {
         CSapROTWrapper sapRotWrapper = new CSapROTWrapper();
         object         sapGuilRot    = sapRotWrapper.GetROTEntry("SAPGUI");
         if (sapGuilRot == null)
         {
             throw new NoSapGuiFoundException("No ROT object found - is SAP GUI running?");
         }
         object engine = sapGuilRot.GetType().InvokeMember(
             "GetScriptingEngine",
             BindingFlags.InvokeMethod,
             null,
             sapGuilRot,
             null
             );
         if (engine == null)
         {
             throw new NoSapGuiFoundException("No engine - is SAP GUI Scripting enabled?");
         }
         application = (GuiApplication)engine;
     }
     return(application);
 }
Beispiel #4
0
        public static GuiConnection CreateConnection(string description, string user, string password, string language)
        {
            CSapROTWrapper sapROTWrapper = new CSapROTWrapper();
            object         SapGuilRot    = sapROTWrapper.GetROTEntry("SAPGUI");
            object         engine        = SapGuilRot.GetType().InvokeMember("GetSCriptingEngine", System.Reflection.BindingFlags.InvokeMethod,
                                                                             null, SapGuilRot, null);

            GuiApplication app = (GuiApplication)engine;
            GuiConnection  conn;

            if (app.Connections.Count == 0)
            {
                conn = app.OpenConnection(description);
                GuiSession       session     = conn.Children.ElementAt(0) as GuiSession;
                GuiTextField     name        = (GuiTextField)session.FindById("wnd[0]/usr/txtRSYST-BNAME");
                GuiPasswordField pass        = (GuiPasswordField)session.FindById("wnd[0]/usr/pwdRSYST-BCODE");
                GuiTextField     lang        = (GuiTextField)session.FindById("wnd[0]/usr/txtRSYST-LANGU");
                GuiMainWindow    mainWindows = (GuiMainWindow)session.FindById("wnd[0]");

                name.Text = user;
                pass.Text = password;
                lang.Text = language;
                mainWindows.SendVKey(0);
            }
            else
            {
                conn = (GuiConnection)app.Connections.ElementAt(0);
            }

            return(conn);
        }
Beispiel #5
0
 public void SetSession(GuiApplication application, GuiConnection connection, GuiSession session)
 {
     this._sapGuiApplication = application;
     this._sapGuiConnection  = connection;
     this._sapGuiSession     = session;
     _currentScreen          = new ScreenData(session.Info.SystemName, session.Info.Transaction, session.Info.Program, session.Info.ScreenNumber, session.ActiveWindow.Text);
     hookSessionEvent();
 }
 public static T FindDescendantByProperty <T>(this GuiApplication Application, Func <T, bool> Property = null)
     where T : class
 {
     if (Property == null)
     {
         Property = new Func <T, bool>(t => true);
     }
     return(findDescendantByPropertyTemplate <T>(Application.Children, Property));
 }
Beispiel #7
0
        internal bool Login(SAPLoginEvent message)
        {
            SAPLogon l = new SAPLogon();

            l.StartProcess();
            GuiSession session = null;

            try
            {
                if (app == null)
                {
                    _app = GetSAPGuiApp(10);
                }
                if (app == null)
                {
                    throw new Exception("Failed launching SAP");
                }
                if (app.Connections.Count != 0)
                {
                    for (int i = 0; i < app.Children.Count; i++)
                    {
                        var con = app.Children.ElementAt(i) as GuiConnection;
                        if (con.Sessions.Count == 0)
                        {
                            continue;
                        }

                        for (int j = 0; j < con.Sessions.Count; j++)
                        {
                            var ses = con.Children.ElementAt(j) as GuiSession;
                            if (ses.Info.SystemName.ToLower() == message.SystemName.ToLower())
                            {
                                session = ses;
                                return(true);
                            }
                        }
                        if (session != null)
                        {
                            break;
                        }
                    }
                }
                // SAPTestHelper.Current.CloseAllConnections();
                // SAPTestHelper.Current.SetSession();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            if (app == null || session == null)
            {
                l.OpenConnection(message.Host);
                return(l.Login(message.Username, message.Password, message.Client, message.Language));
            }
            return(false);
        }
 public void OpenConnection(string server, int secondsOfTimeout = 10)
 {
     lock (_lockObj) {
         _sapGuiApplication = GetSAPGuiApp(secondsOfTimeout);
         _sapGuiApplication.OpenConnectionByConnectionString(server);
         var index = _sapGuiApplication.Connections.Count - 1;
         this._sapGuiConnection = _sapGuiApplication.Children.ElementAt(index) as GuiConnection;
         index = _sapGuiConnection.Sessions.Count - 1;
         this._sapGuiSession = _sapGuiConnection.Children.Item(index) as GuiSession;
     }
 }
Beispiel #9
0
        public static GuiApplication GetSAPGuiApp(int secondsOfTimeout = 10)
        {
            GuiApplication result = null;

            try
            {
                // https://answers.sap.com/questions/12487790/what-are-the-differences-in-vba-methods-to-connect.html?childToView=12494892
                SapROTWr.CSapROTWrapper sapROTWrapper = new SapROTWr.CSapROTWrapper();
                result = getSAPGuiApp(sapROTWrapper, secondsOfTimeout);
            }
            catch (Exception ex)
            {
                Program.log(ex.ToString());
            }
            return(result);
        }
        public GuiSession GetSession()
        {
            GuiApplication application = GetApplication();

            //Direkt abspeichern, um später Referenz nicht zu verlieren
            SAPFEWSELib.GuiSession session = application.ActiveSession;

            if (session == null)
            {
                System.Diagnostics.Debug.WriteLine("Session angefordert - keine active Session da :(");
                return(new NullSession());
            }
            else
            {
                System.Diagnostics.Debug.WriteLine("Session angefordert - Active Session is da");
            }
            return(new SapGuiSession(session));
        }
 public GuiApplication GetApplication()
 {
     if (application == null)
     {
         SapROTWr.CSapROTWrapper sapRotWrapper = new SapROTWr.CSapROTWrapper();
         object sapGuilRot = sapRotWrapper.GetROTEntry("SAPGUI");
         if (sapGuilRot == null)
         {
             throw new NoSapGuiFoundException("No rot object found - is Sap Gui running?");
         }
         application = (GuiApplication)sapGuilRot.GetType().InvokeMember(
             "GetScriptingEngine",
             System.Reflection.BindingFlags.InvokeMethod,
             null,
             sapGuilRot,
             null);
     }
     return(application);
 }
        /// <summary>
        /// This function opens the new SAP session
        /// </summary>
        /// <param name="env">Provide the available environment name, for example 'NRP'</param>
        /// <returns></returns>
        public GuiSession openSAP(string env)
        {
            GuiApplication appSAP = new GuiApplication();
            GuiConnection  connSAP;
            string         connectString = null;

            if (env.ToUpper().Equals("DEFAULT"))
            {
                connectString = "1.0 Test ERP (DEFAULT)";
            }
            else
            {
                connectString = env;
            }
            connSAP = appSAP.OpenConnection(connectString, Sync: true); //creates connection
            var output = appSAP.ActiveSession;

            return((GuiSession)connSAP.Sessions.Item(0)); //creates the Gui session off the connection you made
        }
Beispiel #13
0
 public void OpenConnection(string server, int secondsOfTimeout = 10)
 {
     _sapGuiApplication = SAPTestHelper.GetSAPGuiApp(secondsOfTimeout);
     _sapGuiApplication.OpenConnectionByConnectionString(server);
     var index = _sapGuiApplication.Connections.Count - 1;
     this._sapGuiConnection = _sapGuiApplication.Children.ElementAt(index) as GuiConnection;
     index = _sapGuiConnection.Sessions.Count - 1;
     this._sapGuiSession = _sapGuiConnection.Children.Item(index) as GuiSession;
 }
Beispiel #14
0
 public static T FindById <T>(this GuiApplication Application, string Id)
     where T : class
 {
     return(findByIdTemplate <T>(Id, Application.FindById));
 }
 public SAPGuiSession(SAPLogon logon)
 {
     this.app        = logon.Application;
     this.connection = logon.Connection;
     this.session    = logon.Session;
 }
Beispiel #16
0
        private void btn_run_Click(object sender, EventArgs e)
        {
            if (tb_id.Text == "" || tb_plant.Text == "")
            {
                return;
            }
            if (tb_id.TextLength > 4)
            {
                MessageBox.Show("ID item not valid", "ERRO", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }

            if (tb_plant.TextLength != 4)
            {
                MessageBox.Show("Centro inválido", "ERRO", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }


            //INICIAR ALTERAÇÃO

            #region
            if (dataGridView1.RowCount == 1)
            {
                return;
            }

            try
            {
                //Get the Windows Running Object Table
                SapROTWr.CSapROTWrapper sapROTWrapper = new SapROTWr.CSapROTWrapper();
                //Get the ROT Entry for the SAP Gui to connect to the COM
                object SapGuilRot = sapROTWrapper.GetROTEntry("SAPGUI");
                //Get the reference to the Scripting Engine
                object engine = SapGuilRot.GetType().InvokeMember("GetScriptingEngine", System.Reflection.BindingFlags.InvokeMethod, null, SapGuilRot, null);
                //Get the reference to the running SAP Application Window
                GuiApplication GuiApp = (GuiApplication)engine;
                //Get the reference to the first open connection
                GuiConnection connection = (GuiConnection)GuiApp.Connections.ElementAt(0);
                //get the first available session
                GuiSession session = (GuiSession)connection.Children.ElementAt(0);
                //Get the reference to the main "Frame" in which to send virtual key commandss
                //GuiFrameWindow frame = session.ActiveWindow;
                GuiFrameWindow frame = (GuiFrameWindow)session.FindById("wnd[0]");
                //((GuiFrameWindow)frame.FindById("wnd[0]"));
                ((GuiOkCodeField)frame.FindById("wnd[0]/tbar[0]/okcd")).Text = "/nztvc025";
                frame.SendVKey(0);


                int linhas1 = dataGridView1.RowCount;
                int i;


                ((GuiTextField)frame.FindById("wnd[0]/usr/ctxtP_PRFID")).Text = "0001";
                ((GuiTextField)frame.FindById("wnd[0]/usr/ctxtP_WERKS")).Text = tb_plant.Text;


                for (i = 0; i < linhas1 - 1; i++)
                {
                    try
                    {
                        ((GuiButton)frame.FindByName("btn[8]", "GuiButton")).Press();


                        string material   = dataGridView1.Rows[i].Cells[0].Value.ToString();
                        int    tam        = material.Length;
                        string component1 = dataGridView1.Rows[i].Cells[1].Value.ToString();

                        if (tam == 11)
                        {
                            ((GuiTextField)session.ActiveWindow.FindById("wnd[1]/usr/sub:SAPLSPO4:0300/ctxtSVALD-VALUE[1,21]")).Text = material;
                        }

                        else
                        {
                            ((GuiTextField)session.ActiveWindow.FindById("wnd[1]/usr/sub:SAPLSPO4:0300/ctxtSVALD-VALUE[0,21]")).Text = material;
                        }

                        ((GuiButton)frame.FindByName("btn[0]", "GuiButton")).Press();

                        ((GuiToolbarControl)frame.FindById("wnd[0]/usr/subSUBSCR_TIPOS:SAPLZRVC_ZBOM_POS_ESP:0200/cntlCONT_TIPOS/shellcont/shell/shellcont[1]/shell[0]")).PressButton("&FIND");
                        ((GuiTextField)session.ActiveWindow.FindById("wnd[1]/usr/txtLVC_S_SEA-STRING")).Text = tb_id.Text.ToString();
                        ((GuiButton)frame.FindByName("btn[0]", "GuiButton")).Press();
                        ((GuiButton)frame.FindByName("btn[0]", "GuiButton")).Press();

                        if (session.Children.Count != 2)
                        {
                            dataGridView1.Rows[i].Cells[2].Value = "N/A";
                            goto acabou;
                        }

                        ((GuiButton)frame.FindByName("btn[0]", "GuiButton")).Press();

                        for (int j = 1; j < 100; j++)
                        {
                            string esp;
                            if (j < 10)
                            {
                                esp = "          ";
                            }
                            else
                            {
                                esp = "         ";
                            }

                            ((GuiTree)frame.FindById("wnd[0]/usr/subSUBSCR_TIPOS:SAPLZRVC_ZBOM_POS_ESP:0200/cntlCONT_TIPOS/shellcont/shell/shellcont[1]/shell[1]")).SelectItem(esp + j, "&Hierarchy");
                            ((GuiTree)frame.FindById("wnd[0]/usr/subSUBSCR_TIPOS:SAPLZRVC_ZBOM_POS_ESP:0200/cntlCONT_TIPOS/shellcont/shell/shellcont[1]/shell[1]")).EnsureVisibleHorizontalItem(esp + j, "&Hierarchy");
                            ((GuiTree)frame.FindById("wnd[0]/usr/subSUBSCR_TIPOS:SAPLZRVC_ZBOM_POS_ESP:0200/cntlCONT_TIPOS/shellcont/shell/shellcont[1]/shell[1]")).DoubleClickItem(esp + j, "&Hierarchy");


                            if (((GuiCTextField)session.ActiveWindow.FindByName("ZTBVC_657-ID_ITEM", "GuiCTextField")).Text == tb_id.Text.ToString())
                            {
                                ((GuiToolbarControl)frame.FindById("wnd[0]/usr/subSUBSCR_TIPOS:SAPLZRVC_ZBOM_POS_ESP:0200/cntlCONT_TIPOS/shellcont/shell/shellcont[1]/shell[0]")).PressButton("EDIT_ESP");
                                ((GuiTextField)frame.FindById("wnd[0]/usr/subSUBSCR_ESPEC:SAPLZRVC_ZBOM_POS_ESP:0300/subSUBSCR_ESP:SAPLZRVC_ZBOM_POS_ESP:0340/ctxtZTBVC_658-IDNRK")).Text = component1;
                                ((GuiButton)frame.FindByName("btn[8]", "GuiButton")).Press();
                                dataGridView1.Rows[i].Cells[2].Value = "OK";
                                break;
                            }
                        }



                        acabou : ((GuiButton)frame.FindByName("btn[2]", "GuiButton")).Press();
                    }
                    catch
                    {
                        ((GuiButton)frame.FindByName("btn[2]", "GuiButton")).Press();
                    }

                    #endregion
                }

                MessageBox.Show("Alteração Concluída", "Alteração Concluída", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            catch
            {
                MessageBox.Show("Erro ao conectar com SAP", "Erro", MessageBoxButtons.OK, MessageBoxIcon.Error);

                return;
            }
        }
Beispiel #17
0
 public static T FindChildByProperty <T>(this GuiApplication Application, Func <T, bool> Property = null)
     where T : class
 {
     return(findChildByPropertyTemplate <T>(Application.Children, Property));
 }
Beispiel #18
0
 public static IEnumerable <T> FindDescendantsByProperty <T>(this GuiApplication Application, Func <T, bool> Property = null)
     where T : class
 {
     return(findDescendantsByPropertyTemplate <T>(Application.Children, Property));
 }