Click here to Skip to main content
15,880,905 members
Articles / Programming Languages / C#
Technical Blog

Revealed: How to Create Universal Dialog Handlers Testing Framework

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
29 May 2016Ms-PL2 min read 7.8K   3  
Create tests that handle different dialogs via Testing Framework. Provide universal utilities, so that you do not need to write boilerplate code.

Introduction

Not all tests play out directly inside a browser. Web pages can display popup dialog windows such as alerts, confirmations and others. Testing Framework allows you to track and respond to dialog windows and can handle HTML popups and Win32 dialogs. It is not a difficult task to write code that handles dialogs. However, I will show you how you can create universal dialog handlers so that you do not need to write boilerplate code.

Image 1Download File Dialog

The first case that we are going to cover is the one for downloading files.

 Image 2

If you are just getting started with Testing Framework I recommend you to read my post- Getting Started with Telerik Testing Framework C# in 10 Minutes. Also, you can download the framework from the official site.

Download Dialog Handler Testing Framework

You should first create DownloadDialogsHandler with the appropriate parameters. After that, you start a dialog monitor and navigate to the desired page. After you click on the download button, the dialog is automatically handled by Testing Framework engine.

C#
[TestMethod]
public void DownloadDialogNotUniversal()
{
    string fileToDownload =
        Path.Combine(
            AppDomain.CurrentDomain.BaseDirectory,
            @"myw3schoolsimage.jpg");
    if (File.Exists(fileToDownload))
    {
        File.Delete(fileToDownload);
    }
    var dialog = new DownloadDialogsHandler(
        manager.ActiveBrowser,
        DialogButton.SAVE,
        fileToDownload,
        manager.Desktop);

    manager.DialogMonitor.Start();
    manager.ActiveBrowser.NavigateTo(
        "http://www.w3schools.com/tags/tryit.asp?filename=tryhtml5_a_download");
    ArtOfTest.WebAii.Core.Browser myFrame =
        manager.ActiveBrowser.Frames.ById("iframeResult");
    HtmlImage image =
        myFrame.Find.AllByTagName<HtmlImage>("img")[0];
    image.Click(false);

    dialog.WaitUntilHandled();
}

If the file exists on the disk, the test deletes it. Through AppDomain.CurrentDomain.BaseDirectory we access the folder of the tests' assemblies.

Universal Download Dialog Handler

We can create a static class that hides the complexity of the Testing Framework dialog handler.

C#
public static class DownloadDialogHandler
{
    public static void DownloadFile(
        Action action, 
        string saveLocation)
    {
        Manager.Current.DialogMonitor.Start();
        Browser browser = Manager.Current.ActiveBrowser;
        DownloadDialogsHandler handler = 
            new DownloadDialogsHandler(
                browser, 
                DialogButton.SAVE, 
                saveLocation,
                browser.Desktop);

        action();

        handler.WaitUntilHandled();
    }
}

In general, the code does the same. We need to pass the clicking of the download button as an anonymous action.

C#
[TestMethod]
public void DownloadDialogUniversal()
{
    string fileToDownload =
        Path.Combine(
            AppDomain.CurrentDomain.BaseDirectory,
            @"myw3schoolsimage.jpg");
    if (File.Exists(fileToDownload))
    {
        File.Delete(fileToDownload);
    }

    manager.ActiveBrowser.NavigateTo(
        "http://www.w3schools.com/tags/tryit.asp?filename=tryhtml5_a_download");
    DownloadDialogHandler.DownloadFile(
        () =>
        {
            Browser myFrame = manager.ActiveBrowser.Frames.ById("iframeResult");
            HtmlImage image = myFrame.Find.AllByTagName<HtmlImage>("img")[0];
            image.Click(false);
        },
        AppDomain.CurrentDomain.BaseDirectory);
}

Upload File Dialog

The next thing that we want to be able to do is to upload files.

Image 3 

Upload File Dialog Handler Testing Framework

The code is almost identical to the previous one except this time we create FileUploadDialog and pass different parameters.

C#
[TestMethod]
public void UploadFileDialogNotUniversal()
{
    string fileToBeUploadedPath =
        Path.Combine(
            AppDomain.CurrentDomain.BaseDirectory,
            @"myw3schoolsimage.jpg");
    var fileDialog = new FileUploadDialog(
        manager.ActiveBrowser,
        fileToBeUploadedPath,
        DialogButton.OPEN);
    manager.DialogMonitor.AddDialog(fileDialog);
    manager.DialogMonitor.Start();

    manager.ActiveBrowser.NavigateTo(
        "http://nervgh.github.io/pages/angular-file-upload/examples/simple/");
    HtmlInputFile fileInput =
        manager.ActiveBrowser.Find.ByXPath<HtmlInputFile>(
            "//*[@id='ng-app']/body/div/div[2]/div[1]/input[2]");
    fileInput.Click(false);
}

