Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Whitebox Testing with Selenium

0.00/5 (No votes)
30 Jun 2015 1  
This tip describes how to write Selenium based tests while allowing you to mock/fake parts of the server components.

Introduction

Selenium is an amazing tool to automate ASP.NET web tests and those tests are most valuable because they represent how users interact with your application at the highest level but still, web tests are most hard to set up and maintain.

Setting up web tests is almost never done well and as a result the web tests are unstable, hard to debug, are not run frequently or are simply turned off because of the high number of crashes. Add to it that most developers, somehow, prefer writing web tests more than writing unit tests which only make the problem even bigger.

On the other hand, ASP.NET does not help much with writing sensible tests for a controller action in an easy way that do not rely on implementation details. As a result, only helper classes of the web project are covered in unit tests.

This is why I developed Xania AspNet Simulator (Xim). Xim is a hosting environment developed and optimized for testing purposes which enables you to build up and run an MVC application dynamically by injecting custom instances of controllers and thus allows you to mock, fake or stub parts of the application specifically for your test case.

Background

In my previous post, I described how to use Xim to write clean unit tests at the controller action level and since then I have added more features to it like razor and http which enables browser based testing.

Using the Code

The following code snippet illustrates how to use Xim to start and run an MVC application dynamically. For a complete example, download the sample project.

[Test]
public void SeleniumIsolatedUnitTest()
{
    ...

    server.UseMvc(new MyController(), contentProviderMock.Object)
        .EnableRazor();

    // act
    driver.Navigate().GoToUrl("http://localhost:8080/my/index");

    // assert
    driver.FindElement(By.TagName("div")).Text.Should().Be("Hello Simulator");

    ...
}

And for more UI and non UI test cases, please refer to MvcApplication1 project (based on default project template of MVC 4 application) and corresponding Selenium Tests.

Conclusion

In general, Xim has no static fields to hold state. All state information is stored in transient objects and in some cases, the state is copied from ASP.NET MVC runtime static properties like RouteTable.Routes, ViewEngines.Engines, BundleTable.Bundles to internal transient collections.

So, there is no need to create new AppDomains to isolate state like ASP.NET MVC runtime does. As a consequence, Xim is very flexible and is extremely fast to start (cold start < 3 sec and subsequently < 10 ms) and you could run multiple Xim's in parallel.

The current state of the application is beta for the most important reason that it is not yet fully tested with a real enterprise application. Second, Xim has no support for a lot of Legacy (old and crappy) ASP.NET components like HttpContext, FormsAuthentication, WebSecurity, OAuthWebSecurity, etc. and I would like to receive your feedback on that matter.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here