Home » none » Automated Web Testing with the WebBrowserControl

Automated Web Testing with the WebBrowserControl

G03A0003

Actually, I used this technique to automate submitting information to a set of web sites, but you could use this same technique to script any web site, including one you are currently developing, so that you can verify that the site actually does what it is supposed to do.

You might already have a testing tool that you use, so you might wonder why you’d want to use this technique.

With a small exception, most of the tools that we have available to us do not test the application at the browser level.  Are you writing an AJAX application?  How do you know the client side is doing all that it should?

There are only two other tools that I know of that will do what I’m about to show you.  One is iMacro, which you can get for free.  It works in IE and Firefox.  It has a semi-proprietary scripting language that kind of allows JavaScript, but you can’t use ONLY JavaScript to do your testing.  The other is QA Test Complete.  This also works with IE and Firefox but costs an arm and a leg.  Both have significant advantages over just using the WebBrowserControl, but using the WebBrowserControl works when you can’t, or don’t want to, use either of the other two.

For the purposes of this article, I’ve made the assumption that there is a web browser control on a Windows Form.  The first thing you’ll want to do is to wire up the DocumentCompleted even of the WebBrowserControl to code that looks like this.

private void webBrowser1_DocumentCompleted(object sender, 
    WebBrowserDocumentCompletedEventArgs e)
{
    System.Threading.Thread.Sleep(1000);            
    if (webBrowser1.ReadyState !=
        WebBrowserReadyState.Complete)
            return;
    webBrowser1.Tag = true;
}

The reason I have this code is that I found that just because the event got fired does not mean the page is done.  So I sleep for one second and then check the browser’s ready state.  If it isn’t ready, I return and basically wait for this event to get fired again.  If we really are ready, I set the Tag property to true, which my calling code is looking at to determine if it is time to move on.

I created a base class for everything else.  The class takes the browser control as a parameter which I assign to a property named “Browser”.

One thing you’ll commonly want to have access to when you are scripting is the DOM’s Document property.  Here’s the code I use for that.

public HtmlDocument Document
{
    get{
    d _d = _document;
    return (HtmlDocument)(Browser.Invoke(_d));}
}

You’ll want to create your own “Navigate” method that calls the WebBrowserControl’s Navigate method and then calls a wait method that looks like this.

protected void Wait() { int i; for (i = 0; i < 100; i++ ) { while (!(bool)(Browser.Tag)) { Application.DoEvents(); System.Threading.Thread.Sleep(10); } Browser.Tag = false;
// you might need some other sanity checks in here. } if (i > 99) throw new Exception( "Waited long enough in Base Class"); }

What this code does is wait for the page to load.  Once it loads, it resets the Tag property to false so we can use it on the next Navigate, and then verifies that I got to the right page.  In my case, I needed to verify that I had not hit a proxy server error.  If you don’t need a sanity check, you can just leave that part out.

The rest is just a matter of accessing the DOM off the Document object like you would do in JavaScript.

Other places talking about the WebBrowserControl

Displaying custom HTML in WebBrowser control – Gunnar Peipman’s … – I am using WebBrowser control to show preview of automatically generated HTML. Users can select options and preview pane reflects those changes automatically. WebBrowser control has some problems that have been here for years already. …

.NET Web Browser Control Gotchas – NET Web browser control for Windows applications? I bet most of you, at some point, must have done some head-scratching as to why Microsoft did not make this control as powerful as it should have been. This control does offer all of the …

Leveraging The Windows Forms WebBrowser Control (For The Win) – I’ve been working on a little utility to experiment with the XMPP protocol. The idea was to write a tool that would allow me to send, receive, and display the XML stream and XML stanza messages at the core of XMPP. …

XML in a Web Browser Control – Since I have been slow to add anything to this site, I decided any time I find a useful bit of .Net code I will post it here. Today I needed to display XML in a web browser control. I found some one has modified the build in IE XSL to …

A WebBrowser Implementation That Actually Raises Events! – The reason for this is the fact that the WebBrowser control is an ActiveX-based control. Essentially, all of the events that would normally be raised by the control itself, are instead captured inside the document that resides within …

Like this Article? Subscribe to get every article sent to your email.

Related Post

  • Running Selenium In Parallel With Any .NET Unit Testing ToolRunning Selenium In Parallel With Any .NET Unit Testing Tool Running Selenium in parallel from .NET seems to be a problem because, as of the time of this writing, I’ve yet to find a viable way of running selenium test on multiple browsers using […]
  • Test Driven SpecificationsTest Driven Specifications Several years ago, long before the community was actively talking about Test Driven Development, I worked for a short time at a company as a “bug fixer.”  That was my role.  They […]
  • TDD Saves Time – A StoryTDD Saves Time – A Story I recently had an experience writing code that proved to me, once again, that using Test Driven Development really is faster than the way I have been working.You will remember a couple […]
  • What Browser Is Selenium Currently RunningWhat Browser Is Selenium Currently Running This probably doesn’t happen all that often, but this last week I came across the need to know which browser I was running my selenium test against.  I figured that buried deep in the […]
  • What is SpecFlow…What is SpecFlow… …And why do I want it? That’s what I kept asking myself every time I saw this product.Well, the last time I looked, something caught my eye enough that I decided to download it and […]

About Dave Bush

Dave Bush is a Full Stack ASP.NET developer. His commitment to quality through test driven development, vast knowledge of C#, HTML, CSS and JavaScript as well as his ability to mentor younger programmers and his passion for Agile/Scrum as defined by the Agile Manifesto and the Scrum Alliance will certainly be an asset to your organization.

One Pingback/Trackback