Skip to content

Blocks.Engine

Avrigeanu Laurian edited this page Sep 5, 2022 · 5 revisions

The Engine class contains a method that will allow you to create your own Sequence, Flowchart or StateMachine.

No matter what, the Engine actions will always attempt to continue chronologically unless otherwise stated in Conditions or Actions.

The Engine class has the following private variables:

  • Dictionary<object, object> Output
  • Func<object, object>[] actions
  • Dictionary<string, Func<string>[]> TransitionConditions
  • Dictionary<string, bool> ContinueOnError
  • int index
  • int oldIndex
  • bool isRunning

The Engine class also has two constructors:

  • Engine()
  • Engine(Func<object, object>[] _actions, Dictionary<string, Func<string>[]> _TransitionConditions = null, Dictionary<string, bool> _ContinueOnError = null)

You can initialize the Engine class in a variable like this:

  • var machine = new Engine()

Afterwards, you may add Actions, Conditions or ContinueOnError checks to this machine like this:

machine.AddAction((ClickWebElement) => WebEvents.Action.Click(chromeDriver,"XPath"));

machine.AddAction((ClickWebElement2) => WebEvents.Action.Click(chromeDriver,"XPath2"));

machine.AddCondition("ClickWebElement2", () => "ClickWebElement");

machine.AddContinueOnError("ClickWebElement",true);

Warning: An action must always return a value.

To explain what this does in a grammatical fashion, we add two actions that we want the machine to do, we want it to Click a Web Element with the specified XPaths, however, when "ClickWebElement2" finishes execution, we want to go to "ClickWebElement" and retry and just in case "ClickWebElement" gives throws an exception we added a way to resume afterwards instead of having our execution blocked.

The machine variable at this moment in time can be started like this:

machine.Execute();

Afterwards, you may read the values of the Actions like this:

var ClickWebElement_value = machine.GetOutput("ClickWebElement");

Or, you may want to read the Dictionary<object, object> which contains all of the Actions and values of those Actions like this:

var theDictionary = machine.GetDictionary();

Nested Adding of actions, conditions or continueonerrors

The above example of adding actions, conditions or continueonerrors can also be written like this:

machine.AddAction((ClickWebElement) => WebEvents.Action.Click(chromeDriver,"XPath")).AddAction((ClickWebElement2) => WebEvents.Action.Click(chromeDriver,"XPath2")).AddCondition("ClickWebElement2", () => "ClickWebElement").AddContinueOnError("ClickWebElement",true);

There are also array methods that will allow you to insert actions, conditions or continueonerrors in bulk, such as:

  • AddActions

  • AddConditions

  • AddContinueOnErrors

    There are also methods to Remove singular or multiple actions, conditions or continueonerrors.

  • RemoveAction - RemoveActions

  • RemoveCondition - RemoveConditions

  • RemoveContinueOnError - RemoveContinueOnErrors

These methods only accept the Action name, using the example above I can write this:

machine.RemoveAction("ClickWebElement");

machine.RemoveCondition("ClickWebElement2");

machine.RemoveContinueOnError("ClickWebElement");

And now our machine only has one action in it, with no conditions or continueonerrors, if you want to remove in bulk, know that the bulk methods will want an array of strings that contain the names of the actions, conditions or continueonerrors.

Method acting

The Engine class can act like you tell it to, for example:

  • Stop();

  • Resume();

  • JumpTo();

    The Stop(); method can be used whenever during runtime to stop the engine execution and continue code execution afterwards.

    The Resume(); method can be used to resume the engine execution from where it stopped, whenever during code execution you desire.

    The JumpTo(); method can be used to tell the engine which line to jump to, all it requires is the name of the Action to jump to, like this:

  • machine.JumpTo("ClickWebElement2");

Using the advanced constructor

The advanced constructor is just a shorthandle to avoid using multiple nested methods to achieve the same result.

However, the end result might not be as easily readable by a junior, observe the below example:

             var engine = new Engine(new Func<object, object>[]
                {
                    (myAction) => WebEvents.Action.Click(new ChromeDriver(), "")
                }, new Dictionary<string, Func<string>[]>()
                {
                    ["myAction"] = new Func<string>[] {() => "myAction"}
                },
                new Dictionary<string, bool>()
                {
                    {"myAction", true}
                });

Clone this wiki locally