I am currently using SimpleTest to test a complex multi-step form implementation in Drupal 7. It made sense to do it this way as there are a lot of factors involved that all need to be accounted for and automating what form elements appeared on what page was the most robust solution. In order to test how the form worked I needed to submit to the form once (using a $this->drupalPost() method) and then submit the form again using the same method. The tricky bit here was that when calling the drupalPost() method with a URL it first called drupalGet() on the URL before posting to the form. This basically meant that the form was initiated twice and never got past the second page.
The solution to this was pretty straightforward, but it took me a while to find it. Essentially, to stop the drupalPost() method from running drupalGet() first just supply the URL paramter (the first parameter) of drupalPost() as NULL. This skips the reloading of the form and posts the form state back to the form. Here is the code that will test the first two stages of a multistep form, I have left out the rest of the DrupalWebTestCase class.
- // post to first step of multistep form
- $this->drupalPost('first/step/in/multistep/form', array(), 'Next');
- // do assertions
- // Form element exists on second page of form.
- $this->assertFieldByName('form_element', '');
- // second step
- $input = array(
- 'form_element' => 'form_value',
- $this->drupalPost(NULL, $input, 'Next');
- // second step assertions
This method can also be used to run repeated submissions to a form when testing form validation.