Use variables in code

Best practices, code snippets for common functionality, examples, and guidelines.
yaro2o
Posts: 67
Joined: Mon Jan 29, 2018 11:19 am

Use variables in code

Post by yaro2o » Tue Nov 27, 2018 1:20 pm

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 :)
Attachments
2018-11-27_13-14-39.png
2018-11-27_13-14-39.png (175.93 KiB) Viewed 78 times

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

Re: Use variables in code

Post by odklizec » Tue Nov 27, 2018 2:21 pm

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.
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

yaro2o
Posts: 67
Joined: Mon Jan 29, 2018 11:19 am

Re: Use variables in code

Post by yaro2o » Tue Nov 27, 2018 2:39 pm

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);
        }

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

Re: Use variables in code

Post by odklizec » Tue Nov 27, 2018 3:03 pm

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/
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

yaro2o
Posts: 67
Joined: Mon Jan 29, 2018 11:19 am

Re: Use variables in code

Post by yaro2o » Wed Nov 28, 2018 11:10 am

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. ?

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

Re: Use variables in code

Post by odklizec » Wed Nov 28, 2018 11:12 am

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;
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

yaro2o
Posts: 67
Joined: Mon Jan 29, 2018 11:19 am

Re: Use variables in code

Post by yaro2o » Wed Nov 28, 2018 11:25 am

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

yaro2o
Posts: 67
Joined: Mon Jan 29, 2018 11:19 am

Re: Use variables in code

Post by yaro2o » Wed Nov 28, 2018 11:31 am

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

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

Re: Use variables in code

Post by odklizec » Wed Nov 28, 2018 11:34 am

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 ;)
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

yaro2o
Posts: 67
Joined: Mon Jan 29, 2018 11:19 am

Re: Use variables in code

Post by yaro2o » Wed Nov 28, 2018 2:22 pm

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.
Attachments
Snap1.rxsnp
(151.85 KiB) Downloaded 2 times

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

Re: Use variables in code

Post by odklizec » Wed Nov 28, 2018 2:34 pm

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).
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

yaro2o
Posts: 67
Joined: Mon Jan 29, 2018 11:19 am

Re: Use variables in code

Post by yaro2o » Wed Nov 28, 2018 3:00 pm

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

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

Re: Use variables in code

Post by odklizec » Wed Nov 28, 2018 3:06 pm

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.
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

yaro2o
Posts: 67
Joined: Mon Jan 29, 2018 11:19 am

Re: Use variables in code

Post by yaro2o » Wed Nov 28, 2018 3:16 pm

you can give an example of what exactly you mean,I attach a movie as it looks to me
Attachments
2018-11-28_15-11-50.gif
2018-11-28_15-11-50.gif (949.96 KiB) Viewed 43 times

yaro2o
Posts: 67
Joined: Mon Jan 29, 2018 11:19 am

Re: Use variables in code

Post by yaro2o » Wed Nov 28, 2018 3:23 pm

If i add recording to TC in which I have added actions on this element I can do binding which dataconnector