count of elements from RXPath

Class library usage, coding and language questions.
pvikramsharma
Posts: 6
Joined: Wed Aug 27, 2014 10:43 am

count of elements from RXPath

Post by pvikramsharma » Wed Aug 27, 2014 10:57 am

Hi,

My RX path returns me 2 elements using Ranorex Spy and I want to get the same count in user code by writing the code. How do I get the count using C#

Ranorex path is /dom[@domain='bigbasket.com']//div[#'uiv2-slideshow']/ul/li.

Let me know if the question is not clear.

THanks,
Vikram

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

Re: count of elements from RXPath

Post by krstcs » Wed Aug 27, 2014 2:41 pm

Code: Select all

UlTag ul = "/dom[@domain='bigbasket.com']//div[#'uiv2-slideshow']/ul";

List<LiTag> liList = ul.Find<LiTag>("li");

int count = liList.Count;
Shortcuts usually aren't...

pvikramsharma
Posts: 6
Joined: Wed Aug 27, 2014 10:43 am

Re: count of elements from RXPath

Post by pvikramsharma » Wed Aug 27, 2014 3:02 pm

Ranorex Guru, thanks a lot for the solution and it worked as expected but had to change to IList instead of List as it was showing me some compiler error.

I am new to this tool and also for the coding and hence trying to figure out so many things.

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

Re: count of elements from RXPath

Post by krstcs » Wed Aug 27, 2014 3:42 pm

AH :D

Yeah, I mistyped it, was supposed to be an iList<T>. Sorry about that!



Welcome to Ranorex!

A couple of suggestions for you:


1. You are developing software so, as with any software development process, use a code version control system, like SVN or Git (what I use). Make branches to work in and stick with it.


2. As with 1, you are developing software, so keep it simple. Too much complexity causes issues and can make it harder for others to know what is going on. Ultimately you want to make it so that anyone can create tests.


3. Keep the Ranorex Recording Modules as small as possible. For example, let's say you need to click on an OK button in your AUT. Make a module called something like "Click_OKButton" or "OK_Click". Then, ONLY do that one click action in the module. This will allow you to reuse modules in your test cases. And the test cases and suites just become drag-and-drop test flow structures. Modules don't control flow, they just take actions. As another example, for entering data, say a UserName, make a module called "Enter_UserName" and have it only do the actions necessary to enter the username in the username field, and validate that it is there. It would look like this in the recording module's action table ():

Code: Select all

1. Mouse -> Click -> Left -> Center -> UserName  (This is what a user would do to get focus onto the field)
2. Key Shortcut -> "Ctrl-A" -> UserName  (Selects all text so we can overwrite)
3. Key Shortcut -> "Del" -> UserName (Optional, but makes sure we don't have other text there)
4. Key Sequence -> $UserName -> UserName ($UserName is the module variable)
5. Validate -> AttributeEqual -> Text (or Value, etc.) -> $UserName -> UserName
Given a module variable called "UserName" (see where I'm going??), the field and variable are the same so we know what we are putting in and where.


4. Learn as much as you can about how RanoreXPath works and use it, along with the repository. It will make updating tests much easier. And RXPath will do wonders in many situations where you might otherwise have to use code modules. Think outside the box.


If you have any questions, ask. There are lots of great Ranorex users and support personnel on this forum.
Shortcuts usually aren't...

pvikramsharma
Posts: 6
Joined: Wed Aug 27, 2014 10:43 am

Re: count of elements from RXPath

Post by pvikramsharma » Thu Aug 28, 2014 5:04 am

Thanks a lot Guru, will keep all the points in mind mentioned in your previous posts.Will definitely help me lot.

Any suggestion from your experience on how I can learn more on using RXPath in code in various scenarios.Example How did you analyse and get the above solution. Is there any document with regard to this.

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

Re: count of elements from RXPath

Post by krstcs » Thu Aug 28, 2014 2:31 pm

Well, mostly a lot of reading and trial and error.

I was a .NET developer before I started working with Ranorex, so much of the underlying principles I already understood. This is another thing that you might want to start getting familiar with. Pick up an intro .NET (especially C#) book, like "C# for Dummies". The more you understand about the underlying framework of .NET, the more you will understand Ranorex. I would suggest using C# instead of VB for several reasons (some personal, I don't care for VB in any incarnation), but mainly because it is similar to Java and C/C++ in syntax and structure, where VB is not like anything else that is widely used. Also, more developers use C# than VB, not suggesting that just because more people use it that it is better, but that you will probably have more people that can help with creating and editing code.



As for RXPath, the biggest thing you need to understand is that it is essentially a directory structure of the GUI elements of the application under test. Also, Ranorex will ALWAYS (ALWAYS!!) stop searching when it finds the FIRST element (or elements if you are doing a FIND, where it gets all that match) that matches the path given, so make sure that you have the path correct. Read the documentation, "cover-to-cover", here: http://www.ranorex.com/support/user-guide-20.html. Also, the Ranorex API is very useful for understanding the different objects that you have available to you: http://www.ranorex.com/Documentation/Ranorex/. You don't need to read it all, just use it as a reference (this is where knowing .NET will come in handy).

Use Ranorex Spy A LOT. Use Ranorex Spy A LOT. Use Ranorex Spy A LOT! ( :D Hint, hint!) It will help you visualize the structure, see attributes and figure out different paths to use. If you get stuck on a path, use Spy. If you can't figure out which attribute to use, use Spy. You get the point. I find it to be the most important and under-appreciated tool in the set. Use Ranorex Spy A LOT. :D

In addition to learning XPath, you will want to get very familiar with Regex (Regular Expressions). I recommend this site: http://www.regular-expressions.info/. It has tons of information about how Regex works and what all of the different expressions and syntax do. One thing to note though is that Ranorex, due to being built on .NET, uses the .NET regex syntax, while most places on the web use JavaScript regex syntax. The site above usually does a good job of differentiating between the different forms when necessary.

Finally, for XPath, you need to understand what attributes are and how they help identify the elements. Take a simple HTML tag like this: <div id="myDiv">Some Text</div>. If you want to get this element every time, regardless of the InnerText (the "Some Text" part - it's inside the tag and it's text, so InnerText), then you would use the path "//div[@id='myDiv']" or "//div[#'myDiv'] ("#" means "Unique ID" and can only be used when searching for an id attribute, and the id's value must be unique on the page). If you have an OK button for example, you could use the id, or the text (since it probably won't change), like this: "//button[@text='OK']". This will find the FIRST button under this object's Parent (in the repo) that has the text of "OK". The nice thing is that you can many times make things like OK buttons generic enough that you can create just one repo object and one module and it will work for every OK button in your application. The only problem is when you have more than one OK button on the same page, and both are visible and enabled.


The best way to learn is to create a fake test suite and just start doing stuff.
Shortcuts usually aren't...

pvikramsharma
Posts: 6
Joined: Wed Aug 27, 2014 10:43 am

Re: count of elements from RXPath

Post by pvikramsharma » Thu Aug 28, 2014 3:00 pm

Wonderful will go through the mentioned sites. Now I am trying to get hang on the C# and then may be like you said it should be easy for me to understand Ranorex well.

I kind of find it difficult to use the IList, UITag and the methods, did not find any good document which explains me well, guess I have to do trial n error to understand it well.