Reputation: 3
Wondering if it is possible to page object an id that will be dynamic?
Example code below:
int index = 0;
foreach (var row in rows)
{
if (randNumber == row.FindElement(By.Id($"r-number-{index}")).Text)
{
row.FindElement(By.Id($"view-record-{index}")).Click();
return;
}
index++;
}
By.Id($"r-number-{index}")).Text
is used in multiple places and in multiple specflow steps so would be great to be able to turn it into a page object.
RandPage.RandNumber+index+
in simple terms what I would ideally need.
Anyone knows of any possible ways?
Upvotes: 0
Views: 335
Reputation: 301
You could try something like this.
private IList<IWebElement> ListOfElements => Driver.FindElements(By.XPath("//p[contains(@id,'r-number-')]"));
//this finds all the p elements on the page with an id that contains r-number-
public void ClickEachOne()
{
// click each element in that list
foreach (var element in ListOfElements)
{
element.Click();
}
}
Upvotes: 1
Reputation: 570
I would suggest to add parameter in the PageObject constructor which will be the index.
And then you can create the page for that index and use it.
E.g:
public class MyPage
{
private int _index = 0;
...
public MyPage(int index)
{
_index = index;
}
...
public MyMethod()
{
//do something with the _index here
}
}
Upvotes: 1