Monday 29 January 2018

How To Use eCATT Script from PowerShell

eCATT is a very powerful tool for test automation in the context of SAP. In this blog I describe how to use eCATT script from PowerShell. So we can combine the possibilities of eCATT on the one hand and PowerShell on the other hand.

Hint: To realize this scenario you need the SAP dotNET connector NCo. Look at my blog posts to find examples how to use NCo with PowerShell.

1. We create with the TAC SECATT a system data container.

SAP ABAP Tutorials and Materials, SAP ABAP Guides, SAP ABAP Certifications, SAP ABAP Learning

SAP ABAP Tutorials and Materials, SAP ABAP Guides, SAP ABAP Certifications, SAP ABAP Learning

2. Now we create a test script.

SAP ABAP Tutorials and Materials, SAP ABAP Guides, SAP ABAP Certifications, SAP ABAP Learning

SAP ABAP Tutorials and Materials, SAP ABAP Guides, SAP ABAP Certifications, SAP ABAP Learning

SAP ABAP Tutorials and Materials, SAP ABAP Guides, SAP ABAP Certifications, SAP ABAP Learning

As you can see the eCATT script does nothing special. It receives a string in ip_input parameter and delivers a string at ep_output parameter

*-Begin-----------------------------------------------------------------

lv_input = ip_input.

ABAP.

  lv_output = `Hello World with ` && lv_input && ` from ` && sy-sysid.

ENDABAP.

ep_output = lv_output.

*-End-------------------------------------------------------------------​

3. To check it we execute it via F8.

SAP ABAP Tutorials and Materials, SAP ABAP Guides, SAP ABAP Certifications, SAP ABAP Learning

SAP ABAP Tutorials and Materials, SAP ABAP Guides, SAP ABAP Certifications, SAP ABAP Learning

We see, it works.

4. In our last step at eCATT we create a test configuration.

SAP ABAP Tutorials and Materials, SAP ABAP Guides, SAP ABAP Certifications, SAP ABAP Learning

SAP ABAP Tutorials and Materials, SAP ABAP Guides, SAP ABAP Certifications, SAP ABAP Learning

SAP ABAP Tutorials and Materials, SAP ABAP Guides, SAP ABAP Certifications, SAP ABAP Learning

Here we combine the test script and the data container.

5. Now we create a remote enabled function module to get the export parameter of an eCATT script.

SAP ABAP Tutorials and Materials, SAP ABAP Guides, SAP ABAP Certifications, SAP ABAP Learning

FUNCTION Z_ECATT_LOG_GET_EXPORT.
*"----------------------------------------------------------------------
*"*"Local Interface:
*"  IMPORTING
*"     VALUE(IV_SCRIPT) TYPE  ETOBJ_NAME
*"     VALUE(IV_LOGID) TYPE  ETLOG_ID
*"  EXPORTING
*"     VALUE(ET_EXPORT_VALUES) TYPE  ETLOG_EXPO_TABTYPE
*"  EXCEPTIONS
*"      LOG_NOT_FOUND
*"----------------------------------------------------------------------

  CALL FUNCTION 'ECATT_LOG_GET_EXPORT'
    EXPORTING
      im_script     = iv_script
      im_logid      = iv_logid
    IMPORTING
      export_values = et_export_values
    EXCEPTIONS
      log_not_found = 1
      others        = 2
    .

  IF sy-subrc <> 0.
    RAISE LOG_NOT_FOUND.
  ENDIF.

ENDFUNCTION.​

A lot of eCATT function modules are remote enabled, but ECATT_LOG_GET_EXPORT is not. So it is necessary to create an own one which offers this possibility.

6. Now we create a RFM to call ECATT_EXECUTE. This FM is remote enabled, but I use a wrapper to simplify the call from PowerShell.

