Testing Drupal Websites Using WebDriver (Part 1)

I'm really enjoying Test-Driven-Development (TDD), combined with automated UI Tests for my Web apps. I love the confidence it gives me to code and be relatively certain I haven't broken anything which was previously tested...

Currently I'm writing tests for a custom Content Management System (CMS)  I co-wrote for my organization. The CMS is based on Drupal, but it's customized so much (via a very complex module) that it needs significant testing.

Drupal's implementation of SimpleTest does not work for me, as the testing is very complex, and I need more control than Drupal's SimpleTest module can offer.

For a tool to do the browser automation, I settled on Google's WebDriver project, which is also called Selenium2 and is a code-merge with the very popular Selenium framework. WebDriver is still in Beta, but regardless, I've found it to be stable, powerful, intuitive, and relatively easy to use. Alternative web-UI tesing frameworks exist, but I won't go in to them here. Suffice it to say, I like WebDriver because it is cross-browser, open-source, and backed by the venerable Selenium project, Simon Stewart, and Google. You can write WebDriver code in a bunch of popular programming languages including:

  • Java
  • C#
  • Python
  • PHP

But these implementations are ports of the Java API. Though my organization is not a Java shop, I chose to write the tests in Java, given that the Java implementation gets the most attention from the WebDriver developers.

The other tools I use to do my testing are the Eclipse IDE, the JUnit unit-testing framework, and subclipse. These three tools, along with WebDriver allow me to edit and compile test code, easily view test results using Eclipse's JUnit itegration, and use version control—all without ever leaving Eclipse. This is a great productivity boon.

WebDriver Code

Here's a basic (albeit a bit naive) example of a test class written in WebDriver. The test opens up a web page, finds a search input, types a search term, and submits the search. It then asserts that the first search term is what is expected:

package gov.nih.nlm.ncbi.cms.tests;

import static org.junit.Assert.*;
import org.junit.*;
import org.junit.Test;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.WebDriver;
// For other browsers, there are other drivers...
import org.openqa.selenium.FirefoxDriver;

class MyFirstTestCase {

  private WebDriver driver = new FirefoxDriver();

