【问题标题】:Drupal 8 Behat Testing for a URL用于 URL 的 Drupal 8 Behat 测试
【发布时间】:2020-07-16 05:27:36
【问题描述】:

我对我的 Drupal 8 站点的 Behat 测试不熟悉,并尝试编写一个场景来测试页面上不存在 URL,但我似乎无法让它工作。 body 字段有一个<a href="www.google.com"> cool</a> 元素。

@api

功能:网址 场景:查找 URL 鉴于我在“/你好” 那么我不应该看到链接“www.google.com”

public function iShouldNotSeeTheLink($href, $index = 0, $message = '') {
    $xpath = $this
      ->buildXPathQuery('//a[contains(@href, :href)]', [
      ':href' => $href,
    ]);
    $message = $message ? $message : strtr('Link containing href %href found.', [
      '%href' => $href,
    ]);
    $links = $this->session
      ->getPage()
      ->findAll('xpath', $xpath);
    $this
      ->assert(!empty($links[$index]), $message);
  }

【问题讨论】:

    标签: bdd drupal-8 behat


    【解决方案1】:

    我最终得到了这个工作,并认为我会发布以防其他人需要它,它可能不是最好的,但它可以满足我的需要。

        public function iClickOnTheElementWithXPath($xpath)
    {
        $session = $this->getSession(); // get the mink session
        $element = $session->getPage()->find(
            'xpath',
            $session->getSelectorsHandler()->selectorToXpath('xpath', $xpath)
        ); // runs the actual query and returns the element
    
        // errors must not pass silently
        if (null === $element) {
            throw new \InvalidArgumentException(sprintf('Could not evaluate XPath: "%s"', $xpath));
        }
        
        // ok, let's click on it
        $element->click();
    
    }
    
      Scenario: yahoo link check
    Given I am on "/hello"
    Given I click on the element with xpath "//a[not(contains(@href,'yahoo.com'))]"
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-07-05
      • 1970-01-01
      • 1970-01-01
      • 2012-10-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多