FUNCTION Z_ECATT_EXECUTE.
*"----------------------------------------------------------------------
*"*"Lokale Schnittstelle:
*"  IMPORTING
*"     VALUE(IV_OBJ_NAME) TYPE  ETOBJ_NAME
*"     VALUE(IV_INPUT) TYPE  STRING
*"     VALUE(IV_TARGETSYSTEM) TYPE  ETCMP_CMP
*"  EXPORTING
*"     VALUE(EV_LOG_ID) TYPE  ETLOG_ID
*"  EXCEPTIONS
*"      NOTHING_TO_DO
*"      TOO_MANY_SCRIPTS_CALLED
*"      OTHER_ERROR
*"----------------------------------------------------------------------

  DATA:
    lt_exe       TYPE etexe_obj_tabtype,
    lt_startopt  TYPE etname_value_tabtype,
    lt_param     TYPE etexec_par_vals_tabtype,
    lt_exe_param TYPE etexec_params_tabtype,
    ls_log       TYPE etlog_key
    .

  lt_exe = VALUE #(
    ( obj_type = 'ECTC' obj_name = IV_OBJ_NAME )
  ).

  lt_startopt = VALUE #(
    ( name = 'start_type'         value = 'S'             )
    ( name = 'startmode'          value = 'A'             )
    ( name = 'mode_sapgui_close'  value = 'N'             )
    ( name = 'interrupt'          value = 'X'             )
    ( name = 'display_log'        value = ''              )
    ( name = 'targetsystem'       value = IV_TARGETSYSTEM )
  ).

  lt_param = VALUE #(
    ( pname = 'IP_INPUT' pvalue = IV_INPUT pvalue_type = SPACE )
  ).

  lt_exe_param = VALUE #(
    ( paramtab = lt_param )
  ).

  CALL FUNCTION 'ECATT_EXECUTE'
    EXPORTING
      TO_EXECUTE              = lt_exe
      DISPLAY_LOG             = ''
      IT_STARTOPTIONS         = lt_startopt
      IT_TO_EXECUTE_PARAMS    = lt_exe_param

    IMPORTING
      LOGID                   = ls_log
    EXCEPTIONS
      NOTHING_TO_DO           = 1
      TOO_MANY_SCRIPTS_CALLED = 2
      OTHERS                  = 3
    .
  CASE sy-subrc.
    WHEN 0.
      "Nothing to do
    WHEN 1.
      RAISE NOTHING_TO_DO.
    WHEN 2.
      RAISE TOO_MANY_SCRIPTS_CALLED.
    WHEN OTHERS.
      RAISE OTHER_ERROR.
  ENDCASE.

  EV_LOG_ID = ls_log-logid.

ENDFUNCTION.​

7. To get all start options use the FM ECATT_GET_START_OPTIONS_TABLE with the TAC SE37. Here the results:

---------------------
| 58 Entries |
---------------------
--------------------------------------------
|NAME |VALUE |
-------------------------------------------|
|AGSSMT_BPCA_TRC | |
|APL_LOG | |
|ARCHIVE | |
|BCSET_ACTIVATION_MODE | |
|CANCEL | |
|DEBUG | |
|DISPLAY_LOG | |
|ESF_DEBUG | |
|EXEC_CTRL | |
|FSTARTMODE | |
|GUIMINI | |
|GUIPIC1 | |
|GUIPIC2 | |
|GUIPIC3 | |
|GUIPIC4 | |
|GUIPIC_PATH | |
|GUIPIC_PATH_E | |
|INTERRUPT | |
|MAXRUNTIME | |
|MODE_EXTTOOL | |
|MODE_EXTTOOL_DB | |
|MODE_EXTTOOL_LOG | |
|MODE_SAPGUI | |
|MODE_SAPGUI_BREAK | |
|MODE_SAPGUI_CLOSE | |
|MODE_SAPGUI_DEBUG | |
|MODE_SAPGUI_HIGHLIGHT | |
|MODE_SAPGUI_IN_ONE_SESS | |
|MODE_SAPGUI_PIC | |
|MODE_SAPGUI_SYNC | |
|OLD_LOG_ID | |
|PASSWORD | |
|PERF_TRACE | |
|PERF_TRACE_BUF | |
|PERF_TRACE_ENQ | |
|PERF_TRACE_RESCON | |
|PERF_TRACE_RFC | |
|PERF_TRACE_SQL | |
|PRECONDITION | |
|PROJECT | |
|REM_CATT_INT_TSYS | |
|RFC_USE_ASYNC | |
|SCOV_EVALUATION_PERIOD | |
|SCOV_INTERRUPT | |
|SCOV_MEASUREMENT_TYPE | |
|SCOV_SDL_SWITCH | |
|SHORT_LOG | |
|STARTMODE | |
|START_TYPE | |
|SYSTEMDATA | |
|TARGETSYSTEM | |
|TRANSFER_EXTTOOL_LOG | |
|TWB_RFC_CLOSE | |
|TWB_STATE_SET | |
|USERNAME | |
|VARIANT_ID | |
|WEB_DURATION | |
|WEB_MODE | |
-------------------------------------------|

