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.
How to traverse through a DOM object with C#?
How to traverse through a DOM object with C#?
You do not have the required permissions to view the files attached to this post.
Re: How to traverse through a DOM object with C#?
Hi,
Basically, you need to search for the appearance of given attribute, using this xpath (starting from DOM):
If you want to return only visible elements, you need to use this xpath:
Sample code:
Basically, you need to search for the appearance of given attribute, using this xpath (starting from DOM):
Code: Select all
//*[@class~'accordion-toggle']
Code: Select all
//*[@class~'accordion-toggle'][@visible='true']
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 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
Re: How to traverse through a DOM object with C#?
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:
The first report log appears, the second with "elementList populated." not.
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());
}
}
You do not have the required permissions to view the files attached to this post.
Re: How to traverse through a DOM object with C#?
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"
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 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
Re: How to traverse through a DOM object with C#?
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.
Re: How to traverse through a DOM object with C#?
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.Sae1962 wrote: IList<Ranorex.Unknown> elementList = repo.Home.Self.Find<Ranorex.Unknown>("//*[@class~'accordion-toggle'][@visible='true']");
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
Re: How to traverse through a DOM object with C#?
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.
You do not have the required permissions to view the files attached to this post.
Re: How to traverse through a DOM object with C#?
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?
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 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