Skip to content

Instantly share code, notes, and snippets.

@mahizsas
Forked from orient-man/SmokeTests.cs
Created December 23, 2013 11:44
Show Gist options
  • Save mahizsas/8095768 to your computer and use it in GitHub Desktop.
Save mahizsas/8095768 to your computer and use it in GitHub Desktop.
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