Creating Custom Validation Logic
While Ranorex provides a rich set of built-in validation actions, you may encounter scenarios that require custom validation logic. This guide explains how to create custom validation methods, implement powerful assertion patterns, and handle error reporting programmatically.
Custom Validation Methods
The most straightforward way to create custom validation is by writing your own methods that encapsulate the validation logic.
Simple Validation Method
A simple validation method might check a specific property of a UI element and report a success or failure.
Example:
public void ValidateElementColor(Adapter element, string expectedColor)
{
string actualColor = element.Element.GetAttributeValueText("ControlText");
if (actualColor == expectedColor)
{
Report.Success("Validation OK", $"Element color is '{expectedColor}'.");
}
else
{
Report.Failure("Validation Failed", $"Expected element color to be '{expectedColor}' but was '{actualColor}'.");
}
}
Using Validate.Attribute
For more standard attribute validations, you can use the Validate.Attribute method, which integrates seamlessly with the Ranorex reporting system.
Example:
public void ValidateElementText(Adapter element, string expectedText)
{
Validate.Attribute(element.Element, "Text", expectedText);
}
Assertion Patterns
For more complex scenarios, you can implement more advanced assertion patterns.
Fluent Assertions
Fluent assertion libraries like FluentAssertions can make your validation code more readable and expressive.
Example (with FluentAssertions library):
// Add FluentAssertions NuGet package to your project first.
using FluentAssertions;
public void ValidateItemCount(int actualCount, int expectedCount)
{
actualCount.Should().Be(expectedCount, "because that's the number of items we expected.");
}
Soft Assertions
Soft assertions allow you to perform multiple validations and report all failures at the end, rather than stopping at the first failure.
Example:
public void ValidateUserDetails(User user)
{
bool allValid = true;
if (user.FirstName != "John")
{
Report.Error("Validation Failed", "First name is incorrect.");
allValid = false;
}
if (user.LastName != "Doe")
{
Report.Error("Validation Failed", "Last name is incorrect.");
allValid = false;
}
if (allValid)
{
Report.Success("User details are correct.");
}
else
{
Report.Failure("One or more user details are incorrect.");
}
}
Error Reporting
Customizing how errors are reported can provide clearer and more actionable feedback.
Custom Log Messages
Use the Report class to log custom messages, including screenshots.
Example:
public void ValidateSomethingComplex()
{
// ... complex logic ...
if (isInvalid)
{
// Take a screenshot of a specific element
Bitmap screenshot = Imaging.CaptureImage(someElement);
Report.Log(ReportLevel.Failure, "Complex validation failed.", new RecordItemIndex(0), Imaging.GetImageFormat(screenshot), screenshot);
}
}
Throwing Custom Exceptions
For critical failures, you can throw custom exceptions that can be caught by your test framework.
Example:
public class CriticalValidationException : Exception
{
public CriticalValidationException(string message) : base(message) { }
}
public void ValidateCriticalSystemState()
{
if (isSystemUnstable)
{
throw new CriticalValidationException("System is in an unstable state, aborting test.");
}
}
By creating custom validation logic, you can tailor your tests to the specific needs of your application and achieve more precise and meaningful test results.