Monday, July 5, 2010

Using Selenium for Meta tag testing

I recently tweeted that I had figured out how to use Selenium to test for meta tag content. Here is in an example using http://www.protegra.com/. On the Protegra home page, I wanted to make sure the following 2 tags existed:

<meta name="title" content="Protegra.com" /> 
<meta name="description" content="Business. Technology. Solutions." />

Obviously, meta tags don't show on the page at all, but do exist in the HTML. To test this manually, I would need to open the page in a browser, click view source, search for 'meta' and then manually compare the results to the expected meta tags. To test this using Selenium, all you need to do is create a test case that opens up Protegra.com and then uses VerifyElementPresent to search for each meta tag. The VerifyElementPresent command allows you to enter the name of the meta tag and the expected content in the format of :

//meta[@name='name of the meta tag' and @content='the expected content']

The Selenium test case html for the two meta tags I'm looking for looks like this:
<tr>
  <td>verifyElementPresent</td>
  <td>//meta[@name='title' and @content='Protegra.com']</td>
  <td></td>
</tr>
<tr>
  <td>verifyElementPresent</td>
  <td>//meta[@name='description' and @content='Business. Technology. Solutions.']</td>
  <td></td>
</tr>

Now I can run this test repeatedly to test for the expected meta tag content on the home page whenever I want without using manual effort.  I can also create similar tests for each page throughout the site and run them all consecutively.  Additionally, I could use Excel to generate the Selenium test case html for all my pages and meta tags without having to write the html manually or I could export this test case into C# (or any of the other programming languages supported by Selenium) and transform this test to lookup pages and meta tag values in a database table.  Fast and easy.