assertFieldByXPath example

/** * Tests that elements are rendered properly. * * @internal */
  protected function assertRenderedElement(array $element, string $xpath, array $xpath_args = []): void {
    $this->render($element);

    $xpath = $this->buildXPathQuery($xpath$xpath_args);
    $element += ['#value' => NULL];
    $this->assertFieldByXPath($xpath$element['#value']new FormattableMarkup('#type @type was properly rendered.', [
      '@type' => var_export($element['#type'], TRUE),
    ]));
  }

}
public function testExposedFormRender() {
    $view = Views::getView('test_exposed_form_buttons');
    $this->executeView($view);
    $exposed_form = $view->display_handler->getPlugin('exposed_form');
    $output = $exposed_form->renderExposedForm();
    $this->setRawContent(\Drupal::service('renderer')->renderRoot($output));

    $this->assertFieldByXpath('//form/@id', Html::cleanCssIdentifier('views-exposed-form-' . $view->storage->id() . '-' . $view->current_display), 'Expected form ID found.');

    $view->setDisplay('page_1');
    $expected_action = $view->display_handler->getUrlInfo()->toString();
    $this->assertFieldByXPath('//form/@action', $expected_action, 'The expected value for the action attribute was found.');
    // Make sure the description is shown.     $result = $this->xpath('//form//div[contains(@id, "edit-type--2--description") and normalize-space(text())="Exposed description"]');
    $this->assertCount(1, $result, 'Filter description was found.');
  }

  /** * Tests the exposed form raw input. */
  public function testExposedFormRawInput() {
    $node_type = NodeType::create(['type' => 'article']);
    $node_type->save();

    
$message = new FormattableMarkup('Found field with name @name', [
          '@name' => var_export($name, TRUE),
        ]);
      }
      else {
        $message = new FormattableMarkup('Found field with name @name and value @value', [
          '@name' => var_export($name, TRUE),
          '@value' => var_export($value, TRUE),
        ]);
      }
    }
    return $this->assertFieldByXPath($this->constructFieldXpath('name', $name)$value$message);
  }

  /** * Asserts that a field does not exist with the given name and value. * * @param string $name * Name of field to assert. * @param string $value * (optional) Value for the field, to assert that the field's value on the * page doesn't match it. You may pass in NULL to skip checking the * value, while still checking that the field doesn't exist. However, the * default value ('') asserts that the field value is not an empty string. * @param string $message * (optional) A message to display with the assertion. Do not translate * messages: use \Drupal\Component\Render\FormattableMarkup to embed * variables in the message text, not t(). If left blank, a default message * will be displayed. * * @return bool * TRUE on pass, FALSE on fail. */
Home | Imprint | This part of the site doesn't use cookies.