Skip to content Skip to navigation

Behat Custom Step Definition: Wait for Batch API to Finish

If you're using Behat and the Drupal Extension, you might find the following code snippet helpful if you want to add a step to wait for batch jobs to finish.

If one of your Behat scenarios kicks off a batch job (e.g., a Feeds import), and you want to wait for that batch job to finish before moving on to the next step, add this step definition in your FeatureContext.php file:

  /**
   * Wait until the id="updateprogress" element is gone,
   * or timeout after 3 minutes (180,000 ms).
   *
   * @Given /^I wait for the batch job to finish$/
   */
  public function iWaitForTheBatchJobToFinish() {
    $this->getSession()->wait(180000, 'jQuery("#updateprogress").length === 0');
  }

Then, in your featurename.feature file, you can call this step like so:

When I press the "Save" button
And I wait for the batch job to finish
Then I should see "created"

This will cause the web driver to wait until the batch job is finished (or, more accurately, to wait until there is no longer an id="updateprogress" element on the page), or else timeout after 3 minutes (180000 ms). You can adjust the timeout to whatever you want by changing that 1800000 number. You will have to use the @javascript context in your feature to use this step definition.

(Note that a request to add this step definition to the Behat Drupal Extension has been submitted.)

See The Code

Categories: