Stop running module on condition

Best practices, code snippets for common functionality, examples, and guidelines.
bluestockings
Certified Professional
Certified Professional
Posts: 6
Joined: Thu Nov 24, 2016 11:03 am

Stop running module on condition

Post by bluestockings » Thu Nov 24, 2016 12:59 pm

I'd like to stop a currently running recording module based on a condition.
My solution was to add a user code at the beginning of the recording module, if the condition is true it should validate something and then stop the current module, if false execute the module normally.

C# Code in called user code:
if(condition){
      Validate.Exists(Path);
      TestReport.EndTestModule();
}
This will end the currently running module, but also always marks the module as "Failed" in the report, even when the validation returns true. Is there another way of stopping the currently running module?

Running with Ranorex 6.1.0 on a Windows 7 machine

bluestockings
Certified Professional
Certified Professional
Posts: 6
Joined: Thu Nov 24, 2016 11:03 am

Re: Stop running module on condition

Post by bluestockings » Thu Nov 24, 2016 1:45 pm

With Ranorex 6.1.1 it doesn't work either...

Martin
Posts: 152
Joined: Fri Aug 15, 2014 12:24 pm

Re: Stop running module on condition

Post by Martin » Thu Nov 24, 2016 3:17 pm

Well you could build your own logic around it.

What you could do is convert all the recorded actions to user code methods and then call them depending on the condition.

So you would have userCode methods lets say like 1. ClickButton() 2. ClickLink() 3. ValidateTitle()
if (conditionIsMet)
{
    ClickButton();
    ClickLink();
    ValidateTitle();
} else {
    Report.Info("Conditions were not met");
}
So basically if conditions are not met the module methods are skipped and test moves to the next module.

bluestockings
Certified Professional
Certified Professional
Posts: 6
Joined: Thu Nov 24, 2016 11:03 am

Re: Stop running module on condition

Post by bluestockings » Thu Nov 24, 2016 3:37 pm

Problem is: converting the recorded actions into code is not an option.

I need this logic in a lot of modules and someone with no coding skills needs to be able to maintain the modules.

In general the method I used does work, except for it always returning that the module failed.
Weird thing is: although the module is marked as failed, the test case and report show all went well...
Attachments
failed_but_not_failed.png
Report
failed_but_not_failed.png (24.35 KiB) Viewed 1714 times

Martin
Posts: 152
Joined: Fri Aug 15, 2014 12:24 pm

Re: Stop running module on condition

Post by Martin » Thu Nov 24, 2016 3:41 pm

Hey again

To me what the report seems to depict is correct. Seems to display the X icon next to the test case without marking it Failed as the main marker shows that every test went well.

I can't really see anything wrong with it. Even the testcase itself shows that it passed on the left side of it. Just take the x as a marker to show what was skipped.

bluestockings
Certified Professional
Certified Professional
Posts: 6
Joined: Thu Nov 24, 2016 11:03 am

Re: Stop running module on condition

Post by bluestockings » Thu Nov 24, 2016 3:50 pm

Maybe we can live with that for now.
Still think it is a Bug to mark it as failed when it just has been (partially) skipped.

Martin
Posts: 152
Joined: Fri Aug 15, 2014 12:24 pm

Re: Stop running module on condition

Post by Martin » Fri Nov 25, 2016 8:44 am

bluestockings wrote:Maybe we can live with that for now.
Still think it is a Bug to mark it as failed when it just has been (partially) skipped.
And again I can't really see no indication that anything failed. The test case where the x is posted has passed. And the main ball that groups all the test cases shows no fails either.

bluestockings
Certified Professional
Certified Professional
Posts: 6
Joined: Thu Nov 24, 2016 11:03 am

Re: Stop running module on condition

Post by bluestockings » Fri Nov 25, 2016 1:26 pm

Just noticed another problem with this:

The report has no entries after the module that has been stopped through code except for the Headers and the last Teardown doesn't get executed.

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

Re: Stop running module on condition

Post by odklizec » Fri Nov 25, 2016 1:48 pm

To be honest, I would not recommend to use the approach as you do it now (stopping the module during its execution and without raising an exception). In my opinion, it's a flawed test design?

If you want to check something before execution of a module, I would personally do it via a separate code module, placed before the module in question. Then based of the condition in code module, disable the following module so it does not get executed at all. Even this is not a very nice test workflow, but at least the test module is not started and then abruptly killed ;)
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

bluestockings
Certified Professional
Certified Professional
Posts: 6
Joined: Thu Nov 24, 2016 11:03 am

Re: Stop running module on condition

Post by bluestockings » Fri Nov 25, 2016 4:55 pm

Thanks Pavel for the idea to do it the other way round :) .
Now I have the validation in one Module with a code module which only enables the next Module if the condition is met.
Enabling a module via code works perfectly fine and has no side effects to the report.

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

