Last active
September 10, 2017 20:50
-
-
Save orient-man/7804310 to your computer and use it in GitHub Desktop.
Smoke test for our ASP.NET MVC application using FakeHost (based on MvcIntegrationTestFramework: http://blog.stevensanderson.com/2009/06/11/integration-testing-your-aspnet-mvc-application/)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections.Generic; | |
using System.IO; | |
using System.Linq; | |
using System.Net; | |
using System.Text.RegularExpressions; | |
using System.Web; | |
using FakeHost; | |
using FluentAssertions; | |
using HtmlAgilityPack; | |
using NUnit.Framework; | |
namespace Pincasso.MvcApp.Tests.Integration | |
{ | |
[TestFixture, Category("Integration")] | |
public class SmokeTests | |
{ | |
[TestFixtureSetUp] | |
public void SetUp() | |
{ | |
Browser.InitializeAspNetRuntime( | |
Path.GetFullPath(@"..\..\..\Pincasso.MvcApp")); | |
} | |
[Test] | |
public void VisitMainPageAndLinksFromMainMenu() | |
{ | |
using (var browser = new Browser()) | |
{ | |
// set up user (security disabled) | |
browser.Cookies.Add(new HttpCookie("PincassoUser", "administrator")); | |
var result = browser.Get("/"); | |
result.StatusCode | |
.Should().Be((int)HttpStatusCode.OK, "Main page failed"); | |
var menuLinks = GetMenuLinks(result.ResponseText).ToList(); | |
menuLinks.Should().NotBeEmpty("There should be some links in menu"); | |
foreach (var link in menuLinks) | |
{ | |
System.Diagnostics.Debug.WriteLine("Visiting: " + link); | |
result = browser.Get(link); | |
result.StatusCode | |
.Should().Be((int)HttpStatusCode.OK, "Wrong status code for page: " + link); | |
// Global filter catches all exceptions and put "user friendly" Error.cshtml instead | |
// ... which contains special maker (Yeah, I know but it works) | |
result.ResponseText | |
.Should() | |
.NotContain( | |
"<!-- error marker -->", | |
"Page {0} contains error(s)", link); | |
} | |
} | |
} | |
private static IEnumerable<string> GetMenuLinks(string html) | |
{ | |
var mainPage = new HtmlDocument(); | |
mainPage.LoadHtml(html); | |
var menuLinks = | |
mainPage | |
.DocumentNode | |
.SelectNodes("//div[@class='mainmenu']//a[@href]"); | |
// Doh!, instead of empty collection it returns null! | |
if (menuLinks == null) | |
return Enumerable.Empty<string>(); | |
// skip empty links and special pages | |
return menuLinks | |
.Select(o => o.Attributes["href"].Value) | |
.Where( | |
o => !string.IsNullOrWhiteSpace(o) && | |
!Regex.IsMatch(o, @"(^#$|^\/Tools\/.*|^\/Tests\/.*)")) | |
.Distinct(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment