Check your version

This video covers a topic in Drupal 7 which may or may not be the version you're using. We're keeping this tutorial online as a courtesy to users of Drupal 7, but we consider it archived.

Alternate resources: 

Navigating a Site with SimpleTest's Browser

Video loading...

Join Drupalize.Me to watch this video

Join today and gain instant access to our entire video library.

Log in Sign up
  • 0:03
    Navigating a Site with SimpleTest's Browser
  • 0:05
    with Joe Shindelar
  • 0:07
    Since we're primarily focused
  • 0:09
    on writing functional tests, a lot of what we're going to be doing
  • 0:12
    is attempting to simulate a human interaction with our application.
  • 0:17
    As humans, we interact with web applications
  • 0:19
    by doing things like typing addresses into the URL bar,
  • 0:23
    or if we don't know the URL of a page,
  • 0:25
    maybe we click on a series of links in order
  • 0:27
    to navigate from one area of the application to another.

Navigating a Site with SimpleTest's Browser

Loading...

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

Downloads: 
Log in or sign up to download companion files.