Re: Stop running module on condition

Post by odklizec » Sat Nov 26, 2016 12:46 pm

Nice to hear you found the idea useful :) Good luck.
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

Marc
Posts: 10
Joined: Wed Feb 15, 2017 5:54 pm

Re: Stop running module on condition

Post by Marc » Thu Aug 10, 2017 11:39 pm

@odklizec
regarding this:
If you want to check something before execution of a module, I would personally do it via a separate code module, placed before the module in question. Then based of the condition in code module, disable the following module so it does not get executed at all.
how do you do this?
I'm really quite new to Ranorex, but not to QA or test automation.


-marc

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

Re: Stop running module on condition

Post by odklizec » Fri Aug 11, 2017 7:55 am

Hi Marc,

Well, I'm doing exactly what's described in this post ;) Unfortunately, it's hard to describe it in more details without seeing your actual AUT and the test case you are trying to automate.

Basically, I have a code module placed before the testcase/smartfolder, in which I'm evaluating a state of test or actual value of data connector, and based of found result, I enable or disable testcase/smartfolder form code. This can be done via code like this:

Code: Select all

	// get current test case name
	var curTestCase = (TestCaseNode)TestSuite.CurrentTestContainer; 
	string curTCName = curTestCase.Name; 
	//...
	if (curTCName == "STC_Configuration")
	{
		var getTestCase = TestSuite.Current.GetTestContainer(curTCName);
		// get actual value from data connector ("fileName" column) 
		string fileName = getTestCase.DataContext.CurrentRow.Values[curTC.DataContext.Source.Columns["fileName"].Index];
		if (fileName == "3.xml")
		{
			TestSuite.Current.GetTestContainer("SPU_SetRouteName_" + cm_Customer).Checked = false;
		}
		else
		{
			TestSuite.Current.GetTestContainer("SPU_SetRouteName_" + cm_Customer).Checked = true;		
		}
	}
However, since Ranorex 7.1, it's possible to run a testcase/smartfolder, based of a condition defined in a properties of given testcase/smartfolder! So there is no need to do it from code (in most cases) anymore! So I would suggest you to investigate this way...
https://www.ranorex.com/blog/how-to-cre ... ut-coding/
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

Marc
Posts: 10
Joined: Wed Feb 15, 2017 5:54 pm

Re: Stop running module on condition

Post by Marc » Thu Aug 17, 2017 11:04 pm

odklizec wrote: However, since Ranorex 7.1, it's possible to run a testcase/smartfolder, based of a condition defined in a properties of given testcase/smartfolder! So there is no need to do it from code (in most cases) anymore! So I would suggest you to investigate this way...
https://www.ranorex.com/blog/how-to-cre ... ut-coding/
Thanks! got it working.

Is there any issues updating from 6.1.1 to 7.1?

sstellaccio
Posts: 3
Joined: Thu Sep 14, 2017 3:09 pm

Re: Stop running module on condition

Post by sstellaccio » Wed Sep 20, 2017 12:01 am

Hi to everyone!

First of all, I'm using Ranorex 7.0.1 with .NET Runtime Version 4.0.30319.34209 on Microsoft Windows 7 32 bits.

I was wondering if someone can help me with the original question in the post: stopping running recording module and continue with next one.
In my case, is not viable to use the trick of a smart folder, because my recording module is inside "Setup" section of the test case. Basically I do a sort of things at the beginning of every test case (Setup) like open the application under test and make a login. But in every test case I CLOSE the application ONLY if something goes wrong during the test (that's because the application remains in a unuseful state). So the next test case must execute the recording module inside its Setup section ONLY if the application under test is not already opened.
My thought is running the recording module based on a condition. Converting it to a User Code Module is a really bad idea due to the ammount of objects that the module use and the comfort of the "actions" structure of the recording module.

I've tried to create a User Code Method (inside a User Code Collection) that is called as the first action of the recording module with this code:
[UserCodeMethod]
        public static void EndModuleIfObjectExist(Ranorex.Core.Repository.RepoItemInfo iObject)
        {

        	if (iObject.Exists(1000)) {
        		
        		Ranorex.Core.Reporting.TestReport.EndTestModule();
        		
        	}
        
        }
When I include the User Code Method as the first action of the recording module, y set the application form object on iObject parameter. If the application is already opened, the Ranorex.Core.Reporting.TestReport.EndTestModule() instruction is executed, but then the recording module continue with the following actions inside it (which is not desired and lead to a error on the execution).

I don't know if I'm clear with the point, but basically I need on a recording module that the first action inside it define if the next actions must be executed or not and, if not, continue with the next recording module of the Setup section of the test case.

Thanks to everyone in advanced. Regards,

Simón