如何使用C#自动执行SAP GUI [英] How do I automate SAP GUI with c#

查看:437
本文介绍了如何使用C#自动执行SAP GUI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用C#语言自动化SAP GUI窗口。我可以在VBScript中做到这一点,但是代码重用是可怕的。此外,我想使用线程代替运行80个或更多进程。在哪里可以找到有关此操作的任何文档和样本?这是我正在使用的代码。基本上,我面临的问题是-如何建立与SAP GUI的连接,然后动态创建SAP GUI,然后开始进行交易并在某些字段中输入文本。

I would like to automate an SAP GUI window using the C# language. I am able to do it in VBScript but code reuse is horrible. Besides Id like to use threading instead of having 80 or more processes running. Where can I find any documentation and samples of how to do this? Here is the code I am working with. Basically, the problem I am facing is - how do I make a connection to SAP GUI then create an SAP GUI on the fly then start making transactions and entering text in some fields.

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using White.Core.Factory;
using White.Core.UIItems.Finders;
using White.Core.InputDevices;
using System.Threading;
using System.Diagnostics;
using SAP.Connector;
using SAP;


namespace SAP_Automation
{
    class Program
    {
        public static void Main(string[] args)
        {
            string ExeSourceFile = @"C:\Program Files\SAP\SapSetup\setup\SAL\SapLogon.s8l";
            White.Core.Application _application;
            White.Core.UIItems.WindowItems.Window _mainWindow;

            var c = SAP.Connector.Connection.GetConnection("**");
            var c = new SAPConnection("ASHOST=*; GWHOST=*; GWSERV=*; ASHOST=*; SYSNR=00;USER=user; PASSWD=**;");
            c.Open();


            }
        }
    }
}

如您所见,我可以创建连接,但是我不知道如何创建与GUI的会话并开始在字段中输入文本。任何示例和示例都将不胜感激。

As you can see I can create a connection but I dont know how to create a session to the GUI and start entering text in fields. Any examples and samples would be appreciated.

推荐答案

这可能是死线程,但我处于类似的工作环境。我们出于测试目的需要SAP GUI自动化,它可以与我们用C#编写的其余自主开发自动化平台集成。我帮助创建了一个解决方案的提案,该解决方案利用了SAP提供的GUI自动化库,该库可用作SAP自动化层的基础。

This might be necro-threading but I was in a similar situation where I work. We needed SAP GUI Automation for testing purposes that could integrate with the rest of our homegrown automation platform written in C#. I helped create a proposal for one solution that took advantage of a SAP provided library for GUI automation that could be used as the basis for an automation layer for SAP.

SAP文件安装中是否存在以下文件? x:\Program Files\SAP\FrontEnd\SAPGui\sapfewse.ocx?

Does the following file exist on your SAP file installation? x:\Program Files\SAP\FrontEnd\SAPGui\sapfewse.ocx?

如果是这样,请将其添加到Visual Studio(或您使用的任何IDE)中作为参考。它基本上是一个类库,其中包含一堆特定于SAP的对象,您可以与它们进行交互。它非常有效,因为它可以显示SAP GUI中的大部分需求。在其他尝试中,我们发现SAP中的许多对象不可用。

If so, add it to Visual Studio (or whatever IDE you're using) as a reference. It is basically a class library which contains a bunch of SAP specific objects that will allow you to interact with. It is very effective because it exposes most of what you need from the SAP GUI. We discovered in other attempts that a lot of the objects in SAP were not available.

这是我所做的早期概念证明。使用连接字符串启动SAP,输入凭据,导航到事务代码。

This is an early proof of concept I did. Start SAP with a connection string, enter credentials, navigate to a transaction code.

using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
using SAPFEWSELib;

namespace SAPGuiAutomated
{
//created a class for the SAP app, connection, and session objects as well as for common methods. 
    public class SAPActive
    {
        public static GuiApplication SapGuiApp { get; set; }
        public static GuiConnection SapConnection { get; set; }
        public static GuiSession SapSession { get; set; }

        public static void openSap(string env)
        {
            SAPActive.SapGuiApp = new GuiApplication();

            string connectString = null;
            if (env.ToUpper().Equals("DEFAULT"))
            {
                connectString = "1.0 Test ERP (DEFAULT)";
            }
            else
            {
                connectString = env;
            }
            SAPActive.SapConnection = SAPActive.SapGuiApp.OpenConnection(connectString, Sync: true); //creates connection
            SAPActive.SapSession = (GuiSession)SAPActive.SapConnection.Sessions.Item(0); //creates the Gui session off the connection you made
        }

        public void login(string myclient, string mylogin, string mypass, string mylang)
        {
            GuiTextField client  = (GuiTextField)SAPActive.SapSession.ActiveWindow.FindByName("RSYST-MANDT", "GuiTextField");
            GuiTextField login  = (GuiTextField)SAPActive.SapSession.ActiveWindow.FindByName("RSYST-BNAME", "GuiTextField");
            GuiTextField pass  = (GuiTextField)SAPActive.SapSession.ActiveWindow.FindByName("RSYST-BCODE", "GuiPasswordField");
            GuiTextField language  = (GuiTextField)SAPActive.SapSession.ActiveWindow.FindByName("RSYST-LANGU", "GuiTextField");

            client.SetFocus();
            client.text = myclient;
            login.SetFocus();
            login.Text = mylogin;
            pass.SetFocus();
            pass.Text = mypass;
            language.SetFocus();
            language.Text = mylang; 

            //Press the green checkmark button which is about the same as the enter key 
            GuiButton btn = (GuiButton)SapSession.FindById("/app/con[0]/ses[0]/wnd[0]/tbar[0]/btn[0]");
            btn.SetFocus(); 
            btn.Press();

        }
    }
    //--------------------------//
    //main method somewhere else 
    public static void Main(string[] args)
    {
        SAPActive.openSAP("my connection string");
        SAPActive.login("10", "jdoe", "password", "EN");
        SAPActive.SapSession.StartTransaction("VA03");
    }

您是对的,关于此主题的文档很多。以下是一些帮助我入门的资源

You're right there is not a lot of documentation on this subject. Below are a few sources that helped me get started

-我们计划的原始资源
http://scn.sap.com/thread/1729689

-Original source of our plan http://scn.sap.com/thread/1729689

-API上的文档(适用于VB和javascript,但一般规则和对象相同)。绝对阅读SAP GUI运行时层次结构中的部分。它会回答很多问题。
http://www.synactive .com / download / sap%20gui%20scripting / sap%20gui%20scripting%20api.pdf

-Documentation on the API (For VB and javascript but the general rules and objects are identical). Definitely read the portion on the SAP GUI Runtime hierarchy. It'll answer a lot of questions. http://www.synactive.com/download/sap%20gui%20scripting/sap%20gui%20scripting%20api.pdf

这篇关于如何使用C#自动执行SAP GUI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