HttpWatch Automation Reference - Version 11.x
Samples / Selenium Form Fill Sample for Internet Explorer (C#)
In This Topic
    Selenium Form Fill Sample for Internet Explorer (C#)
    In This Topic

    Overview

    This C# sample program uses the HttpWatch automation interface to record and analyze the HTTP traffic generated by submitting a web page form. It is not possible to directly interact with web page controls using HttpWatch, so this program uses the Selenium framework to enter a value into a form field and then click on the submit button.

    Selenium is a multi-platform open source browser automation framework that can be used to drive and interrogate web pages in IE and other browsers. To run this sample you will need to perform the following steps:

    1. Open the solution file form_fill_selenium.sln in Visual Studio 2015 or later
    2. Build  and Run the program. It will start IE, submit a form on a web page and then display page load timings from HttpWatch

    The Visual Studio C# project uses the following Nuget packages to automatically download and install the required Selenium libraries:

    If you want to avoid using Nuget the software can be manually downloaded and installed from http://seleniumhq.org/download/ .

     The sample program is in the following folder:

    %ProgramFiles%\HttpWatch\api_examples\ie\form_fill_selenium\
    

    It is worth making a working copy of this folder elsewhere; particularly on Windows Vista or later as UAC (User Account Control) will normally stop you making changes to that directory.

    Once you have done this, you can open the application in Visual Studio by double-clicking on the form_fill_selenium.sln solution file.

    Solution Implementation

    The Visual Studio solution contains a single console application project. If the References node is expanded, you can see that a reference to the HttpWatch automation library has been added. When developing an application from scratch, you will need to create this reference, as described in Using HttpWatch Automation with C#.

    There is also a reference to the Selenium Webdriver assembly that was installed by the Nuget Selenium Web Driver package.

    All the functional code for this project is in the main.cs source file.

    Program Operation

    The program is hard-coded to use the web page form on https://www.httpwatch.com/httpgallery/redirection/. It could be adapted for your own web pages, but you would need to change the names and ids used to identify the target controls on the page.

    Selenium is used to create the instance of IE and then HttpWatch is attached to it using a unique page title as shown below:

    Attaching HttpWatch to Selenium created instance of IE
    Copy Code
    // Create IE instance using Selenium
    // When you first run the program you may get a Windows Firewall warning about the IE Driver. 
    // You can uncheck the private and public network access unless you are planning to access the IEDriver across the network.
    // Also, the IEDriver will throw an error if Protected Mode is not enable in all IE security zones, e.g. Intranet, 
    // Internet. You can change this in IE by going to Tools->Internet Options->Security and selecting each zone.
    InternetExplorerDriver driver = new InternetExplorerDriver();
                
    // Set a unique initial page title so that HttpWatch can attach to it
    string uniqueTitle = Guid.NewGuid().ToString();
    driver.ExecuteScript("document.title = '" + uniqueTitle + "';");
     // Attach HttpWatch to the instance of IE created through Selenium
    Plugin plugin = control.AttachByTitle(uniqueTitle);
    

     The sample first loads up the web page using the Selenium GoToUrl method: 

    Loading a page with Selenium
    Copy Code
    // Goto to the URL and wait for the page to be loaded
    driver.Navigate().GoToUrl(url);
    

     After recording has been started the Selenium interface is used to put a value in to the Amount field and  click on the Submit button:

    The Wait method is then used to wait for the form submitted and the resulting page to be downloaded.

    The sample code then prints out some information about the loading of the page and uses Selenium to access the value that was displayed in the updated Account Balance field:

     

    Running the Application

    To run the application from Visual Studio, drop down the Debug menu and click Start Without Debugging (or hold the Ctrl key down and press the F5 key). The program first loads the page in IE and then fills out the form field before clicking the Submit button. As soon as the resulting page is loaded IE is closed and the results are displayed in the console window.

    See Also