Page 1 of 2

Use variables in code

Posted: Tue Nov 27, 2018 1:20 pm
by yaro2o
hi, I have a question, I create one code module that is used in every TC. In this code, I want to set value Text for an example field but so that the value of the text is variable for each tc different. I still learning that's why I'm asking for your understanding :)

Re: Use variables in code

Posted: Tue Nov 27, 2018 2:21 pm
by odklizec
Hi,

Where is bound the variable? If you want the content of variable to be different for each test case, then you must create a parameter or data connector in each test case and bind the variable to each parameter/data connector.

Re: Use variables in code

Posted: Tue Nov 27, 2018 2:39 pm
by yaro2o
Hi, i need bound _VAR1, and put them to setAtrributeValue("Text", bound variable)

Code: Select all

string _VAR1 = "Test";
[TestVariable("c8321a2b-24fa-47e6-8f68-2d236974215b")]
public string VAR1
{
	get { return _VAR1; }
	set { _VAR1 = value; }
}

        
        void ITestModule.Run()
        {
            repo.ZapisWydruku.ZapisywanieJako.NazwaRaportu.Element.SetAttributeValue("Text",  _VAR1);
        }

Re: Use variables in code

Posted: Tue Nov 27, 2018 3:03 pm
by odklizec
Hi,

At first, the name of variable to be used in code is VAR1, not _VAR1! So the code should look like this:
repo.ZapisWydruku.ZapisywanieJako.NazwaRaportu.Element.SetAttributeValue("Text", VAR1);
At next, you must bind the variable to the data connector or test case parameter of your choice. According to your screenshot, you already bound the variable to a global parameter or test case parameter?

For more details about data binding, I would suggest you to read these user guide chapters:
https://www.ranorex.com/help/latest/ran ... a-binding/
https://www.ranorex.com/help/latest/ran ... anagement/

Re: Use variables in code

Posted: Wed Nov 28, 2018 11:10 am
by yaro2o
OK I did it with that, the mistake was that I had a static method that did not see a variable, now it is public and works fine. But i heave another question is there a way to access the variable from the repository using the code. ?

Re: Use variables in code

Posted: Wed Nov 28, 2018 11:12 am
by odklizec
Hi,

Repository variables should be accessible via code. But maybe I misunderstood something? Could you please elaborate, what exactly do you want to do?

BTW, if you want to access the repo variable in code module, you must first instantiate the repository in code module, like this...

Code: Select all

private static projectName.repoNameRepository repo = projectName.repoNameRepository.Instance;
Then you should be able to use repo variable in code module like this:

Code: Select all

string varFromRepo = repo.variableName;

Re: Use variables in code

Posted: Wed Nov 28, 2018 11:25 am
by yaro2o
I heave drop down menu and 3 option to choose. And i want the program choose one of three options depending on what I type in variable in data binding.

I have a written code that I want to use in every TC and use a different databinding for each test
The only option is change accesiblename

Re: Use variables in code

Posted: Wed Nov 28, 2018 11:31 am
by yaro2o
TC1
CodeSeaveRap Databinding changing $variable attribute to test1
TC2
CodeSeaveRap Databinding changing $variable attribute to test2
TC3
CodeSeaveRap Databinding changing $variable attribute to test3

And now ranorex for each TC click anotcher option in drop down menu

Re: Use variables in code

Posted: Wed Nov 28, 2018 11:34 am
by odklizec
Hi,

In my opinion, again, unless I'm not missing something, there is no need to use code at all. You can create one data connector with 3 rows, then limit the data range to just one row per test case. For example, let's say you have a date connector like this:
optionName
option1
option2
option3
Now set all three test cases to use the same data connector, but in TC1, set the data range of above data connector to use just row 1, TC2=row 2 and TC3=row 3. Then simply bind the variable to data connector and use your code/recording to click the repo item with associated variable. That's all ;)

Re: Use variables in code

Posted: Wed Nov 28, 2018 2:22 pm
by yaro2o
I attach a snap, the question is how to replace for each TC raw text. If I add item FAK PREFIX/5/3/2018/SUF to recording I cant access to set value raw text only option is custom.

Re: Use variables in code

Posted: Wed Nov 28, 2018 2:34 pm
by odklizec
Hi,

You cannot use the SetValue action for RawText. It's expected behavior, because RawText is not an input-based element.

What exactly do you want to achieve with this cell? Just to find and click the cell, using the associated data connector? In this case, you should create a repo element with xpath like this...

Code: Select all

/form[@controlname='FrmMain']/?/?/form[@controlname='FrmKsiegNieruch']/element[@controlname='pgcNaglowek']/container[@controlname='tsSprzedaz']//container[@controlname='tsZatwierdzone']//rawtext[@rawtext=$elementName]
Where 'elementName' variable, should be associated with data connector of your choice, with data connector column called elementName, which contains text 'FAK PREFIX/5/3/2018/SUF'. If you properly bind the data connector with repo variable, Ranorex should be able to find the RAW text and do whatever you want it to do (e.g. click).

Re: Use variables in code

Posted: Wed Nov 28, 2018 3:00 pm
by yaro2o
Ok, I create repo element with this xpath, but i dont see $variable to choose in bind options, only if i add new module variable to my code i can see it in bind options

Re: Use variables in code

Posted: Wed Nov 28, 2018 3:06 pm
by odklizec
Repo variable is only available for binding, if the repo element (with repo variable) is used either in recording or code module. Simply drag & drop the repo element from repository to recording/code module and add an action you want it to perform over the repo element.

Re: Use variables in code

Posted: Wed Nov 28, 2018 3:16 pm
by yaro2o
you can give an example of what exactly you mean,I attach a movie as it looks to me

Re: Use variables in code

Posted: Wed Nov 28, 2018 3:23 pm
by yaro2o
If i add recording to TC in which I have added actions on this element I can do binding which dataconnector