Monday, January 16, 2012

How to get IE Status Bar information with WatiN

Does your application have unhandled javascript errors?
 
Should it be tested?
The solution is simple  (but only for IE):
((IEBrowser)(browser.NativeBrowser)).WebBrowser.StatusText 
where browser - is you local instance of Browser class.


by using WebBrowser you have access to all members of IWebBrowser2 Interface.
For more details  about IWebBrowser2 please see  - http://msdn.microsoft.com/en-us/library/aa752127(v=vs.85).aspx


Wednesday, March 9, 2011

Load Test Video


Really nice video, that help step by step understand the workflow with Load Test. Especially,  how to work with the new load test features in VS 2010 - ASP.NET Profiler and Network Emulation :

Sunday, February 27, 2011

WatiN – How to handle confirmation window

This example will illustrate how to handle confirmation window from web page. For example, web page generate confirmation message, when user wants to delete a records and click “Delete” button. Web page includes some javascript like the following:
javascript:{if (confirm('Confirm that you want to delete this record.'));}

For click OK button on the message and continue test we need to add following ConfirmDialogHandler():
var alertDialogHandler = new ConfirmDialogHandler();
using (new UseDialogOnce(_browser.DialogWatcher,
                         alertDialogHandler))
{
    myPage.DeleteButton.ClickNoWait();
    alertDialogHandler.WaitUntilExists();
    alertDialogHandler.OKButton.Click();
}
I guess, that the most tricky part of this code is the ClickNoWait(). Usually when I need to click on any button during the test I use Click() method. But in case we are looking for confirmation or alert window after button was clicked, we should use ClickNoWait().

Let see what is the difference between these two methods:
public virtual void Click()
{
    if (!this.Enabled)
    {
        throw new ElementDisabledException(this.IdOrName, this);
    }
    Logger.LogAction("Clicking {0} '{1}', {2}", new object[] { base.GetType().Name, this.IdOrName, base.Description });
    this.Highlight(true);
    this.NativeElement.ClickOnElement();
    try
    {
        this.WaitForComplete();
    }
    finally
    {
        this.Highlight(false);
    }
}

public virtual void ClickNoWait()
{
    if (!this.Enabled)
    {
        throw new ElementDisabledException(this.IdOrName, this);
    }
    Logger.LogAction("Clicking (no wait) {0} '{1}', {2}", new object[] { base.GetType().Name, this.IdOrName, base.Description });
    this.Highlight(true);
    INativeElement nativeElement = this.NativeElement;
    UtilityClass.AsyncActionOnBrowser(new ThreadStart(nativeElement.ClickOnElement));
    this.Highlight(false);
}
In the method ClickNoWait() the new thread is launched and click method is executed in it. As result, using this method we can continue without waiting for the click event to be finished. Due to this, confirmation window become available for user actions.

Thursday, December 2, 2010

Coded UI tests - IE8 validation URL trick

Any test should include “assert” part. Validation of expected Url is common for any web tests. I use “Coded UI Test Builder” for capturing application objects and for finding out their right properties that are need to be validated. When object is captured it is indicated by the blue outline.

I selected the Address Bar in IE8, but this object did not include any property with url address. I was surprised, because it worked as expected for IE7.



After several efforts, I decided to post the question to the “Visual Studio UI Automation Testing” forum (see more detail here)
Finally, the right object was found: control type – HtmlDocument, property – PageUrl.

Saturday, November 13, 2010

Custom Web Request Test Plugin – Extracted by Regex

Web test requests might include Form Post or QueryString Parameters. For example, if some link of the web site should be clicked, these parameters might include ID of this link. In previous post I showed how to use data source in such case. But data source contains Name of the target link, but not it ID value. Of course, we can add ID value to the data source, but if this ID is unknown for us. For example, in the first request we create application data with corresponding name from the data source and in the second request we click on the newly created item. For such case following custom web request plugin can be created:
[Description("Extract by RegEx \n Sample BeginRegEx + e.WebTest.Context[TextToSearch].ToString() + EndRegEx")]
public class ExtractByRegEx : WebTestRequestPlugin
{
    [Description("Context paramete with UserName")]
    public string ContextParamWithSearchValue
    { set; get; }

    [Description("Context param name to store UserID value")]
    public string ContextParamToStoreValue
    { set; get; }

    [Description("First part of RegEx")]
    public string BeginRegEx
    { set; get; }

    [Description("End part of RegEx")]
    public string EndRegEx
    { set; get; }

    public override void PostRequest(
        object sender, PostRequestEventArgs e)
    {
        if (e.Response == null)
        {
            return;
        }

        var context = e.WebTest.Context;
        var match =
            Regex.Match(
                e.Response.BodyString,
                string.Format("{0}{1}{2}",
                    BeginRegEx,
                    context[ContextParamWithSearchValue],
                    EndRegEx)
            )
            .Groups[1].Value;

        if (context.ContainsKey(ContextParamToStoreValue))
        {
            context[ContextParamToStoreValue] = match;
        }
        else
        {
            context.Add(ContextParamToStoreValue, match);
        }
    }
}
When plugin is created, add it to the request and fill all necessary parameters:
BeginRegEx – this is the first part of the regex expression,
ContestParamToStoreValue – context parameter for extracted value,
ContextParamWithSearchValue – data source value,
EndRegEx – last part of the regular expression.


Context Parameters with extracted value can be used as the data source in further requests.


For create and check regular expression you may use this link.

Sunday, October 31, 2010

PDC10: Test Automation Everywhere

Overview of automation tests in VS 2010 and how they can be integrated with build process - Test Automation Everywhere.

Download Materials - FT53 Test Automation Everywhere.

Friday, October 22, 2010

Web Test & Data Source

One of the benefits of web tests in VS that they can be combined into the scenario. For example, Scenario includes two web tests and each of them uses the same data from data source.  I guess, it will be more elegant to have such data source in Scenario and do not add it to the each of test separately. Magic property of data source “Select all columns” will help us to do this.


After that, all columns will be accessible in the Context. Each column will be automatically binded to the corresponding context parameter with name “DataSourceName.TableName.ColumnName”.

For example, we need to check response and find value from the data source. Custom Validation rule will help.
[Description("Validate text from data source column at the response")]
public class ValidateData : ValidationRule
{
    [Description("Binded to the DataSource Context parameter name = DataSourceName.TableName.ColumnName")]
    public string DataSourceName { set; get; }

    public override void Validate(object sender, ValidationEventArgs e)
    {
        if (DataSourceName == null)
            return;
        if (e.Response.BodyString == null)
            return;

        var searchData = e.WebTest.Context[DataSourceName].ToString();
        e.IsValid = e.Response.BodyString.Contains(searchData);
        e.Message = e.IsValid
            ? string.Format("{0} found", searchData)
            : string.Format("Did not find  {0}", searchData);
    }
}

Validation rule can be added by the right click on the request that should be validated. Fill DataSourceName value and run the test.
Validation result will be at the “Details” tab of the web test result file.