Tip: Test Your Custom Activity with an Independent Console Application

In a discussion about creating a custom activity with C# in .NET 6 @Michael_Halpin published a few code snippets to call activities of a Windows workflow. These days I worked very intensively on a customer activity and was able to successfully test this approach in the context of developing a custom activity for the cross-platform compatibility mode. It is very easy and comfortable to use this approach, as we shall see in a moment. :wink:

You can find more information to create a custom activity here.

At first an (empty) example activity, which should be tested. This code is not explained further, because its representation is only important in the context of the use of the namespace, the class name and the arguments.

//-Begin----------------------------------------------------------------

using System;
using System.Activities;
using System.ComponentModel;

namespace MyNamespace {

  public class MyClass : CodeActivity {

    [Category("Input")]
    public InArgument<double> FirstArgument { get; set; }

    [Category("Input")]
    public InArgument<string> SecondArgument { get; set; }

    [Category("Output")]
    public OutArgument<string> Result { get; set; }

    protected override void Execute(CodeActivityContext context) {
      //Your code here
    }

  }

}

//-End------------------------------------------------------------------

Here now the console application with which the activity can be executed independently. The comments in the code explain the steps.

//-Begin----------------------------------------------------------------

using System;
using System.Activities;
using System.Collections.Generic;
using System.Xaml;
//-Add your namespace here----------------------------------------------
using YourNamespace;

class UnitTest {

  public static void Main() {

    try {

      //-Instantiates the class-----------------------------------------
      YourClass yourClass = new YourClass();

      //-Sets the input arguments of the activity-----------------------
      Dictionary<string, object> arguments = new Dictionary<String, Object> {
        { "FirstArgument", 1.0 },
        { "SecondArgument", "Bambi" }
      };

      //-Invokes the activity with the arguments------------------------
      IDictionary<string, object> outputs = 
        WorkflowInvoker.Invoke(yourClass, arguments);

      //-Gets the result of the ouput argument--------------------------
      string result = outputs["Result"].ToString();

    } catch(Exception ex) { }

  }

}

//-End------------------------------------------------------------------

With that approach it is so simple possible to test custom activities for UiPath in another environment, here as console application. This opens up many perspectives for a custom activity developer, especially the possibilities of debugging and test automation. I tried this approach for Windows (x64) and Cross-platform compatibility modes successfully.

You can find more information about the programming of customer activites with .NET 6 here.

Addendum 24.09.2022: I tested this approach in a Linux environment and it worked without any problems.

1 Like