Page 1 of 1

Getting Index was out of range error even when right range used

Posted: Thu Apr 25, 2019 11:49 am
by sureshkondapally
Hi,

I am getting Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: indexerror when executing my code.
I have a 3 div tags and an anchor tag somewhere inside each div. my requirement here is to click on that A tag. I have written below code.
var objEle = Object_Repository.Portfolios.Instance.XYz.Portfolios.test;
DivTag dt = (DivTag)objEle;

IList<DivTag> PCards = dt.Find<DivTag>("//div[@class='portfolios__portfolio-card']"); //getting 3 Devs here without any issue.
for(int i=0;i<PCards.Count;i++)
{
Report.Info("I = ",i.ToString());
PCards.FindDescendants<ATag>()[0].PerformClick();
// Report.Info("Divs",PCards.FindDescendants<ATag>()[0].InnerText.ToString());
Delay.Seconds(5);
repo.XYz.Menu_Items.Portfolios.Click();//I am getting back to main menu to select another A(link)
}

For 2nd iteration its throwing that error. So initially I thought its i value but Im getting the same error if I get my code out of the loop also, as below.
//Code without loop but getting same error when reached to next click.
PCards[0].FindDescendants<ATag>()[0].PerformClick();

Delay.Seconds(5);
repo.XYz.Menu_Items.Portfolios.Click();
PCards[1].FindDescendants<ATag>()[0].PerformClick(); // getting the same error here.

Delay.Seconds(5);
repo.XYz.Menu_Items.Portfolios.Click();
PCards[2].FindDescendants<ATag>()[0].PerformClick();

Delay.Seconds(5);
repo.XYz.Menu_Items.Portfolios.Click();

AND THE CODE IS WORKING VERY FINE if I am clicking only one for the entire test execution i.e. PCards[1].FindDescendants<ATag>()[0].PerformClick(); and PCards[2].FindDescendants<ATag>()[0].PerformClick(); are working fine If I execute a single statement for whole test run, but if Im continuing one by one in single execution Im getting that error.

Any help will be appreciated. Thanks in advance.

Re: Getting Index was out of range error even when right range used

Posted: Thu Apr 25, 2019 12:06 pm
by odklizec
Hi,

Could you please post a Ranorex snapshot (NOT screenshot), which would show us the structure of UI you are working with?

At next, I see a problem in bolded part of the code:
...
IList<DivTag> PCards = dt.Find<DivTag>("//div[@class='portfolios__portfolio-card']"); //getting 3 Devs here
...
PCards.FindDescendants<ATag>()[0].PerformClick();
Since the PCards is a list, FindDescendants may not return expected elements? But it's hard to say without seeing the snapshot. I would suggest to try this code instead:

Code: Select all

			var objEle = Object_Repository.Portfolios.Instance.XYz.Portfolios.test;
			DivTag dt = (DivTag)objEle;
			
			IList<DivTag> PCards = dt.Find<DivTag>("//div[@class='portfolios__portfolio-card']"); //getting 3 Devs here without any issue.
			foreach (DivTag pCard in PCards) 
			{
				pCard.FindDescendans<ATag>()[0].PerformClick();
				Delay.Seconds(5);
				repo.XYz.Menu_Items.Portfolios.Click();//I am getting back to main menu to select another A(link)
			}
Hope this helps?

Re: Getting Index was out of range error even when right range used

Posted: Thu Apr 25, 2019 3:06 pm
by sureshkondapally
Hi,

Thanks for your response. I am getting below error when used your code.
'Ranorex.DivTag' does not contain a definition for 'FindDescendans' and no extension method 'FindDescendans' accepting a first argument of type 'Ranorex.DivTag' could be found (are you missing a using directive or an assembly reference?) (CS1061) -

I am attaching screenshot of the elements hierarchy here as I am afraid to share snapshot as it is revealing some project details. Please see if that is helpful to provide some solution to me. Thanks again.

Suresh.

Re: Getting Index was out of range error even when right range used

Posted: Fri Apr 26, 2019 8:26 pm
by N612
From what I read, it seems like this can be achieved easily without code with a simple RxPath. Is there a reason you are trying to do this in code? Are you able to provide a Ranorex Snapshot (not a screenshot)? This will greatly help us better understand your AUT, environment, and issue.

Regarding Pavels code, it looks like there was a small typo: pCard.FindDescendans => pCard.FindDescendants