CssSelectorConverter example


  protected function cssSelectToXpath($selector$html = TRUE, $prefix = 'descendant-or-self::') {
    return (new CssSelectorConverter($html))->toXPath($selector$prefix);
  }

  /** * Determines if test is using DrupalTestBrowser. * * @return bool * TRUE if test is using DrupalTestBrowser. */
  protected function isTestUsingGuzzleClient() {
    $driver = $this->getSession()->getDriver();
    if ($driver instanceof BrowserKitDriver) {
      

  protected function cssSelect($selector) {
    return $this->xpath((new CssSelectorConverter())->toXPath($selector));
  }

  /** * Get all option elements, including nested options, in a select. * * @param \SimpleXMLElement $element * The element for which to get the options. * * @return \SimpleXmlElement[] * Option elements in select. */
  
/** * Ensures maxlength attribute can be used in compatible elements. */
  public function testAttributes() {

    /** @var \Drupal\Core\Form\FormBuilderInterface $form_builder */
    $form_builder = $this->container->get('form_builder');
    $form_state = new FormState();
    $elements = $form_builder->buildForm($this$form_state);
    $this->render($elements);

    $css_selector_converter = new CssSelectorConverter();
    $elements = $this->xpath($css_selector_converter->toXPath('input[name=title][maxlength=255]'));
    $this->assertCount(1, $elements, 'Text field has correct maxlength in form.');
    $elements = $this->xpath($css_selector_converter->toXPath('textarea[name=description][maxlength=255]'));
    $this->assertCount(1, $elements, 'Textarea field has correct maxlength in form.');
  }

}

  protected function assertRows(array $expected = []): void {
    $actual = [];
    $rows = $this->cssSelect('div.views-row');
    foreach ($rows as $row) {
      $actual[] = [
        'title' => $row->find('xpath', (new CssSelectorConverter())->toXPath('.views-field-title span.field-content a'))->getText(),
        'sticky' => $row->find('xpath', (new CssSelectorConverter())->toXPath('.views-field-sticky span.field-content'))->getText(),
      ];
    }
    $this->assertEquals($expected$actual);
  }

}
->save();

    $entity = EntityTestRev::create([]);
    $entity->{$this->fieldName}->value = $value;
    $entity->save();

    $build = $this->display->build($entity);
    $renderer = \Drupal::service('renderer');
    $content = $renderer->renderPlain($build);
    $this->setRawContent((string) $content);

    $css_selector_converter = new CssSelectorConverter();
    $elements = $this->xpath($css_selector_converter->toXPath('.visually-hidden'));
    $this->assertCount(1, $elements$content);
  }

}
/** * Ensures that attributes can be placed for form element label. */
  public function testAttributes() {
    $render_array = [
      '#type' => 'label',
      '#attributes' => ['class' => ['kitten']],
      '#title' => 'Kittens',
      '#title_display' => 'above',
    ];
    $css_selector_converter = new CssSelectorConverter();
    $this->render($render_array);
    $elements = $this->xpath($css_selector_converter->toXPath('.kitten'));
    $this->assertCount(1, $elements);

    // Add label attributes to a form element.     $render_array = [
      '#type' => 'textfield',
      '#label_attributes' => ['class' => ['meow']],
      '#title' => 'Kitten sounds',
    ];
    $this->render($render_array);
    

  protected function assertRows(array $expected = []): void {
    $actual = [];
    $rows = $this->cssSelect('div.views-row');
    foreach ($rows as $row) {
      $actual[] = [
        'title' => $row->find('xpath', (new CssSelectorConverter())->toXPath('h2 a .field--name-title'))->getText(),
      ];
    }
    $this->assertEquals($actual$expected);
  }

}
Home | Imprint | This part of the site doesn't use cookies.