Change record status: 
Project: 
Introduced in branch: 
9.1.x
Introduced in version: 
9.1.0
Description: 

Using UiHelperTrait::drupalPostForm() in functional tests is deprecated. Use a combination of ::drupalGet and ::submitForm instead.

::drupalPostForm() used to visit the URL specified in the first argument before posting the form with ::submitForm(). Please note that:

  1. ::submitForm does not allow a NULL $edit argument. If you do not need to fill in any field before submitting, pass an empty array.
  2. differently from ::drupalPostForm(), ::submitForm does not have a returned value. In case you were inspecting the output of ::drupalPostForm(), use WebAssert methods instead.

If you have a $path specified, first call ::drupalGet, then ::submitForm:

BEFORE:

    $this->drupalPostForm('admin/structure/block/add/test_settings_validation/classy', ['region' => 'content', 'settings[digits]' => 'abc'], 'Save block');

AFTER:

    $this->drupalGet('admin/structure/block/add/test_settings_validation/classy');
    $this->submitForm(['region' => 'content', 'settings[digits]' => 'abc'], 'Save block');

If you have a NULL $path, then your current page is already the one with the form to be submitted, so call ::submitForm directly:

BEFORE:

    $block = [];
    $block['id'] = strtolower($this->randomMachineName());
    $block['theme'] = 'classy';
    $block['region'] = 'content';
    $this->drupalPostForm('admin/structure/block/add/system_powered_by_block', $block, 'Save block');
    $this->drupalPostForm(NULL, [], 'Save blocks');

AFTER:

    $this->drupalGet('admin/structure/block/add/system_powered_by_block');
    $block = [];
    $block['id'] = strtolower($this->randomMachineName());
    $block['theme'] = 'classy';
    $block['region'] = 'content';
    $this->submitForm($block, 'Save block');
    $this->submitForm([], 'Save blocks');
Impacts: 
Module developers

Comments

ajv009’s picture

How to manage Ajax fields in browsertest?

cburschka’s picture

Note that because the replacement functions (::submitForm and ::drupalGet) have existed since Drupal 8.1 (in BrowserTestBase 8.1-8.6, and UiHelperTrait in 8.7-9.1), this deprecation can be fixed without losing compatibility with older Drupal core versions.