Jump to content

TeamCity and W3C Validator

Posted on:February 7, 2017 at 01:00 AM


As you may know I’m a big fan of automation. I like to have things done in background and just receive notification if something happens. This saves time and allows me to forget about yet another thing to check situation.


Recently I found out that few months back I’ve introduced a html bug on one of my websites. It was not visible, but SEO dropped. Honestly I do not even remember why I did one particular change in code. Anyway, the result is that site’s google ranking dropped due to that.


I started to think how to ensure this does not happen in future. First idea was to make some unit tests! 100% code coverage, or even 120%… but it requires some cumbersome work.

Instead of that I wrote a simple system test. In my case I use TeamCity to run that tests periodically. Once a day a bunch of similar tests is executed. Automatically. Sample test looks like following.


public class WebAppIntegrationTests
    public void W3CValidatorTests(string pathToValidate)
        string url = "https://validator.w3.org/nu/?doc=" + HttpUtility.UrlEncode(pathToValidate)+"&out=json";
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
        request.AutomaticDecompression = DecompressionMethods.GZip;
        request.UserAgent = "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36";
        string html = String.Empty;

        using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
        using (Stream stream = response.GetResponseStream())
        using (StreamReader reader = new StreamReader(stream))
            html = reader.ReadToEnd();
            Assert.That(html, Does.Contain("\"messages\":[]"));
            Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.OK));

As you can see I use W3C Validator. I decided to go for that one because it is single source of truth for me when it comes to HTML stuff. The test is executed daily in TeamCity. It is an easy way that protects me from making stupid mistakes in HTML.