FocusFirstCommandTest.php in Drupal 9
File
core/tests/Drupal/FunctionalJavascriptTests/Ajax/FocusFirstCommandTest.php
View source
<?php
namespace Drupal\FunctionalJavascriptTests\Ajax;
use Drupal\FunctionalJavascriptTests\WebDriverTestBase;
class FocusFirstCommandTest extends WebDriverTestBase {
protected static $modules = [
'ajax_test',
];
protected $defaultTheme = 'stark';
public function testFocusFirst() {
$page = $this
->getSession()
->getPage();
$assert_session = $this
->assertSession();
$this
->drupalGet('ajax-test/focus-first');
$has_focus_id = $this
->getSession()
->evaluateScript('document.activeElement.id');
$this
->assertNotContains($has_focus_id, [
'edit-first-input',
'edit-first-container-input',
]);
$page
->pressButton('SelectorNothingTabbable');
$this
->assertNotNull($assert_session
->waitForElementVisible('css', '#edit-selector-has-nothing-tabbable[data-has-focus]'));
$has_focus_id = $this
->getSession()
->evaluateScript('document.activeElement.id');
$this
->assertEquals('edit-selector-has-nothing-tabbable', $has_focus_id);
$page
->pressButton('SelectorNotExist');
$this
->assertNotNull($assert_session
->waitForElementVisible('css', '#edit-selector-does-not-exist[data-has-focus]'));
$has_focus_id = $this
->getSession()
->evaluateScript('document.activeElement.id');
$this
->assertEquals('edit-selector-does-not-exist', $has_focus_id);
$page
->pressButton('focusFirstContainer');
$this
->assertNotNull($assert_session
->waitForElementVisible('css', '#edit-first-container-input[data-has-focus]'));
$has_focus_id = $this
->getSession()
->evaluateScript('document.activeElement.id');
$this
->assertEquals('edit-first-container-input', $has_focus_id);
$page
->pressButton('focusFirstForm');
$this
->assertNotNull($assert_session
->waitForElementVisible('css', '#ajax-test-focus-first-command-form #edit-first-input[data-has-focus]'));
$this
->assertNotNull($page
->find('css', '#ajax-test-focus-first-command-form #edit-second-input'));
$has_focus_id = $this
->getSession()
->evaluateScript('document.activeElement.id');
$this
->assertEquals('edit-first-input', $has_focus_id);
$page
->pressButton('SelectorMultipleMatches');
$this
->assertNotNull($assert_session
->waitForElementVisible('css', '#edit-inside-same-selector-container-1[data-has-focus]'));
$this
->assertNotNull($page
->findById('edit-inside-same-selector-container-2'));
$this
->assertNull($assert_session
->waitForElementVisible('css', '#edit-inside-same-selector-container-2[data-has-focus]'));
$has_focus_id = $this
->getSession()
->evaluateScript('document.activeElement.id');
$this
->assertEquals('edit-inside-same-selector-container-1', $has_focus_id);
$page
->pressButton('focusableContainerNotTabbableChildren');
$this
->assertNotNull($assert_session
->waitForElementVisible('css', '#focusable-container-without-tabbable-children[data-has-focus]'));
$has_focus_id = $this
->getSession()
->evaluateScript('document.activeElement.id');
$this
->assertEquals('focusable-container-without-tabbable-children', $has_focus_id);
}
}