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.

// Find all Html elements that have an inline style attribute

var inlineStyledElements = Find.AllElements().Where(element =>

element.ContainsAttribute("style"));

AllControls Gets an IEnmerable for TControl to be used for LINQ queries. This will return only elements that are convertable to TControl.

// Find all images on a page.

var images = Find.AllControls<HtmlImage>();

 

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:

 

C#

// 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);

 

Visual Basic

' Find the first element that contains "Go Google"
Dim el As Element = Find.ByCustom(Function(e) e.TextContent.Contains("Go Google"))
  
' Find the first HTML anchor whose parent element does not have an input type
Dim a As HtmlAnchor = Find.ByCustom(Of HtmlAnchor)(Function(e) e.BaseElement.InputElementType = InputElementType.NotSet)
Assert.IsNotNull(a)
  
' Find the first HTML anchor element that contains "Go Google"
Dim l As HtmlAnchor = Find.ByCustom(Of HtmlAnchor)(Function(r) r.TextContent.Contains("Go Google"))
Assert.IsNotNull(l)
Assert.IsTrue(el.IdAttributeValue = "a0")
  
' Find the first HTML anchor element that contains "Go Google"
Dim rr As HtmlAnchor = (From b In Find.AllControls(Of 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
 Dim rd As IList(Of HtmlAnchor) = (From b In Find.AllControls(Of 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
Dim li As IList(Of Element) = (From b In Find.AllElements() _
    Where b.IdAttributeValue.Contains("a") _
    Select b).ToArray()
Assert.IsTrue(li.Count > 0)

 

For complete information on what LINQ is and how to use it see the MSDN documentation here: The LINQ Project.