---------------------
| 39 Entries |
---------------------
---------------------------------------------------------------------------------------
|FIELDNAME |DATATYPE |OUTPUT|SHORTTEXT |VALUEHELP_TAB |FIELDTEXT |
---------------------------------------------------------------------------------------|
|APL_LOG |C |000001|eCATT: Application Log Handling | 2 Entries |Collect Appl. Log |
|ARCHIVE |C |000001|Archive ON/OFF | 2 Entries |Archive |
|DEBUG |C |000001|eCATT - Debug Modes | 4 Entries |Debug.mode |
|ESF_DEBUG |C |000001|ESF Debugging | 2 Entries |ESF Debugging |
|FSTARTMODE |C |000001|X = Use Start Mode, Do Not Use Mode for TCD | 2 Entries |Overwrites TCD Mode |
|GUIMINI |C |000001|eCATT - Minimize SAP GUI | 2 Entries |Minimize eCATT GUI |
|GUIPIC1 |C |000001|eCATT - SAP GUI Screenshot: In Case of Error | 2 Entries |In Case of Error |
|GUIPIC2 |C |000001|eCATT - SAP GUI Screenshot: Before User Input | 2 Entries |Before User Input |
|GUIPIC3 |C |000001|eCATT - SAP GUI Screenshot: After User Input | 2 Entries |After User Input |
|GUIPIC4 |C |000001|eCATT - SAP GUI Screenshot: After Each Change by User | 2 Entries |After Methode/Prop. |
|GUIPIC_PATH |C |000255|eCATT - SAP GUI Path to Screenshots | 0 Entries |Screenshot Path |
|GUIPIC_PATH_E |C |000001|eCATT - SAP GUI Screenshot: Create Separate Directory | 2 Entries |Separate Directory |
|INTERRUPT |C |000001|Error Behavior (X=Cancel,Cont.:V=Variant,T=Config,S=Command)| 4 Entries |Error behavior |
|MAXRUNTIME |N |000006|Time requirement | 0 Entries |Time req. |
|MODE_EXTTOOL |C |000001|Processing Mode for External Tool | 3 Entries |Mode for Ext. Tool |
|MODE_EXTTOOL_DB |C |000001|External Tool: Delete Execution Database? | 4 Entries |Delete Execution DB |
|MODE_SAPGUI |C |000001|Error Mode for SAP GUI Command | 3 Entries |SAP GUI Error Mode |
|MODE_SAPGUI_BREAK |C |000001|Break Mode for SAPGUI Command | 8 Entries |Break SAP GUI When |
|MODE_SAPGUI_CLOSE |C |000001|Close Sessions Created by SAP GUI Command | 4 Entries |Close GUIs |
|MODE_SAPGUI_DEBUG |C |000001|eCATT - SAP GUI Debugger | 2 Entries |SAP GUI Stop in Dbg |
|MODE_SAPGUI_HIGHLIGHT |C |000001|eCATT - SAP GUI Highlight | 2 Entries |Highlight SAP GUI |
|MODE_SAPGUI_IN_ONE_SESS |C |000001|eCATT - SAP GUI Sessions | 2 Entries |SAP GUI in One Sess.|
|MODE_SAPGUI_PIC |C |000001|eCATT - SAP GUI Screenshot | 2 Entries |Save Screenshot |
|MODE_SAPGUI_SYNC |C |000001|Synchronization Mode for SAP GUI Command | 2 Entries |SAP GUI Replay Mode |
|PASSWORD |C |000032|Password for External Tool | 0 Entries |Password |
|PERF_TRACE |C |000001|Trace State in eCATT Execution (On/Off) | 2 Entries |Trace in eCATT |
|PERF_TRACE_RESCON |C |000001|Close RFC Connection at PERF/ENDPERF | 5 Entries |RESCON at PERF |
|PRECONDITION |C |000001|Activate/Deactivate Precondition Handling | 2 Entries |Precondition |
|PROJECT |C |000032|Project Name in External Tool | 0 Entries |Ext. Project Name |
|REM_CATT_INT_TSYS |C |000030|Internal Target System for REMOTECATT | 0 Entries |Internal CATT Sys. |
|RFC_USE_ASYNC |C |000001|eCATT - Use Asynchronous RFC | 2 Entries |Use Asynchr. RFC |
|STARTMODE |C |000001|Replay Mode (A=Fgrnd, N=Bgrnd, E=Error, X=Asynch, Y=N.Loc) | 6 Entries |Replay Mode TCD |
|START_TYPE |C |000001|eCATT - Start Type for eCATT Start | 8 Entries |Start Type |
|SYSTEMDATA |C |000030|System Data Container | 0 Entries |System Data Cont. |
|TARGETSYSTEM |C |000030|Target System (Key in System Data Container) | 0 Entries |Target System |
|TRANSFER_EXTTOOL_LOG |C |000001|eCATT - Load Log of External Test Tool to SAP System | 2 Entries |Transfer Log |
|TWB_RFC_CLOSE |C |000001|eCATT _ RFC Close | 2 Entries |Close RFC Connection|
|USERNAME |C |000032|User for External Tool | 0 Entries |User Name |
|WEB_MODE |C |000001|Replay Mode for Web Dynpro | 3 Entries |WEBDYNPRO Proc. Mode|
-------------------------------------------------------------------------------------|

