ranorex

How to use Global variables in Ranorex?


I need to create and use global variable as optional parameter, but do not know how I can implement it.

I created the global variable in Ranorex studio:

enter image description here

Also this variable appeared in Data binding tag:

enter image description here

But I can't use this variable in the code. (ASECore package do not contains any parameters).


Solution

  • You can use the global Variables in Ranorex Record module or Ranorex Code module. Let me please first make an introduction of how to use them.

    Create in Record module

    In the Record module, click button Variables... in the top right corner and add the variables you want to use in the redord module.

    enter image description here

    Then use them in your recording: enter image description here


    Create in Code module

    When you create a code module, it will look like this:

    /// <summary>
    /// Description of MyCode.
    /// </summary>
    [TestModule("32310FEC-5336-4F83-B448-ABC851EE5731", ModuleType.UserCode, 1)]
    public class MyCode : ITestModule
    {
        /// <summary>
        /// Constructs a new instance.
        /// </summary>
        public MyCode()
        {
            // Do not delete - a parameterless constructor is required!
        }
    
        /// <summary>
        /// Performs the playback of actions in this module.
        /// </summary>
        /// <remarks>You should not call this method directly, instead pass the module
        /// instance to the <see cref="TestModuleRunner.Run(ITestModule)"/> method
        /// that will in turn invoke this method.</remarks>
        void ITestModule.Run()
        {
            Mouse.DefaultMoveTime = 300;
            Keyboard.DefaultKeyPressTime = 100;
            Delay.SpeedFactor = 1.0;
        }
    }
    

    Now, right click in the code and choose "Insert new module variable". Then you can set a name and default value. Press ok and it will add something like this:

    string _MyVariable = "DefaultValue";
    [TestVariable("de0fb4a9-32ba-4635-8f0f-4ff6db184c3f")]
    public string MyVariable
    {
        get { return _MyVariable; }
        set { _MyVariable = value; }
    }
    

    Now, you can use the variables in the run method like normal C# properties:

    repo.Calculator.CalculatorResults.PressKeys(Input_1);
    repo.Calculator.PlusButton.Click();
    repo.Calculator.CalculatorResults.PressKeys(Input_2);
    repo.Calculator.EqualButton.Click();
    

    How to bind Variables in Suite

    When you created the global parameters, it's true that you can not bind them on the suite level. enter image description here

    Therefore close the dialog and right click on the Record/Code module and choose "Data binding" enter image description here

    In the lower table you can bind your variables of the Record/Code module to the global variables. If they have the same name, you can also Auto-bind them.

    When you now execute the test suite, the values of the global variables will be used in test. If you execute the Record/Code module standalone, then the default values will be used in test.