Run Recording in Teardown only if testcase failed?

Ask general questions here.
mrt
Posts: 257
Joined: Mon Mar 16, 2020 11:31 am

Run Recording in Teardown only if testcase failed?

Post by mrt » Fri Jul 01, 2022 9:06 am

Dear all,

I want to know if there is a way to run a recording in the teardown of a testcase, only if the testcase execution failed?

Running a user code in the teardown on fail is easy:

Code: Select all

public static void MyUserCodeMethod()
{
   if (TestSuite.CurrentTestContainer.Status == Ranorex.Core.Reporting.ActivityStatus.Failed)
   {
     ... my code ...
   }
}
Previously this was just very short code in the user code method, but now the code grew bigger and I decided to move the actions to a recording instead for easier overview and maintenance.
But, as there are no smart folders for conditions or other structural containers available in the teardown, the only solution I can think of is running the recording from a user code (which I don't really like):

Code: Select all

public static void MyUserCodeMethod()
{
   if (TestSuite.CurrentTestContainer.Status == Ranorex.Core.Reporting.ActivityStatus.Failed)
   {
      myRecording.Start();
   }
}
Is there any (better) solution to accomplish this?
thanks!

User avatar
odklizec
Ranorex Guru
Ranorex Guru
Posts: 7470
Joined: Mon Aug 13, 2012 9:54 am
Location: Zilina, Slovakia

Re: Run Recording in Teardown only if testcase failed?

Post by odklizec » Fri Jul 01, 2022 10:05 am

Hi,

I'm using code module, located as a very first module in TearDown section and using this code

Code: Select all

ITestContainer lastTestCase; 
if (cm_Customer=="rdi")
{
    lastTestCase = TestSuite.Current.GetTestContainer(curTestCase_prefix + "_RepeatFileUpload_"+cm_Customer+"_"+cm_TCLocation);
}
else
{
    lastTestCase = TestSuite.Current.GetTestContainer(curTestCase_prefix + "_RepeatFileUpload_"+cm_Customer);
}
if (lastTestCase.Status.Equals(Ranorex.Core.Reporting.ActivityStatus.Failed))
{  
    foreach(TestModuleLeaf module in curTestCase.AllModules)  
    {  
        if (module.IsDescendantOfTearDownNode())
        {
            module.Enabled = false;  
        }
    }  
}
In case of failure, it disables all modules in TearDown section. But I'm quite sure you can easily modify it to skip only particular recordings. Hope this helps?
Pavel Kudrys
Ranorex explorer at Descartes Systems

Please add these details to your questions:
  • Ranorex Snapshot. Learn how to create one >here<
  • Ranorex xPath of problematic element(s)
  • Ranorex version
  • OS version
  • HW configuration

mrt
Posts: 257
Joined: Mon Mar 16, 2020 11:31 am

Re: Run Recording in Teardown only if testcase failed?

Post by mrt » Fri Jul 01, 2022 10:39 am

Ahh yes, I did not think of disabling modules themselves.

That will do, thanks!

mrt
Posts: 257
Joined: Mon Mar 16, 2020 11:31 am

Re: Run Recording in Teardown only if testcase failed?

Post by mrt » Tue Jul 05, 2022 9:51 am

I will try to keep the code a bit more dynamic, so I want to add a code module, that enables / disables the next module.

Is there a way to get the current module easily, so I can do a +1 to get to the next module without knowing its name or any other static attribute?

mrt
Posts: 257
Joined: Mon Mar 16, 2020 11:31 am

Re: Run Recording in Teardown only if testcase failed?

Post by mrt » Tue Jul 05, 2022 10:21 am

Got it:

Code: Select all

TestModuleLeaf.Current
gives the actual test module.

Enabling next test module when current test case failed:

Code: Select all

TestCaseNode curTc = (TestCaseNode)TestSuite.CurrentTestContainer;
            
if (curTc.Status.Equals(Ranorex.Core.Reporting.ActivityStatus.Failed))
{
    // skip until current test module is found, then skip it an pick the (single) next one
    TestModuleLeaf module = (TestModuleLeaf)curTc.AllModules.SkipWhile(x => x != TestModuleLeaf.Current).Skip(1).Single();
    Report.Info("Module", "Test case failed, enabling test module '" + module.Name + "'");
    module.Enabled = true;
 }