Jan 18
2010

WatiN - developed in C# - brings to you an easy way to automate your tests with Internet Explorer and FireFox using .Net.
Often WatiN is used to click through the UI of your web application.
In this case it is important to use a sure method to find controls in a page.

A really simple way to find a control for example is:

ie.Link(Find.ById("ctl03_lnkbtnConfirmStartingProcess")).Click();

or

var link = ie.Link("ctl03_repCustomers_ctl00_repProducts_ctl00_lnkSlideDownStatistics");

 

Below there are two examples how to find controls in a page using regular expression. In my opinion this is more comfortable to use.

Example 1 - Find a dropdownlist in a testpage and check dropdown values:

//some other code...
ie.GoTo(
string.Format("{0}{1}", baseURL, "testpage.html"
));
var
selectStatusNames = new[] { "all", "online", "offline", "scheduled" };
var statusOpts = ie.SelectList(FindWithRegEx("ddlState")).Options;
for (var i = 0; i < selectStatusNames.Length; i++)
{
    if (String.Equals(statusOpts[i].InnerHtml, selectStatusNames[i])) continue;
    var msg = "Search in articles: selectbox 'Status': option #" + i + " must be " + selectStatusNames[i];
    trace(msg);
    Assert.Fail(msg);
}

 

Example 2 - Find a hyperlink in a testpage and click it:

//some other code...
ie.GoTo(string.Format("{0}{1}", baseURL, "testpage.html"));
var lnkSubmit = ie.Link(FindWithRegEx("lnkbtnSubmitPage"));
Assert.IsTrue(lnkChangePassword.Exists, "cannot find linkbutton to submit page");
lnkChangePassword.Click();

 

helpmethods:

/// <summary>helpmethod to find a control in a page using regular expressions</summary>
/// <param name="idCtrl">part of control ID as string</param>
/// <returns>control ID as string</returns>
public static Regex FindWithRegEx(string idCtrl)
{
    return new Regex(".*" + idCtrl + "$");
}

/// <summary>helpmethod to trace and debug output to console</summary>
/// <param name="msg">debug message
</param>
/// </summary>
/// <param name="msg">message as string</param>
public
static void trace(String msg)
{
    Console.WriteLine(msg);
}

Tags: Tags:

Related posts

Comments are closed