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:
Sep 22
2009

Here a simple example to get a commaseparated list of cities to show them in the UI.
The result could be "New York, Madrid, Rome and Munich".

/// <summary>
/// commaseparated string of location cities
/// </summary>
        public string LocationsCitiesAsString
        {
            get
            {
                string strVacLocations = null;

                //put all vacancyLocations commaseparated to a string:
                foreach (var location in Vacancy.Locations)
                {
                     strVacLocations += location.City + ", ";
                }

                //if existing delete comma and whitespace at the end of string:
                if (strVacLocations.EndsWith(", "))
                {
                    strVacLocations = strVacLocations.Remove(strVacLocations.Length - 2, 2);
                }

                //replace last ", " with " and":
                int lastSpace = strVacLocations.LastIndexOf(", ");
                if (lastSpace > 0)
                {
                    strVacLocations = strVacLocations.Remove(lastSpace, 2).Insert(lastSpace, " and ");
                }

                return strVacLocations;
            }
        }

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:
Jun 04
2009

Unter www.bing.com ist seit einigen Tagen die neue Suchmaschine von Microsoft zu erreichen. Obwohl noch relativ neu, so hat sich im Web schon eine interessante Diskussion über die Leistungsfähigkeit von bing.com entwickelt. Wie bei fast allen Dingen ist es am besten, man probiert die Technik selbst aus. Am besten in der linken Bildschirmhälfte BING darstellen, auf der rechten Bildschirmhälfte GOOGLE. Dann mit den gleichen Suchbegriffen einige Suchen starten und die Ergebnisse sowie die Darstellung der Ergebnisse gegenüberstellen. Wer schon vorher einen kurzen Einblick haben möchte, kann hier einen kurzen Film in deutscher Sprache ansehen.

Ich denke, ein vorschnelles Urteil sollte man sich nicht erlauben. Nur allein weil die Suchmaschine von Microsoft zur Verfügung gestellt wird oder nur allein, weil es die Nachfolge von livesearch darstellt, reicht für eine realistische Bewertung nicht aus. Erst nach intensivem Testing und dem Einsatz im täglichen Gebrauch wird sich für den Einzelnen zeigen, ob bing.com eine Alternative zu Google oder anderen Anbietern darstellen kann.

 

Tags:
Apr 14
2009

Im allgemeinen verursacht ein ASP.NET LinkButton einen PostBack. Vereinzelt ist es jedoch notwendig, trotz einer Aktion auf dem Button den PostBack zu unterdrücken.  Mit einem simplen "return false;" im clientseitigen Eventhandler läßt sich das PostBack unterdrücken:

<script language="javascript" type="text/javascript">
    function alertMessage()
    {
        alert('Ich bin ein LinkButton, der keinen Postback verursacht!');
        return false;
    }
</script>

<asp:Button
    ID="btnTestAlert"
    runat="server"
    Text="abschicken"
    ToolTip="Ich bin ein ToolTip zum Button btnTestAlert"
    OnClientClick="return alertMessage();" />

 

Tags:
Mar 03
2009

Das Virtual Earth Kartencontrol erlaubt ab der Version 6.2 die Lokalisierung seiner Controls. Dazu muss lediglich bei der Einbindung des Plugins ein weiterer Parameter angehängt werden:

<script type="text/javascript" src="http://dev.virtualearth.net/mapcontrol/mapcontrol.ashx?v=6.2&mkt=de-DE"></script>

Weiteres siehe auf der Microsoft Website unter Returning Localized Results.
Tags: Tags: