executeScript example

$web_assert->elementNotContains('css', static::ANNOUNCE_SELECTOR, 'Tabbing is constrained to a set of');
  }

  /** * Gets the number of elements that are tabbable. * * @return int * The number of tabbable elements. */
  protected function getTabbableElementsCount() {
    // Mark all tabbable elements.     $this->getSession()->executeScript("jQuery(window.tabbable.tabbable(document.body)).attr('data-marked', '');");
    // Count all marked elements.     $count = count($this->getSession()->getPage()->findAll('css', "[data-marked]"));
    // Remove set attributes.     $this->getSession()->executeScript("jQuery('[data-marked]').removeAttr('data-marked');");
    return $count;
  }

}
'access contextual links',
      'administer nodes',
      'administer blocks',
      'administer views',
      'edit any page content',
    ]));
    // Ensure same contextual links work correct with fresh and cached page.     foreach (['fresh', 'cached'] as $state) {
      $this->drupalGet('user');
      $contextual_id = '[data-contextual-id^="node:node=1"]';
      $this->assertJsCondition("(typeof jQuery !== 'undefined' && jQuery('[data-contextual-id]:empty').length === 0)");
      $this->getSession()->executeScript("jQuery('#block-first $contextual_id .trigger').trigger('click');");
      $contextual_links = $this->assertSession()->waitForElementVisible('css', "#block-first $contextual_id .contextual-links");
      $this->assertTrue($contextual_links->isVisible(), "Contextual links are visible with $state page.");
    }
  }

}
(function ($) { var selection = document.querySelectorAll('.js-media-library-selection'); selection.forEach(function (widget) { $(widget).children().each(function (index, child) { $(child).find('.js-media-library-item-weight').val(index); }); }); })(jQuery) JS;

    $this->getSession()->executeScript($script);
  }

  /** * Tests the preview displayed by the field widget. */
  public function testWidgetPreview() {
    $assert_session = $this->assertSession();
    $page = $this->getSession()->getPage();

    $node = $this->drupalCreateNode([
      'type' => 'basic_page',
      
    $this->waitForOffCanvasToClose();

    $this->enableEditMode();

    // Open block form by clicking an element inside the block.     // This confirms that default action for links and form elements is     // suppressed.     $this->openBlockForm("$block_selector {$element_selector}", $block_selector);
    $web_assert->elementTextContains('css', '.contextual-toolbar-tab button', 'Editing');
    $web_assert->elementAttributeContains('css', '.dialog-off-canvas-main-canvas', 'class', 'js-settings-tray-edit-mode');
    // Simulate press the Escape key.     $this->getSession()->executeScript('jQuery("body").trigger(jQuery.Event("keyup", { keyCode: 27 }));');
    $this->waitForOffCanvasToClose();
    $this->getSession()->wait(100);
    $this->getSession()->getPage()->find('css', static::TOOLBAR_EDIT_LINK_SELECTOR)->mouseOver();
    $this->assertEditModeDisabled();
    $this->assertNotEmpty($web_assert->waitForElement('css', '#drupal-live-announce:contains(Exited edit mode)'));
    $web_assert->assertNoElementAfterWait('css', '.contextual-toolbar-tab button:contains(Editing)');
    $web_assert->elementAttributeNotContains('css', '.dialog-off-canvas-main-canvas', 'class', 'js-settings-tray-edit-mode');

    // Clean up test data so each test does not impact the next.     $block->delete();
    if ($permissions) {
      
$this->webAssert->pageTextNotContains($field->label());

    // Have the field appear on the registration form.     $user_registration_form->setComponent('test_user_field', ['type' => 'test_field_widget'])->save();

    $this->drupalGet('user/register');
    $this->webAssert->pageTextContains($field->label());

    // In order to check the server side validation the native browser     // validation for required fields needs to be circumvented.     $session = $this->getSession();
    $session->executeScript("jQuery('#edit-test-user-field-0-value').prop('required', false);");

    // Check that validation errors are correctly reported.     $name = $this->randomMachineName();
    $this->page->fillField('edit-name', $name);
    $this->page->fillField('edit-mail', $name . '@example.com');

    $this->page->pressButton('edit-submit');
    $this->webAssert->pageTextContains($field->label() . ' field is required.');

    // Invalid input.     $this->page->fillField('edit-test-user-field-0-value', '-1');
    

  protected function showHiddenFields() {
    $script = <<<JS var hidden_fields = document.querySelectorAll(".hidden"); [].forEach.call(hidden_fields, function(el) { el.classList.remove("hidden"); }); JS;

    $this->getSession()->executeScript($script);
  }

}
$child_1 = $page->find('css', $child_1_selector);

    $child_2_selector = '#edit-element-2';
    $child_2 = $page->find('css', $child_2_selector);

    // Assert that the child in the second vertical tab becomes visible.     // It should be visible after initial load due to the fragment in the URI.     $this->assertTrue($child_2->isVisible(), 'Child 2 is visible due to a URI fragment');

    // Click on a fragment link pointing to an invisible child inside an     // inactive vertical tab.     $session->executeScript("jQuery('<a href=\"$child_1_selector\"></a>').insertAfter('h1')[0].click()");

    // Assert that the child in the first vertical tab becomes visible.     $web_assert->waitForElementVisible('css', $child_1_selector, 50);

    // Trigger a URI fragment change (hashchange) to show the second vertical     // tab again.     $session->executeScript("location.replace('$child_2_selector')");

    // Assert that the child in the second vertical tab becomes visible again.     $web_assert->waitForElementVisible('css', $child_2_selector, 50);

    
$this->assertSame($expected_form_id$form_id_element->getValue());
    $this->assertTrue($form_id_element->getParent()->isVisible());
  }

  /** * Marks the page to assist determining if the page has been reloaded. * * @todo Remove in https://www.drupal.org/project/drupal/issues/2909782. */
  private function markCurrentPage() {
    $this->pageReloadMarker = $this->randomMachineName();
    $this->getSession()->executeScript('document.body.appendChild(document.createTextNode("' . $this->pageReloadMarker . '"));');
  }

  /** * Asserts that the page has not been reloaded. * * @todo Remove in https://www.drupal.org/project/drupal/issues/2909782. */
  private function assertPageNotReloaded(): void {
    $this->assertSession()->pageTextContains($this->pageReloadMarker);
  }

}


  /** * Toggles the visibility of a contextual trigger. * * @param string $selector * The selector for the element that contains the contextual link. */
  protected function toggleContextualTriggerVisibility($selector) {
    // Hovering over the element itself with should be enough, but does not     // work. Manually remove the visually-hidden class.     $this->getSession()->executeScript("jQuery('{$selector} .contextual .trigger').toggleClass('visually-hidden');");
  }

}
// The Source Editing plugin settings form should now be present and should       // have no allowed tags configured.       $page->clickLink('Source editing');
      $this->assertNotNull($assert_session->waitForElementVisible('css', '[data-drupal-selector="edit-editor-settings-plugins-ckeditor5-sourceediting-allowed-tags"]'));

      $javascript = <<<JS const allowedTags = document.querySelector('[data-drupal-selector="edit-editor-settings-plugins-ckeditor5-sourceediting-allowed-tags"]'); allowedTags.value = '<span>'; allowedTags.dispatchEvent(new Event('input')); JS;
      $this->getSession()->executeScript($javascript);
    }
    $this->assertNotEmpty($assert_session->waitForElement('css', ".ckeditor5-toolbar-item-$toolbar_item_name"));
    $this->triggerKeyUp(".ckeditor5-toolbar-item-$toolbar_item_name", 'ArrowDown');
    $assert_session->assertWaitOnAjaxRequest();
    $this->saveNewTextFormat($page$assert_session);

    ConfigurableLanguage::createFromLangcode($langcode)->save();
    $this->config('system.site')->set('default_langcode', $langcode)->save();

    $this->drupalGet('node/add');
    $this->assertNotEmpty($assert_session->waitForElement('css', '.ck-editor'));
    
// Test that selecting elements as a part of bulk operations works.     $page->selectFieldOption('Media type', '- Any -');
    $assert_session->elementExists('css', '#views-exposed-form-media-library-page')->submit();
    $this->waitForText('Dog');

    // Select the "Delete media" action.     $page->selectFieldOption('Action', 'Delete media');
    $this->waitForText('Dog');

    // This tests that anchor tags clicked inside the preview are suppressed.     $this->getSession()->executeScript('jQuery(".js-click-to-select-trigger a")[4].click()');
    $this->submitForm([], 'Apply to selected items');
    $assert_session->pageTextContains('Dog');
    $assert_session->pageTextNotContains('Cat');
    // For reasons that are not clear, deleting media items by pressing the     // "Delete" button can fail (the button is found, but never actually pressed     // by the Mink driver). This workaround allows the delete form to be     // submitted.     $assert_session->elementExists('css', 'form')->submit();
    $assert_session->pageTextNotContains('Dog');
    $assert_session->pageTextContains('Cat');

    
$this->assertWaitPageContains('<tr id="table-first" class="odd"><td>first row</td></tr>');
    $this->assertWaitPageContains('<tr class="even"><td>second row</td></tr>');

    // Tests the 'settings' command.     $test_settings_command = <<<JS Drupal.behaviors.testSettingsCommand = { attach: function (context, settings) { window.jQuery('body').append('<div class="test-settings-command">' + settings.ajax_forms_test.foo + '</div>'); } }; JS;
    $session->executeScript($test_settings_command);
    // @todo: Replace after https://www.drupal.org/project/drupal/issues/2616184     $session->executeScript('window.jQuery("#edit-settings-command-example").mousedown();');
    $this->assertWaitPageContains('<div class="test-settings-command">42</div>');
  }

  /** * Tests the various Ajax Commands with legacy parameters. * @group legacy */
  public function testLegacyAjaxCommands() {
    $session = $this->getSession();
    
// Manually call the method under test, as it cannot be caught by PHPUnit     // when triggered from assertPostConditions().     $this->failOnJavaScriptErrors();
  }

  /** * Clear the JavaScript error log to prevent this test failing for real. * * @postCondition */
  public function clearErrorLog() {
    $this->getSession()->executeScript("sessionStorage.removeItem('js_testing_log_test.errors')");
  }

}
$this->triggerKeyUp('.ckeditor5-toolbar-item-sourceEditing', 'ArrowDown');
    $assert_session->assertWaitOnAjaxRequest();
    // The Source Editing plugin settings form should now be present and should     // have no allowed tags configured.     $page->clickLink('Source editing');
    $this->assertNotNull($assert_session->waitForElementVisible('css', '[data-drupal-selector="edit-editor-settings-plugins-ckeditor5-sourceediting-allowed-tags"]'));
    $javascript = <<<JS const allowedTags = document.querySelector('[data-drupal-selector="edit-editor-settings-plugins-ckeditor5-sourceediting-allowed-tags"]'); allowedTags.value = '<span>'; allowedTags.dispatchEvent(new Event('input')); JS;
    $this->getSession()->executeScript($javascript);
    // Dispatching an `input` event does not work in WebDriver. Enabling another     // toolbar item which has no associated HTML elements forces it.     $this->triggerKeyUp('.ckeditor5-toolbar-item-undo', 'ArrowDown');
    $assert_session->assertWaitOnAjaxRequest();

    // Confirm there are no longer any warnings.     $assert_session->waitForElementRemoved('css', '[data-drupal-messages] [role="alert"]');

    // The language plugin config form should now be present.     $assert_session->elementExists('css', '[data-drupal-selector="edit-editor-settings-plugins-ckeditor5-language"]');

    

  public function testGlobalEvents() {
    $session = $this->getSession();
    $assert = $this->assertSession();
    $expected_event_order = implode('', ['ajaxSuccess', 'ajaxComplete', 'ajaxStop']);

    $this->drupalGet('ajax-test/global-events');

    // Ensure that a non-Drupal Ajax request triggers the expected events, in     // the correct order, a single time.     $session->executeScript('jQuery.get(Drupal.url("core/COPYRIGHT.txt"))');
    $assert->assertWaitOnAjaxRequest();
    $assert->elementTextEquals('css', '#test_global_events_log', $expected_event_order);
    $assert->elementTextEquals('css', '#test_global_events_log2', $expected_event_order);

    // Ensure that an Ajax request to a Drupal Ajax response, but that was not     // initiated with Drupal.Ajax(), triggers the expected events, in the     // correct order, a single time. We expect $expected_event_order to appear     // twice in each log element, because Drupal Ajax response commands (such     // as the one to clear the log element) are only executed for requests     // initiated with Drupal.Ajax(), and these elements already contain the     // text that was added above.
Home | Imprint | This part of the site doesn't use cookies.