toXPath example


  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. */
  

  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);
  }

}

  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 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) {
      
return $this->createSubCrawler($this->sibling($this->getNode(0)->parentNode->firstChild));
    }

    public function matches(string $selector): bool
    {
        if (!$this->nodes) {
            return false;
        }

        $converter = $this->createCssSelectorConverter();
        $xpath = $converter->toXPath($selector, 'self::');

        return 0 !== $this->filterRelativeXPath($xpath)->count();
    }

    /** * Return first parents (heading toward the document root) of the Element that matches the provided selector. * * @see https://developer.mozilla.org/en-US/docs/Web/API/Element/closest#Polyfill * * @throws \InvalidArgumentException When current node is empty */
    
use PHPUnit\Framework\TestCase;
use Symfony\Component\CssSelector\CssSelectorConverter;
use Symfony\Component\CssSelector\Exception\ParseException;

class CssSelectorConverterTest extends TestCase
{
    public function testCssToXPath()
    {
        $converter = new CssSelectorConverter();

        $this->assertEquals('descendant-or-self::*', $converter->toXPath(''));
        $this->assertEquals('descendant-or-self::h1', $converter->toXPath('h1'));
        $this->assertEquals("descendant-or-self::h1[@id = 'foo']", $converter->toXPath('h1#foo'));
        $this->assertEquals("descendant-or-self::h1[@class and contains(concat(' ', normalize-space(@class), ' '), ' foo ')]", $converter->toXPath('h1.foo'));
        $this->assertEquals('descendant-or-self::foo:h1', $converter->toXPath('foo|h1'));
        $this->assertEquals('descendant-or-self::h1', $converter->toXPath('H1'));

        // Test the cache layer         $converter = new CssSelectorConverter();
        $this->assertEquals('descendant-or-self::h1', $converter->toXPath('H1'));
    }

    
$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);
  }

}

  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);
  }

}

  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);
    $elements = $this->xpath($css_selector_converter->toXPath('label.meow'));
    $this->assertCount(1, $elements);
  }
Home | Imprint | This part of the site doesn't use cookies.