Module Development

Navigating a Site with SimpleTest's Browser for Drupal 7

This page is archived

We're keeping this page up as a courtesy to folks who may need to refer to old instructions. We don't plan to update this page.

Alternate resources

Sprout Video

If we're going to do functional testing of the various pages in our application we'll need to be able to navigate to the pages in question. The DrupalWebTestCase's built in browser provides us with two different ways to navigate our site. In this lesson we'll explore using the DrupalWebTestCase'::drupalGet(), and DrupalWebTestCase::clickLink() functions in order to navigate throughout our application. The former is useful when we know the exact URL of the page we want to navigate to and the latter is useful when we don't necessarily know the URL but we know which link to follow to get there.

I like to think of DrupalWebTestCase::drupalGet() as typing an address into the URL bar in my browser. I know the page I want to navigate to so take me right to it. DrupalWebTestCase::drupalGet() can be used inside a test case like so:


public function testHelloWorld() {
  $this->drupalGet('helloworld');
}

This points the SimpleTest internal browser to the helloworld page on our site, and loads the HTML content of that page into the buffer so we can perform assertions on its content.

DrupalWebTestCase::clickLink() on the other hand is more like looking at the content of the page, reading through the links in the navigation menu and then deciding that you would like to click on the one labeled "Hello World". You don't know what address it's going to take you to, but that's okay because what matters is that you end up on the page. DrupalWebTestCase::clickLink() operates by scanning the SimpleTest browsers buffer of the current page's HTML content for any link whose label matches the value provided.


public function testHelloWorld() {
  $this->clickLink('Hello World');
}

This would have the effect of clicking on the following link:


<a href="helloworld">Hello World</a>

Or really, just extracting the value of the href attribute from that a tag and feeding it to DrupalWebTestCase::drupalGet().

I find the best reference for these helper methods is located in the Drupal.org handbook. https://www.drupal.org/node/265762

Additional resources

SimpleTest helper method reference

DrupalWebTestCase API documenation