我想创建一个让Behat解析我的HTML并告诉我他找到特定文本的次数的函数。我找到了无数种方法,但由于我想重用这个概念,我不能给他一个特定的div类,在哪里找到它,因为它可能在div之外。
这是我到目前为止所做的
testfeature.feature
And I should see "CISIA - Arcueil" 2 times
FeatureContext.php
public function iShouldSeeTimes($text, $count) {
$element = $this->getSession()->getPage();
$result = $element->findAll('css', sprintf('div:contains("%s")', $text));
if(count($result) == $count && strpos(reset($result)->getText(), $text)) {
return;
}
else {
throw new ExpectationException('"' . $text . '" was supposed to appear ' . $count . ' times, got ' . count($result) . ' instead', $this->getSession());
}
}
这有点乱,但一旦工作,我会整理所有这些。到目前为止,使用此代码,我得到19个元素,这是我要检查的页面中每个div包含的每个文本。在某种程度上,我有可能达到我的目标,但我想要的是在$result
内直接得到我需要的东西(我的两次出现)。但是,看起来我对sprintf()
函数做错了。
有没有办法让Behat查找特定文本?
提前谢谢
答案 0 :(得分:2)
使用匹配任何类型的元素而不是XPath
的{{1}}。
例如:
css
第二种选择是使用正则表达式。我推荐第一个。