AjaxColorFormTest.php in Examples for Developers 3.x
File
modules/form_api_example/tests/src/FunctionalJavascript/AjaxColorFormTest.php
View source
<?php
namespace Drupal\Tests\form_api_example\FunctionalJavascript;
use Drupal\FunctionalJavascriptTests\WebDriverTestBase;
use Drupal\Core\Url;
class AjaxColorFormTest extends WebDriverTestBase {
protected $defaultTheme = 'stark';
public static $modules = [
'form_api_example',
];
public function testModalForm() {
$this
->drupalGet(Url::fromRoute('form_api_example.ajax_color_demo'));
$assert = $this
->assertSession();
$page = $this
->getSession()
->getPage();
$this
->assertEmpty($page
->find('css', 'select[name="color"]'));
$color_matrix = [
'warm' => [
'red',
'orange',
'yellow',
],
'cool' => [
'blue',
'purple',
'green',
],
];
foreach ($color_matrix as $temperature => $colors) {
$this
->assertNotEmpty($color_temperature = $page
->find('css', 'select[name="temperature"]'));
$color_temperature
->setValue($temperature);
$assert
->assertWaitOnAjaxRequest();
$this
->assertNotEmpty($color_select = $page
->find('css', 'select[name="color"]'));
$this
->assertNotEmpty($color_options = $color_select
->findAll('css', 'option'));
$this
->assertCount(count($colors), $color_options);
foreach ($color_options as $color_element) {
$this
->assertContains($color_element
->getValue(), $colors);
}
foreach ($colors as $color) {
$page
->find('css', 'select[name="temperature"]')
->setValue($temperature);
$assert
->assertWaitOnAjaxRequest();
$page
->find('css', 'select[name="color"]')
->setValue($color);
$page
->findButton('Submit')
->click();
$assert
->pageTextContains("Value for Temperature: {$temperature}");
$assert
->pageTextContains("Value for color: {$color}");
}
}
$this
->getSession()
->getPage()
->find('css', 'select[name="temperature"]')
->setValue('');
$assert
->assertWaitOnAjaxRequest();
$this
->assertEmpty($page
->find('css', 'select[name="color"]'));
}
}