New to Telerik Test Studio? Download free 30-day trial

LINQ Queries

The Find.Byxxx methods now support Language-Integrated Query (LINQ) queries. Some of the Find functions are intended to be used by LINQ queries only. These include:

Methods Description Example
AllElements Gets a IEnumerable for all elements to be used for LINQ queries. var inlineStyledElements = Find.AllElements().Where(element =>
element.ContainsAttribute("style"));
AllControls Gets an IEnumerable for TControl to be used for LINQ queries. This will return only elements that are convertible to TControl. var images = Find.AllControls();

Using LINQ we can create strongly typed advanced queries with intellisense support that we couldn't before. The most basic LINQ example is something like this:

// Find all images on a page.
var images = Find.AllControls<HtmlImage>();

foreach (HtmlImage img in images)
{
    // Do what you want with each img.
}

We can also use lambda expressions like this:

// Find the first element that contains "Go Google"
Element el = Find.ByCustom(e => e.TextContent.Contains("Go Google"));

// Find the first HTML anchor whose parent element does not have an input type
HtmlAnchor a = Find.ByCustom<HtmlAnchor>(e => e.BaseElement.InputElementType == InputElementType.NotSet);
Assert.IsNotNull(a);

// Find the first HTML anchor element that contains "Go Google"
HtmlAnchor l = Find.ByCustom<HtmlAnchor>(r => r.TextContent.Contains("Go Google"));
Assert.IsNotNull(l);
Assert.IsTrue(el.IdAttributeValue == "a0");

// Find the first HTML anchor element that contains "Go Google"
HtmlAnchor rr = (from b in Find.AllControls<HtmlAnchor>() where b.TextContent.Contains("Go Google") select b).First();
Assert.IsNotNull(rr);

// Fetch a list of HTML anchor's that contain 'a' in the ID
IList<HtmlAnchor> rd = (from b in Find.AllControls<HtmlAnchor>() where b.ID.Contains("a") select b).ToArray();
Assert.IsTrue(rd.Count > 0);

// Fetch a list of HTML anchor's that contain 'a' in the ID
IList<Element> li = (from b in Find.AllElements() where b.IdAttributeValue.Contains("a") select b).ToArray();
Assert.IsTrue(li.Count > 0);
In this article