Remove TestStep programmatically

I would like to remove/disable TestStep “0 Initialize instruments” from my TestPlan.
Bez názvu

Am i doing it wrong or is there another solution?
However, this solution does not work.

Thank you

It seems like you’re trying to remove a TestStep named “0 Initialize instruments” from a TestPlan programmatically, possibly using OpenTAP or a similar test automation framework. From the provided context, the correct approach to remove a TestStep would involve accessing the TestPlan, finding the specific TestStep by name or index, and then removing it following proper API usage of the framework, and finally saving the TestPlan.

Based on the context provided (which includes incomplete and fragmented code), a potential correction to the code snippet could be as follows:

public void DisableInitializeStep(string TestProgramPath)
{
    // Load the TestPlan
    TestPlan myTestPlan = TestPlan.Load(TestProgramPath);

    // Find the TestStep to remove
    TestStep stepToRemove = myTestPlan.Steps.FirstOrDefault(step => step.Name == "0 Initialize instruments");

    // Remove the TestStep if found
    if (stepToRemove != null)
    {
        myTestPlan.Steps.Remove(stepToRemove);
        myTestPlan.Save(TestProgramPath); // Save the changes to the TestPlan
        PrintAllStepNames(TestProgramPath); // Assuming this function prints out all step names
    }
    else
    {
        Debug.WriteLine("Step \"0 Initialize instruments\" was not found.");
    }
}

Make sure to:

  1. Correctly load the TestPlan using TestPlan.Load.
  2. Look for the TestStep within the loaded TestPlan’s Steps collection.
  3. Remove the TestStep only if it is found.
  4. Save the TestPlan after making changes to persist them to the file.

Please adapt the code as per the actual names of methods, properties, and classes as defined in the specific test automation framework you’re using. If the framework you are using provides different methods or procedures for such actions, refer to the official documentation for the exact method calls required. If the context does not match the functionality of the software you are using, provide additional details or consult the software’s documentation for the correct implementation approach.

Helpful Source:

Keysight-Instrument-Plugins/Power Supplies/E364x Dual Output DC Power Supplies/1.0.1/E364xD/Step.cs at main · opentap/Keysight-Instrument-Plugins · GitHub


Disclaimer: I am an AI Helper, my responses are supplementary solutions for you to use while waiting for our developers to get back to you.

Hi @jakub, and welcome to the forum.

From looking at the code snippet, I think the issue is that you have two different instances of the same teststep. The step list of TestPlan uses the default equality comparer to find the test step you are trying to remove, which in this case is comparing items by reference since TestStep does not override equality.

In the snippet you provided, you are creating different instances of the same step by using TestPlan.Load to load a new instance of the test plan.

What I would suggest instead is to either:

  1. Do the scan on myTestPlan.Steps
  2. Locate the step in myTestPlan.Steps by comparing the ID instead: myTestPlan.Steps.FirstOrDefault(step => step.Id == stepToRemove.Id)

Hope this helps!

3 Likes

Working perfectly. Thank you Alexander!

3 Likes