InCisif.net 2.1, C# 3.0, LINQ and Lambda expressions

12/28/2007 3:44:00 AM

Visual Studio 2008 is out, so we are updating InCisif.net API to support C# 3.0 features : LINQ and Lambda expressions.
By internally rewriting our HTMLControls class this way, we add LINQ support.
public class HTMLControls :  System.Collections.Generic.List<HTMLControl> {
}
We also added anonymous method support therefore lambda expression to a bunch of methods.
InCisif.net 2.1 is still compiled with C# 2.0, but can leverage C# 3.0 features.

So here is a first example, where I used LINQ, to verify the number of images in a pages.


[InCisif.net.Library.Test("Using LINQ to verify images count in a page")]
public void InCisifNetAndLINQ_VerifyingImages(){

using ( InCisif.net.Library.Test t = new InCisif.net.Library.Test(Language.CSharp, this) ) {

Page.URL = "http://www.incisif.net/demo2.1/HTMLControlsArrayDemo/HTMLControlsArrayDemo1.asp";
Page.WaitForPage("/HTMLControlsArrayDemo1.asp");

//
// Count the number of HTML <IMG> tags containing a jpg image filename starting with CreditCard
//

// Syntax 1
var JpgImages1 = from p in Page.Elements()
where p.Tag=="IMG" && p.Src.EndsWith(".jpg") && p.Name.StartsWith("CreditCard")
select p;
t.ASSERT(JpgImages1.Count() == 3, "Verify the number of jpg credit card images");

// Syntax 2 - Here we can query the collection of html controls <IMG> more than once by
// storing it first in a variable.
InCisif.net.Library.HTMLControls HTMLControlIMGs = Page.Elements(c => c.Tag == "IMG");

var JpgImages2 = from p in HTMLControlIMGs // Query for the jpg images
where p.Src.EndsWith(".jpg") && p.Name.StartsWith("CreditCard")
select p;
t.ASSERT(JpgImages2.Count() == 3, "Verify the number of jpg credit card images");

var GifImages2 = from p in HTMLControlIMGs // Query for the gif images
where p.Src.EndsWith(".gif")
select p;
t.ASSERT(GifImages2.Count() == 1, "Verify the number of gif in the page");

// Other syntax with LINQ extension methods and lambda expressions explicitly
var JpgImages3 = HTMLControlIMGs.Where(c => c.Src.EndsWith(".jpg") && c.Name.StartsWith("CreditCard"));
t.ASSERT(JpgImages3.Count() == 3, "Verify the number of jpg credit card images");

// One Liner
t.ASSERT((HTMLControlIMGs.Where(c => c.Src.EndsWith(".jpg") && c.Name.StartsWith("CreditCard"))).Count() == 3, "Verify the number of jpg credit card images");

t.Passed = true;
}
}