You are here

public function AjaxWizardTest::testWizardSteps in Examples for Developers 3.x

Same name and namespace in other branches
  1. 8 ajax_example/tests/src/FunctionalJavascript/AjaxWizardTest.php \Drupal\Tests\ajax_example\FunctionalJavascript\AjaxWizardTest::testWizardSteps()

Test that we can successfully submit the wizard following the steps.

It verifies that each step displays the correct fields and we can finally see the success message once we submit the form.

File

modules/ajax_example/tests/src/FunctionalJavascript/AjaxWizardTest.php, line 32

Class

AjaxWizardTest
Test the user interactions for the Ajax Wizard example.

Namespace

Drupal\Tests\ajax_example\FunctionalJavascript

Code

public function testWizardSteps() {

  // Get our Mink stuff.
  $session = $this
    ->getSession();
  $page = $session
    ->getPage();
  $assert = $this
    ->assertSession();

  // Get the page.
  $form_url = Url::fromRoute('ajax_example.wizard');
  $this
    ->drupalGet($form_url);

  // Check our initial state.
  $assert
    ->fieldExists('name');
  $assert
    ->fieldNotExists('address');
  $assert
    ->fieldNotExists('city');
  $assert
    ->buttonExists('Next step');
  $assert
    ->buttonNotExists('Previous step');
  $assert
    ->buttonNotExists('Submit your information');

  // We fill the  name at first step and continue with the second step.
  $page
    ->fillField('name', 'José Saramago');
  $page
    ->pressButton('Next step');
  $assert
    ->assertWaitOnAjaxRequest();

  // Check the state of the second step.
  $assert
    ->fieldExists('address');
  $assert
    ->fieldNotExists('name');
  $assert
    ->fieldNotExists('city');
  $assert
    ->buttonExists('Next step');
  $assert
    ->buttonExists('Previous step');
  $assert
    ->buttonNotExists('Submit your information');

  // We fill the address at the second step and continue with the last step.
  $page
    ->fillField('address', 'Rua dos Bacalhoeiros, 10');
  $page
    ->pressButton('Next step');
  $assert
    ->assertWaitOnAjaxRequest();

  // Check the state of the third step.
  $assert
    ->fieldExists('city');
  $assert
    ->fieldNotExists('name');
  $assert
    ->fieldNotExists('address');
  $assert
    ->buttonNotExists('Next step');
  $assert
    ->buttonExists('Previous step');
  $assert
    ->buttonExists('Submit your information');

  // We fill the city at the third step and we finally submit the form.
  $page
    ->fillField('city', 'Lisboa');
  $page
    ->pressButton('Submit your information');

  // We check the output and assert that the already set values are displayed.
  $assert
    ->pageTextContains('Your information has been submitted:');
  $assert
    ->pageTextContains('Name: José Saramago');
  $assert
    ->pageTextContains('Address: Rua dos Bacalhoeiros, 10');
  $assert
    ->pageTextContains('City: Lisboa');
}