NSelene
NSelene copied to clipboard
Consider implementing something like collection.IndexOfElementBy(condition)
Here is an example of potential method as extension:
public static class SeleneCollectionExtensions
{
public static int IndexOfElementBy(
this SeleneCollection collection,
Condition<SeleneElement> condition,
int amongExpectedOfNumber=-1
)
{
if (amongExpectedOfNumber>0)
{
collection.Should(Have.Count(amongExpectedOfNumber));
}
collection.FindBy(condition).Should(Be.InDom);
return ((IEnumerable<SeleneElement>)collection)
.Select((Value, Index) => new {Value, Index})
.First(its => condition.Apply(its.Value))
.Index;
}
}
Open Point: should we add it to SeleneCollection, or keep it as extension?
One point - such implementation is not lazy... Let's see this in the following use case... Imagine you have some crazy markup without testIds/Ids/etc:
<html>
<head>
<meta charset="utf-8">
<title>Contacts Dictionary</title>
</head>
<body>
<table border="1">
<caption>Contacts</caption>
<tr>
<th>Name</th>
<th>Phone</th>
</tr>
<tr><td>Yashaka</td><td>+380001234567</td></tr>
<tr><td>Akahsay</td><td>+380007654321</td></tr>
</table>
</body>
</html>
And you know that you want to get the phone number from second row, then with the extension method from above you can do:
var secondPhoneCell = SS("tr")[1].SS("td")[SS("th").IndexOfElementBy(Have.ExactText("Phone"))]
So the problem with the code above is that the secondPhoneCell is not lazy. It will be executed at the moment of the call. Question - can we find a better lazy alternative for such use case?
Maybe something like:
var secondPhoneCell = SS("tr")[1].SS("td").FindBy(Have.IndexWhere(SS("th"), Have.ExactText("Phone"))
Now such SeleneElement – secondPhoneCell – is completely lazy and can be easyly used e.g. as a PageObject field;)
Do we then even need this IndexOfElementBy? Maybe really as an extension method only...