How to bind variables to global parameters

Ranorex Spy, Recorder, and Studio.
A_C
Posts: 11
Joined: Fri Jun 21, 2013 11:23 am

How to bind variables to global parameters

Post by A_C » Wed Jul 25, 2018 11:30 am

Hi

I want to bind a module variable to a global variable.
The problem is that the module is only called in user code and so it is not part of the test suite.
Is there an easy way to do that?

McTurtle
Posts: 191
Joined: Thu Feb 23, 2017 10:37 am
Location: Benedikt, Slovenia

Re: How to bind variables to global parameters

Post by McTurtle » Wed Jul 25, 2018 2:09 pm

Hello A_C,

Let's say that this is the code of the user code module that you call from another user code module:
namespace ModuleInModuleCall
{
	[TestModule("EC0F68E4-12E6-419B-B799-567CC9F0A536", ModuleType.UserCode, 1)]
	public class CalledModule : ITestModule
	{
		public CalledModule()
		{
			// Do not delete - a parameterless constructor is required!
		}

		string _TestVariable = "";
		[TestVariable("d6c86bb6-81b4-4368-bb40-dccd86c90437")]
		public string TestVariable
		{
			get { return _TestVariable; }
			set { _TestVariable = value; }
		}

		void ITestModule.Run()
		{
			Report.Info(TestVariable);
		}
	}
}
Then the module that is calling the "sub-module" from the test suite needs to look like this:
namespace ModuleInModuleCall
{
	[TestModule("D789D756-E923-4AC0-8DB0-AB1944B20930", ModuleType.UserCode, 1)]
	public class CallingModule : ITestModule
	{
		public CallingModule()
		{
			// Do not delete - a parameterless constructor is required!
		}

		string _varFromOutside = "";
		[TestVariable("d3c66faa-267d-45f8-9713-2287655c93d4")]
		public string varFromOutside
		{
			get { return _varFromOutside; }
			set { _varFromOutside = value; }
		}
		void ITestModule.Run()
		{
			Mouse.DefaultMoveTime = 300;
			Keyboard.DefaultKeyPressTime = 100;
			Delay.SpeedFactor = 1.0;
			
			var myCalledModule=new CalledModule();
			myCalledModule.TestVariable=varFromOutside;
			TestModuleRunner.Run(myCalledModule);
		}
	}
}
Now you can bind "varFromOutside" to a global parameter and it will be passed to this user code module and the same value will be passed over to the instance of "CalledModule" via "myCalledModule.TestVariable=varFromOutside;".

The key is in this line:
ModuleInModuleCall.png
ModuleInModuleCall.png (10.77 KiB) Viewed 227 times
Does this help?

Regards,
McTurtle

A_C
Posts: 11
Joined: Fri Jun 21, 2013 11:23 am

Re: How to bind variables to global parameters

Post by A_C » Thu Jul 26, 2018 6:56 am

Hello McTurtle ,

thanks for your advice, that works.
But is have also found an easier solution.
You can read and set global parameters with TestSuite.Current.Parameters["GlobalParam"].
This line in the calles module does the job.
namespace ModuleInModuleCall
{
variable = TestSuite.Current.Parameters["GlobalParam"]

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

Re: How to bind variables to global parameters

Post by odklizec » Thu Jul 26, 2018 7:34 am

Hi A_C,

Although the solution you found is easier, it's not something I would personally rely on. Calling global parameters (and parameters as such) directly from code could make your solution messy and not so easy to read and maintain. But it should be OK in case it's just one call/code module ;)
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