1

I'm writing front end tests for a custom webform. These tests work fine when testing on the webform page of the form.

$this->drupalGet('/webform/custom_form'); 
$this->assertResponse(200);

However, testing the form using a custom theme and the page that the form is displayed on, does not work and the assertion fails returning a 404 response.

<?php


namespace Drupal\Tests\test_module\Kernel;


use Drupal\Tests\BrowserTestBase;
use Drupal\Tests\webform\Functional\Element\WebformElementBrowserTestBase;

class WebformValidationTest extends WebformElementBrowserTestBase {

  public static $modules = [
    'webform',
    'test_module',
  ];

  public static $testWebforms = ['custom_form'];

  protected $defaultTheme = 'custom_theme';

  /**
   * Test custom_form page from site with custom_theme theme install.
   *
   * @throws \Exception
   */
  public function testThemeFormPage() {
    $this->drupalGet('/page-that-displays-form');
    $this->assertResponse(200);
  }
}

I have also tried creating a submodule that imports the full config and installs all dependent modules from the site. This also did not work.

Is writing front end tests for specific nodes something that is possible with Drupal Browser Tests? If not, how does one write tests for webforms (validation, fields, display, etc)?

Thanks.

2 Answers 2

2

I Would recommend reading https://www.previousnext.com.au/blog/introducing-drupal-testing-traits-drupal-extension-testing-existing-sites

in this article it explains using https://gitlab.com/weitzman/drupal-test-traits

in this way you can do...

use weitzman\DrupalTestTraits\ExistingSiteBase;

/**
 * Test webform on theme
 */
class WebformValidationTest extends ExistingSiteBase
{
    public function testThemeFormPage()
    {
        $this->drupalGet('/page-that-displays-form');
        // Assert stuff here.
    }
}
1

It is possible to get a custom theme to load during a test.

Checkout the webform_bootstrap_test_theme included in the core Webform module. Make sure to includes all the needed blocks.

You can use the WebformNodeBrowserTestTrait to create a webform node during a test.

Still, it is worth checking out DrupalTestTraits.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.