modifying path from code

Ask general questions here.
skhanpara
Posts: 101
Joined: Wed Apr 23, 2014 3:55 pm

modifying path from code

Post by skhanpara » Thu Jun 26, 2014 2:37 pm

Is there any way i can retrieve path of specific item from code module

there is a text field which is generated everytime you click on Add Button and the Path for that item is
.//input[#'txtDOB1']
.//input[#'txtDOB2']
.//input[#'txtDOB3']
and so on

i have made click ad data driven and want to remove the number from the path
i.e .//input[#'txtDOB']

i want to add content to the path from code module based on the data.
if there any way i can retrieve Path of a specific item

Thanks
Skhanpra

krstcs
Posts: 2683
Joined: Tue Feb 07, 2012 4:14 pm
Location: Austin, Texas, USA

Re: modifying path from code

Post by krstcs » Thu Jun 26, 2014 2:55 pm

You can use a regular expression (regex) in your path. ".//input[@id~'txtDOB[0-9]+']" will find all of the elements, and will return the first one when used in a test. If you need to get a specific one, you can also add "[1]", etc., to the end. So for the 4th item it would look like ".//input[@id~'txtDOB[0-9]+'][4]".


NOTE: You will not be able to use the '#' (UniqueID) symbol with the path if you are manipulating the ID value in the path. The '#' symbol denotes that a value is unique and it will not work with variables, concatenation ('txtDOD' + var) or regular expressions. You will have to change to "@id=" or "@id~", etc.
Shortcuts usually aren't...

skhanpara
Posts: 101
Joined: Wed Apr 23, 2014 3:55 pm

Re: modifying path from code

Post by skhanpara » Thu Jun 26, 2014 2:58 pm

thanks for the reply

I found out that you can even create RXpath from code using Element

Can you please give a example of how to use it


Thanks

skhanpara
Posts: 101
Joined: Wed Apr 23, 2014 3:55 pm

Re: modifying path from code

Post by skhanpara » Thu Jun 26, 2014 3:04 pm

i used your path .//input[@id~'txtDOB[1-9]+']
but it click on first Text field everytime

Thank you

krstcs
Posts: 2683
Joined: Tue Feb 07, 2012 4:14 pm
Location: Austin, Texas, USA

Re: modifying path from code

Post by krstcs » Thu Jun 26, 2014 3:39 pm

In my first paragraph I stated that it would do that and how to work around it.

Ranorex always returns the FIRST item that matches the given path, so if you want an item other than the first, you will have to tell it which one you want. One way to do that is by adding the index to the end of the path.

For your example, if you want the second item use the path ".//input[@id~'txtDOB[0-9]+'][2]". Notice the "[2]" at the end.

Or, you can add a variable to the path and have the variable be set to the correct value:

Code: Select all

.//input[@id='txtDOB' + $ItemNumber]
Shortcuts usually aren't...

skhanpara
Posts: 101
Joined: Wed Apr 23, 2014 3:55 pm

Re: modifying path from code

Post by skhanpara » Thu Jun 26, 2014 3:50 pm

okay thanks

skhanpara
Posts: 101
Joined: Wed Apr 23, 2014 3:55 pm

Re: modifying path from code

Post by skhanpara » Thu Jun 26, 2014 3:54 pm

I am not able to add a variable after 'txtDOB'
i used your code but even that didnt work

skhanpara
Posts: 101
Joined: Wed Apr 23, 2014 3:55 pm

Re: modifying path from code

Post by skhanpara » Thu Jun 26, 2014 3:58 pm

i want to add variable after txtDOB in code module
not make it data driven

can we create or modify a ranorex path of a item from user code???

thanks

krstcs
Posts: 2683
Joined: Tue Feb 07, 2012 4:14 pm
Location: Austin, Texas, USA

Re: modifying path from code

Post by krstcs » Thu Jun 26, 2014 4:10 pm

Yes, you can do the same thing I said in my last post, but you just need to change the path to concatenate your variable instead of the module variable.

Code: Select all

string itemNumber = 1;
string path = @".//input[@id='txtDOB" + itemNumber + "']"

//or

string path2 = string.Format(@".//input[@id='txtDOB{0}']", itemNumber);
Then use that path to create the object you need.
Shortcuts usually aren't...

skhanpara
Posts: 101
Joined: Wed Apr 23, 2014 3:55 pm

Re: modifying path from code

Post by skhanpara » Thu Jun 26, 2014 9:13 pm

repo.Dropdown2.TxtDOB2 contains path .//input[@id='txtDOB1']

how do i edit it from code module ?

krstcs
Posts: 2683
Joined: Tue Feb 07, 2012 4:14 pm
Location: Austin, Texas, USA

Re: modifying path from code

Post by krstcs » Fri Jun 27, 2014 1:31 pm

I'm not sure I understand what you are trying to do.

If you already have the object in the repository, why not just add a repository variable to the path as in my first example, and just change that variable's value? This would be the best approach, even if you are not really data-driving the test.

The code I gave you in the last post will create the new path, you just need to use that path to create a new object in code.

I would advise against manipulating the path of an existing object for several reasons, but mostly because it wouldn't be saved in the repository after the run, so you wouldn't have an easy way of making sure it was doing what you wanted. This could cause all sorts of side-effects. It is much better to just create a new object with the path you want when manipulating paths in user-code.

I would suggest that you look at the Ranorex documentation (http://www.ranorex.com/Documentation/Ranorex/). It will show you the attributes of the repository objects that you can use.
Shortcuts usually aren't...

skhanpara
Posts: 101
Joined: Wed Apr 23, 2014 3:55 pm

Re: modifying path from code

Post by skhanpara » Fri Jun 27, 2014 4:37 pm

what i want to do is

I have form with add button and if that button is clicked 10 times there are 10 different path created with just the last character different
i.e dob1, dob2, dob3.......

based on which i need to click on each field and enter a value
if there are so i just want to get the path and modify the last character
i.e 1,2,3,4 .... and so on
if i assign a variable which it will only click on last one
it is data driven and if the value is 5, it should click on 1,2,3,4,5
i know how to create a path in code module and i all the condition but i am just stuck with creating item in the repository and assign path to it

if you can give me a example of how to add a new item to a repository and assign path to it.

thank you

skhanpara
Posts: 101
Joined: Wed Apr 23, 2014 3:55 pm

Re: modifying path from code

Post by skhanpara » Wed Jul 02, 2014 4:02 pm

Waiting for your reply

User avatar
Support Team
Site Admin
Site Admin
Posts: 12145
Joined: Fri Jul 07, 2006 4:30 pm
Location: Houston, Texas, USA
Contact:

Re: modifying path from code

Post by Support Team » Thu Jul 03, 2014 1:02 pm

Hello skhanpara,

Unfortunately, Ranorex does not support repository item creation within user code. I would suggest creating a new object with the new RxPath as shown below:
Ranorex.Button btn = repo.Form.Self.FindSingle("./button[@controlname='<YourValue>']");
Using this code snippet you should be able to implement the desired behavior.

Regards,
Robert

skhanpara
Posts: 101
Joined: Wed Apr 23, 2014 3:55 pm

Re: modifying path from code

Post by skhanpara » Thu Jul 10, 2014 1:21 pm

Thanks It works fine