How can I validate user input for a settings parameter on a Test Step?

When we need to restrict what values a user can input into a settings field on a Test Step, we can use the Rules object defined by the Test Step.

This allows developers to fully customize what input is allowed as well as provide an error message to display to the user.

The following example shows how to check whether the value of a setting is less than 10 using an anonymous function.

// In the constructor of the TestStep
{ 
    Rules.Add(() => Setting1 < 10, "Setting 1 should be less than 10", "Setting1");
}

For more complex rules, you can also use custom functions rather than anonymous functions for better abstraction.
Example:

private bool IsSetting1Valid() 
{
    return Setting1 < 10;
}

// In the constructor ...
{
    Rules.Add(IsSetting1Valid, "Setting1 should be less than 10", "Setting1");
}
5 Likes