Verifying the Links in your Web Application Using a Web Performance Test

Here are two samples on how you can use a Web performance test to verify that the links are working in your application. In the first sample, the links are verified against a single hard coded request url (https://MyStagingServer.com). In the second sample, the test reads a text file containing a list with multiple url links which you can manage and update. Both samples are simply performing new requests for each HtmlTag containing the string "https://". This can be used to verify that the all the links contained on your page are legitimate (not misspelled, or deleted).  In these samples, the ParseDependentRequests is equal to false suppressing dependent request for Image tags, Script files (JavaScript, VBScript), Cascading style sheets, Body background images, Table background images, Image buttons and XML files.

I created this code for testing the Web performance and load testing topics that I author: https://msdn.microsoft.com/en-us/library/dd293540.aspx. I plan on adding these samples to the MSDN library for the product documentation at a later point. I thought I'd post my first draft here. Let me know if you have any comments or feedback.

 

{
    using System;
    using System.Collections.Generic;
    using System.Text;
    using Microsoft.VisualStudio.TestTools.WebTesting;
    using Microsoft.VisualStudio.TestTools.WebTesting.Rules;

    public class TestLinks : WebTest
    {
        public TestLinks()
        {
            this.PreAuthenticate = true;
        }

        public override IEnumerator<WebTestRequest> GetRequestEnumerator()
        {
            string linkToVerify;

            WebTestRequest request = new WebTestRequest("https://MyStagingServer.com");
            yield return request;
            request = null;

            foreach (HtmlTag tag in this.LastResponse.HtmlDocument.GetFilteredHtmlTags("a"))
            {
                linkToVerify = tag.GetAttributeValueAsString("href");
                if (! linkToVerify.Contains("https://")) continue;
                WebTestRequest linkRequest = new WebTestRequest(linkToVerify);
                linkRequest.ParseDependentRequests = false;
                yield return linkRequest ;
                linkRequest = null;
            }
        }
    }
}

namespace TestProject1
{
    using System;
    using System.Collections.Generic;
    using System.Text;
    using Microsoft.VisualStudio.TestTools.WebTesting;
    using Microsoft.VisualStudio.TestTools.WebTesting.Rules;

    public class TestLinksFromFile : WebTest
    {
        public TestLinksFromFile()
        {
            this.PreAuthenticate = true;
        }

        public override IEnumerator<WebTestRequest> GetRequestEnumerator()
        {
            string linkToVerify;
            string[] requests = System.IO.File.ReadAllText(@"c:\public\urllist.txt").Split('\n');
           
            foreach (string url in requests) // Url entires in the urllist.txt file.
            {
                WebTestRequest request = new WebTestRequest(url);
                yield return request;
                request = null;

                foreach (HtmlTag tag in this.LastResponse.HtmlDocument.GetFilteredHtmlTags("a"))
                {
                    linkToVerify = tag.GetAttributeValueAsString("href");
                    if (!linkToVerify.Contains("https://")) continue;
                    WebTestRequest linkRequest = new WebTestRequest(linkToVerify);
                    linkRequest.ParseDependentRequests = false;
                    yield return linkRequest;
                    linkRequest= null;
                }
            }
        }
    }
}