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:
Jul 09
2009

Sometimes it is necessary to read an HTML File, to replace some sections and to response the achievment. A good way to to this is using regular expressions.

For example:

Read the HTML file:
string strHTMLBefore = ReadHTML(@"c:\example.html");

Execute regEx operations:
string strHTMLAfter = ExecuteRegExOperations(strHTMLBefore);

Print string to a literal:
litHTMLAfterReplace.Text = strHTMLAfter;

 

C#:

        /// <summary>
        /// reads an html file from filesystem and returns ist
        /// </summary>
        /// <param name="filePath">path to file on disk</param>
        /// <returns>file content</returns>
        private string ReadHTML(string filePath)
        {
            bool bExists = File.Exists(filePath);
            string fileOriginal = null;

            if (bExists)
            {
                StreamReader objStreamReader = new StreamReader(filePath, Encoding.Default);
                fileOriginal = objStreamReader.ReadToEnd();
                objStreamReader.Close();
            }
            else
            {
                lblMessage.Visible = true;
                lblMessage.Text = "file not found";
            }
            return fileOriginal;
        }

 

         /// <summary>
        /// executes RegEx operations
        /// </summary>
        /// <param name="strHTMLBefore">input string</param>
        /// <returns>output string</returns>
        private string ExecuteRegExOperations(string strHTMLBefore)
        {
            string strBeforeRegEx = strHTMLBefore;
            string strAfterRegEx = null;

            //vacancyTitle:
            string patternExample  = "(<!--###JS24Begin:Name_Title###-->)(([\\r,\\n]|.)*?)(<!--###JS24End:Name_Title###-->)";
            Regex myRegex = new Regex(patternExample);
            strAfterRegEx = myRegex.Replace(strBeforeRegEx, "string to be inserted");

            return strAfterRegEx;
        }

 

example.html:

<td width="40%" style="padding: 5px; border: 1px solid #666666">
        <!--###JS24Begin:Name_Title###-->this text will be replaced by regex<!--###JS24End:Name_Title###-->
</td>

 

It works fine. But a malfunction must be described. If the regex does not find the "Begin" tag in aspx code, for example a mistake in writing, the program does not stop and the text will not be replaced.
That may be ok. But if the regex does not find the "end" tag the program will be lead to an endless loop and your cpu willl work up to 100% :-)
Until now I didn't found a way to avoid this. Do you have an idea?

Tags: