waitFor example


  private function waitForHelper(int $timeout, callable $callback) {
    return $this->session->getPage()->waitFor($timeout / 1000, $callback);
  }

  /** * Waits for the button specified by the locator and returns it. * * @param string $locator * The button ID, value or alt string. * @param int $timeout * (Optional) Timeout in milliseconds, defaults to 10000. * * @return \Behat\Mink\Element\NodeElement|null * The page element node if found, NULL if not. */
// Test the same functionality on multiple themes.     foreach ($this->getTestThemes() as $theme) {
      $this->enableTheme($theme);
      // Testing at the wider width.       $this->getSession()->resizeWindow($narrow_width_breakpoint + $offset$height);
      $this->drupalGet('/off-canvas-test-links');
      $this->assertFalse($page->find('css', '.dialog-off-canvas-main-canvas')->hasAttribute('style'), 'Body not padded on wide page load.');
      $page->clickLink("Open side panel 1");
      $this->waitForOffCanvasToOpen();
      // Check that the main canvas is padded when page is not narrow width and       // tray is open.       $page->waitFor(10, function D$page) {
        return $page->find('css', '.dialog-off-canvas-main-canvas')->hasAttribute('style');
      });
      $web_assert->elementAttributeContains('css', '.dialog-off-canvas-main-canvas', 'style', 'padding-right');

      // Testing at the narrower width.       $this->getSession()->resizeWindow($narrow_width_breakpoint - $offset$height);
      $this->drupalGet('/off-canvas-test-links');
      $this->assertFalse($page->find('css', '.dialog-off-canvas-main-canvas')->hasAttribute('style'), 'Body not padded on narrow page load.');
      $page->clickLink("Open side panel 1");
      $this->waitForOffCanvasToOpen();
      $this->assertFalse($page->find('css', '.dialog-off-canvas-main-canvas')->hasAttribute('style'), 'Body not padded on narrow page with tray open.');
    }

  protected function markRowHandleForDragging(NodeElement $handle) {
    $class = self::DRAGGING_CSS_CLASS;
    $script = <<<JS document.evaluate("{$handle->getXpath()}", document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null) .singleNodeValue.classList.add('{$class}'); JS;

    $this->getSession()->executeScript($script);
    $has_class = $this->getSession()->getPage()->waitFor(1, function D) use ($handle$class) {
      return $handle->hasClass($class);
    });

    if (!$has_class) {
      throw new \Exception(sprintf('Dragging css class was not added on handle "%s".', $handle->getXpath()));
    }
  }

  /** * Waits until the dragging operations are finished on a row handle. * * @param \Behat\Mink\Element\NodeElement $handle * The draggable row handle element. * * @throws \Exception * Thrown when the dragging operations are not completed on time. */
$edit_button->mouseOver();
    $edit_button->press();
  }

  /** * Assert that edit mode has been properly disabled. */
  protected function assertEditModeDisabled() {
    $web_assert = $this->assertSession();
    $page = $this->getSession()->getPage();
    $page->find('css', static::TOOLBAR_EDIT_LINK_SELECTOR)->mouseOver();
    $this->assertTrue($page->waitFor(10, function D$page) {
      return !$page->find('css', '.contextual .trigger:not(.visually-hidden)');
    }));
    // Contextual triggers should be hidden.     $web_assert->elementExists('css', '.contextual .trigger.visually-hidden');
    // No contextual triggers should be not hidden.     $web_assert->elementNotExists('css', '.contextual .trigger:not(.visually-hidden)');
    // The toolbar edit button should read "Edit".     $web_assert->elementContains('css', static::TOOLBAR_EDIT_LINK_SELECTOR, 'Edit');
    // The main canvas element should NOT have the "js-settings-tray-edit-mode"     // class.     $web_assert->elementNotExists('css', '.dialog-off-canvas-main-canvas.js-settings-tray-edit-mode');
  }
