Table of Contents

Programmatic Test Execution Flow Control

Controlling the flow of your test execution programmatically allows for creating dynamic and resilient test scenarios. This guide covers how to manage test suite execution, orchestrate modules, and implement robust error handling patterns using the Ranorex API.

Test Suite Execution

You can programmatically control the execution of an entire test suite, a specific test case, or even a single module.

Running a Test Suite

The TestSuite.Run() method allows you to start a test suite from your code. This is useful for custom runners or when integrating Ranorex tests into a broader testing framework.

Example:

// Run the entire test suite
TestSuite.Run();

Running a Specific Test Case

You can also run a specific test case by its name.

Example:

// Run a specific test case
TestSuite.Current.GetTestCase("MyTestCase").Run();

Module Orchestration

Individual modules can be orchestrated to create complex test scenarios that adapt to runtime conditions.

Running a Module Programmatically

You can instantiate and run any module from your code. This is powerful for creating reusable logic and conditional execution paths.

Example:

// Instantiate a recording module
var myRecordingModule = new MyRecordingModule();
myRecordingModule.Run();

// Instantiate a code module
var myCodeModule = new MyCodeModule();
myCodeModule.Run();

Passing Data Between Modules

Data can be passed between modules using variables. You can set module variables from your code before running the module.

Example:

var loginModule = new LoginModule();

// Set a variable on the module instance
loginModule.Username = "myuser";
loginModule.Password = "mypassword";

// Run the module
loginModule.Run();

Error Handling Patterns

Robust error handling is crucial for stable and reliable test automation.

Try-Catch Blocks

Use standard C# try-catch blocks to handle exceptions that may occur during module execution.

Example:

try
{
    var someModule = new SomeModuleThatMightFail();
    someModule.Run();
}
catch (Ranorex.ValidationException ex)
{
    Report.Failure("Validation failed: " + ex.Message);
    // Add recovery steps here
}
catch (Ranorex.ElementNotFoundException ex)
{
    Report.Failure("Element not found: " + ex.Message);
    // Add recovery steps here
}

Using Test Case Teardown

The TestCase.TearDown() method is a good place to put cleanup logic that should run even if the test case fails.

Example:

public partial class MyTestCase
{
    void ITestModule.Run()
    {
        // Test case logic
    }

    public override void TearDown()
    {
        // This code will run whether the test case passes or fails
        // e.g., close the application
        var repo = MyProjectRepository.Instance;
        Host.Current.KillApplication(repo.MyApp.Self);
    }
}

By leveraging these programmatic control flow techniques, you can create sophisticated and resilient test automation solutions that go beyond simple record-and-playback.