Page 1 of 1

Dynamic coding using C# - Find children

Posted: Thu Feb 12, 2015 4:20 am
by vivek
Hi Team

I need to select the check boxes and radio buttons in my screen. Please refer the below code snippet.It works fine and I am selecting the checkboxes 1 after the other and also I am hardcoding in to less then or equal to 3

Code snippet 1:
******************

DivTag DTtag = webdoc.FindSingle("/dom[@domain>'cvgu' and @browser~'.*Guidewire.*']//div[@id~'.*QuestionSetLV-body']");
for(int i=1;i<=3;i++)
{
ImgTag img = DTtag.FindSingle("/dom[@domain>'cvgu' and @browser~'.*Guidewire.*']//tr[@id~'gridview*']//img[@src~'data:image*' and @class='x-grid-checkcolumn' and @tagname='img']");
img.Click();
}

Here, I have 2 questions.

1) I have tried the same by using "FindChildrens", however i not successful. It's always gives the count as 0. Attached code snippet 2 for your reference.
2) Is there any way to select all the checkboxes at 1 shot?

Please advise

Code snippet 2:
*****************
DivTag DTtag = webdoc.FindSingle("/dom[@domain>'cvgu' and @browser~'.*Guidewire.*']//div[@id~'.*QuestionSetLV-body']");
ImgTag img = DTtag.FindSingle("/dom[@domain>'cvgu' and @browser~'.*Guidewire.*']//tr[@id~'gridview*']//img[@src~'data:image*' and @class='x-grid-checkcolumn' and @tagname='img']");
IList<ImgTag> ImgChild = img.FindChildren<ImgTag>("/dom[@domain>'cvgu' and @browser~'.*Guidewire.*']//tr[@id~'gridview*']//img[@src~'data:image*' and @class='x-grid-checkcolumn' and @tagname='img']");
foreach (ImgTag obj in ImgChild)
{
obj.Click();
}

Re: Dynamic coding using C# - Find children

Posted: Thu Feb 12, 2015 1:37 pm
by Support Team
vivek wrote:I have tried the same by using "FindChildrens", however i not successful
Attention, the FindChildren(string defaultLabel) does not take a RanoreXPath as argument, but a label. As this already caused confusion, the method is marked as obsolete (you get a warning if using it) and will be deleted in a future version of Ranorex.

IMHO what you are searching for is the Find(RxPath) method which returns you all elements satisfying the passed RanoreXPath.
vivek wrote:Is there any way to select all the checkboxes at 1 shot?
Just use the Find instead of the FindChildren method to retrieve all ImgTag elements and loop through the returned elements.

Regards,
Alex
Ranorex Team

Re: Dynamic coding using C# - Find children

Posted: Fri Feb 13, 2015 4:08 am
by vivek
Hi Alex

Thanks for your help. I have coded using "Find" and it works.

Please refer the code snippet. It works this way. Here I am finding my object once again inside the loop and click it

Code snippet Worked

ImgTag img = webdoc.FindSingle("/dom[@domain>'cvgu' and @browser~'.*Guidewire.*']//tr[@id~'gridview*']//img[@src~'image*' and @class='x-grid-checkcolumn' and @tagname~'img']");
IList<ImgTag> ImgChild = img.Find<ImgTag>("/dom[@domain>'cvgu' and @browser~'.*Guidewire.*']//tr[@id~'gridview*']//img[@src~'image*' and @class='x-grid-checkcolumn' and @tagname~'img']"
foreach (ImgTag obj in ImgChild)
{
ImgTag imgclk = webdoc.FindSingle("/dom[@domain>'cvgu' and @browser~'.*Guidewire.*']//tr[@id~'gridview*']//img[@src~'image*' and @class='x-grid-checkcolumn' and @tagname~'img']");
imgclk.Click();
}

On the flip side, See below. I am referencing the for each loop object and clicked it. I am not successful here, It find the first image object but it't not able to find the other object.

I am getting error message as "Could not get a valid element rectangle image". I have checked in spy and it can able to recognize all the images.

Any help will be appreciated

Code snippet - having issues
********************************

ImgTag img = webdoc.FindSingle("/dom[@domain>'cvgu' and @browser~'.*Guidewire.*']//tr[@id~'gridview*']//img[@src~'image*' and @class='x-grid-checkcolumn' and @tagname~'img']");
IList<ImgTag> ImgChild = img.Find<ImgTag>("/dom[@domain>'cvgu' and @browser~'.*Guidewire.*']//tr[@id~'gridview*']//img[@src~'image*' and @class='x-grid-checkcolumn' and @tagname~'img']"
foreach (ImgTag obj in ImgChild)
{
obj.Click();
}

Re: Dynamic coding using C# - Find children

Posted: Mon Feb 16, 2015 12:13 pm
by Support Team
Hi vivek,

It is most probably a problem with caching. You are searching for all img elements and save this elements in your list, and it seems that your web page is reloaded during the click actions Ranorex performs. This is why Ranorex can no longer get a valid rectangle, since the img element saved does no longer exist, a new one was created instead.
May I ask you to post a Ranorex snapshot file of your img elements here? This will help me to to help you writing a suitable method.
The following link will show you how to generate a snapshot file: Creating Ranorex Snapshot Files.

Why Ranorex always clicks on the first element in the first method is because you are always searching for the same element, with the same RxPath. When there are more elements with the same RxPath Ranorex will just return the first one.

Regards,
Markus