使用c#GUI测试在SAP中输入交易代码

时间:2018-08-19 13:38:49

标签: c# testing automated-tests sap sap-gui

我在运行c#测试时在SAP中输入交易代码时遇到问题。我不知道我在哪里做错工作。

登录到SAP enviro后,我尝试输入事务,但未输入。它给了我错误。我尝试了很多方法。

感谢您的帮助。

enter image description here

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

namespace SAPGuiAutomated.Program
{
    //created a class for the SAP app, connection, and session objects as well as for common methods. 
    public static 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 v)
        {
            GuiApplication Application;
            GuiConnection Connection;
            GuiSession Session;

            Application = (GuiApplication)System.Activator.CreateInstance(Type.GetTypeFromProgID("SapGui.ScriptingCtrl.1"));

            // How do I find the connection string that I use to connect to SAP?
            Connection = Application.OpenConnectionByConnectionString("..ipAddressOfmySAP..", false, true);
            Session = (GuiSession)Connection.Sessions.Item(0);

            Session.TestToolMode = 1;

            ((GuiTextField)Session.ActiveWindow.FindByName("RSYST-MANDT", "GuiTextField")).Text = "..client..";
            ((GuiTextField)Session.ActiveWindow.FindByName("RSYST-BNAME", "GuiTextField")).Text = "..loginName..";
            ((GuiTextField)Session.ActiveWindow.FindByName("RSYST-BCODE", "GuiPasswordField")).Text = "..password..";
            ((GuiTextField)Session.ActiveWindow.FindByName("RSYST-LANGU", "GuiTextField")).Text = "..language..";

            // Press the green checkmark button which is about the same as the enter key 
            GuiButton btn = (GuiButton)Session.ActiveWindow.FindByName("btn[0]", "GuiButton");
            btn.SetFocus();
            btn.Press();

            // Enter transaction 
            ((GuiTextField)Session.ActiveWindow.FindById("wnd[0]/tbar[0]/okcd")).Text = "se16n";

            //((GuiTextField)Session.FindById("wnd[0]/tbar[0]/okcd");
            //GuiCTextField transactionInput = (GuiCTextField)SAPActive.SapSession.ActiveWindow.FindById("wnd[0]/tbar[0]/okcd");
            //transactionInput.Text = "se16n";
            //GuiCTextField transactionInput = (GuiCTextField)SAPActive.SapSession.ActiveWindow.FindByName("okcd", "GuiCTextField");
            //transactionInput.Text = "Se16n";
            btn.Press();
        }
    }
}

-我知道了:((GuiTextField)Session.ActiveWindow.FindById("wnd[0]/tbar[0]/okcd")).Text = "se16n";

错误:

System.InvalidCastException
  HResult=0x80004002
  Message=Unable to cast COM object of type 'System.__ComObject' to interface type 'SAPFEWSELib.GuiTextField'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{B4D89EE3-6EFD-4F4C-9F42-AD42B71C8EB7}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).
  Source=SAP_test
  StackTrace:
   at SAPGuiAutomated.Program.SAPActive.OpenSAP(String v) in C:\Users\Martin\source\repos\SAP_test\SAP_test\Program.cs:line 85
   at SAPGuiAutomated.RunProgram.Program.Main(String[] args) in C:\Users\Martin\source\repos\SAP_test\SAP_test\RunProgram.cs:line 17

-当我使用此GuiCTextField transactionInput = (GuiCTextField)SAPActive.SapSession.ActiveWindow.FindById("wnd[0]/tbar[0]/okcd")时  我收到此错误:

  

(GuiCTextField)SAPActive.SapSession.ActiveWindow.FindById(“ wnd [0] / tbar [0] / okcd”); '((GuiCTextField)SAPActive.SapSession.ActiveWindow.FindById(“ wnd [0] / tbar [0] / okcd”)'   抛出类型异常   'System.NullReferenceException'SAPFEWSELib.GuiCTextField   {System.NullReferenceException}

GuiCTextField transactionInput = (GuiCTextField)SAPActive.SapSession.ActiveWindow.FindByName("okcd", "GuiCTextField"); 我收到此错误:

  

(GuiCTextField)SAPActive.SapSession.ActiveWindow.FindByName(“ okcd”,   “ GuiCTextField”); '((GuiCTextField)SAPActive.SapSession.ActiveWindow.FindByName(“ okcd”,   “ GuiCTextField”)'引发了类型异常   'System.NullReferenceException'SAPFEWSELib.GuiCTextField   {System.NullReferenceException}

1 个答案:

答案 0 :(得分:0)

好的,我找到了:D

Session.StartTransaction("transactionCode");