8. Now all preparations are done and we can use this scenario with PowerShell.

SAP ABAP Tutorials and Materials, SAP ABAP Guides, SAP ABAP Certifications, SAP ABAP Learning

#-Begin-----------------------------------------------------------------

  #-Sub Load-NCo--------------------------------------------------------
  Function Load-NCo {

    [String]$ScriptDir = "C:\NCo"

    If ([Environment]::Is64BitProcess) {
      [String]$Path = $ScriptDir + "\x64\"
    } Else {
      [String]$Path = $ScriptDir + "\x86\"
    }

    [String]$File = $Path + "sapnco.dll"; Add-Type -Path $File
    $File = $Path + "sapnco_utils.dll"; Add-Type -Path $File

  }

  #-Function Get-Destination--------------------------------------------
  Function Get-Destination {

    #-Verbindungsparamter-----------------------------------------------
    $cfgParams = `
      New-Object SAP.Middleware.Connector.RfcConfigParameters
    $cfgParams.Add($cfgParams::Name, "TEST")
    $cfgParams.Add($cfgParams::AppServerHost, "NSP")
    $cfgParams.Add($cfgParams::SystemNumber, "00")
    $cfgParams.Add($cfgParams::Client, "001")
    $cfgParams.Add($cfgParams::User, "BCUSER")
    $cfgParams.Add($cfgParams::Password, "minisap")

    Return [SAP.Middleware.Connector.RfcDestinationManager]::GetDestination($cfgParams)

  }
  
  #-Sub Invoke-SAPFunctionModule----------------------------------------
  Function Invoke-SAPFunctionModule {

    $destination = Get-Destination

    #-ECATT_EXECUTE-----------------------------------------------------
    $eCATTExe = $destination.Repository.CreateFunction("Z_ECATT_EXECUTE")

    #-Set import parameters---------------------------------------------
    $eCATTExe.SetValue("IV_OBJ_NAME", "Z_ECTC_TEST_001")
    $eCATTExe.SetValue("IV_INPUT", "PowerShell")
    $eCATTExe.SetValue("IV_TARGETSYSTEM", "NONE")

    #-Call function module----------------------------------------------
    $eCATTExe.Invoke($destination)

    #-Get export parameter----------------------------------------------
    $LogID = $eCATTExe.GetValue("EV_LOG_ID")

    #-ECATT_LOG_GET_EXPORT----------------------------------------------
    $eCATTLog = $destination.Repository.CreateFunction("Z_ECATT_LOG_GET_EXPORT")

    #-Set import parameters---------------------------------------------
    $eCATTLog.SetValue("IV_SCRIPT", "Z_ECSC_TEST_001")
    $eCATTLog.SetValue("IV_LOGID", $LogID)

    #-Call function module----------------------------------------------
    $eCATTLog.Invoke($destination)

    #-Get export parameter----------------------------------------------
    [SAP.Middleware.Connector.IRfcTable]$ExpVal = $eCATTLog.GetTable("ET_EXPORT_VALUES")
    ForEach ($Line In $ExpVal) {
      Write-Host $Line.GetValue("PNAME") " = " $Line.GetValue("VALUE")
    }

  }

  #-Sub Main------------------------------------------------------------
  Function Main () {
    Load-NCo
    Invoke-SAPFunctionModule
  }

  #-Main----------------------------------------------------------------
  Main

  #-Error routine-------------------------------------------------------
  Trap {
    [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") > $Null
    [Void] [System.Windows.Forms.MessageBox]::Show( `
      $_.Exception.GetType().FullName + [System.Environment]::NewLine + `
      "Error at line " + $_.InvocationInfo.ScriptLineNumber + `
      " in " + $_.InvocationInfo.ScriptName + `
      [System.Environment]::NewLine + [System.Environment]::NewLine + `
      $_.Exception.Message, "An Error Occurred", 0)
    Exit
  }

#-End-------------------------------------------------------------------​

As you can see all works as expected. PowerShell executes the eCATT script, gets the result and writes it on the screen.

If you ask now, after all this effort, why all that? You are right, but I would like to focus your attention on a little something: The parameter IV_TARGETSYSTEM allows you to execute your script on any SAP system in your system landscape directory resp. which are customized in your SM59. On this way you have now the possibility to combine SAP eCATT with PowerShell and to realize a bidirectional communication between an eCATT script and a PowerShell script and vis-รก-vis. And PowerShell opens the gate wide to use Selenium, AutoIt etc., so you can build easily integrated solutions.

No comments:

Post a Comment