Page 1 of 1

Calculate Difference of two Variables and validate

Posted: Fri May 12, 2017 7:13 pm
by nas
I have two variables in my Ranorex view, that have been 'GetValued' from two different Ranorex recordings in the same Project.

How do I calculate the difference of the two and validate that they are greater than 0.

For example

Validate True if: ($myValue1 - $myValue2) > 0

I created a new UserCodeModule but when I typed in $myValue1 (or myValue1) it could not see the variable that was a 'GetValue' from a Ranorex Recording.

1. How does my UserCodeModule.cs have visibility to $myValue1 (or myValue1) and $myValue2 (or myValue2) created by two separate Ranorex recordings in my project, and

2. How do I Validate True if: ($myValue1 - $myValue2) > 0

Thx

Re: Calculate Difference of two Variables and validate

Posted: Sun May 14, 2017 10:53 am
by odklizec
Hi,

This blog post should teach you how to work with variables between modules...
https://www.ranorex.com/blog/sharing-da ... o-another/

Re: Calculate Difference of two Variables and validate

Posted: Mon May 15, 2017 8:43 pm
by nas
I created a new module and added the two variables I needed via the 'data binding' mechanics as specified in the post, however when I went to the usercode of my new module, and typed in the variable names to do the difference calculation as mentioned, it errored saying:

myVariable name does not exist in the current context (CS0103)

Re: Calculate Difference of two Variables and validate

Posted: Tue May 16, 2017 7:24 am
by odklizec
Hi,

I'm afraid, without seeing your solution, it's pretty hard to tell what's wrong. My guess is that you did something wrong with the data binding? But to be clear, by following the blog post from my previous comment, you will not get the direct access to variable from first recording and be able to use it in second recording. The example simply connects a variable from recording 1 with variable in recording 2. So if you want to use content of variable from recording 1 in recording 2, you simply have to create a new variable in recording 2 a connect it (via test case parameter and data binding) with variable in recording 1. It may sound messy, but it's pretty simple ;)

Re: Calculate Difference of two Variables and validate

Posted: Thu Oct 19, 2017 7:19 pm
by moadipp
I don't know if is to late but i think i got somethink interresting!

public bool calcul(string One, string Two, string Three)
        {
            int a = 0;
            if (!int.TryParse(One, out a))
            {
                return false;
            }

            int b = 0;
            if (!int.TryParse(Two, out b))
            {
                return false;
            }
            int c = 0;
            if (!int.TryParse(Three, out c))
            {
                return false;
            }

            if ((a + b) != c)
            {
                return false;
            }

            return true;

        }

Re: Calculate Difference of two Variables and validate

Posted: Thu Oct 19, 2017 8:39 pm
by Support Team
Hello nas,

Thank you for posting your question about how to confirm two variables are greater than zero, and to calculate the difference between the two variables.

Say, for instance (like your example), you are using GetValue() on a repository element, and loading that into myVariableOne. You are also doing GetValue() on another repository object and loading that into myVariableTwo. This is just fine, but you must understand the scope of these variables is limited to only this module. Odklizec's link tells you how to bind module variables to parameters so the scope is more than just the module, and data can be shared to another module. You do not have to do this, however, so I will explain.

Now that GetValue has loaded the value into your two string variables, then you can "Add new action" to your action table, and choose "User code" > "New user code method". Name the method in the action table, and hit enter. Now you can double click the action table step for your new method, and it will open up a new tab right where you need to add the following code:

Code: Select all

bool isFailed = false;
string myReason = "";
int myFirstInt = Convert.ToInt16(myVariableOne);
int mySecondInt = Convert.ToInt16(myVariableTwo);
int mySolution = 0;
if (myFirstInt > 0) {
	if (mySecondInt > 0) {
		if (myFirstInt - mySecondInt > 0) {
			mySolution = myFirstInt - mySecondInt;
		} else {
			mySolution = mySecondInt - myFirstInt;
		}
		Report.Info("Variable 1: " + myVariableOne);
		Report.Info("Variable 2: " + myVariableTwo);
		Report.Info("Difference of two variables: " + mySolution.ToString());
	} else {
		isFailed = true;
		myReason = "myVariableTwo not greater than zero: " + myVariableTwo;
	}
} else {
	isFailed = true;
	myReason = "MyVariableOne not greater than zero: " + myVariableOne;
}

if (isFailed) {
	Report.Failure(myReason);
}
The advantage here is that you're in the same module, so the scope of the variables does not need to be considered, and you can continue in the same action table after this method has ran.

Moadipp's answer is more along the lines of a reusable method you could add to a user code collection. You can read more about that topic at the following link:

https://www.ranorex.com/help/latest/use ... nd-methods

I hope this information is helpful to you, and I look forward to your response.

Sincerely,

- M. Kendall McIntosh
Automation Support Engineer