How to traverse through a DOM object with C#?

Ranorex Spy, Recorder, and Studio.
Sae1962
Posts: 15
Joined: Tue Feb 06, 2018 4:25 pm

How to traverse through a DOM object with C#?

Post by Sae1962 » Mon Apr 30, 2018 1:07 pm

I have a Web page where I have to traverse the DOM to find out all elements that have a class that contains "accordion-toggle ". Each such object gets a similar class assigned, when the accordion widget is opened/closed (see attached image).

To make an automated black-box test, where all accordions are clicked & their behaviour checked, I need a DOM-like object that contains all accordions in a page so that the automated testware can click through this objects & check whether they behave properly. How can I extract this information? I am trying for two days now to see the rather very hierarchical structure of the page and wonder, if there is an easy way to extract this information.
Attachments
20180430 IT Services & Solutions.rxsnp
The snapshot of one of the test pages
(183.55 KiB) Downloaded 10 times
20180430 accordion-toggles.PNG
An accordion toggle closed/opened
20180430 accordion-toggles.PNG (44.5 KiB) Viewed 359 times

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

Re: How to traverse through a DOM object with C#?

Post by odklizec » Mon Apr 30, 2018 1:24 pm

Hi,

Basically, you need to search for the appearance of given attribute, using this xpath (starting from DOM):

Code: Select all

//*[@class~'accordion-toggle']
If you want to return only visible elements, you need to use this xpath:

Code: Select all

//*[@class~'accordion-toggle'][@visible='true']
Sample code:

Code: Select all

IList<Ranorex.Unknown> elementList = repo.DOMElement.Self.Find<Ranorex.Unknown>("//*[@class~'accordion-toggle'][@visible='true']",5000);  
foreach (Ranorex.Unknown elementItem in elementList)  
{  
     //do whatever you want with found elementItem
}
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

Sae1962
Posts: 15
Joined: Tue Feb 06, 2018 4:25 pm

Re: How to traverse through a DOM object with C#?

Post by Sae1962 » Mon Apr 30, 2018 2:21 pm

Thank you for your swift reply, Pavel! I started with the topmost element in repo that I use instead of your dummy called DOMElement. Unfortunately, I get an unjustified System.OutOfMemoryException (see attachements). Perhaps, a better choice for the "DOMElement" would do better. Here is the code:

Code: Select all

		/// <summary>
		/// Finds the accordion widgets
		/// </summary>
		public void FindAccordionWidgets()
		{
			Report.Log(ReportLevel.Info, "Code", "FindAccordionWidgets() started…");
			IList<Ranorex.Unknown> elementList = repo.Home.Self.Find<Ranorex.Unknown>("//*[@class~'accordion-toggle'][@visible='true']");
			Report.Log(ReportLevel.Info, "Code", "elementList populated.");
			
			foreach (Ranorex.Unknown elementItem in elementList)
			{
				Report.Log(ReportLevel.Info, "Code", "elementItem " + elementItem.FlavorName + isOfType + elementItem.GetType());
			}
		}
The first report log appears, the second with "elementList populated." not.
Attachments
20180430 OutOfMemoryException (2).png
OutOfMemoryException
20180430 OutOfMemoryException (2).png (180.03 KiB) Viewed 342 times
20180430 Exception 1 (2).png
The first exception before out of memory
20180430 Exception 1 (2).png (143.39 KiB) Viewed 348 times

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

Re: How to traverse through a DOM object with C#?

Post by odklizec » Mon Apr 30, 2018 2:47 pm

Well, if you start somewhere else than in DOM, it may not find all appearances of given element?

Anyway, the error you got looks like an internal Ranorex error, which may or may not cause problems ;) Does the code return something? What Ranorex version do you use and what web browser (I guess FF)? There are some problems with Ranorex plugin for FF, which throws errors like you got. We discussed these FF plugin-related errors here:
https://www.ranorex.com/forum/lineagegl ... 12017.html
However, I saw similar errors (just much less frequently, in IE and Chrome.

PS: sorry, my mistake...I overlooked the "out of memory exception" screenshot ;) Anyway, I think Alex answered your question perfectly. I'm always forgetting about the "dot" ;)
Last edited by odklizec on Mon Apr 30, 2018 3:11 pm, edited 1 time in total.
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

Sae1962
Posts: 15
Joined: Tue Feb 06, 2018 4:25 pm

Re: How to traverse through a DOM object with C#?

Post by Sae1962 » Mon Apr 30, 2018 2:50 pm

No, I use Google Chrome v 66.0.3359.139 (official build) (64 bit) for the current test. I thought first that using something down the ladder from repo.Home may not cause an exception, but the code should run on any page. So, the repo.Home will exist for sure. Yes, I got the System.OutOfMemoryException several times before, although there seemed to be no memory problem.

ahoisl
Certified Professional
Certified Professional
Posts: 125
Joined: Fri Sep 07, 2007 8:16 am

Re: How to traverse through a DOM object with C#?

Post by ahoisl » Mon Apr 30, 2018 2:55 pm

Sae1962 wrote: IList<Ranorex.Unknown> elementList = repo.Home.Self.Find<Ranorex.Unknown>("//*[@class~'accordion-toggle'][@visible='true']");
Regarding the memory problem: This line will search every application, because you did not precede the path with a "." to signal a relative path. So it will search everything from the root, which may be slow.
An OutOfMemoryException will be thrown if the process cannot get any more memory. If you compile to 32bit, then this will be at about 2GB of RAM usage. Only if you compile to AnyCPU you will be able to use more memory, but still I would have a look first what really takes up so much memory...

Regards,
Alex
Ranorex Team

Sae1962
Posts: 15
Joined: Tue Feb 06, 2018 4:25 pm

Re: How to traverse through a DOM object with C#?

Post by Sae1962 » Mon Apr 30, 2018 3:07 pm

The dot changed the problem immediately! Now, I find only one element, but there are several accordion-toggle elements on the test page. Currently, seven (7) are visible. And the one element is not an accordion widget.
Attachments
IVMWebsiteTest_20180430_160448.zip
Report file
(393.14 KiB) Downloaded 10 times
20180430 One-element IList.png
A one-element IList is now found
20180430 One-element IList.png (206.2 KiB) Viewed 334 times

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

Re: How to traverse through a DOM object with C#?

Post by odklizec » Tue May 01, 2018 7:50 am

Hi,

In the initial snapshot you posted, there is just one visible element with attribute accordion-toggle. So you either need to post another snapshot, where we can evaluate the rest of elements, which you think should be returned by the advised xpath. Or the simple answer is, that the class attribute of the rest of elements simply does not contain "accordion-toggle" string and so you must identify them with something else common to all elements in question?
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