Thursday, August 30, 2012

Load testing with loadUI and a Console Application using WebBrowser

To load test an ASP.NET web application I created a Console Application that simulated the process one user would go through to complete adding a new order, from automatic login, to product selection, to logout.

The Console Application

  1. Create a thread to host an instance of System.Windows.Forms.WebBrowser in.
    See WebBrowser Control in a new thread
  2. Create the URL that the user navigation will start from. The included single sign on details that would establish the Session.
  3. Register for the DocumentCompleted event from the WebBrowser so I can detect when the page has been completely loaded.
  4. Call WebBrowser.Navigate() with the URL.
  5. Start the STA Thread running.
  6. When the DocumentCompleted event fires check if the event args URL matches the browsers URL.
  7. If the URLs match the navigation has been completed and the browser can be transitioned to the next step.
  8. Use WebBrowser.Navigate("javascript: document.getElementById('ctl00_MainContentPlaceHolder_btnContinue').click();"); to simulate the user pressing the Continue button.
  9. Keep checking the DocumentCompleted event until the WebBrowser reaches a page that indicates the process is complete. When it is, exit the thread to return to the main program. System.Windows.Forms.Application.ExitThread();
  10. The Console Application would close with and Exit Code of 0 if the process was successful. Otherwise the Exit code would correspond to a enum flag indicating what issue was encountered.

Why use a WebBrowser instance rather than HttpWebRequest/HttpWebResponse and a CookieContainer?

I started down this path but there were a couple of issues.

Firstly, this would only send the requests I explicitly coded for where as the WebBrowser will load the page like a user would. For load testing this made the WebBrowser more realistic as it would also send requests for CSS, JavaScripts, Images and iframes before considering the load complete

Using loadUI to start and monitor the Console Application

  • Create a Fixed Rate Generator
  • Connect it to a Process Runner