This is a migrated thread and some comments may be shown as answers.

Disabling test steps during runtime

2 Answers 106 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Jon
Top achievements
Rank 2
Jon asked on 20 Sep 2016, 03:08 PM

Hello,

I am using Test Studio to develop automated tests for a complex WPF application.

I am wondering: Is there any API calls I can make in a coded step that allows me to disable specific steps from being run during test execution? 

 

For example:

if (some condition)

{

  disable steps 2-4 of this test

}

else

{

  execute all steps of this test

}

 

I want to have the condition be the value of an environmental variable on the computer where the test will be ran. (Will be ran on multiple different computers with the same environmental variable set to different values).

 

Let me know if there are any methods out there I could use.

 

Thanks,

Jon

2 Answers, 1 is accepted

Sort by
0
Accepted
Nikolay Petrov
Telerik team
answered on 22 Sep 2016, 06:51 AM
Hello Jon,

It is possible to skip a test step during the runtime of the test. To complete that follow the steps:

- add an assembly references to your project for:
 System.Runtime.Serialization - .NET library  (C:\Windows\Microsoft.NET\Framework\v4.0.30319)
 Telerik.TestStudio.Interfaces - \bin directory of the TestStudio installation (C:\Program Files (x86)\Telerik\Test Studio\Bin)
 
- usings:
using ArtOfTest.WebAii.Design.ProjectModel;
using ArtOfTest.Common.Design.ProjectModel;

- code lines:
// loop over all test's steps
foreach (AutomationStepBase step in ExecutionContext.Current.Test.Steps)
{
    // log the number of the test step - for debugging
    Log.WriteLine(step.Order.ToString());
     
    // will skip 2-nd step in the test
    if (step.Order == 2)
    {
        // disable the step
        step.Enabled = false;
    }
}

I hope this example is useful to you.

Regards,
Nikolay Petrov
Telerik by Progress
 
The New Release of Telerik Test Studio Is Here! Download, install,
and send us your feedback!
Nicholas
Top achievements
Rank 1
commented on 09 Aug 2023, 10:54 PM | edited

What about if the steps are inside of a loop. The ExecutionContext.Current.Test.Steps does not include steps that are in a loop or in a if statement. Do you know how to access steps that are inside of these conditions.

Thanks,

Nick

Plamen Mitrev
Telerik team
commented on 10 Aug 2023, 11:57 AM

Hello Nick,

You are right that the above suggested code does not iterate through the steps inside a loop, while or if-else. The scenario, where you need to skip a step during runtime is rarely used. There are other approaches to do it, for example by writing an if-else in a coded step and converting the recorded steps to code that can be executed or excluded.

That said, I worked with the developers to get you an updated version of the previous solution that can drill down in the conditional steps and get all the steps recursively in a list. Check it out below and apply it to your project.

//add this reference
using ArtOfTest.Common.Design.Extensibility.Descriptors;

        [CodedStep(@"New Coded Step")]
        public void WebTest1_CodedStep()
        {
            var allSteps = GetAllSteps();
            allSteps[4].Enabled = false;
            allSteps[6].Enabled = false;
            Log.WriteLine(allSteps.Count.ToString());
            //Log.WriteLine(allSteps[4].Description);
        }
        
        private List<AutomationStepBase> GetAllSteps()
        {
            var allSteps = new List<AutomationStepBase>();
            foreach (AutomationStepBase step in ExecutionContext.Current.Test.Steps)
            {
                GatherStepsRecursively(step, allSteps);
            }
            return allSteps;
        }
        
        private void GatherStepsRecursively(AutomationStepBase step, List<AutomationStepBase> allSteps)
        {
            allSteps.Add(step);
                
            var logicalDescriptor = step.AutoStep as LogicalDescriptor;
            if (logicalDescriptor != null)
            {
                for (int i = 0; i < logicalDescriptor.NumberOfBranches; i++)
                {
                    var innerSteps = logicalDescriptor.GetStepsForBranch(i);
                    foreach(var innerStep in innerSteps)
                    {
                        GatherStepsRecursively(innerStep, allSteps);
                    }
                }
            }
        }
    }

0
Jon
Top achievements
Rank 2
answered on 04 Oct 2016, 06:45 PM
Thank you, this is exactly what I was looking for!
Tags
General Discussions
Asked by
Jon
Top achievements
Rank 2
Answers by
Nikolay Petrov
Telerik team
Jon
Top achievements
Rank 2
Share this question
or