$this->drupalLogin($this->drupalCreateUser([
      'access content',
      'create article content',
    ]));

    $page = $this->getSession()->getPage();
    $this->drupalGet('node/add/article');
    $image_file = current($this->getTestFiles('image'));
    $image_path = $this->container->get('file_system')->realpath($image_file->uri);
    $page->attachFileToField('files[' . $field_name . '_0]', $image_path);
    $elements = $page->waitFor(10, function D) use ($page) {
      return $page->findAll('css', '.messages--error');
    });
    $this->assertCount(1, $elements, 'Ajax validation messages are displayed once.');
  }

}
$visible_categories = $this->filterVisibleElements($categories);
    $this->assertCount(1, $visible_categories);
    $this->assertAnnounceContains('1 block is available in the modified list.');

    // Test Drupal.announce() message when no matches are present.     $filter->setValue('Pan-Galactic Gargle Blaster');
    $visible_rows = $this->filterVisibleElements($blocks);
    $this->assertCount(0, $visible_rows);
    $visible_categories = $this->filterVisibleElements($categories);
    $this->assertCount(0, $visible_categories);
    $announce_element = $page->find('css', '#drupal-live-announce');
    $page->waitFor(2, function D) use ($announce_element) {
      return str_starts_with($announce_element->getText(), '0 blocks are available');
    });

    // Test Drupal.announce() message when all blocks are listed.     $filter->setValue('');
    $this->assertAnnounceContains('All available blocks are listed.');
    // Confirm the Content Fields category remains collapsed after filtering.     $this->assertFalse($promoteToFrontPageLink->isVisible());
  }

  /** * Removes any non-visible elements from the passed array. * * @param \Behat\Mink\Element\NodeElement[] $elements * An array of node elements. * * @return \Behat\Mink\Element\NodeElement[] * An array of visible node elements. */

  protected function clickContextualLink($selector$link_locator$force_visible = TRUE) {
    $page = $this->getSession()->getPage();
    $page->waitFor(10, function D) use ($page$selector) {
      return $page->find('css', "$selector .contextual-links");
    });

    if ($force_visible) {
      $this->toggleContextualTriggerVisibility($selector);
    }

    $element = $this->getSession()->getPage()->find('css', $selector);
    $element->find('css', '.contextual button')->press();
    $element->findLink($link_locator)->click();

    
    WebDriverCurlService::disableRetry();
    $count = 0;
    $wrapper = function DElement $element) use ($callback, &$count) {
      $count++;
      try {
        return call_user_func($callback$element);
      }
      catch (Exception $e) {
        return NULL;
      }
    };
    $result = parent::waitFor($timeout$wrapper);
    if (!$result && $count < 2) {
      // If the callback or the system is really slow, then it might have only       // fired once. In this case it is better to trigger it once more as the       // page state has probably changed while the callback is running.       return call_user_func($callback$this);
    }
    WebDriverCurlService::enableRetry();
    return $result;
  }

}
// Submit the "add more" button of each form twice. After each corresponding     // page update, ensure the same as above.
    for ($i = 0; $i < 2; $i++) {
      $forms = $page->findAll('xpath', $form_xpath);
      foreach ($forms as $offset => $form) {
        $button = $form->findButton('Add another item');
        $this->assertNotNull($button, 'Add Another Item button exists');
        $button->press();

        // Wait for field to be added with ajax.         $this->assertNotEmpty($page->waitFor(10, function D) use ($form$i) {
          return $form->findField('field_ajax_test[' . ($i + 1) . '][value]');
        }));

        // After AJAX request and response verify the correct number of text         // fields (including title), as well as the "Add another item" button.         $this->assertCount($i + 3, $form->findAll('css', 'input[type="text"]'), 'Found the correct number of field items after an AJAX submission.');
        $this->assertNotEmpty($form->findButton('Add another item'), 'Found the "add more" button after an AJAX submission.');
        $this->assertSession()->pageContainsNoDuplicateId();
      }
    }
  }

}
$add_button->click();

    $field_action = $web_assert->waitForField('options[default_action]');
    $this->assertNotEmpty($field_action);
    $field_action->setValue('default');

    $page->selectFieldOption('options[default_argument_type]', 'node');
    $add_button = $page->find('css', '.ui-dialog-buttonset .button--primary');
    $add_button->click();

    // Wait for the dialog to close.     $page->waitFor(10, function D) use ($page) {
      $field = $page->find('css', '.ui-dialog-buttonset .button--primary');
      return empty($field);
    });

    $page->pressButton('edit-actions-submit');

    $page->clickLink('Content: ID');
    // Check that the dialog opens.     $field_action = $web_assert->waitForField('options[default_action]');
    $this->assertNotEmpty($field_action);
  }

}
    $assert_session->fieldExists('Weight', $wrapper)->click();
    $wrapper->pressButton('Hide media item weights');

    // Assert the same has been added twice and remove the items again.     $this->waitForElementsCount('css', '.field--name-field-twin-media [data-media-library-item-delta]', 2);
    $assert_session->hiddenFieldValueEquals('field_twin_media[selection][0][target_id]', 4);
    $assert_session->hiddenFieldValueEquals('field_twin_media[selection][1][target_id]', 4);
    $wrapper->pressButton('Remove');
    $this->waitForText('Dog has been removed.');
    $wrapper->pressButton('Remove');
    $this->waitForText('Dog has been removed.');
    $result = $wrapper->waitFor(10, function D$wrapper) {
      /** @var \Behat\Mink\Element\NodeElement $wrapper */
      return $wrapper->findButton('Remove') == NULL;
    });
    $this->assertTrue($result);

    // Assert the selection is persistent in the media library modal, and     // the number of selected items is displayed correctly.     $this->openMediaLibraryForField('field_twin_media');
    // Assert the number of selected items is displayed correctly.     $this->assertSelectedMediaCount('0 of 2 items selected');
    // Select a media item, assert the hidden selection field contains the ID of

  public function testWorkflowSummary() {
    $this->drupalGet('admin/structure/types/manage/test');
    $page = $this->getSession()->getPage();
    $page->find('css', 'a[href="#edit-workflow"]')->click();
    $this->assertSession()->waitForElementVisible('css', '[name="options[status]"]');
    $page->findField('options[status]')->uncheck();
    $page->findField('options[sticky]')->check();
    $page->findField('options[promote]')->check();
    $page->findField('options[revision]')->check();
    $locator = '[href="#edit-workflow"] .vertical-tabs__menu-item-summary';
    $this->assertTrue($page->waitFor(10, function D) use ($page$locator) {
      $summary = $page->find('css', $locator)->getText();
      return str_contains($summary, 'Not published');
    }));
    $summary = $page->find('css', $locator)->getText();
    $this->assertEquals('Not published, Promoted to front page, Sticky at top of lists, Create new revision', $summary);
  }

}
// Load the saved image style.     $style = ImageStyle::load($style_name);

    // Edit back the effects.     foreach ($style->getEffects() as $uuid => $effect) {
      $effect_path = $admin_path . '/manage/' . $style_name . '/effects/' . $uuid;
      $this->drupalGet($effect_path);
      $page->findField('data[test_parameter]')->setValue(111);
      $ajax_value = $page->find('css', '#ajax-value')->getText();
      $this->assertSame('Ajax value bar', $ajax_value);
      $this->getSession()->getPage()->pressButton('Ajax refresh');
      $this->assertTrue($page->waitFor(10, function D$page) {
        $ajax_value = $page->find('css', '#ajax-value')->getText();
        return (bool) preg_match('/^Ajax value [0-9.]+ [0-9.]+$/', $ajax_value);
      }));
      $page->pressButton('Update effect');
      $assert->statusMessageContains('The image effect was successfully applied.', 'status');
    }
  }

}
/** * Asserts that page contains an expected value after waiting. * * @param string $expected * A needle text. * * @internal */
  protected function assertWaitPageContains(string $expected): void {
    $page = $this->getSession()->getPage();
    $this->assertTrue($page->waitFor(10, function D) use ($page$expected) {
      // Clear content from empty styles and "processed" classes after effect.       $content = str_replace([' class="processed"', ' processed', ' style=""'], '', $page->getContent());
      return stripos($content$expected) !== FALSE;
    }), "Page contains expected value: $expected");
  }

  /** * Tests that Ajax errors are visible in the UI. */
  public function testUiAjaxException() {
    $themes = [
      
$checkbox->click();
    $assert_session->hiddenFieldValueEquals('current_selection', $selected_item_id);
    $this->assertTrue($assert_session->fieldExists('Add files')->hasAttribute('multiple'));
    $png_uri_5 = $file_system->copy($png_image->uri, 'public://');
    $this->addMediaFileToField('Add files', $this->container->get('file_system')->realpath($png_uri_5));
    $this->assertMediaAdded();
    $page->fillField('Alternative text', $this->randomString());
    // Assert the pre-selected items are shown.     $selection_area = $this->getSelectionArea();
    $assert_session->checkboxChecked("Select $existing_media_name", $selection_area);
    $selection_area->uncheckField("Select $existing_media_name");
    $page->waitFor(10, function D) use ($page) {
      return $page->find('hidden_field_selector', ['hidden_field', 'current_selection'])->getValue() === '';
    });
    // Close the details element so that clicking the Save and select works.     // @todo Fix dialog or test so this is not necessary to prevent random     // fails. https://www.drupal.org/project/drupal/issues/3055648     $selection_area->find('css', 'summary')->click();
    $this->saveAnd('select');
    $this->waitForText("Select $existing_media_name");
    $media_items = Media::loadMultiple();
    $added_media = array_pop($media_items);
    $added_media_name = $added_media->label();
    
Home | Imprint | This part of the site doesn't use cookies.