Enter transaction code in SAP with c# GUI test

1

I have problems with enter transaction code in SAP when I am running c# test. I don't know where I am doing incorrect work.

After log in to SAP enviro I am trying to enter transaction but it doesn't enter. It gives me error. I tried many ways.

Thanks for any help.

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();
        }
    }
}

-- I got with this: ((GuiTextField)Session.ActiveWindow.FindById("wnd[0]/tbar[0]/okcd")).Text = "se16n";

error:

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

-- when I used this GuiCTextField transactionInput = (GuiCTextField)SAPActive.SapSession.ActiveWindow.FindById("wnd[0]/tbar[0]/okcd") I got this error:

(GuiCTextField)SAPActive.SapSession.ActiveWindow.FindById("wnd[0]/tbar[0]/okcd"); '(GuiCTextField)SAPActive.SapSession.ActiveWindow.FindById("wnd[0]/tbar[0]/okcd")' threw an exception of type 'System.NullReferenceException' SAPFEWSELib.GuiCTextField {System.NullReferenceException}

when this GuiCTextField transactionInput = (GuiCTextField)SAPActive.SapSession.ActiveWindow.FindByName("okcd", "GuiCTextField"); I got this error:

(GuiCTextField)SAPActive.SapSession.ActiveWindow.FindByName("okcd", "GuiCTextField"); '(GuiCTextField)SAPActive.SapSession.ActiveWindow.FindByName("okcd", "GuiCTextField")' threw an exception of type 'System.NullReferenceException' SAPFEWSELib.GuiCTextField {System.NullReferenceException}

c#
testing
automated-tests
sap
sap-gui
asked on Stack Overflow Aug 19, 2018 by martin pb

1 Answer

0

Ok, I found it :D

Session.StartTransaction("transactionCode");
answered on Stack Overflow Aug 20, 2018 by martin pb

User contributions licensed under CC BY-SA 3.0