Scripting

This page contains a quick introduction to scripting. For more information see the API.

The code in a script action can access the _Scenario object and the _Results object as well as any variables created in the scenario.
The code can also call any other script actions by calling the Method Name of the script action and passing the _Scenario object and the _Results object as a parameters.

Each script item requires a return type of System.string.
The string returned is added to the notes field in the results.

Any Exceptions thrown will be added to the results as Errors.

NB: When using custom .net modules, ensure that the dll is in the GAC or the same directory as the application.

Contents

Accessing Actions

The list of actions in the scenario is accessed by the code
_Scenario.Actions 
This returns an ActionItemCollection (Inherits from IList, ICollection and IEnumerable). Individual actions can be accessed by name or index
_Scenario.Actions["action_1"] 
// Or
_Scenario.Actions[0] 

Actions are returned as an ActionItem, this is an abstract class that represents all actions.
This obejct has the following properties that can be accessed.
ActionItem action = _Scenario.Actions[0]; // Gets the first action in the scenario
// Properties
string s =  action.Name;
ActionType at = action.ActionType;
string ct = action.CustomType;
bool hc = action.HasChildren;
bool cac= action.CanAddChildren;
ActionItem p = action.Parent;
int i = action.Index;
// Methods
ActionItem ai = action.FindChild(Guid key);
ActionItem[] array = action.FindChildren(Guid key);
  • ActionItem.Name : the name of the action.
  • ActionItem.ActionType : legacy value. This is only used to differentiate between actions that use compiled code (If statements, variables, c# scripts) and those that don't.
  • ActionItem.CustomType : the type of the action
  • ActionItem.HasChildren : if this action has any child actions
  • ActionItem.CanAddChildren : if this action accepts new children
  • ActionItem.Parent : the parent of this action.
  • ActionItem.Index : this actions index in its parent.

Accessing Results

The results obejct contains information about any actions that have already been executed and is accessed from the object:
_Results
This returns a ScenarioResults object.

_Results.Results
  • This returns an ActionResultsCollection (Inherits from IList, ICollection and IEnumerable). Individual actions can be accessed by name, index or key.
  • The key is the same as the ActionItem that was used to create it.
  • If the an ActionItem runs multiple times (due to loops, etc.) the suffix " (n)" is added to the original name, where n is the count.
    • the count is zero base, but only shows on the second item onwards.

ActionResults result = _Results.Results[Guid key];
// Properties
string result.Name;
Guid result.Key;
string result.CustomType;
bool result.HasErrors;
List<string> result.Errors
bool result.HasWarnings;
List<string> result.Warnings;
Dictionary<string, object> result.Data;
Dictionary<string, object> result.Results;
// Methods

Action Types

  • The ActionType can be used to determine with a type comparison of the action or by using the CustomType field.
if(action.GetType().Equals(typeof(ActionItem_WebRequest)))
{
    ActionItem_WebRequest webrequest = (ActionItem_WebRequest)action;
}
else if(action.CustomType.Equals(ActionItem_Folder.Type))
{
    ActionItem_Folder folder = (ActionItem_Folder)action;
}
else if(action.GetType().Equals(typeof(ActionItem_If)))
{
    ActionItem_If if_action = (ActionItem_If)action;
}
else if(action.CustomType.Equals(ActionItem_Loop.Type))
{
    ActionItem_Loop loop = (ActionItem_Loop)action;
}
else if(action.GetType().Equals(typeof(ActionItem_Script)))
{
    ActionItem_Script script = (ActionItem_Script)action;
}
else if(action.GetType().Equals(typeof(ActionItem_Variable)))
{
    ActionItem_Variable variable = (ActionItem_Variable)action;
}
  • Once cast to a specific type, the options specific to that action can be accessed.
  • See the API for more details on these options.
// The following example changes the uri of the WebRequest with name "request_01"
ActionItem action = _Scenario.Actions["request_01"];
if(action != null && action.ActionType == ActionType.WebRequest)
{
    ActionItem_WebRequest request_1 = (ActionItem_WebRequest)action;
    request_1.Uri = new Uri("www.webpaltt.codeplex.com");
}

Cookies

  • Each scenario contains a list of cookies (List<Cookies> not a CookieContainer or CookieCollection)
  • New cookies are automatically added to when a response contains a set-cookie header.
  • This list of cookies are also added to each request when sent.

To manually alter the cookies the list can be accessed as follows.
// Adds a new cookie with name "uid" and value "username" to the list if it is empty.
if(_Scenario.Cookies.Count == 0)
{
    _Scenario.Cookies.Add(new Cookie("uid", "username"));
}

Last edited Jun 5, 2012 at 3:26 AM by Thkng, version 14

Comments

No comments yet.