Identify elements not in repository

Best practices, code snippets for common functionality, examples, and guidelines.
MikeMitchell
Posts: 3
Joined: Thu Oct 19, 2017 7:40 pm

Identify elements not in repository

Post by MikeMitchell » Thu Oct 19, 2017 8:12 pm

Longtime UFT developer and new to Ranorex.

I've recorded of a small script that will add a segment, click a link and fill out some drop downs on a web page. In the repository are the objects captured from that recording. However, I intend the script to be much larger as I will add segments and click similar links, etc...essentially repeating the pre-recorded script but clicking on links and boxes that are created on the fly at different points on the page. These newer objects differ slightly by their ID.

I would rather not record the entire script and add all objects to the repository one by one. Instead I would like to perform all new object identification and interaction dynamically via C# code. I know what the names of the IDs are going to be. However, I'm having trouble identifying these controls because I'm unable to construct the appropriate code to accomplish this. I've searched the forums and Google extensively and cannot seem to capture the correct syntax for this task.

Attached is the snapshot of the dom. The controls in question have the ID of:

ctl00_phMain_lvSegments_ctrl0_fieldUI_lvField_ctrl0_btnEdit <-- this is the only one in the repository, the following will be new:
ctl00_phMain_lvSegments_ctrl1_fieldUI_lvField_ctrl0_btnEdit
ctl00_phMain_lvSegments_ctrl2_fieldUI_lvField_ctrl0_btnEdit

I'm familiar with GetElementById() and am looking for similar functions or snippets to identify elements.

Thanks.

Ranorex 6.1.0
Win7 SP1
You do not have the required permissions to view the files attached to this post.

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

Re: Identify elements not in repository

Post by krstcs » Thu Oct 19, 2017 9:44 pm

The easiest way to do what you want is to start with the repository object ("repo"), get the dom object and then do a "Find<T>(RxPath pathFromDom)".

Code: Select all

ButtonTag myButton = repo.MyDom.Self.Find<ButtonTag>("//buttontag[@id='MyButtonId']")[0];  //you can use "//buttontag[#'MyButtonId']" if the id is unique
Note that Ranorex uses their own special version of XPath, which is very powerful, but you must always use it, unlike Selenium where you can just identify by id. Ranorex does that, but you have to do it through the RxPath like above.
Shortcuts usually aren't...

Vaughan.Douglas
Posts: 254
Joined: Tue Mar 24, 2015 5:05 pm
Location: Des Moines, Iowa, USA

Re: Identify elements not in repository

Post by Vaughan.Douglas » Fri Oct 20, 2017 12:48 pm

I want to reiterate krstcs, using an object repository or similar is simply best practice for any automation effort. It allows you to keep your AUT objects organized (although the Ranorex implementation could be improved upon).

I agree that recording each step is not optimal, and in your situation you shouldn't need to do it. In your object repository you can just create a new objects, give it a name, and toss in the RxPath.

Using a hybrid method, such as you're asking about, can be troublesome when it comes to maintenance. Now you have some objects in the repository and others buried in code. The object repository adds a layer of abstraction that makes changes in the AUT easier to manage. Conversely you can go without the object repository completely, but I wouldn't recommend that route as it just ignores a powerful Ranorex feature. At that point you may as well just use selenium (NTTAWWT).
Doug Vaughan

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

Re: Identify elements not in repository

Post by krstcs » Fri Oct 20, 2017 2:18 pm

Yeah, I have to agree with Doug, you should really consider using the object repository and Ranorex framework as it is. It may seem to make the initial work take longer, but in the long run it will make maintenance much easier. My original post was meant to point you in the direction of what you specifically asked about. :D
Shortcuts usually aren't...

MikeMitchell
Posts: 3
Joined: Thu Oct 19, 2017 7:40 pm

Re: Identify elements not in repository

Post by MikeMitchell » Mon Oct 23, 2017 12:24 pm

Thanks to both krstcs and Doug - your explanations make total sense. I appreciate the detail and justification. I'll give it a try and post back.

MikeMitchell
Posts: 3
Joined: Thu Oct 19, 2017 7:40 pm

Re: Identify elements not in repository

Post by MikeMitchell » Fri Nov 03, 2017 6:35 pm

Just wanted to reply back that the code example above while it worked, it was awfully slow to execute. It allowed me to code without adding a ton of objects to the repository. Eventually I gave in and added everything to the repository and determined that it wasn't half bad. Compared to other tools it's quite pain-free to add and manage. And having the ability to search on an object and be told whether it's being used is a plus to keeping the repo clean. Thanks to all for your help.

Vaughan.Douglas
Posts: 254
Joined: Tue Mar 24, 2015 5:05 pm
Location: Des Moines, Iowa, USA

Re: Identify elements not in repository

Post by Vaughan.Douglas » Fri Nov 10, 2017 12:59 pm

MikeMitchell wrote:Just wanted to reply back that the code example above while it worked, it was awfully slow to execute. It allowed me to code without adding a ton of objects to the repository. Eventually I gave in and added everything to the repository and determined that it wasn't half bad. Compared to other tools it's quite pain-free to add and manage. And having the ability to search on an object and be told whether it's being used is a plus to keeping the repo clean. Thanks to all for your help.
I'm glad you've found something that works! After several years of bending things to my will, I've learned that one should always take advantage of a products builtin utility before trying to customize everything. Even with Ranorex I had to learn the hard way. My first solution was all custom code. Sure it worked, but no one else could maintain it and their eyes would gloss over whenever I try to explain anything. Eventually I went back and put everything into recorded modules and handed it off.

I still customize my RxPath though. The object recorder produces less than elegant results.
Doug Vaughan