使用Behat/Mink,如何匹配一个确切的数字?

With Behat/Mink, how to match an exact number?

我正在尝试将页面元素的确切数字与 Behat/Mink 相匹配。

我的测试是这样的:

Then the "td.points" element should contain "1"

如果 td.points1(好),它匹配,但如果 td.points1021(坏),它也匹配。

我试过使用这样的正则表达式:

Then the "td.views-field-field-int-repetitions" element should contain "\b1\b"

但是正则表达式没有被拾取。

我试图深入研究代码,发现 MinkContext 有 assertElementContains,但我找不到像 AssertElementIs.

这样的东西

我想要的是这样的东西

Then the "td.points" element should be exactly "1"

我该如何实现?

编辑:这是包含来自 MinkContext.php:

的特征的元素
/**
 * Checks, that element with specified CSS contains specified HTML
 * Example: Then the "body" element should contain "style=\"color:black;\""
 * Example: And the "body" element should contain "style=\"color:black;\""
 *
 * @Then /^the "(?P<element>[^"]*)" element should contain "(?P<value>(?:[^"]|\")*)"$/
 */
public function assertElementContains($element, $value)
{
    $this->assertSession()->elementContains('css', $element, $this->fixStepArgument($value));
}

要从您可以用于数字的步骤中提取数字:

the "(.*)" element should contain (\d+)

或字符串

the "(.*)" element should contain "(.*)"

或字符串的其他示例

the "(.*)" element should contain (.*)

断言取决于你的代码是如何组织的,使用你拥有的或者你可以做的:

if($someActual != $expected)
{
  throw new \Exception("something meaningful");
}

感谢@lauda,我能够编写我想要的代码:

  /**
   * @Then the :element element should be exactly :value
   *
   * Checks that the element with the specified CSS is the exact value.
   */
  public function theElementShouldBeExactly($element, $value) {
    $page = $this->getSession()->getPage();
    $element_text = $page->find('css', "$element")->getText();
    if ($element_text === NULL || strlen($element_text < 1)) {
      throw new Exception("The element $element had a NULL value.");
    }
    if ($element_text !== $value) {
      throw new Exception("Element $element_text did not match value $value.");
    }
  }