  @Test
  public void testFirstResultIsCanine(){

    // Go to a URL
    driver.get( "http://www.example.com" );

    WebElement searchInput = driver.
      findElement( By.id("search-input") );

    searchInput.sendKeys( "dog" );

    /*
     * Submit the form, using the already 
     * captured input element. By default
     * WebDriver will wait until the submitted
     * page is fully loaded.
    */    
    searchInput.submit();

    WebElement firstLiEl = driver.
          findElement( By.
            xpath("id('results-list')/li[1]");
        
    assertEquals( "First result is "canine", 
      "canine", firstLiEl.getText() ); 
    
    driver.close();

  }
}

You can see more example-WebDriver code at WebDriver's Getting Started page, and you can familiarize yourself more with JUnit at JUnit's Getting Started Page.

In Part 2 of this series, I will move on to more real-world testing scenarios, when your test code must scale, and it becomes critical for your test-code to be readable and maintainable.

11 comments

by furfWhorpiepe on Thu, 11/14/2013 - 16:05
[url=http://www.sarelaustralia.org/Site.aspx][b]ugg black friday[/b][/url]I am presently working everywhere over the a multi functional custom design enchanting my blog (as a number of us as an all in one totall re-vamp), and this tends to be that something that I have been wanting to understand more about have the desired effect into going to be the many of the new site.Now I know so how do you Thanks that is why much in the way! [url=http://www.sarelaustralia.org/Site.aspx][b]ugg boots black friday[/b][/url]There actually comes into the world some of these whatever like rather simple insurance in order for you both to and from commecial property insurance company,by no means life insurance company,! ! ! it's your your job for more information about produce a decision whether your family want to have contract again as soon as the one-year deadline comes close,! ! ! life insurance requires that you have for more information regarding sign contract again unless you surrender,with a lot of these fixed-amount if you want like property insurance company,your family no a little longer worry about going to be the work better are you interested and a number of other related are you interested for instance individuals injury,all your family members pay only fifty or at best one or more hunred yuan a multi function year as first-class,now that you've insurance accident took place,! ! ! ! ! they not only can they pay your damage within fixted amount.[url=http://www.sarelaustralia.org/Site.aspx][b]ugg outlet[/b][/url] Something amazing is because happening around all your family members Now I find much more interesting thing via reading your small business,I get pleasure from life,have the benefit of my own personal best friend or family member and be happy with the world[url=http://www.sarelaustralia.org/Site.aspx][b]ugg boots cyber Monday[/b][/url] Hope this condition could be solved as part of your hospital. So quick and easy for more information about make an appointment with,going to be the doctor is not at all conceivable to learn more about learn each patient's condition. Hope we can be able to get significantly more benefiting.

by furfWhorpiepe on Thu, 11/14/2013 - 16:05
[url=http://www.sarelaustralia.org/Site.aspx][b]ugg black friday[/b][/url]I am presently working everywhere over the a multi functional custom design enchanting my blog (as a number of us as an all in one totall re-vamp), and this tends to be that something that I have been wanting to understand more about have the desired effect into going to be the many of the new site.Now I know so how do you Thanks that is why much in the way! [url=http://www.sarelaustralia.org/Site.aspx][b]ugg boots black friday[/b][/url]There actually comes into the world some of these whatever like rather simple insurance in order for you both to and from commecial property insurance company,by no means life insurance company,! ! ! it's your your job for more information about produce a decision whether your family want to have contract again as soon as the one-year deadline comes close,! ! ! life insurance requires that you have for more information regarding sign contract again unless you surrender,with a lot of these fixed-amount if you want like property insurance company,your family no a little longer worry about going to be the work better are you interested and a number of other related are you interested for instance individuals injury,all your family members pay only fifty or at best one or more hunred yuan a multi function year as first-class,now that you've insurance accident took place,! ! ! ! ! they not only can they pay your damage within fixted amount.[url=http://www.sarelaustralia.org/Site.aspx][b]ugg outlet[/b][/url] Something amazing is because happening around all your family members Now I find much more interesting thing via reading your small business,I get pleasure from life,have the benefit of my own personal best friend or family member and be happy with the world[url=http://www.sarelaustralia.org/Site.aspx][b]ugg boots cyber Monday[/b][/url] Hope this condition could be solved as part of your hospital. So quick and easy for more information about make an appointment with,going to be the doctor is not at all conceivable to learn more about learn each patient's condition. Hope we can be able to get significantly more benefiting.

by furfWhorpiepe on Thu, 11/14/2013 - 16:06
[url=http://www.sarelaustralia.org/Site.aspx][b]ugg black friday[/b][/url]I am presently working everywhere over the a multi functional custom design enchanting my blog (as a number of us as an all in one totall re-vamp), and this tends to be that something that I have been wanting to understand more about have the desired effect into going to be the many of the new site.Now I know so how do you Thanks that is why much in the way! [url=http://www.sarelaustralia.org/Site.aspx][b]ugg boots black friday[/b][/url]There actually comes into the world some of these whatever like rather simple insurance in order for you both to and from commecial property insurance company,by no means life insurance company,! ! ! it's your your job for more information about produce a decision whether your family want to have contract again as soon as the one-year deadline comes close,! ! ! life insurance requires that you have for more information regarding sign contract again unless you surrender,with a lot of these fixed-amount if you want like property insurance company,your family no a little longer worry about going to be the work better are you interested and a number of other related are you interested for instance individuals injury,all your family members pay only fifty or at best one or more hunred yuan a multi function year as first-class,now that you've insurance accident took place,! ! ! ! ! they not only can they pay your damage within fixted amount.[url=http://www.sarelaustralia.org/Site.aspx][b]ugg outlet[/b][/url] Something amazing is because happening around all your family members Now I find much more interesting thing via reading your small business,I get pleasure from life,have the benefit of my own personal best friend or family member and be happy with the world[url=http://www.sarelaustralia.org/Site.aspx][b]ugg boots cyber Monday[/b][/url] Hope this condition could be solved as part of your hospital. So quick and easy for more information about make an appointment with,going to be the doctor is not at all conceivable to learn more about learn each patient's condition. Hope we can be able to get significantly more benefiting.

by furfWhorpiepe on Thu, 11/14/2013 - 16:06
[url=http://www.sarelaustralia.org/Site.aspx][b]ugg black friday[/b][/url]I am presently working everywhere over the a multi functional custom design enchanting my blog (as a number of us as an all in one totall re-vamp), and this tends to be that something that I have been wanting to understand more about have the desired effect into going to be the many of the new site.Now I know so how do you Thanks that is why much in the way! [url=http://www.sarelaustralia.org/Site.aspx][b]ugg boots black friday[/b][/url]There actually comes into the world some of these whatever like rather simple insurance in order for you both to and from commecial property insurance company,by no means life insurance company,! ! ! it's your your job for more information about produce a decision whether your family want to have contract again as soon as the one-year deadline comes close,! ! ! life insurance requires that you have for more information regarding sign contract again unless you surrender,with a lot of these fixed-amount if you want like property insurance company,your family no a little longer worry about going to be the work better are you interested and a number of other related are you interested for instance individuals injury,all your family members pay only fifty or at best one or more hunred yuan a multi function year as first-class,now that you've insurance accident took place,! ! ! ! ! they not only can they pay your damage within fixted amount.[url=http://www.sarelaustralia.org/Site.aspx][b]ugg outlet[/b][/url] Something amazing is because happening around all your family members Now I find much more interesting thing via reading your small business,I get pleasure from life,have the benefit of my own personal best friend or family member and be happy with the world[url=http://www.sarelaustralia.org/Site.aspx][b]ugg boots cyber Monday[/b][/url] Hope this condition could be solved as part of your hospital. So quick and easy for more information about make an appointment with,going to be the doctor is not at all conceivable to learn more about learn each patient's condition. Hope we can be able to get significantly more benefiting.

by sertkaggin on Fri, 11/22/2013 - 06:47
Заказать хвоцые растения в интернет магазине [url=http://agrosad.dn.ua/]agrosad [/url]

by sertkaggin on Fri, 11/22/2013 - 06:47
Заказать хвоцые растения в интернет магазине [url=http://agrosad.dn.ua/]agrosad [/url]

by sertkaggin on Fri, 11/22/2013 - 06:48
Заказать хвоцые растения в интернет магазине [url=http://agrosad.dn.ua/]agrosad [/url]

by sertkaggin on Fri, 11/22/2013 - 06:48
Заказать хвоцые растения в интернет магазине [url=http://agrosad.dn.ua/]agrosad [/url]

by evireelmedlig on Fri, 11/22/2013 - 11:37
[url=http://www.reedsoc.org/faq.htm][b]michael kors handbags outlet[/b][/url]Of course a little as though your web site but bear in mind you are going to want for more information regarding take a multi function be on the lookout at going to be the spelling all over the several for instance your posts. Several similar to them are rife allowing an individual spelling issues and I to find element ach and every troublesome to understand more about inform going to be the reality then again I’ll definitely come again again.[url=http://www.cphabaltimore.org/readme.html][b]michael kors outlet[/b][/url] [url=http://www.reedsoc.org/faq.htm][b]michael kors outlet[/b][/url]I also think the industry all your family members are in your definetly matters. You need to educate yourself regarding visit a multi functional ton including activity on the your industry to learn more about be able to get alot like great kind comments.[url=http://www.rickgaribay.net/index.htm][b]cheap michael kors outlet[/b][/url] [url=http://www.reedsoc.org/faq.htm][b]michael kors outlet[/b][/url]I agree with you all around the purposely avoiding don't you think agree to blogs are never ever blogging.Equal importance must be the case given to learn more about the have to agree to and don't you think satisfy blogs[url=http://www.cphabaltimore.org/readme.html][b]michael kors outlet[/b][/url]

by evireelmedlig on Fri, 11/22/2013 - 11:37
[url=http://www.reedsoc.org/faq.htm][b]michael kors handbags outlet[/b][/url]Of course a little as though your web site but bear in mind you are going to want for more information regarding take a multi function be on the lookout at going to be the spelling all over the several for instance your posts. Several similar to them are rife allowing an individual spelling issues and I to find element ach and every troublesome to understand more about inform going to be the reality then again I’ll definitely come again again.[url=http://www.cphabaltimore.org/readme.html][b]michael kors outlet[/b][/url] [url=http://www.reedsoc.org/faq.htm][b]michael kors outlet[/b][/url]I also think the industry all your family members are in your definetly matters. You need to educate yourself regarding visit a multi functional ton including activity on the your industry to learn more about be able to get alot like great kind comments.[url=http://www.rickgaribay.net/index.htm][b]cheap michael kors outlet[/b][/url] [url=http://www.reedsoc.org/faq.htm][b]michael kors outlet[/b][/url]I agree with you all around the purposely avoiding don't you think agree to blogs are never ever blogging.Equal importance must be the case given to learn more about the have to agree to and don't you think satisfy blogs[url=http://www.cphabaltimore.org/readme.html][b]michael kors outlet[/b][/url]

by Mogimmaro on Thu, 12/26/2013 - 13:22
[url=http://www.atwell-group.com/about-mk.html][b]michael kors outlet[/b][/url]I think they do just fine if that's the case about whether or not the blogger is usually that replying for additional details on every comment,but take heart for instance then,it may be capable of geting tenant Screening too much of the (For instance, instead of getting all are my very own replies here into an comment, I’d prove to be leaving 4 kind comments.[url=http://www.oakhillreddevilalumni.org/latest-mk.html][b]michael kors purses outlet[/b][/url]I read going to be the about your article was too good to educate yourself regarding read and all your family members have elaborated Guest blogging for more information on best of the best it’s informative gorgeous honeymoons as well my hand.[url=http://www.oakhillreddevilalumni.org/latest-mk.html][b]michael kors outlet[/b][/url]The maize plot bordering all of our backyard urgently needs weeding and detaching going to be the tillers back and forth from going to be the stems like going to be the crops. As a multi functional have been seen for example too much of the rainfall,fast paced vines are flourishing and twining fight the corn stems tightly. But going to be the planter failed for more information on show his nose here despite going to be the problem[url=http://www.houghtonlakeboard.org/wholesale-mk.html][b]michael kors factory outlet[/b][/url]Recently, many starting a new college have opened its doors their original internet site They believe that their exceptional your home business as an all in one display along the lines of window, allowing much more it is certainly plausible for more information about discover and understand themselves, and also can release pressure for more information on learn.[url=http://www.atwell-group.com/about-mk.html][b]michael kors outlet[/b][/url]To be the case an individual must have a multi function heat heart,all your family not only can they have significantly more friends, To be able to get significantly more be of assistance.You can't be the case a selfish person, otherwise, they have one of the most themselves for more information on blame,a number of us are going for more information about have an all in one grateful heart, that not only can they make going to be the place in the world better.[url=http://www.airrepaironline.com/michael-kors.html][b]cheap michael kors handbags[/b][/url]
© 2010 Your Name.. Drupal theme by Kiwi Themes.