First Example

Let's start by looking at the code required to execute the robot named NewsMagazine.robot, which is located in the Tutorials folder of the default project. The robot outputs its results using the Return Value step action, which makes it easy to handle the output programmatically using the API. Other robots (typically those run in a schedule by the Management Console) store their data directly in a database using the Store in Database step action, in which case data collected by the robot will not be returned to the API client.

In the following, we will look at how to execute the NewsMagazine robot and process the output programmatically.

using System;
using System.Collections.Generic;
using System.Text;
using Com.KapowTech.RoboSuite.Api;
using Com.KapowTech.RoboSuite.Api.Repository.Construct;
using Com.KapowTech.RoboSuite.Api.Construct;

namespace Examples
{
    class Program
    {
        static void Main(string[] args)
        {
            var server = new RoboServer("localhost", 50000);
            var ssl = false;
            var cluster = new Cluster("MyCluster", new RoboServer[]{ server}, ssl);

            Request.RegisterCluster(cluster); // you can only register a cluster once per application

            var request = new Request("Library:/Tutorials/NewsMagazine.robot");
            request.RobotLibrary = new DefaultRobotLibrary();
            RqlResult result = request.Execute("MyCluster");

            foreach (RqlObject value in result.GetOutputObjectsByName("Post")) {
                var title = value["title"];
                var preview = value["preview"];
                Console.WriteLine(title + ", " + preview);
            }
            Console.ReadKey();
        }
    }
}
                            

Execute a Robot without input


Let's start by looking at the classes involved and their responsibilities.

RoboServer This is a simple value object that identifies a RoboServer which can execute robots. Each RoboServer must be activated by a Management Console and assigned KCU before use.
Cluster A cluster is a group of RoboServer functioning as a single logical unit.
Request This class is used to construct the robot request. Before you can execute any requests you must register a cluster with the Request class.
DefaultRobotLibrary A robot library instructs RoboServer where to find the robot identified in the request. Later examples will explore the various robot library types and when/how to use them.
RQLResult This contains the result of a robot execution. The result contains value responses, log and server messages.
RQLObject Each value that is returned from a robot using the Return Value action can be accessed as an RQLObject.


Now let's go through each line in the example an look at the specifics.

The first line tells the API that our RoboServer is running on localhost port 50000.

var server = new RoboServer("localhost", 50000);


The next three lines defines a cluster with a single RoboServer. The cluster is registered with the Request class, allowing you to execute request on this cluster. Each cluster may only be registered once per application, this is usually done during the initialization of the application.

var ssl = false;
var cluster = new Cluster("MyCluster", new RoboServer[]{ server}, ssl);
Request.RegisterCluster(cluster);

Registering a cluster


This is then followed by code that creates a request that will execute the robot named NewsMagazine.robot located at Library:/Tutorials. Library:/ refers to the robot Library configured for the request. Here the DefaultRobotLibrary is used, which instructs RoboServer to look for the robot in the servers local file system, see Robot Libraries for details on how to use robot libraries.

var request = new Request("Library:/Tutorials/NewsMagazine.robot");
request.RobotLibrary = new DefaultRobotLibrary();


The next line executes the robot on the cluster named MyCluster (the cluster we previously registered) and returns the result once the robot is done. If an error occurs while the robot is executing an exception will be thrown here.

RqlResult result = request.Execute("MyCluster");


Finally we process the extracted values. First we get all extracted values of the type named Post, and iterate through them. For each RQLObject we access the attributes of the Post type, and print the result. We will look at attributes and mappings in a later section.

foreach (RqlObject value in result.GetOutputObjectsByName("Post")) {
    var title = value["title"];
    var preview = value["preview"];
    Console.WriteLine(title + ", " + preview);
}