reference to repository elements dinamically

Best practices, code snippets for common functionality, examples, and guidelines.
BJosef
Posts: 4
Joined: Tue Jun 07, 2016 2:23 pm

reference to repository elements dinamically

Post by BJosef » Mon Oct 31, 2016 5:06 pm

Hi,
I have the following question.

I'd like to refer to some repository elements dinamically.

For example, I have a button.

Let's say it is a delete button.

It's name is constructed  like this:
suppose I want to delete an element called "Joe"
In this case the delete button is called in Spy:

Joe.DeleteButton

In case of Bill, it is called

Bill.DeleteButton

If I want to implement a click on it I'd like to use a statement like this:

repo.Joe.DeleteButton.Click();
repo.Bill.DeleteButton.Click();

But the name is determined during runtime.

Can I set , construct this repo....  statement dinamically or is there a way torefer dinamically (during runtime)?
Last edited by BJosef on Wed Nov 02, 2016 4:07 pm, edited 1 time in total.

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

Re: reference to repository elements dinamically

Post by odklizec » Mon Oct 31, 2016 5:50 pm

Hi,

Could you please post a Ranorex snapshot of the element(s) in question? Or at least the xpath for both elements? Basically, it does not make much sense to make the repo structure dynamical, nor I'm aware of any possibility to do so. What you are looking for is making the xpaths dynamical. Let's say there is a "name" attribute for each button, which contains value like Joe.DeleteButton or Bill.DeleteButton.
So all you need is to create a repo item "DeleteButton", with the end of xpath like this:

Code: Select all

//button[@name~'\.DeleteButton']
This xpath should match both delete buttons. Exact xpath depends of the available attributes and your GUI structure. This is why I've asked for Ranorex snapshot.

In case both (or more) delete buttons are visible in GUI at the same time, you will most probably have to handle them via some kind of coded loop and a repository variable, which should be filled with exact name (Joe, Bill, etc.) during each loop iteration. But it's just my wild guess :) Without the GUI snapshot, it's really hard to suggest something reliable. So please, post the snapshot (not screenshot!). Thanks.
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

BJosef
Posts: 4
Joined: Tue Jun 07, 2016 2:23 pm

Re: reference to repository elements dinamically

Post by BJosef » Wed Nov 02, 2016 4:12 pm

Hi,

the xpath looks like these


/form[@title='Delete: Joe']//button[@text='&Finish']

/form[@title='Delete: Bill']//button[@text='&Finish']

/form[@title='Delete: Tom']//button[@text='&Finish']


And in my Ranorex user code module I'd like to refer to one of these.
Joe, Bill, Tom are nodes of a tree - and if I click to Joe then Delete button of Joe should activated
If I click to Bill then Delete button of Bill should activated... etc

Something like this:

repo.Joe_DeleteButton.Click();

But instead of Joe_DeleteButton a dinamically usable button should be activated.

"Are you sure you want to delete the node Joe?"

Something like this..

tvu
Posts: 195
Joined: Tue Apr 07, 2015 10:47 pm

Re: reference to repository elements dinamically

Post by tvu » Wed Nov 02, 2016 6:31 pm

You can add a variable into the Xpath that represents the name:
/form[@title='Delete: ' + $nameOfPerson + '']//button[@text='&Finish']
or
/form[@title~$nameOfPerson]//button[@text='&Finish']

Then in your code, you would do something like this:
repo.nameOfPerson = "Joe";
repo.DeleteButton.Click();

If you are using a recording then adding the repo item will automatically add $nameOfPerson as a recording parameter. Then you don't have to do repo.nameOfPerson = "Joe" in your code.

BJosef
Posts: 4
Joined: Tue Jun 07, 2016 2:23 pm

Re: reference to repository elements dinamically

Post by BJosef » Wed Nov 02, 2016 7:19 pm

Hi,

tvu , yes I I need such an implementation.

I use C# , not recording - I will use something similar you wrote in your reply.

Thanks for the idea :-)