Selenium smoke testing using sitemap

It seems to me that if you have a smaller sized site, its probably useful to run selenium against all the sitemap.xml files, just to make sure all the pages work and don't immediately come up with errors - a very basic smoke test.

After spending 15 minutes googling and not finding anything exactly what I was looking for, I thought I would jot down a bit of code in case anyone else finds it useful.

 

private static string baseUrl = "http://site.com";
private static string sitemapRelativeUrl = "/google-sitemap.aspx";
private ISelenium selenium;

[TestFixtureSetUp]
public void SetupTest()
{
selenium = new DefaultSelenium("localhost", 4444, "*chrome", baseUrl);
selenium.Start();
}

[TestFixtureTearDown]
public void TeardownTest()
{
selenium.Stop();
}

/// <summary>
/// Get a navigator for the sitemap
/// </summary>
/// <returns></returns>
private XPathNavigator GetSitemapNavigator()
{
System.Xml.XmlReader xmlReader = new System.Xml.XmlTextReader(baseUrl + sitemapRelativeUrl);
System.Xml.XPath.XPathDocument doc = new System.Xml.XPath.XPathDocument(xmlReader);
var nav = doc.CreateNavigator();
return nav;
}

/// <summary>
/// Get a namespace manager for the sitemap
/// </summary>
/// <param name="namespaceAlias"></param>
/// <returns></returns>
private XmlNamespaceManager GetSitemapManager(XPathNavigator nav, string namespaceAlias)
{
XmlNamespaceManager manager = new XmlNamespaceManager(nav.NameTable);
manager.AddNamespace(namespaceAlias, "http://www.sitemaps.org/schemas/sitemap/0.9");
return manager;
}

/// <summary>
/// Make sure that the sitemap can be loaded and has entries
/// </summary>
[Test]
public void LoadSitemapAndCheckQuery()
{
var nav = GetSitemapNavigator();
var manager = GetSitemapManager(nav, "sitemap");
Assert.IsTrue(nav.Select("//sitemap:loc", manager).Count > 0);
}

/// <summary>
/// Load up each of the sitemap pages
/// </summary>
[Test]
public void LoadAllSitemapPages()
{

List<string> ExpectedRedirects = new List<string>();
ExpectedRedirects.Add(string.Format("{0}/url.aspx", baseUrl));

var nav = GetSitemapNavigator();
var manager = GetSitemapManager(nav, "sitemap");
XPathNodeIterator iterator = nav.Select("//sitemap:loc", manager);
if (iterator.Count > 0)
{
Uri url = null;
while (iterator.MoveNext())
{
try
{
if (Uri.TryCreate(iterator.Current.InnerXml, UriKind.RelativeOrAbsolute, out url))
{
selenium.Open(url.ToString());
string currentBodyText = selenium.GetBodyText();

Assert.IsFalse(currentBodyText.IndexOf("Error parsing XSLT file:") >= 0, string.Format("Error parsing XSLT File url: {0}", url));
Assert.IsFalse(currentBodyText.IndexOf("Error reading XSLT file:") >= 0, string.Format("Error reading XSLT File url: {0}", url));
Assert.IsFalse(currentBodyText.IndexOf("Server Error in '/' Application.") >= 0, string.Format("Server error url: {0}", url));

// Unless we are expecting the page to redirect, throw an error if it does
if (!ExpectedRedirects.Contains(url.ToString()))
Assert.AreEqual(url.ToString().Replace("'", "%27"), selenium.GetLocation(), "{0} was redirected to {1}", url.ToString(), selenium.GetLocation());
}
}
catch (Exception ex)
{
Assert.Fail(string.Format("Exception found. Last url: {0} Exception: {1}", url, ex));
}
}
}
}

Post a comment