Universal File Upload Dialog Handler

Again we create a static class that hides the file dialog handler code's complexity.

C#
public static class FileUploader
{
    public static void Upload(Action action, string filePath)
    {
        Manager.Current.ActiveBrowser.Window.Maximize();
        var dialog = new FileUploadDialog(
            Manager.Current.ActiveBrowser, 
            filePath, 
            DialogButton.OPEN, 
            "OPEN");
        try
        {
            Manager.Current.DialogMonitor.AddDialog(dialog);
            Manager.Current.ActiveBrowser.RefreshDomTree();
            action();
            dialog.WaitUntilHandled();
        }
        finally
        {
            Manager.Current.DialogMonitor.RemoveDialog(dialog);
        }
    }
}

The usage in tests is straightforward.

C#
[TestMethod]
public void UploadFileDialogUniversal()
{
    string fileToBeUploadedPath =
        Path.Combine(
            AppDomain.CurrentDomain.BaseDirectory,
            @"myw3schoolsimage.jpg");
    manager.ActiveBrowser.NavigateTo(
        "http://nervgh.github.io/pages/angular-file-upload/examples/simple/");
    FileUploader.Upload(
        () =>
        {
            HtmlInputFile fileInput =
                manager.ActiveBrowser.Find.ByXPath<HtmlInputFile>(
                    "//*[@id='ng-app']/body/div/div[2]/div[1]/input[2]");
            fileInput.Click(false);
        },
        fileToBeUploadedPath);
}

Confirmation Dialog

The last scenario that I am going to cover is dedicated to the confirmation dialog. 

Confirmation Dialog Handler Testing Framework

C#
[TestMethod]
public void ConfirmDialogNotUniversal()
{
    var dialog = new ConfirmDialog(manager.ActiveBrowser, DialogButton.OK);
    manager.DialogMonitor.AddDialog(dialog);

    manager.DialogMonitor.Start();

    manager.ActiveBrowser.NavigateTo(
        "http://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_confirm");
    Browser myFrame = manager.ActiveBrowser.Frames.ById("iframeResult");
    HtmlButton alertButton = myFrame.Find.AllByTagName<HtmlButton>("button")[0];
    alertButton.Click(false);

    dialog.WaitUntilHandled();
    manager.DialogMonitor.RemoveDialog(dialog);
}

Universal Confirmation Dialog Handler

We create a static ConfirmationDialogHandler class. You can even specify which dialog button to be clicked.

C#
public static class ConfirmDialogHandler
{
    public static void Handle(
        System.Action action = null, 
        DialogButton dialogButton = DialogButton.OK)
    {
        ConfirmDialog confirmDialog = 
            new ConfirmDialog(Manager.Current.ActiveBrowser, dialogButton);
        try
        {
            Manager.Current.DialogMonitor.AddDialog(confirmDialog);
            Manager.Current.DialogMonitor.Start();
            if (action != null)
            {
                action.Invoke();
            }
            confirmDialog.WaitUntilHandled();
            confirmDialog.Handle();
        }
        finally
        {
            Manager.Current.DialogMonitor.RemoveDialog(confirmDialog);
            Manager.Current.DialogMonitor.Stop();
        }
    }
}

The usage in tests is as simple as before.

C#
[TestMethod]
public void ConfirmDialogUniversal()
{
    manager.ActiveBrowser.NavigateTo(
        "http://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_confirm");
    ConfirmDialogHandler.Handle(
        () =>
        {
            Browser myFrame = manager.ActiveBrowser.Frames.ById("iframeResult");
            HtmlButton alertButton =
                myFrame.Find.AllByTagName<HtmlButton>("button")[0];
            alertButton.Click(false);
        });
}

You can find more code examples in the official framework's documentation.

Telerik Testing Framework Series

License

This article, along with any associated source code and files, is licensed under The Microsoft Public License (Ms-PL)


Written By
CEO Automate The Planet
Bulgaria Bulgaria
CTO and Co-founder of Automate The Planet Ltd, inventor of BELLATRIX Test Automation Framework, author of "Design Patterns for High-Quality Automated Tests: High-Quality Test Attributes and Best Practices" in C# and Java. Nowadays, he leads a team of passionate engineers helping companies succeed with their test automation. Additionally, he consults companies and leads automated testing trainings, writes books, and gives conference talks. You can find him on LinkedIn every day.

Comments and Discussions

 
-- There are no messages in this forum --